in modules/layers/src/mode-handlers/draw-90degree-polygon-handler.ts [157:198]
getIntermediatePoint(coordinates: Position[]) {
let pt;
if (coordinates.length > 4) {
const [p1, p2] = [...coordinates];
const angle1 = bearing(p1, p2);
const p3 = coordinates[coordinates.length - 3];
const p4 = coordinates[coordinates.length - 4];
const angle2 = bearing(p3, p4);
const angles = { first: [], second: [] };
// calculate 3 right angle points for first and last points in lineString
[1, 2, 3].forEach((factor) => {
const newAngle1 = angle1 + factor * 90;
// convert angles to 0 to -180 for anti-clock and 0 to 180 for clock wise
angles.first.push(newAngle1 > 180 ? newAngle1 - 360 : newAngle1);
const newAngle2 = angle2 + factor * 90;
angles.second.push(newAngle2 > 180 ? newAngle2 - 360 : newAngle2);
});
const distance = turfDistance(point(p1), point(p3));
// Draw imaginary right angle lines for both first and last points in lineString
// If there is intersection point for any 2 lines, will be the 90 degree point.
[0, 1, 2].forEach((indexFirst) => {
const line1 = lineString([
p1,
destination(p1, distance, angles.first[indexFirst]).geometry.coordinates,
]);
[0, 1, 2].forEach((indexSecond) => {
const line2 = lineString([
p3,
destination(p3, distance, angles.second[indexSecond]).geometry.coordinates,
]);
const fc = lineIntersect(line1, line2);
if (fc && fc.features.length) {
// found the intersect point
pt = fc.features[0].geometry.coordinates;
}
});
});
}
return pt;
}