Say I have a triangle made with 3 points.
JavaScript
x
2
1
makeTriangle(x1, y1, x2, y2, x3, y3);
2
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
1
60
60
1
let Size, x, y, moving;
2
3
//runs once at the start of the program
4
function setup() {
5
6
createCanvas(400, 400);
7
8
Size = 100;
9
x = 10;
10
y = 10;
11
moving = false;
12
13
}
14
//runs once every frame
15
function draw() {
16
17
background(220);
18
19
handleMouse();
20
21
fill("grey");
22
noStroke();
23
24
square(x, y, Size, 5);
25
26
fill("black");
27
28
triangle( x + Size * 0.9, y + Size * 0.9,
29
x + Size * 0.7, y + Size * 0.9,
30
x + Size * 0.9, y + Size * 0.7 );
31
32
}
33
34
function handleMouse(){
35
36
if(mouseInTriangle(/* x1, y1, x2, y2, x3, y3 */) && mouseIsPressed || mouseIsPressed && moving){
37
38
moving = true;
39
40
} else {
41
42
moving = false;
43
44
}
45
46
if(moving){
47
48
Size = max((mouseX + mouseY)/2 + x + y, 50);
49
50
}
51
52
}
53
54
function mouseInTriangle(x1, y1, x2, y2, x3, y3){
55
56
//Is mouse in triangle?
57
58
return true;
59
60
}
JavaScript
1
1
1
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/p5.js"></script>
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
1
13
13
1
function getArea(a, b, c) {
2
return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
3
}
4
5
function mouseInTriangle(x1, y1, x2, y2, x3, y3){
6
let point = [mouseX, mouseY];
7
let area = getArea([x1, y1], [x2, y2], [x3, y3]);
8
let areaA = getArea([x1, y1], [x2, y2], point);
9
let areaB = getArea(point, [x2, y2], [x3, y3]);
10
let areaC = getArea([x1, y1], point, [x3, y3]);
11
return abs(areaA + areaB + areaC - area) < 0.001;
12
}
13
JavaScript
1
56
56
1
let x1, y1, x2, y2, x3, y3;
2
let Size, x, y, moving;
3
4
//runs once at the start of the program
5
function setup() {
6
7
createCanvas(400, 400);
8
9
Size = 100;
10
x = 10;
11
y = 10;
12
moving = false;
13
14
}
15
//runs once every frame
16
function draw() {
17
18
background(220);
19
handleMouse();
20
21
fill("grey");
22
noStroke();
23
square(x, y, Size, 5);
24
fill("black");
25
26
x1 = x + Size * 0.9;
27
y1 = y + Size * 0.9;
28
x2 = x + Size * 0.7;
29
y2 = y + Size * 0.9;
30
x3 = x + Size * 0.9;
31
y3 = y + Size * 0.7;
32
triangle(x1, y1, x2, y2, x3, y3);
33
34
}
35
36
function handleMouse(){
37
if(mouseIsPressed && (moving || mouseInTriangle(x1, y1, x2, y2, x3, y3))) {
38
moving = true;
39
Size = max((mouseX + mouseY)/2 + x + y, 50);
40
} else {
41
moving = false;
42
}
43
}
44
45
function getArea(a, b, c) {
46
return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
47
}
48
49
function mouseInTriangle(x1, y1, x2, y2, x3, y3){
50
let point = [mouseX, mouseY];
51
let area = getArea([x1, y1], [x2, y2], [x3, y3]);
52
let areaA = getArea([x1, y1], [x2, y2], point);
53
let areaB = getArea(point, [x2, y2], [x3, y3]);
54
let areaC = getArea([x1, y1], point, [x3, y3]);
55
return abs(areaA + areaB + areaC - area) < 0.001;
56
}
JavaScript
1
1
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>