2011-09-13 32 views
3

給定圓的中心點和半徑,如何知道某個點(x,y)是否在圓內?任何人都知道嗎?謝謝。圓內的點

+4

一般問題與您的標籤無關....並且其易於搜索 –

+2

總之:確定[從中心到點的距離](http://en.wikipedia.org/wiki/Pthathagoras#Pythagorean_theorem ),然後將其與半徑進行比較。 – Znarkus

+0

不是專門Objective-C,但這應該有所幫助。你應該能夠相當容易地轉換功能:[http://stackoverflow.com/questions/481144/how-do-you-test-if-a-point-is-inside-a-circle](http:/ /stackoverflow.com/questions/481144/how-do-you-test-if-a-point-is-inside-a-circle) –

回答

8

最初你問過Objective-C。

CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2) 
{ 
    CGFloat dx = point2.x - point1.x; 
    CGFloat dy = point2.y - point1.y; 
    return sqrt(dx*dx + dy*dy); 
}; 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    CGFloat distance = DistanceBetweenTwoPoints(self.circleCenter, point); 
    if(distance < self.radius){ 
     //inside the circle 
    } 
} 

此代碼假定您處理子類查看中的圓。

+0

謝謝,真的很感謝你的回答。 – james

+0

如何檢測一個點是否在一個扇區內? – Johnykutty