[OpenLayers-Users] Get point on line?

Bill Thoen bthoen at gisnet.com
Wed Apr 29 09:21:30 EDT 2009


shane_china wrote:
> In openlayers,
> I have a straight line with two endpoints. How could I get a point on the
> line with given distance to one endpoint? Thank you. 
>   

You can find a point anywhere on a line by using a ratio calculation.  
Start with the distance to your point divided by the total distance of 
your line, then your new X coordinate = the difference between the X 
coordinates of the endpoints times the ratio (plus the X coordinate of 
the starting line endpoint. For example, if your endpoints are at 
coordinates (X0, Y0) and (X1,Y1) then the Cartesian distance is

D = sqrt((X1-X0)*(X1-X0) + (Y1-Y0)*(Y1-Y0))

Then if the ratio between this and the distance to your point is d, your 
new point's coordinates will be:

X= X0+(X1-X0)*d/D
Y=Y0+(Y1-Y0)*d/D

HTH,
- Bill Thoen




More information about the Users mailing list