Home > SYS_Math > distanceToPoint
SYS_Math.distanceToPoint() method
Calculate the shortest distance from a point to the polygon boundary
Signature
typescript
public distanceToPoint(polygon: TSYS_MathPolygonInput, point: ISYS_MathPoint): number;1
Parameters
Parameter | Type | Description |
|---|---|---|
polygon | Polygon | |
point | The point to calculate |
Returns
number
Shortest distance. If the point is inside the polygon, 0 is returned
Example
javascript
// 1. 定义矩形区域(右边界在 x = 100)
const zone = [{ x: 0, y: 0 }, { x: 100, y: 0 }, { x: 100, y: 80 }, { x: 0, y: 80 }];
// 2. 外部点:到右边界的水平距离为 50
const outsidePoint = { x: 150, y: 40 };
console.log('外部点到边界的最短距离:', eda.sys_Math.distanceToPoint(zone, outsidePoint));
// 3. 内部点:在多边形内部时返回 0
const insidePoint = { x: 50, y: 40 };
console.log('内部点到边界的最短距离:', eda.sys_Math.distanceToPoint(zone, insidePoint));
// 4. 斜向最近边不是正对的那条边时,结果取真实垂线距离
const diagonalPoint = { x: 130, y: 110 };
console.log('斜向点到边界的最短距离:', eda.sys_Math.distanceToPoint(zone, diagonalPoint));1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14