2013-08-20 2 views
2

타원과 선 사이의 교차점을 찾으려고하지만 그렇게 할 수없는 것처럼 보입니다. 두 가지 접근 방법을 시도해 보았습니다. 하나는 코드 에서처럼 LineString과 LinearRing 간의 교차점을 찾기 위해 매끈하게하지만 사용 가능한 값은 얻지 못했습니다. 문제 중 하나는 elipse 항상 중심 오프와 작거나 높은 각도타원과 선 사이의 교차점 찾기

# -*- coding: utf-8 -*- 
""" 
Created on Mon Aug 19 17:38:55 2013 

@author: adudchenko 
""" 
from pylab import * 
import numpy as np 
from shapely.geometry.polygon import LinearRing 
from 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 = [] 
    for 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 + a * ca * ct - b * sa * st 
     p[:, 1] = y0 + a * 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 for p in mp] 
    y = [p.y for 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() 

나는 또한 예를 들어 울부 짖는 소리 같이 fsolve 사용하여 시도에있을 것입니다 만, 실제로는 하나의 잘못된 점을 포인트 교차하는 잘못을 발견 (또는 .

from pylab import * 
from scipy.optimize import fsolve 
import numpy as 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 = [] 
    for 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 + a * ca * np.cos(t) - b * sa * np.sin(t) 
     p[:, 1] = y0 + a * 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 + a * ca * np.cos(t) - b * sa * np.sin(t)-x,y0 + a * 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=[],[] 
for i in range(10): 
    xl.append(i) 
    yl.append(m*i+bl) 
figure() 
plot(x, y, "o") 
plot(a[:,0], a[:,1]) 
plot(xl,yl) 

어떤 도움을 appriceated 될 것이다?를 매끈한 부분

+0

완벽 정말 내가 생각 –

+0

을 http://math.stackexchange.com/하지 않기에 맞는 문제가 될 것 같습니다 . 이것을 직접 분석 할 수 없다고 가정하면 수치적인 방법이 필요합니다.이 경우 게시 할 수 있습니다. 그렇지 않으면 math.stackexchange.com이 최선의 방법입니다. – usethedeathstar

+0

@usethedeathstar ... 좋은 지적! 나는 이런 식으로 생각하지 않았다 ... –

답변

0

, def intersections(a, line) 고정 할 수있다. 첫째,이 line 매개 변수를 사용하는 글로벌 b를 대신 참조하는 오류가있다, 어떤 무시됩니다. 따라서 비교는 두 개의 타원 ab 사이이며 각 타원 사이의 차이는 line이 아닌 것으로 생각됩니다.

또한 공백 세트, 점 (1 교차점), 다중 점 (1 교차점 이상), 선 스트링 (선 스트링의 일부가 겹치는 경우) 중 하나 일 수 있습니다. 서로 평행하다), 또는 점과 선의 모음. 가정에만 처음 세 개의 결과 :

def intersections(a, line): 
    ea = LinearRing(a) 
    mp = ea.intersection(line) 
    if mp.is_empty: 
     print('Geometries do not intersect') 
     return [], [] 
    elif mp.geom_type == 'Point': 
     return [mp.x], [mp.y] 
    elif mp.geom_type == 'MultiPoint': 
     return [p.x for p in mp], [p.y for p in mp] 
    else: 
     raise ValueError('something unexpected: ' + mp.geom_type) 

그래서 지금은이 정확한 모양 :

>>> intersections(a, line) 
([2.414213562373095], [2.414213562373095]) 
>>> intersections(b, line) 
([0.0006681263405436677, 2.135895843256409], [0.0006681263405436642, 2.135895843256409])