[Tutor] How to detect colinearity?

Danny Yoo dyoo at hashcollision.org
Fri Sep 5 21:35:17 CEST 2014


> But if you're think I'm expert with python because I'm work with computer vision
> I'm not I'm newbie to both..

Ok.  Since you've stated that you are a beginner, we now have to
recalibrate how we're answering your questions.

In that case, I would strongly suggest going through a basic
programming tutorial first, before tackling anything with computer
vision.  Otherwise, you'll keep getting tripped up on issues that
people will assume you already know about.

There are several tutorials you can take a look at:

    https://wiki.python.org/moin/BeginnersGuide/NonProgrammers

Personally, I like:

    http://openbookproject.net/thinkcs/python/english2e/

but the other tutorials from the link above should be fine.



> If you ask me about the basic slope formula or basic theory of
> intersection of point with a line I know, that's why I try with "if" code
> since the line is have the same y coordinate there's no slope, so I think if
> the y coordinate point same with y coordinate line no matter the value of x
> coordinate as long as the y coordinate same, the counter increasing.

So you've tried to use the idea of slope, but have discovered that
when the line is vertical, using a slope approach does not work on
such lines.  Yes.  That's one of the problems with using the slope
approach.

There are more general approaches that avoid the whole slope problem, such as:

http://stackoverflow.com/questions/328107/how-can-you-determine-a-point-is-between-two-other-points-on-a-line-segment

which talks about an alternative approach.  The proposed function they
use there is:

#############################################################
def isBetween(a, b, c):
    crossproduct = (c.y - a.y) * (b.x - a.x) - (c.x - a.x) * (b.y - a.y)
    if abs(crossproduct) > epsilon : return False

    dotproduct = (c.x - a.x) * (b.x - a.x) + (c.y - a.y) * (b.y - a.y)
    if dotproduct < 0 : return False

    squaredlengthba = (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)
    if dotproduct > squaredlengthba: return False

    return True
#############################################################

where a, b, and c are objects with x and y components.  It's more
general because it doesn't use division: rather, they use cross
products and dot products.  Vector arithmetic to the rescue,
basically.


But note the "epsilon" free variable there: that's an indication that
we have to be careful about floating point arithmetic.

I used the term "floating point arithmetic" earlier in one of my first
replies because I hoped that would trigger concepts that I had assumed
that you had learned about already.

I saw that you were using exact equality on numbers, and for folks who
have done some beginner-style computer arithmetic, the term "floating
point arithmetic" should be a big warning sign to be careful about
equality.

But now that you've said more of your background, I can't assume you
know what "floating point" arithmetic means.

Please read:

    https://docs.python.org/2/tutorial/floatingpoint.html

for a brief introduction.

After reading that, you should have a better understanding as to why
your programs not use exact equality when floating point numbers are
concerned.


More information about the Tutor mailing list