I am making a triangle.

Hi, i have a question about checking if a point(when mouse is clicked) is in a triangle or not.So, i am working on it with MFC.
here is thing.

A triangle is made when i clickek mouse up.
Then, if i click any point on the screen, the program i am working on will check wheather the pointer is in a shape or not.
It is really hard for me to find it.
I believe you understing me.

Thank you in advance.
method 1) Point is in a triangle if it is on the correct side of each edge. You have a triangle ABC. Find vectors AB, BC and CA. Then find vectors perpendicular to them pAB, pBC and pCA. Check the sign of dot product of pAB and AC. Then check the dot product of AM (where M is the mouse ) and pAB, pBC, pCA. If all signs are the same as the first one, M is in ABC.

method 2) If M is in ABC, area of AMB + BMC + CMA = area of ABC. IIRC area of a triangle is half of the cross product of two vectors coming from one point (use absolute value). Though checking for equality will often fail due to floating point number imprecision. I think you should check that the sum of any two smaller striangles is lower than the area of ABC.

method 3) Something with matrices. Don't ask..
Take a matrix
ABx ABy
ACx ACy
and multiply it's inverse with vector AM. Now if M was in ABC, AM' will be in a triangle with corners at coordinates 0;0, 1;0 and 0;1 and it's trivial to check that.

I'm sure there are more methods but I can't think of any.

I hope I didn't mess up these 3..
good one...!!

I love second mothod you mentioned that is pretty understanable for me.
Thank you!!!!! I love you hamsterman. LOL
Last edited on
hum... second mothod is quite complicated. so i use first one.
I got a problem while i was working on it. However, it worked finally.
Thank you again.
Topic archived. No new replies allowed.