unnamed
unnamed

Reputation: 856

How to Determine if a Triangle contains a specific point?

I've question to ask you guys...

Now lets say I've a virtual grid, and I have junction points on these virtual grid as show below

Junction Points On Virtual Grid

Now lets say I've drawn a triangle onto this virtual grid as show below

Triangle with Points

Now I wonder if any of the virtual grid's point is contained by the triangle... How can I achieve this using C#.Net?

Note: I also know the points for the triangles, and the question is for 2D plane..

My best regards...

Upvotes: 3

Views: 5084

Answers (3)

Eugen Rieck
Eugen Rieck

Reputation: 65314

  • Say your triangle consists of points A,B and C
  • Say you want to test point P for being inside the triangle

Being inside the triangle can be resolved as being to the same side (left or right) of all of the vectors AB, BC and CA. So you

  • Create 3d vectors AB, BC, CA with z=0 (this has to be done only once per triangle)
  • Create 3d vectors AP, BP, CP with z=0 (this has to be done once per point P)
  • Calculate p=ABxAP, q=BCxBP and r=CAxCP (this has to be done once per point P)

Your point is inside the triangle, if p,q and r have the same sign on their z coordinates

Upvotes: 4

Denis Palnitsky
Denis Palnitsky

Reputation: 18387

Define your triangle as GraphicsPath and use GraphicsPath.IsVisible(Point) method to determine if this path contains the point.

Upvotes: 3

Krishna
Krishna

Reputation: 2481

I found a general approach to find if a point is within a triangle here

hope this helps

Upvotes: 8

Related Questions