簡體   English   中英

查找橢圓和直線之間的交點

[英]Finding intersection between ellipse and a line

我試圖找到橢圓和直線之間的交點,但似乎無法做到。 我嘗試了兩種方法,一種通過嘗試在代碼波紋管中找到LineString和LinearRing之間的交點來進行整形,但是並沒有從中獲得任何可用的值。 問題之一是橢圓形將始終偏離中心並以小角度或大角度出現

  # -*- 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)

有什么幫助嗎?

使用Shapely部分,可以固定def intersections(a, line) 首先,出現錯誤,它引用全局b而不是使用line參數,該錯誤被忽略。 因此,比較是在兩個橢圓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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM