views:

120

answers:

2

This is more of an algorithms question, but hopefully someone can help me with this. I have a line made of of latitude/longitude points and I want to create a polygon from it with some predefined thickness. So basically the polygon would have edges that run parallel to the original polyline on either side. Any thoughts on the best approach to take for this?

EDIT: My current plan is to iterate over each of the points, find the slope to the next point, then find the parallel lines to the either side and those make up the sides of the polygons. Just didn't know if there was an easier way to do it.

+1  A: 

What you want to do is create a pair of new lines which are shifted slightly to the left and right of the original line. So:

var polygon = [
  {x:0, y:0},
  {x:10, y:0},
  {x:10, y:10},
  {x:0, y:10}
];
var outerPolygon = [];
var innerPolygon = [];
for(var i=1; i<polygon.length; i++){
  var ret = newLines(polygon[i-1], polygon[i]);
  outerPolygon.push(ret[0]);
  innerPolygon.push(ret[1]);
}
function newLines(start, stop){
  var dx = start.x - stop.x;
  var dy = start.y - stop.y;
  var d = Math.sqrt(dx*dx + dy*dy);
  dx /= d;
  dy /= d;
  var rNormal = {dx: dy, dy:-dx};
  var lNormal = {dx: -dy, dy:dx};
  return [
    {start:{
      x:start.x+rNormal.dx,
      y:start.y+rNormal.dy},
     stop:{
      x:stop.x+rNormal.dx,
      y:stop.y+rNormal.dy}
    },
    {start:{
      x:start.x+lNormal.dx,
      y:start.y+lNormal.dy},
     stop:{
      x:stop.x+lNormal.dx,
      y:stop.y+lNormal.dy}
    },
  ];
}
Marius
+1  A: 

If I understand your question it is the same as this one, which has some very detailed answers already.

Timothy Pratley
yep, looks close. thank you.
Jeff Storey