html画出的正六边形6个点的坐标是啥,是正六边形内的一个点

你要的是找出一个点是否是凸多边形内部,一个是六边形的一个特定情况下的代码。

我做了修改我的使用该功能,我发现我的版本更加清晰。它的打字稿(你只是眯着眼睛,它的JavaScript):

function vectorX(v: Vector): number {

return v[1].x - v[0].x;

}

function vectorY(v: Vector): number {

return v[1].y - v[0].y;

}

function crossProduct(v1: Vector, v2: Vector): number {

return vectorX(v1)*vectorY(v2) - vectorY(v1)*vectorX(v2);

}

function isInConvexPolygon(testPoint: Point, polygon: Polygon): boolean {

// https://stackoverflow.com/a/34689268/516188

if (polygon.length < 3) {

throw "Only supporting polygons of length at least 3";

}

// going through all the edges around the polygon. compute the

// vector cross-product http://allenchou.net/2013/07/cross-product-of-2d-vectors/

// to find out for each edge on which side of the edge is the point.

// if the point is on the same side for all the edges, it's inside

let initCrossIsPositive = undefined;

for (var i=0;i

if (polygon[i].x === testPoint.x &&

polygon[i].y === testPoint.y) {

// testPoint is an edge of the polygon

return true;

}

const curPointOnEdge = polygon[i];

const nextPointOnEdge = polygon[(i+1)%polygon.length];

const vector1 = [curPointOnEdge, nextPointOnEdge];

const vector2 = [curPointOnEdge, testPoint];

const cross = crossProduct(vector1, vector2);

if (initCrossIsPositive === undefined) {

initCrossIsPositive = cross > 0;

} else {

if (initCrossIsPositive !== (cross > 0)) {

return false;

}

}

}

// all the cross-products have the same sign: we're inside

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值