point lies in circle?

This is a discussion on point lies in circle? within the Game Programming forums, part of the General Programming Boards category; How would I best test if a selected point is inside a circle, I have the following three (semi-hackish) ideas: ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    point lies in circle?

    How would I best test if a selected point is inside a circle, I have the following three (semi-hackish) ideas:

    1) Very bad: Draw circle onto a surface which is painted color A, fill with color B, plot point in color C. Go through entire bitmap looking for C's location, etc.

    2) Alright: Decrease radius of circle in for loop and test if point lies on circle.

    3) find all points of circle and draw lines to them from center point (is this realistic)

    I have code for #2 here, but it doesn't seem to work.
    PHP Code:
    bool cSChicken::inRadius(int bxint by)
    {
        
    int r radius;
        
        for(
    r=radius>= 0r--) //shrink circle down to a single point one point at a time
            
    if( pow(bx-cx,2) + pow(by-cy,2) == pow(r,2) ) //if point lies on circle
                
    return true;
        return 
    false;

    cx and cy are the center coords of the circle, radius is (duh) the predefined radius of the circle.

    What else could I do? What is wrong with the code I wrote for #2? Has anyone ever had to do this for something they were writing?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Well, i dont think i'd use any of those methods!!

    If you have a point in the circle, then you know the x and y coordinates. and you obviously know the centre coordinates of the circle, and the radius....

    lets call these

    mx, my (mouse coordinates where the user clicked)
    cx, cy (centre of circle)
    radius (radius of the circle)

    all you need to do is get the distance of the mouse click from the centre and test if it's greater than the radius, if it is, then it's not in the circle.

    distance = sqrt((mx - cx) * (mx - cx) + (my - cy) * (my - cy));

    if(distance > radius)
    {
    not in circle
    } else {
    in the circle
    }


    hope this helps
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    403

    thanks so much!

    thanks, that is a much better method than i had, it seems to be working real well now : )

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    not a problem
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rotating around object (looat)
    By jabka in forum Game Programming
    Replies: 13
    Last Post: 06-18-2008, 05:02 PM
  2. const Point& and const Point are same
    By noobcpp in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2007, 01:39 AM
  3. overloading operator help
    By syrel in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2004, 10:49 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Finding a point within a circle
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2001, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21