I need a fastest way to find any point inside triangle(not edges) in 2D. Any help?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If your triangle is expressed as points, you’re going to have to take every coordinate into consideration, so Steve Jessop’s answer is nearly optimal.
Note all you actually need to do is start at one point and bump it towards the two others a limited amount. That’s what averaging accomplishes, but in a very specific way.
The addition is fast. The division may be slow depending on the platform and compiler.
For example, on modern ARM cores I’d choose this version any day over averaging:
If the variables are integer the compiler should optimize the divisions to bit shifts which, if scheduled correctly, are largely free in ARM. If the variables are floats or doubles the compiler should optimize them to simple exponent decrements. Floating point division is known to be relatively slow.
Ultimately, test and see. Unless you’re doing relatively low level code optimization, you likely won’t see a difference. On the other hand, if you’re using this in a tight inner loop, you might.
To provide some intuition as to why this works: First we find the midpoint of the line segment formed by
(x1, y1)and(x2, y2). Then we find the midpoint between the line segment formed by that point and(x0, y0). If you draw a diagram of a triangle and do this, it becomes immediately clear that this works.