2012-05-14 54 views
-4

我在圓上有2個點,它們之間的角度我想找到這樣定義的圓的中心(當然,最好是兩個中心)。 enter image description here給定2個點上的圓和角度,如何找到中心(在Python中)?

def find_center(p1,p2,angle): 
    # magic happens... What to do here? 
    return (center_x, center_y) 
+0

的角度?你的意思是頂點是圓的中心? – Randy

+4

一個心理圖像似乎說,將有兩個圓的兩個中心,將符合這個標準.. – Randy

+0

它應該涉及每個點的漸變...開始:) – hkf

回答

1

我對這個東西真的很生疏了,所以這可能是有點過,但它應該讓你開始。此外,我不知道Python,所以這只是僞代碼:

//check to ensure... 
    //The two points aren't the same 
    //The angle isn't zero 
    //Other edge cases 

//Get the distance between the points 
x_dist = x2 - x1; 
y_dist = y2 - y1; 

//Find the length of the 'opposite' side of the right triangle 
dist_opp = (sqrt((x_dist)^2 + (y_dist)^2))); 
x_midpoint = (x1 - (x_dist/2); 
y_midpoint = (y1 - (y_dist/2); 
theta = the_angle/2; //the right triangle's angle is half the starting angle 
dist_adj = cotangent(theta) * dist_opp;//find the right triangle's length 

epsilon = sqrt((-y_dist)^2 + x_dist^2); 
segments = epsilon/dist_adj; 

x_center = x_midpoint + (x_dist * segments); 
y_center = y_midpoint + (y_dist * segments); 
1

你必須解決三角形p1 p2 c。你有一個角度。另外兩個角度是(180°角)/ 2計算邊p1 p2(距離)然後計算邊p1 c,這會給出圓的半徑r。解決方案是兩個點是中心p1和中心p2以及半徑r的圓的交點。

3

這裏是我的測試代碼

from pylab import * 
from numpy import * 

def find_center(p1, p2, angle): 
    # End points of the chord 
    x1, y1 = p1 
    x2, y2 = p2 

    # Slope of the line through the chord 
    slope = (y1-y2)/(x1-x2) 

    # Slope of a line perpendicular to the chord 
    new_slope = -1/slope 

    # Point on the line perpendicular to the chord 
    # Note that this line also passes through the center of the circle 
    xm, ym = (x1+x2)/2, (y1+y2)/2 

    # Distance between p1 and p2 
    d_chord = sqrt((x1-x2)**2 + (y1-y2)**2) 

    # Distance between xm, ym and center of the circle (xc, yc) 
    d_perp = d_chord/(2*tan(angle)) 

    # Equation of line perpendicular to the chord: y-ym = new_slope(x-xm) 
    # Distance between xm,ym and xc, yc: (yc-ym)^2 + (xc-xm)^2 = d_perp^2 
    # Substituting from 1st to 2nd equation for y, 
    # we get: (new_slope^2+1)(xc-xm)^2 = d^2 

    # Solve for xc: 
    xc = (d_perp)/sqrt(new_slope**2+1) + xm 

    # Solve for yc: 
    yc = (new_slope)*(xc-xm) + ym 

    return xc, yc 

if __name__=='__main__': 
    p1 = [1., 2.] 
    p2 = [-3, 4.] 
    angle = pi/6 
    xc, yc = find_center(p1, p2,angle) 

    # Calculate the radius and draw a circle 
    r = sqrt((xc-p1[0])**2 + (yc-p1[1])**2) 
    cir = Circle((xc,yc), radius=r, fc='y') 
    gca().add_patch(cir) 

    # mark p1 and p2 and the center of the circle 
    plot(p1[0], p1[1], 'ro') 
    plot(p2[0], p2[1], 'ro') 
    plot(xc, yc, 'go') 

    show() 
+0

'#爲xc求解: xc =(d_perp)/ sqrt(new_slope ** 2 + 1)+ xm#看起來像+ xm被省略了!)' 從Mike McP複製粘貼回答的可見性,因爲它是一個回覆此帖子。 – Erich

1
# Solve for xc: 
xc = (d_perp)/sqrt(new_slope**2+1) +xm # looks like +xm got omitted!) 

# Solve for yc: 
yc = (new_slope)*(xc-xm)+ym 

你還需要檢查X1液= X2

# Slope of the line through the chord 
if x1==x2 
    slope = 999999 
else 
    slope = (y1-y2)/(x1-x2) 
相關問題