Friday, September 26, 2014

Coding and the equation of a circle

A student was creating a tower defence game in my computer class, doing so he learned what the equation of a circle is.  This idea is a Gr. 12 math idea, and he did this in Grade 10.  Here is what happened...

He was coding a certain tower in his game and he asked me "How do I code the tower to only attack units which are within 200 pixels?"  I first asked if he could draw me a picture of what he wanted, and below is computer graphic of what he drew..




I then said, "What do you have?" He then showed me that he created variables:

t_x = x value of the turret
t_y= y value of the turret
u_x= x value of the unit
u_y= y value of the unit
He had currently coded that if the following two inequalities were true the tower would attack.
At a quick glance we realize that this creates a square around the turret not a circle.  This he had already realized.  He then said, "How do I test if the straight line distance is less than 200?".  We then drew a picture as follows:


 He then said "Well I know that once the line from the turret to the unit is less than or equal to 200, the turret will attack but what inequality do I create?"  A student, next to him, said "Would pythagorean theorem work?".  The problem we had was to label the other two sides.  Minutes passed while I let him think, and finally he asked if this would work
 I said.. "lets try it".. sadly the turret would attack the unit if the unit was within 200 units of the origin not the turret.  Once again, I refused to simply give him the answer and I asked him, "what could we do to change from the origin to the turret?"  He replied with "Well the turret isn't always the origin, so we would have to test the distance.. and so can we do.."
I then asked, "Why did you use the absolute value before?" Which is responded "because the code needs to take the positive value, and if the unit was to the left or below the turret I need it to become positive....but....wait....squaring is positive, so can I just remove the absolute value?"  We tried and here was his final test

When tested, this worked perfectly.  Keep in mind this child is in Grade 10, and completed an outcome from Gr. 12 mathematics.



No comments:

Post a Comment