Tuesday, June 25, 2013

Using coding to think deeply in math #codemaths

What led me here...
I write this in hopes of what the next year will look like for my students in Algebra. I find the more that I learn to write code the more important it is that I pass on this skill to my students. Perhaps with daily access to Chromebooks and Google Apps Scripts in their Google Drive that will look like building extension activities parallel to the algebra we are learning in class that will also teach the code skills to my students.
In writing some of more own programs I feel that I've had to think deeply about the mathematics in order to consider all possible outcomes for anticipating errors or situations that may cause the program to break. Last spring I wrote a web app that will take the coordinates (in order) for a convex polygon and identify it (quadrilateral, parallelogram, square, rectangle, rhombus, kite, trapezoid). I had to anticipate what would happen if slopes calculated in the program would be zero or undefined and how to address that situation for the final output. I didn't have students create an actual program but some did choose to make a Google presentation that, through clicked links, would identify what type of a quadrilateral is present at the end by its properties.
Yesterday, while in a workshop on using code to teach mathematics, I decided to dig deeper into my quadrilateral program.
Researching what I thought might be possible
By doing some searching online here and here I found a 'fun' way to calculate the area of any convex quadrilateral using the slope and distances of its diagonals. This was an exercise where using a trig identify was actually necessary! So here is what I found:
You can calculate the area of a quadrilateral (convex) by taking half of pq sin θ where p and q are the lengths of the diagonals and θ is the angle between them.
This prompted me to ask the next question:
How do I calculate the angle between two lines using  their slopes?
Like any good child of the millennial generation I Googled the question I was asking and found this:
 θ = tan^(-1) [(m2 - m1)/(1 + m1*m2)]
Putting it together as a composite function:
Area = 0.5 pq sin ( tan^(-1) [(m2 - m1)/(1 + m1*m2)] )
Considering the range of circumstances
And now I had to think about what this would mean in the program. I know that there is the possibility of having some vertical lines as diagonals (problematic in a program because of division by zero) and also the possibility that a diagonal may be horizontal. I would need to use some if, else if, and else statements to account for this and figure out what to do in that situation. This is where some discovery for me as a math teacher took place:
In the situation of either a vertical or horizontal diagonal I could think of the quadrilateral being split by it, creating two triangles and finding their area by multiplying the length of the vertical/horizontal diagonal and then the difference in either the x coordinates or y coordinates respectively of the other diagonal (basically the altitudes of the two triangles added together). The horizontal diagonal wasn't really problematic in the program but I thought the connection to the vertical diagonal process was interesting.
In the situation of a vertical and horizontal diagonals this would create perpendicular lines causing θ = 90. Since sin 90 = 1 the area calculation simplifies to 0.5 pq which is the formula we teach in Geometry for areas of kites and rhombus (kind of cool to see how all the area formulas are related by trigonometry for me at this point).
For all other quadrilaterals I can just calculate the slopes and distances of the diagonals and use the standard formula above.
Converting the math to code
 So here is the javascript code for calculating the area of quadrilateral ABCD, for sake of time I'm not going to explain the variables but I'm guessing if you've read this far you can figure it out:
 //////////Area of quadrilateral////////////////////
   if (slopeAC == 0){if(slopeBD =="undefined"){var areaCalc = 0.5*distAC*distBD;}else{var areaCalc = Math.abs(distAC*yDiffBD);}}
  else if (slopeAC == "undefined"){if(slopeBD ==0){var areaCalc = 0.5*distAC*distBD;}else{var areaCalc = Math.abs(distAC*xDiffBD);}}
  else if (slopeBD == "undefined"){var areaCalc = Math.abs(distBD*xDiffAC);}
  else if (slopeBD == 0){var areaCalc = Math.abs(distBD*yDiffAC);} //This condition is not necessary and would be covered in the final else statement but is interesting in how it relates to the previous else if statement
  else
  {
  var theta = Math.atan(Math.abs((slopeAC-slopeBD)/(1+slopeAC*slopeBD)));
  var areaCalc = 0.5*distAC*distBD*Math.sin(theta);
  }  
 So now to think how to lead students toward learning these same skills...

2 comments:

  1. That's awesome. Even starting with the simple stuff like linked presentation slides for classification is really powerful but quick constructivist learning. But I watching your path of curiosity go from one formula to the next, then followed by cleanup, is really cool to watch (from the student perspective) and then emulate.

    ReplyDelete
  2. Thanks--it truly was a learning experience for me in seeing the connection to areas of the quadrilateral and slopes. Under the surface a lot of good conversations can take place related to domain and range when errors can happen in programming too. I think when we just focus on the math kids don't see as much at stake when things are undefined or get an error on their calculator.

    ReplyDelete