I did already somthing simular in a ArcMap extension. I show you the code below in C#. I think it can be translated into Javascript with the use of the help of the ArcGis javascript API. You will need the help of esri.geometry.getLength(point1, point2) to calculate the lengths. In the code I use also the angle of a line segment, this you will need to be written in javascript. All coordinates has to be in projected coordinates (not degrees).
The code below does a lot more than what you requires, so some simplicity is needed.
The most interesting function is CalcDxDy.
Sorry for the dutch comments.
Johnny
/// <summary>
/// Verleng een lijn (segment) met een eindpunt als start en een gegeven lengte.
/// </summary>
/// <param name="segmentFrom">Lijn segment</param>
/// <param name="endPoint">eindpunt van het segment</param>
/// <param name="length">lengte, negatief betekent inkorten</param>
/// <returns></returns>
private IPoint MovePoint(ISegment segmentFrom,
IPoint endPoint, double length)
{
double angle=0;
ILine line = new LineClass();
line.SpatialReference = segmentFrom.SpatialReference;
// Verlenging moet steeds gebeuren vanaf het tot punt
if (segmentFrom.FromPoint.Compare(endPoint) == 0)
{
// Beweeg dit eind punt, verwissel de van en tot punt.
line.ToPoint = segmentFrom.FromPoint;
line.FromPoint = segmentFrom.ToPoint;
angle = line.Angle;
IPoint tmpPoint = line.ToPoint;
CalcDxDy(ref tmpPoint, angle, length);
return tmpPoint;
}
else if (segmentFrom.ToPoint.Compare(endPoint) == 0)
{
// Beweeg dit punt, geen omwisseling van punten
line.ToPoint = segmentFrom.ToPoint;
line.FromPoint = segmentFrom.FromPoint;
angle = line.Angle;
IPoint tmpPoint = line.ToPoint;
CalcDxDy(ref tmpPoint, angle, length);
return tmpPoint;
}
return null;
}
private void CalcDxDy(ref IPoint point, double angle,double length)
{
double degree = (angle * 180) / Math.PI;
if (degree < 0)
{
degree += 180;
}
if (degree < 0 || degree >= 360)
{
// ongeldige situatie, doe niks
return;
}
if (degree < 90)
{
point.X += length * Math.Cos(angle);
point.Y += length * Math.Sin(angle);
}
else if (degree < 180)
{
point.X -= length * Math.Cos((Math.PI) -angle);
point.Y += length * Math.Sin((Math.PI) - angle);
}
else if (degree < 270)
{
point.X -= length * Math.Cos(angle - (Math.PI));
point.Y -= length * Math.Sin(angle - (Math.PI));
}
else
{
point.X = length * Math.Cos(2*Math.PI - angle);
point.Y -= length * Math.Sin(2*Math.PI - angle);
}
return;
}