autojs点积判断点是否在矩形内

+关注继续查看

BC和BE,

CD和CE,

DA和DE,

点积

a·b的几何意义为a在b上的投影长度乘以b的模长

a·b=|a||b|cosθ，其中θ为a,b之间的夹角

a·b=x1*x2+y1*y2;

（1）判断两个向量是否垂直 a⊥b <=> a·b=0

（2）求两个向量的夹角，点积<0为钝角，点积>0为锐角

把公式转换为js

js

(B.x - A.x) * (E.x - A.x) + (B.y - A.y) * (E.y - A.y) >= 0;
(C.x - B.x) * (E.x - B.x) + (C.y - B.y) * (E.y - B.y) >= 0;
(D.x - C.x) * (E.x - C.x) + (D.y - C.y) * (E.y - C.y) >= 0;
(A.x - D.x) * (E.x - D.x) + (A.y - D.y) * (E.y - D.y) >= 0;

封装为函数

function dotProduct(pointA, pointB, pointE) {
return (pointB.x - pointA.x) * (pointE.x - pointA.x) + (pointB.y - pointA.y) * (pointE.y - pointA.y);
}
function isPointInFourPoints(pointE, pointA, pointB, pointC, pointD) {
let AB = dotProduct(pointA, pointB, pointE);
let BC = dotProduct(pointB, pointC, pointE);
let CD = dotProduct(pointC, pointD, pointE);
let DA = dotProduct(pointD, pointA, pointE);
return AB >= 0 && BC >= 0 && CD >= 0 && DA >= 0;
}

验证函数是否正确

let pointA = {
x: -1,
y: 1,
};
let pointB = {
x: 1,
y: 1,
};
let pointC = {
x: 1,
y: -1,
};
let pointD = {
x: -1,
y: -1,
};
let pointE = {
x: 0,
y: 0,
};
console.log(isPointInFourPoints(pointE, pointA, pointB, pointC, pointD));

Android版本: 12
Autojs版本: 9.1.14

autojs之酷炫字体效果

44 0
autojs放大镜
autojs放大镜
25 0
autojs颜色渐变效果

17 0
autojs获取网页源代码

56 0
autojs之通信

12 0
autojs之一键加密

43 0
autojs提词器

19 0
autojs之超椭圆

21 0
autojs随机名字

39 0
+关注
329

0