Jump to content

coordinates inside/outside triangle

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
skirmish

skirmish

    Newbie

  • Members
  • Pip
  • 2 posts
I'm making a program which shows if the inputed coordinates of point is inside, outside or on the line of triangle. Triangle's coordiantes are 100, 0; 100, 100; 0,100; I can't figure out algoritm or some kind of comparasion for that. It would be easy for circle or quadrangle but for triangle it's hard(er?) (for me). It would be nice if someone could help.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The interior of the triangle is the set of points that satisfy the following:
1) they are not on a line defined by any pair of the vertices
2) for each line defined by a pair of vertices, they are on the same side of the line as the third vertex.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
skirmish

skirmish

    Newbie

  • Members
  • Pip
  • 2 posts
I understand the idea, but have difficulties transfering it into code. (writing in Pascal).

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Well, give it a shot and let me see what you come up with.

Do you know the math of both statements?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
krwq

krwq

    Newbie

  • Members
  • PipPip
  • 28 posts
there is much simplier method:
Cross product - Wikipedia, the free encyclopedia
for example if you have triangle ABC and input point D
you must create 6 vectors:
AB, DA
BC, DB
CA, DC

and check the signs of the cross products counted one by one pairs (as showed above) - z axis only. if all signs are the same it means that the point D is in the triangle. if at least one of them is 0 it means that point D is on the triangle if signs differs it means that the point is outside of triangle. Try to implement this. If you fail, paste your code here I will help you. analogically you can implement checking for other polygons (convex only).

you can use also other method. create 3 different triangles (from edge of the traingle and point) and check if sum of areas is equal to area of the main triangle.

to count an area of the triangle you can use this
Heron's formula - Wikipedia, the free encyclopedia

#6
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
If your triangle is always the same, and it has these simplistic coordinates, there's a really easy method. It requires just about no calculations whatsoever.
Just consider: your triangle is half a square. Forget vectors.
Obviously, your coordinates must be less than or equal to 100 to be inside or on the triangle. If that isn't so, then it's outside.
Now, how much is x+y for a point outsdide the triangle? How about inside? How about on the line 0,100-100,0? Get the drift? Quite simple....

Posted Image