Can someone here explain me the logic and implementation for finding

collinearity of three points in Ruby?

Thanks in advance…

Can someone here explain me the logic and implementation for finding

collinearity of three points in Ruby?

Thanks in advance…

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)

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?

Thanks a lot RKA

Yeah,its for my home assignment

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs