2014-04-02 39 views
0

我已經得到了一個點(x0,y0)。我想要找到從(x0,y0)開始的所有點,這個點相對於x軸的角度爲θ。我手上只有(x0,y0)和theta。而已。我如何去做這件事?沿特定角度的一條線上的所有點

+0

這個問題更適合http://math.stackexchange.com。你會在那裏得到更多深入的答案。乾杯! – MBlanc

+1

@MBlanc其實更多的是一個計算問題。對於一個數學家來說,會有無數的點數,我認爲這是不需要的。對於有限數量的點,您可能需要https://en.wikipedia.org/wiki/Bresenham's_line_algorithm。但是最好使用合適的庫函數'drawLine(x0,y0,x0 + r * cos(angle),y0 + r * sin(angle))'。 –

回答

1

角度的餘弦給你在x方向上的步驟,角度的正弦給你在y方向上的步驟。最好採用這種方法,而不是找到線的梯度,因爲對於垂直線,梯度是無限的。

在計算機上找不到全部點,因爲它們的數量是無限的,所以您必須決定步長和步數。這在下面的Python程序中進行了說明,該程序選擇步長爲1和100的步驟。

import math, matplotlib.pyplot as plt 

def pts(x0,y0,theta): 
    t = range(101) # t=0,1,2,3,4,5...,100 
    x = [x0 + tt*math.cos(theta) for tt in t] 
    y = [y0 + tt*math.sin(theta) for tt in t] 
    return x,y 

def degrees2radians(degrees): 
    return degrees * math.pi/180 

degrees = 45 
x,y=pts(-100,-100, degrees2radians(degrees)) 
plt.plot(x, y, label='{} degrees'.format(degrees)) 

degrees = 90 
x,y=pts(100,100, degrees2radians(degrees)) 
plt.plot(x, y, label='{} degrees'.format(degrees)) 

plt.xlim(-100,300) 
plt.ylim(-100,300) 
plt.legend() 
plt.show() 

和輸出

enter image description here

下面R程序採用類似的方法。

drawline=function(x0,y0,theta) { 
    t=0:100 # t = 0,1,2,3,4,5,...,100 
    # x formed by stepping by cos theta each time 
    x=x0 + t*cos(theta) 
    # y formed by stepping by sin theta each time 
    y=y0 + t*sin(theta) 
    # plot 
    rng=c(min(x,y),max(x,y)) # range 
    plot(y~x,xlim=rng,ylim=rng,type="l") 
} 

這裏,theta是弧度。因此drawline(-100,-100,pi/4)對應於45度並給出第一個圖,而drawline(100,100,pi/2)對應於90度,並給出第二個圖左側顯示的垂直線。

enter image description here

enter image description here

+0

我面臨的另一個問題是,我需要生成整數座標,比如說10個座標,沿着從(x0,y0)開始的角度theta theta,因爲我正在處理圖像。如果我在角度的餘弦或正弦上使用地板或小屋,我可能會多次生成相同的座標。我如何去做這件事? – user3488947

+0

@ user3488947在這種情況下,您可以使用en.wikipedia.org/wiki/Bresenham's_line_algorithm,如Salix alba的評論中所述。 – TooTone

+0

但Bresenham的算法需要線上的兩點。不是嗎? – user3488947

相關問題