计算四个坐标点是否可以组成一个平行四边形
2020-07-08 本文已影响0人
robotlee8
function isParallelogram (arr) {
var arr1 = []
var arr2 = []
for (let i = 0; i < 4; i++) {
}
for (let i = 0; i < 4; i++) {
for (let j = i + 1; j < 4; j++) {
var distance = (arr[i].x - arr[j].x) * (arr[i].x - arr[j].x) + (arr[i].y - arr[j].y) * (arr[i].y - arr[j].y)
arr1.push({
points: [arr[i], arr[j]],
distanceTag: [i, j],
distance
})
}
}
arr1.sort(function (a, b) {
return b.distance - a.distance
})
// 中点都除以2 所以省略除2
var midpoint1 = {
x: arr1[0].points[0].x + arr1[0].points[1].x,
y: arr1[0].points[0].y + arr1[0].points[1].y
}
for (let i = 0; i < 4; i++) {
if (!arr1[0].distanceTag.includes(i)) {
arr2.push(arr[i])
}
}
var midpoint2 = {
x: arr2[0].x + arr2[1].x,
y: arr2[0].y + arr2[1].y
}
if (midpoint1.x === midpoint2.x && midpoint1.y === midpoint2.y) {
return true
}
return false
}