Is there an easy way to determine if a point is inside a triangle? It's 2D, not 3D.
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- d3.js moving average with previous and next data v
- How to get a fixed number of evenly spaced points
相关文章
- What are the problems associated to Best First Sea
- ceil conterpart for Math.floorDiv in Java?
- Coin change DP solution to keep track of coins
- why 48 bit seed in util Random class?
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- Need help generating discrete random numbers from
It can not be more efficient than this! Each side of a triangle can have independent position and orientation, hence three calculations: l1, l2 and l3 are definitely needed involving 2 multiplications each. Once l1, l2 and l3 are known, result is just a few basic comparisons and boolean operations away.
Honestly it is as simple as Simon P Steven's answer however with that approach you don't have a solid control on whether you want the points on the edges of the triangle to be included or not.
My approach is a little different but very basic. Consider the following triangle;
In order to have the point in the triangle we have to satisfy 3 conditions
In this method you have full control to include or exclude the point on the edges individually. So you may check if a point is in the triangle including only the |AC| edge for instance.
So my solution in JavaScript would be as follows;
Here is a solution in python that is efficient, documented and contains three unittests. It's professional-grade quality and ready to be dropped into your project in the form of a module as is.
There is an additional optional graphical test for the algorithm above to confirm its validity:
Producing the following graphic:
This is the simplest concept to determine if a point is inside or outside the triangle or on an arm of a triangle. Determination of a point is inside a tringle by determinants
The simplest working code: `
`
Since there's no JS answer,
Clockwise & Counter-Clockwise solution:
EDIT: there was a typo for det computation (
cy - ay
instead ofcx - ax
), this is fixed.https://jsfiddle.net/jniac/rctb3gfL/
I'm using here the same method as described above: a point is inside ABC if he is respectively on the "same" side of each line AB, BC, CA.
Java version of barycentric method:
The above code will work accurately with integers, assuming no overflows. It will also work with clockwise and anticlockwise triangles. It will not work with collinear triangles (but you can check for that by testing det==0).
The barycentric version is fastest if you are going to test different points with the same triangle.
The barycentric version is not symmetric in the 3 triangle points, so it is likely to be less consistent than Kornel Kisielewicz's edge half-plane version, because of floating point rounding errors.
Credit: I made the above code from Wikipedia's article on barycentric coordinates.