Forum: Ruby finding Collinearity of three points

Posted by prem c s (premcs)
on 2013-02-17 14:33
Can someone here explain me the logic and implementation for finding
collinearity of three points in Ruby?



Thanks in advance...
Posted by RKA (Guest)
on 2013-02-17 16:53
(Received via mailing list)
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?
Posted by prem c s (premcs)
on 2013-02-23 18:13
Thanks a lot RKA
Yeah,its for my home assignment :)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.