|
@@ -262,21 +262,26 @@ abstract Polygon(Array<Point>) from Array<Point> to Array<Point> {
|
|
|
|
|
|
/**
|
|
|
Return the closest point on the edges of the polygon
|
|
|
+ @param pt The point to test against.
|
|
|
+ @param out Optional Point instance to which closest point is written. If not provided, returns new Point instance.
|
|
|
+ @returns A `Point` instance of the closest point on the edges of the polygon.
|
|
|
**/
|
|
|
- public function projectPoint(pt: h2d.col.Point) {
|
|
|
+ public function projectPoint(pt: h2d.col.Point, ?out : h2d.col.Point) {
|
|
|
var p1 = points[points.length - 1];
|
|
|
- var closestProj = null;
|
|
|
+ var closest = new h2d.col.Point();
|
|
|
+ if (out == null) out = new Point();
|
|
|
var minDistSq = 1e10;
|
|
|
for(p2 in points) {
|
|
|
- var proj = new Segment(p1, p2).project(pt);
|
|
|
- var distSq = proj.distanceSq(pt);
|
|
|
- if(distSq < minDistSq) {
|
|
|
- closestProj = proj;
|
|
|
+ new Segment(p1, p2).project(pt, out);
|
|
|
+ var distSq = out.distanceSq(pt);
|
|
|
+ if (distSq < minDistSq) {
|
|
|
+ closest.load(out);
|
|
|
minDistSq = distSq;
|
|
|
}
|
|
|
p1 = p2;
|
|
|
}
|
|
|
- return closestProj;
|
|
|
+ out.load(closest);
|
|
|
+ return out;
|
|
|
}
|
|
|
|
|
|
/**
|