c语言直线和椭圆的交点,查找椭圆和线之间的交点

我正在尝试找到椭圆和线之间的交点,但似乎无法这样做。我尝试了两种方法,一种是通过尝试找到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

似乎是完全适合http://math.stackexchange.com/ –

+0

不是真的我想到一个问题。假设这不能直接解析,所以你需要一个数值方法,在这种情况下,这是发布的正确地方。否则math.stackexchange.com是要走的路。 –

+0

@usethedeathstar ...好点!我没有这样想过...... –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值