/// <summary> /// Returns the coordinate at an offset of the lineString /// </summary> /// <param name="lineString"></param> /// <param name="distance"></param> /// <returns></returns> /// <remarks> /// Be aware that this function can suffer from double precision issue, like: 100.0*0.55 evaluates to 55.000000000000007 /// </remarks> public static Coordinate LineStringCoordinate(ILineString lineString, double distance) { double partialDistance = 0; Coordinate[] coordinates = lineString.Coordinates; for (int i = 1; i < coordinates.Length; i++) { Coordinate c1 = coordinates[i - 1]; Coordinate c2 = coordinates[i]; double segmentDistance = Distance(c1.X, c1.Y, c2.X, c2.Y); if ((partialDistance + segmentDistance) > distance) { double factor = (distance - partialDistance) / (segmentDistance); return(GeometryFactoryEx.CreateCoordinate( coordinates[i - 1].X + factor * (coordinates[i].X - coordinates[i - 1].X), coordinates[i - 1].Y + factor * (coordinates[i].Y - coordinates[i - 1].Y))); } partialDistance += segmentDistance; } return((Coordinate)lineString.Coordinates[lineString.Coordinates.Length - 1].Clone()); }
static public Coordinate NearestPointAtSegment(double Ax, double Ay, double Bx, double By, double cx, double cy) { // if (AB . BC) > 0) if (Dot(Ax, Ay, Bx, By, cx, cy) > 0) { return(GeometryFactoryEx.CreateCoordinate(Bx, By)); } // else if ((BA . AC) > 0) else if (Dot(Bx, By, Ax, Ay, cx, cy) > 0) { return(GeometryFactoryEx.CreateCoordinate(Ax, Ay)); } else // both dot products < 0 -> point between A and B { double AC = Distance(Ax, Ay, cx, cy); double BC = Distance(Bx, By, cx, cy); return(GeometryFactoryEx.CreateCoordinate(Ax + ((AC) / (AC + BC)) * (Bx - Ax), Ay + ((AC) / (AC + BC)) * (By - Ay))); } }