Skip to content
Advertisement

Check if mouse is in triangle with three x,y coordinates

Say I have a triangle made with 3 points.

JavaScript

How do I check if said triangle contains the a certain set of points?

I’m trying to make an interactive UI with P5.js that includes an arrow that allows you to resize the object. The wireframe code is:

JavaScript
JavaScript

Is there a dynamic way to tell if a point is within a triangle?

Advertisement

Answer

I suggest to use an algorithm that compares the areas of triangles. See Check whether a given point lies inside a triangle or not.If the point is in a triangle, that point divides the triangle into 3 smaller triangles. Calculate the sum of the areas of these 3 triangles and compare it to the area of the originally triangle:

JavaScript

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement