find my point if inside a circle.

Discussion in 'iOS Programming' started by chhoda, Jan 30, 2012.

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #1
    I have two cgpoints say pt1 pt2

    pt2 is center of a circle.

    how can i find if pt1 is within the perifery of a 50px radius circle centered at pt2 ?

    thanks in advance
    ch
     
  2. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #2
    Sounds like you need the distance formula.

    Go to www.google.com and type this into the search bar. "what is the distance between two points"

    If your result is greater than your radius, your point is not in your circle.
     
  3. mobilehaathi macrumors G3

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #3
    Just check if they are within 50px of each other.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Are you familiar with

    A^2 = B^2 + C^2?

    A is 50
    B is the difference in X's between your two points.
    C is the difference in Y's between your two points.

    The formula defines the boundary of your point. So you'd actually want to use
    A^2 > B^2 + C^2
    to find out if your two points were within 50 of each other.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    No. But I am familiar with A^2 + B^2 = C^2. ;)

    I'd think something like:
    Code:
    if (sqrt(b*b + c*c) <= 50.0)
    would be more clear.
     
  6. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #6
    I may be running on a shortage of sleep today...

    xD
     
  7. maril1111 macrumors 68000

    maril1111

    Joined:
    Mar 14, 2010
    Location:
    Denmark
    #7
    This is probably one of the first times i see this formula being applied and used in real life, i always thought its just another pointless topic that the school forces you to learn.
     
  8. mobilehaathi macrumors G3

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #8
    More clear, yes, and hopefully the OP understands now. However, wouldn't a sqrt function be rather expensive compared to an extra multiplication? Of course that only matters if the check is happening frequently.
     
  9. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #9
    Math is meaningless without programming.

    (Ok, fine, the other sciences give meaning to math as well...)

    Edit: And math team. Somehow math team gives math purpose.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    I don't know. Do you?
     
  11. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #11
    I made a quick app that tests which is faster, this is the output:
    Code:
    2012-01-30 13:13:53.915 SquareRootCost3[22875:f803] Beginning Test with squares.
    2012-01-30 13:13:53.941 SquareRootCost3[22875:f803] Finished test with squares - found 369212
    2012-01-30 13:13:53.941 SquareRootCost3[22875:f803] Beggining test with roots.
    2012-01-30 13:13:53.954 SquareRootCost3[22875:f803] Finished test with roots - found 369212
    So the squares took 0.026 seconds while the roots took 0.013 seconds... it appears to take half as long to use squares. Odd.

    My code, for anyone interested, is this:
    Code:
        srand(time(NULL));
        
        int i = 0;
        float a[500000], b[500000], c[500000];
        for (i = 0; i < 500000; i++)
        {
            a[i] = rand();
            b[i] = rand();
            c[i] = rand();
        }
        
        long int squareans = 0;
        long int rootans = 0;
        
        NSLog(@"Beginning Test with squares.");
        for (int i = 0; i < 500000; i++)
        {
            if (a[i]*a[i] < (b[i]*b[i] + c[i]*c[i]))
            {
                squareans++;
            }
        }
        
        NSLog(@"Finished test with squares - found %ld", squareans);
        NSLog(@"Beggining test with roots.");
        for (int i = 0; i < 500000; i++)
        {
            if (a[i] < sqrtf(b[i]*b[i] + c[i]*c[i]))
            {
                rootans++;
            }
        }
        
        NSLog(@"Finished test with roots - found %ld", rootans);
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    sqrt() can probably take advantage of bitwise operators.
     
  13. mobilehaathi, Jan 30, 2012
    Last edited: Jan 30, 2012

    mobilehaathi macrumors G3

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #13
    Cool, that was my intuition from taking a numerical analysis course long ago, but thanks for demonstrating it. :D

    ----------

    Hmm, it will probably change based on implementation. What is the difference between sqrt() and sqrtf()?

    Edit: Oh it is for floats. It'll probably be slower than one optimized for ints.

    Edit2: Ok, not quite. sqrtf() explicitly takes floats, where as sqrt() is overloaded and can take floats or doubles (or other stuff in C++).
     

Share This Page