Skip to content

Commit

Permalink
Enkele bugs verwijderd.
Browse files Browse the repository at this point in the history
  • Loading branch information
Olsthoorn committed Jun 12, 2018
1 parent 880d391 commit 694989d
Show file tree
Hide file tree
Showing 5 changed files with 509 additions and 219 deletions.
210 changes: 116 additions & 94 deletions coords/intersect.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,18 @@ def perp(xp, yp, x0, y0, alpha):
#perp(xp, yp, x0, y0, alpha)


def perpMany(Xp, Yp, x0, y0, alpha):
def perpMany(Xp, Yp, x0, y0, alpha, verbose=False):
'''Return distance to line given by x0, y0, alpha for many points given by Xp, Yp
parameters
----------
Xp : np.ndarray of floats
x coordinates of points
Yp : np.ndarra of floats
y coordinate of points
x0, y0, alpha : 3 floats
x0, y0, angle(degrees) of point defining the line.
'''

ex = np.cos(np.pi / 180. * alpha)
ey = np.sin(np.pi / 180. * alpha)
Expand All @@ -78,45 +89,43 @@ def perpMany(Xp, Yp, x0, y0, alpha):
lam = lammu[0]
mu = lammu[1]

Xa = x0 + lam * ex
Ya = y0 + lam * ey

Xb = Xp + mu * -ey
Yb = Yp + mu * ex

plt.title('Testing perpendicular lines to n points')
plt.xlabel('x')
plt.ylabel('x')
plt.xlim((-60, 60))
plt.ylim((-60, 60))
plt.text(x0, y0, '(x0, y0)', ha='left')

plt.plot(x0, y0, 'r.')

plt.plot([Xp, Xb], [Yp, Yb], 'b-')
plt.plot( Xp, Yp, 'bo')
plt.plot( Xb, Yb, 'bx')
plt.plot( Xa, Ya, 'r+')

X0 = np.zeros_like(Xa) + x0
Y0 = np.zeros_like(Ya) + y0

plt.plot([X0, Xa], [Y0, Ya], 'r-')

for xpp, ypp, x, y, m in zip(Xp, Yp, Xb, Yb, mu):
xm = 0.5 * (xpp + x)
ym = 0.5 * (ypp + y)
plt.text(xm, ym, 'mu={:.3g}'.format(m), ha='left')

plt.show()


n = 10
Xp = (np.random.random(n) - 0.5) * 100.
Yp = (np.random.random(n) - 0.5) * 100.
x0, y0, alpha = 45., 45., 35.

perpMany(Xp, Yp, x0, y0, alpha)
if np.all(np.isnan(mu)):
return mu

if verbose:
fig, ax = plt.subplots()

Xa = x0 + lam * ex
Ya = y0 + lam * ey

Xb = Xp + mu * -ey
Yb = Yp + mu * ex

ax.title('Testing perpendicular lines to n points')
ax.set_xlabel('x')
ax.set_ylabel('x')
ax.set_xlim((-60, 60))
ax.set_ylim((-60, 60))
ax.text(x0, y0, '(x0, y0)', ha='left')

ax.plot(x0, y0, 'r.')

ax.plot([Xp, Xb], [Yp, Yb], 'b-')
ax.plot( Xp, Yp, 'bo')
ax.plot( Xb, Yb, 'bx')
ax.plot( Xa, Ya, 'r+')

X0 = np.zeros_like(Xa) + x0
Y0 = np.zeros_like(Ya) + y0

ax.plot([X0, Xa], [Y0, Ya], 'r-')

for xpp, ypp, x, y, m in zip(Xp, Yp, Xb, Yb, mu):
xm = 0.5 * (xpp + x)
ym = 0.5 * (ypp + y)
ax.text(xm, ym, 'mu={:.3g}'.format(m), ha='left')

return mu



Expand Down Expand Up @@ -188,37 +197,41 @@ def dist2line(Xp, Yp, x0, y0, alpha, verbose=False):
Xb = Xp + mu * -ey
Yb = Yp + mu * ex

fig, ax = plt.subplots()
ax.set_title('Testing perpendicular lines to n points')
ax.set_xlabel('x')
ax.set_ylabel('x')

X0 = np.zeros_like(Xa) + x0
Y0 = np.zeros_like(Ya) + y0

xmin = min((np.nanmin(Xa), np.nanmin(Xp), np.nanmin(X0)))
xmax = max((np.nanmax(Xa), np.nanmax(Xp), np.nanmax(X0)))
ymin = min((np.nanmin(Ya), np.nanmin(Yp), np.nanmin(Y0)))
ymax = max((np.nanmax(Ya), np.nanmax(Yp), np.nanmax(Y0)))

ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.text(x0, y0, '(x0, y0)', ha='left')

ax.plot(x0, y0, 'r.')

ax.plot([Xp, Xb], [Yp, Yb], 'b-')
ax.plot( Xp, Yp, 'bo')
ax.plot( Xb, Yb, 'bx')
ax.plot( Xa, Ya, 'r+')


ax.plot([X0, Xa], [Y0, Ya], 'r-')
if np.all(np.isnan(mu)):
return mu

for xpp, ypp, x, y, m in zip(Xp, Yp, Xb, Yb, mu):
xm = 0.5 * (xpp + x)
ym = 0.5 * (ypp + y)
ax.text(xm, ym, 'mu={:.3g}'.format(m), ha='left')
if verbose:
X0 = np.zeros_like(Xa) + x0
Y0 = np.zeros_like(Ya) + y0

xmin = min((np.nanmin(Xa), np.nanmin(Xp), np.nanmin(X0)))
xmax = max((np.nanmax(Xa), np.nanmax(Xp), np.nanmax(X0)))
ymin = min((np.nanmin(Ya), np.nanmin(Yp), np.nanmin(Y0)))
ymax = max((np.nanmax(Ya), np.nanmax(Yp), np.nanmax(Y0)))

fig, ax = plt.subplots()
ax.set_title('Testing perpendicular lines to n points')
ax.set_xlabel('x')
ax.set_ylabel('x')

ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.text(x0, y0, '(x0, y0)', ha='left')

ax.plot(x0, y0, 'r.')

ax.plot([Xp, Xb], [Yp, Yb], 'b-')
ax.plot( Xp, Yp, 'bo')
ax.plot( Xb, Yb, 'bx')
ax.plot( Xa, Ya, 'r+')


ax.plot([X0, Xa], [Y0, Ya], 'r-')

for xpp, ypp, x, y, m in zip(Xp, Yp, Xb, Yb, mu):
xm = 0.5 * (xpp + x)
ym = 0.5 * (ypp + y)
ax.text(xm, ym, 'mu={:.3g}'.format(m), ha='left')

return mu

Expand Down Expand Up @@ -276,36 +289,45 @@ def dist2polyline(points, polyline, alpha, verbose=False, maxdist=None):
Mu[OR(Mu<maxdist[0], Mu>maxdist[1])] = np.nan


if not verbose:
return Mu
elif np.all(np.isnan(Mu)):
raise Warning("All distances are Nan, Can't plot the points")
return Mu

if np.all(np.isnan(Mu)):
raise Warning("All distances are Nan, Can't plot the points")
return Mu

Xa = Xp + Mu * ex
Ya = y0 + Mu * ey
if verbose:

fig, ax = plt.subplots()
ax.set_title('Testing distance to polyline')
ax.set_xlabel('x')
ax.set_ylabel('y')
Xa = Xp + Mu * ex
Ya = y0 + Mu * ey

xmin = min((np.nanmin(Xa), np.nanmin(Xp), np.nanmin(X0)))
xmax = max((np.nanmax(Xa), np.nanmax(Xp), np.nanmax(X0)))
ymin = min((np.nanmin(Ya), np.nanmin(Yp), np.nanmin(Y0)))
ymax = max((np.nanmax(Ya), np.nanmax(Yp), np.nanmax(Y0)))

xmin = min((np.nanmin(Xa), np.nanmin(Xp), np.nanmin(X0)))
xmax = max((np.nanmax(Xa), np.nanmax(Xp), np.nanmax(X0)))
ymin = min((np.nanmin(Ya), np.nanmin(Yp), np.nanmin(Y0)))
ymax = max((np.nanmax(Ya), np.nanmax(Yp), np.nanmax(Y0)))
fig, ax = plt.subplots()
ax.set_title('Testing distance to polyline')
ax.set_xlabel('x')
ax.set_ylabel('y')

ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.plot(X0, Y0, 'b', linewidth=3, label='polyline')
ax.plot(Xp, Yp, 'r.')
ax.set_xlim((xmin, xmax))
ax.set_ylim((ymin, ymax))
ax.plot(X0, Y0, 'b', linewidth=3, label='polyline')
ax.plot(Xp, Yp, 'r.')

for xp, yp, mu in zip(Xp, Yp, Mu):
if not np.isnan(mu):
ax.plot([xp, xp + ex * mu], [yp, yp + ey * mu], 'r')
for xp, yp, mu in zip(Xp, Yp, Mu):
if not np.isnan(mu):
ax.plot([xp, xp + ex * mu], [yp, yp + ey * mu], 'r')

ax.legend(loc='best')

ax.legend(loc='best')
return Mu


if __name__ == '__main__' :

n = 10
Xp = (np.random.random(n) - 0.5) * 100.
Yp = (np.random.random(n) - 0.5) * 100.
x0, y0, alpha = 45., 45., 0.35

perpMany(Xp, Yp, x0, y0, alpha, verbose=True)

Loading

0 comments on commit 694989d

Please sign in to comment.