-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathpolygonScaleArea.js
More file actions
25 lines (20 loc) · 817 Bytes
/
polygonScaleArea.js
File metadata and controls
25 lines (20 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { lineAngle } from "../lines/lineAngle";
import { lineLength } from "../lines/lineLength";
import { pointTranslate } from "../points/pointTranslate";
import { polygonCentroid } from "./polygonCentroid";
// Scales a polygon by a scale factor (where 1 is the original size) from an origin point.
// The returned polygon's area is equal to the input polygon's area multiplied by the scaleFactor.
// The origin defaults to the polygon's centroid.
export function polygonScaleArea(polygon, scale, origin){
if (!origin){
origin = polygonCentroid(polygon);
}
let p = [];
for (let i = 0, l = polygon.length; i < l; i++){
const v = polygon[i],
d = lineLength([origin, v]),
a = lineAngle([origin, v]);
p[i] = pointTranslate(origin, a, d * Math.sqrt(scale));
}
return p;
}