检查鼠标是否使用三个X,y坐标为三角形

发布于 2025-02-01 18:30:08 字数 1430 浏览 4 评论 0原文

说我有一个三角形的三角形。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2025-02-08 18:30:09

我建议使用一种比较三角形区域的算法。参见检查是否给定点位于三角形中。计算这3个三角形的区域的总和,并将其与最初三角形的区域进行比较:

function getArea(a, b, c) {
    return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
}

function mouseInTriangle(x1, y1, x2, y2, x3, y3){
    let point = [mouseX, mouseY];
    let area = getArea([x1, y1], [x2, y2], [x3, y3]);
    let areaA = getArea([x1, y1], [x2, y2], point);
    let areaB = getArea(point, [x2, y2], [x3, y3]);
    let areaC = getArea([x1, y1], point, [x3, y3]);
    return abs(areaA + areaB + areaC - area) < 0.001;
}

let x1, y1, x2, y2, x3, y3;    
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");
  
  x1 = x + Size * 0.9;
  y1 = y + Size * 0.9;
  x2 = x + Size * 0.7;
  y2 = y + Size * 0.9;
  x3 = x + Size * 0.9;
  y3 = y + Size * 0.7;
  triangle(x1, y1, x2, y2, x3, y3);
  
}

function handleMouse(){
    if(mouseIsPressed && (moving || mouseInTriangle(x1, y1, x2, y2, x3, y3))) {
        moving = true;
        Size = max((mouseX + mouseY)/2 + x + y, 50);
    } else {
        moving = false;
    }
}

function getArea(a, b, c) {
    return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
}

function mouseInTriangle(x1, y1, x2, y2, x3, y3){
    let point = [mouseX, mouseY];
    let area = getArea([x1, y1], [x2, y2], [x3, y3]);
    let areaA = getArea([x1, y1], [x2, y2], point);
    let areaB = getArea(point, [x2, y2], [x3, y3]);
    let areaC = getArea([x1, y1], point, [x3, y3]);
    return abs(areaA + areaB + areaC - area) < 0.001;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

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:

function getArea(a, b, c) {
    return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
}

function mouseInTriangle(x1, y1, x2, y2, x3, y3){
    let point = [mouseX, mouseY];
    let area = getArea([x1, y1], [x2, y2], [x3, y3]);
    let areaA = getArea([x1, y1], [x2, y2], point);
    let areaB = getArea(point, [x2, y2], [x3, y3]);
    let areaC = getArea([x1, y1], point, [x3, y3]);
    return abs(areaA + areaB + areaC - area) < 0.001;
}

let x1, y1, x2, y2, x3, y3;    
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");
  
  x1 = x + Size * 0.9;
  y1 = y + Size * 0.9;
  x2 = x + Size * 0.7;
  y2 = y + Size * 0.9;
  x3 = x + Size * 0.9;
  y3 = y + Size * 0.7;
  triangle(x1, y1, x2, y2, x3, y3);
  
}

function handleMouse(){
    if(mouseIsPressed && (moving || mouseInTriangle(x1, y1, x2, y2, x3, y3))) {
        moving = true;
        Size = max((mouseX + mouseY)/2 + x + y, 50);
    } else {
        moving = false;
    }
}

function getArea(a, b, c) {
    return abs((a[0]*(b[1]-c[1]) + b[0]*(c[1]-a[1])+ c[0]*(a[1]-b[1]))/2);
}

function mouseInTriangle(x1, y1, x2, y2, x3, y3){
    let point = [mouseX, mouseY];
    let area = getArea([x1, y1], [x2, y2], [x3, y3]);
    let areaA = getArea([x1, y1], [x2, y2], point);
    let areaB = getArea(point, [x2, y2], [x3, y3]);
    let areaC = getArea([x1, y1], point, [x3, y3]);
    return abs(areaA + areaB + areaC - area) < 0.001;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文