python - Finding intersection between ellipse and a line -
i trying find intersection points between elipse , line, seem unable so. have tried 2 approaches, 1 shapely trying find intersection between linestring , linearring in code bellow, did not usable values out of it. 1 of problems, elipse off center , @ small or high angle
# -*- coding: utf-8 -*- """ created on mon aug 19 17:38:55 2013 @author: adudchenko """ pylab import * import numpy np shapely.geometry.polygon import linearring shapely.geometry import linestring def ellipse_polyline(ellipses, n=100): t = np.linspace(0, 2*np.pi, n, endpoint=false) st = np.sin(t) ct = np.cos(t) result = [] x0, y0, a, b, angle in ellipses: angle = np.deg2rad(angle) sa = np.sin(angle) ca = np.cos(angle) p = np.empty((n, 2)) p[:, 0] = x0 + * ca * ct - b * sa * st p[:, 1] = y0 + * sa * ct + b * ca * st result.append(p) return result def intersections(a, line): ea = linearring(a) eb = linearring(b) mp = ea.intersection(eb) print mp x = [p.x p in mp] y = [p.y p in mp] return x, y ellipses = [(1, 1, 2, 1, 45), (2, 0.5, 5, 1.5, -30)] a, b = ellipse_polyline(ellipses) line=linestring([[0,0],[4,4]]) x, y = intersections(a, line) figure() plot(x, y, "o") plot(a[:,0], a[:,1]) plot(b[:,0], b[:,1]) show()
i tried using fsolve in example bellow, finds wrong intersect points ( or 1 wrong point.
from pylab import * scipy.optimize import fsolve import numpy np def ellipse_polyline(ellipses, n=100): t = np.linspace(0, 2*np.pi, n, endpoint=false) st = np.sin(t) ct = np.cos(t) result = [] x0, y0, a, b, angle in ellipses: angle = np.deg2rad(angle) sa = np.sin(angle) ca = np.cos(angle) p = np.empty((n, 2)) p[:, 0] = x0 + * ca * np.cos(t) - b * sa * np.sin(t) p[:, 1] = y0 + * sa * np.cos(t) + b * ca * np.sin(t) result.append(p) return result def ellipse_line(txy): t,x,y=txy x0, y0, a, b, angle,m,lb=1, 1, 2, 1, 45,0.5,0 sa = np.sin(angle) ca = np.cos(angle) return (x0 + * ca * np.cos(t) - b * sa * np.sin(t)-x,y0 + * sa * np.cos(t) + b * ca * np.sin(t)-y,m*x+lb-y) a,b= ellipse_polyline([(1, 1, 2, 1, 45), (2, 0.5, 5, 1.5, -30)]) t,y,x=fsolve(ellipse_line,(0,0,0)) print t,y,x #print a[:,0] m=0.5 bl=0 xl,yl=[],[] in range(10): xl.append(i) yl.append(m*i+bl) figure() plot(x, y, "o") plot(a[:,0], a[:,1]) plot(xl,yl)
any appriceated?
with shapely part, def intersections(a, line)
can fixed. first, there error, references global b
instead of using line
parameter, ignored. comparison between 2 ellipses a
, b
, , not between each ellipse line
, think intended.
also, intersection between 2 lines can 1 of several outcomes: empty set, point (1 intersection) multipoint (more 1 intersection), linestring (if part(s) of linestrings overlap , parallel each other), or collection of points , lines. assuming first 3 outcomes:
def intersections(a, line): ea = linearring(a) mp = ea.intersection(line) if mp.is_empty: print('geometries not intersect') return [], [] elif mp.geom_type == 'point': return [mp.x], [mp.y] elif mp.geom_type == 'multipoint': return [p.x p in mp], [p.y p in mp] else: raise valueerror('something unexpected: ' + mp.geom_type)
so these correct:
>>> intersections(a, line) ([2.414213562373095], [2.414213562373095]) >>> intersections(b, line) ([0.0006681263405436677, 2.135895843256409], [0.0006681263405436642, 2.135895843256409])
Comments
Post a Comment