You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Turf says: "Fast
Takes advantage of the newest algorithms and doesn't require you to send data to a server"
whereas little has been done to make this code efficient.
distances are calculated in spherical coordinates, whereas for comparison - squared distances would suffice
same distances are recalculated
i have improved this at least two-fold by simply projecting the point in question onto consecutive line segments like so:
`
function dot(v1,v2) {
return (v1.xv2.x)+(v1.yv2.y);
}
function project(line, point, force) {
var vline = {
x : line[1].x-line[0].x,
y : line[1].y-line[0].y
};
var vlineLengthSqrd = Math.pow(vline.x, 2)+Math.pow(vline.y,2);
if (vlineLengthSqrd == 0) {
//0-lenght segment => point
return force ? line[0] : undefined;
}
var projectionRatio = dot(
vline,
{
x : point.x - line[0].x,
y : point.y - line[0].y
}
)/vlineLengthSqrd;
if (projectionRatio < 0) {
//beyond the mStart end of the segment
return force ? line[0] : undefined;
} else if (projectionRatio > 1) {
//beyond the mEnd end of the segment
return force ? line[1] : undefined;
}
return {
x: line[0].x + (projectionRatio * vline.x),
y: line[0].y + (projectionRatio * vline.y)
};
}
function comparableDistance(a,b) {
return Math.pow(a.x-b.x, 2) + Math.pow(a.y-b.y, 2);
}
class GeoLineString {
constructor(points) {
this.mPoints = points;
}
projectOnto(point) {
var best = undefined;
for (var i = 1; i < this.mPoints.length; i++) {
var projection = project(
[ this.mPoints[i-1], this.mPoints[i]],
point,
true
);
var distance = comparableDistance(projection, point);
if (!best || best.comparableDistance > distance) {
best = {
projection : projection,
comparableDistance : distance,
index : {
start : i-1,
stop : i
}
}
}
}
if (best) {
best.projection = new LatLng(projection.y, projection.x);
best = {
projection : best.projection,
distanceMts : best.projection.distanceTo(point),
index : best.index
}
}
return best;
}
}`
The text was updated successfully, but these errors were encountered:
Turf says: "Fast
Takes advantage of the newest algorithms and doesn't require you to send data to a server"
whereas little has been done to make this code efficient.
`
function dot(v1,v2) {
return (v1.xv2.x)+(v1.yv2.y);
}
The text was updated successfully, but these errors were encountered: