I have a web application where I need some functionality to draw two points that is the start of a line, and then extend that line to a given length (that is given by a number in meters). I know how to create the line as a polyline based on the 2 points, but how can I extend this by the given distance?


Just calculate the slope of your 2 points, then use geometry to calculate the "new" second point given the slope and the first point and the new distance (d).

  // returns a graphic with a polyLine geometry
  function setDistance(point, slope, d) {
        var symbol = new SimpleLineSymbol();

    // https://math.stackexchange.com/a/656512/42151
    var r = Math.sqrt(1 + Math.pow(slope, 2));
    var newX = point.x + (d/r);
    var newY = point.y + ((d * slope)/r);
    return new Graphic(new Polyline({paths:[[[point.x, point.y], [newX, newY]]], "spatialReference":{wkid: 102100}}), symbol);

Here's an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.