检查鼠标是否使用三个X,y坐标为三角形
说我有一个三角形的三角形。
makeTriangle(x1, y1, x2, y2, x3, y3);
如何检查三角形是否包含一组点?
我正在尝试与P5.JS进行交互式UI,其中包含一个箭头,该箭头使您可以调整对象的大小。线框代码是:
let Size, x, y, moving;
//runs once at the start of the program
function setup() {
createCanvas(400, 400);
Size = 100;
x = 10;
y = 10;
moving = false;
}
//runs once every frame
function draw() {
background(220);
handleMouse();
fill("grey");
noStroke();
square(x, y, Size, 5);
fill("black");
triangle( x + Size * 0.9, y + Size * 0.9,
x + Size * 0.7, y + Size * 0.9,
x + Size * 0.9, y + Size * 0.7 );
}
function handleMouse(){
if(mouseInTriangle(/* x1, y1, x2, y2, x3, y3 */) && mouseIsPressed || mouseIsPressed && moving){
moving = true;
} else {
moving = false;
}
if(moving){
Size = max((mouseX + mouseY)/2 + x + y, 50);
}
}
function mouseInTriangle(x1, y1, x2, y2, x3, y3){
//Is mouse in triangle?
return true;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
有没有动态的方法来判断三角形内的点是否存在?
Say I have a triangle made with 3 points.
makeTriangle(x1, y1, x2, y2, x3, y3);
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:
let Size, x, y, moving;
//runs once at the start of the program
function setup() {
createCanvas(400, 400);
Size = 100;
x = 10;
y = 10;
moving = false;
}
//runs once every frame
function draw() {
background(220);
handleMouse();
fill("grey");
noStroke();
square(x, y, Size, 5);
fill("black");
triangle( x + Size * 0.9, y + Size * 0.9,
x + Size * 0.7, y + Size * 0.9,
x + Size * 0.9, y + Size * 0.7 );
}
function handleMouse(){
if(mouseInTriangle(/* x1, y1, x2, y2, x3, y3 */) && mouseIsPressed || mouseIsPressed && moving){
moving = true;
} else {
moving = false;
}
if(moving){
Size = max((mouseX + mouseY)/2 + x + y, 50);
}
}
function mouseInTriangle(x1, y1, x2, y2, x3, y3){
//Is mouse in triangle?
return true;
}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
Is there a dynamic way to tell if a point is within a triangle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用一种比较三角形区域的算法。参见检查是否给定点位于三角形中。计算这3个三角形的区域的总和,并将其与最初三角形的区域进行比较:
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: