9

Possible Duplicate:
How can I make Turtle recognize a circle?

enter image description hereI have a function that draws a few circles and I am going to place dots inside them. Depending if the dot falls within the circle, depends on wether the colour will change or not.

The biggest issue I am finding is how to check whether or not the dot is within the circle. Does anyone have any idea how to calculate coordinates within a circle or is their a method which can check coordinates against the circle?

Community
  • 1
  • 1
MichaelH
  • 1,600
  • 3
  • 14
  • 20
  • is this a homework assignment? it looks very similar to this question: http://stackoverflow.com/questions/12049536/how-can-i-make-turtle-recognize-a-circle – zenpoy Sep 04 '12 at 12:33

3 Answers3

15

For a circle with center (x,y) and radius r, a point (x1, y1) is within the circle if

(x1-x)² + (y1-y)² <= r²
gefei
  • 18,922
  • 9
  • 50
  • 67
  • 2
    +1, this formulation is actually more elegant than mine and faster to compute since you can cache r². – Fred Foo Sep 04 '12 at 13:31
10
  1. Compute (Euclidean) distance to the circle's center,

    sqrt((x - center_x) ** 2 + (y - center_y) ** 2)
    
  2. Check whether the result is less than the radius.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1

To check if a coordinate lies on a circle you can use the equation of the circle

a² + b² = r²

So to check if the point is inside the circle:

a² + b² <= r²
Minion91
  • 1,911
  • 12
  • 19