Can someone here explain me the logic and implementation for finding collinearity of three points in Ruby? Thanks in advance...
on 2013-02-17 14:33
on 2013-02-17 16:53
Well to find co-linearity of three point in general we can draw a line through any two points and if the line intersects the third point then the points must be co-linear. We can break the problem into a few parts: - Getting the points from the users This is easy; we just use a series of gets. - finding a line that goes through two of them (this involves the point slope formula) We need to take the input and plug it into our point slope formula: y-y1 = m(x-x1) But we need a slope! So before we do that we need to define m = (y2 - y1)/(x2 - x1) We should solve it for y, for reasons that will become apparent soon. So the final formula that we'll be using is this: y = (y2 - y1)/(x2 - x1)(x-x1) - y1 - checking to see if the third point is on the line Now that we have a line, we need to check if the third point is on it. We know that if we plug in the third point's x value, and it's on the same line, it should return the same y value as the possibly co-linear third point. So just compare the two y values, and if they are the same they are co linear! I hope this helps. Is this for a homework assignment?
on 2013-02-23 18:13
Thanks a lot RKA Yeah,its for my home assignment :)