Khan/khan-exercises — pull request #161350 by pjmattingly (2014-04-25 19:01:25+00:00)
Title: Changing kline.equal() to strict collinearity check.

We can check for collinearity by forming a triangle of three of the points of the line segments under considerations. Since line1's points are trivially collinear, we can test its points against each of line2's points one at a time. If any of the points are not collinear then the line segments are not collinear.

Ref: http://mathworld.wolfram.com/Collinear.html

We do this by the "ShoeLace Formula", which calculates the area of a polygon defined by ordered pairs of points. The triangle was the simplest and so is implemented here.

Why not use the formula for a 4 sided polygon and do it in one step?

Because the ordering of the pairs matters.
It appears that the formula assumes that the pairs of points will be given such that they define the perimeter of the figure. We cannot give an unordered set of points and expect an accurate result. Since ordering the points such that they would define a polygon would require some sort of sorting algorithm in addition, this was deemed needlessly complex and a simple loop with _.every() was used instead.

Ref: https://en.wikipedia.org/wiki/Shoelace_formula#Examples

Also this has the added benefit of extensibility, as to examine then N lines to verify collinearity would be a simple matter of:

equal: function(lines*) {
...
_.every(_.flatten(arguments, true), function(dim) {
...

We can extract a list of lists from arguments, then compare them to line1 to verify if all such points are collinear, etc.

Added an extra test with semi-random values to verify.