2

I am drawing an imaginary circle around the middle of a button.

The radius of the circle is the Height/2 if Height>Width or Width/2 if Width>Height. Now i have to calculate which coordinates (in pixels) are in this circle. The idea is that if e.g. the mouse cursor hovers over that circle, something happens.

Nahum
  • 6,959
  • 12
  • 48
  • 69
Tim T
  • 37
  • 1
  • 4
  • 3
    what did you tried? put some sample code please. – VIRA Sep 11 '12 at 07:23
  • Your post is unreadable. Could you improve it with using Code tag etc. – Kirill Bestemyanov Sep 11 '12 at 07:25
  • 1
    Why use that radius? Maybe you want the circumscribed circle of the rectancle. By the Pythagoras theorem, it's `radius * radius` equals `(height/2) * (width/2)` or `height * width / 4`. So you could check (avoiding integer division) if `4 * (deltaX * deltaX + deltaY * deltaY) <= height * width`. – Jeppe Stig Nielsen Sep 11 '12 at 08:28

4 Answers4

4

Calculating each co-ordinate is overkill; just compare the distance to the center. For example:

int radius = 5; // whatever

int deltaX = originX - mouseX, deltaY = originY - mouseY;

// compare the square distance, to avoid an unnecessary square-root
if((deltaX * deltaX) + (deltaY * deltaY) <= (radius * radius)) {
    // inside the circle, or on the edge
}

To avoid a little math, you could also do a quick bounding-box check, i.e. checking the rectangular region (just addition/subtraction). This can be used in combination, i.e.

  • check the bounding box
    • if it isn't in the bounding box it certainly isn't in the circle
    • if it is in the bounding box, do the math to compare the square-distance
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I think the bounding box optimization is slightly overkill once you've reduced the distance calculation to 5 integer operations (square radius can be predetermined), especially since this is in reaction to mouse movement (which can not happen that many times in a second). – Rotem Sep 11 '12 at 07:35
3

You are inside the circle when this equation is satisfied:

Math.pow(mouse_pos_x-center_circle_x,2)+Math.pow(mouse_pos_y-center_circle_y,2)<Math.pow(radius,2)
melopsitaco
  • 176
  • 4
1

The area of a a circle by definition is a group of points whose distance is equal to or less than the center.

All you must do to test if a point is within a circle is to calculate the distance between it and the center point. If this distance is smaller than the radius of the circle, the point is within the circle.

double Distance(Point p1, Point p2)
{
    int x = p1.X - p2.X;
    int y = p1.Y - p2.Y;
    return Math.Sqrt(x * x + y * y);   
}
Rotem
  • 21,452
  • 6
  • 62
  • 109
  • 2
    In most hit-tests, you would simply compare the *square* distance, rather than compute the (relatively expensive) square-root. – Marc Gravell Sep 11 '12 at 07:27
1

You can use next condition:

x^2+y^2<R^2

Where R - radius, All this points are in circle.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38