检查鼠标是否在具有三个 x,y 坐标的三角形中

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

假设我有一个由 3 个点组成的三角形。

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/p5@1.4.1/lib/p5.js"></script>

有没有动态的方法来判断一个点是否在三角形内?

我建议使用比较三角形面积的算法。参见 Check whether a given point lies inside a triangle or not。如果点在三角形中,则该点将三角形分成 3 个较小的三角形。计算这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>