PDA

View Full Version : code for "is pixel(x, y) in circle(cx, cy, r)"?


E-Male
19th July 2005, 09:17
(i know for most programmers here this will sound like a very stupid question)

i'm looking code to find out if a pixel with the coordinates x & y is in a circle with the center coordinates cx & cy and radius r (radius in pixels)

thx in advance for any help


EDIT: would this be right?:
bool incircle = (x - rx)^2 + (y - ry)^2 < r^2

EDIT2:
how would the code look for an ellipse, with x-radius and y-radius?

Wilbert
19th July 2005, 10:33
a: radius long axis
b: radius short axis
(rx,ry) center

ellips: (x-rx)^2/a^2 + (y-ry)^2/b^2 = 1
... reduces to a circle for a=b=r.

reference: http://mathworld.wolfram.com/Ellipse.html

E-Male
19th July 2005, 11:19
great, exactly what i was looking for

i assume for getting the pixels inside the ellipse instead of thosw on the edge i have to replace '=' with '<'

Wilbert
19th July 2005, 12:04
i assume for getting the pixels inside the ellipse instead of thosw on the edge i have to replace '=' with '<'
Yup ...

E-Male
19th July 2005, 13:56
alright
thx again