Start at a point, in this case a point midway along a line. (seg0, and pnt0)
Find the point on the next line that is closest to it. (seg1 and pnt1)
Return basic information, like azimuth and distance.
Basic functions.
import numpy as np
def _pnt_on_seg_(seg, pnt):
"""Mini pnt_on_seg function normally required by pnt_on_poly."""
x0, y0, x1, y1, dx, dy = *pnt, *seg[0], *(seg[1] - seg[0])
dist_ = dx * dx + dy * dy # squared length
u = ((x0 - x1) * dx + (y0 - y1) * dy) / dist_
u = max(min(u, 1), 0) # u must be between 0 and 1
xy = (np.array([dx, dy]) * u) + [x1, y1] # noqa
return xy
def _line_dir_(orig, dest, azimuth):
"""Mini line direction function."""
orig = np.atleast_2d(orig)
dest = np.atleast_2d(dest)
dxy = dest - orig
ang = np.degrees(np.arctan2(dxy[:, 1], dxy[:, 0]))
if azimuth: # if True, correct to North, otherwise return the angle
ang = np.mod((450.0 - ang), 360.)
return ang
Do a run
# planar coordinates in, euclidean values out
seg0 = np.array([[0., 0.], [10., 0.]])
seg1 = np.array([[0., 5.], [10., 6.]])
pnt0 = np.mean(seg0, axis=0)
pnt1 = _pnt_on_seg_(seg1, pnt0)
azim = _line_dir_(pnt0, pnt1, True) # azimuth, use ...False to get angle
d_01 = np.linalg.norm(pnt0 - pnt1) # euclidean distance
azim, d_01
354.29, 5.472704546154941
Now that you have reached your destination, perhaps you want to do something with it, or move on to the next segment or a polygon boundary. The possibilities are endless
And another couple of options.
The first is with the first segment location specified, then the location of the remaining points are calculated.
The second is with each segment having a specified location (50% along line) and closest points calculated between each. The final line extending upwards connects those in sequence.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.