How to get the coordinates inside

This is a discussion on How to get the coordinates inside within the Game Programming forums, part of the General Programming Boards category; What's the algorithm on how to get the coordinates inside of: a) Square b) Triangle c) Circle d) Oval Example ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    California
    Posts
    37

    How to get the coordinates inside

    What's the algorithm on how to get the coordinates inside of:
    a) Square
    b) Triangle
    c) Circle
    d) Oval

    Example points.

    Square:
    P1(1,1)
    P2(1,2)
    P3(2,2)
    P4(2,1)

    Triangle
    P1(1,1)
    P2(2,2)
    P1(3,1)

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Unless I am missing something here, your question doesn't make sense. There is an infinite amount of points that can be found within a given space.

    Can you describe your problem in context?

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I had a similar problem when I programmed my collision detection algorithm. For example if I tried to detect collision with two rectangles I tried if the corners of one rect were inside the other rectangle. If you have a similar problem then it's probably a better idea to ask about it in a maths forum as it's more maths related than programming.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    California
    Posts
    37
    @carrotcake1029

    I'm creating a program that can move any objects in the screen by just holding it with a mouse. I hope you get what I mean. I tried the formula on this site (http://2000clicks.com/mathhelp/Geome...dTriangle3.htm) but sometimes even though the point is inside it says it is not.

    @fighter92

    Yeah. I think I should post there. Thanks.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You want the formula for all points inside those objects then.

    A circle is the easiest since it is all points that lie equidistant from a center point.
    If the distance to center is larger than the radius then the point is outside the circle.

    A point in square (also works for rectangles) is:
    Code:
    if (x > right) return;
    if (y > bottom) return;
    if (x < left) return;
    if (y < top) return;
    
    //If you get here point is inside the square/rectangle
    Points inside of a triangle and oval are not as trivial as the above examples. You should be able to find some info on google. However you can do this check.

    Choose a point outside of the primitive. Draw a line to the point in question. When you cross an edge of the primitive increase your count.

    All odd counts = point is inside the primitive.
    All even counts = point is outside of the primitive.

    However there is actually a point in triangle equation but I do not remember what it is.

    As for an oval they follow this equation (in 2D):

    d = (xcoef)(x * x) + (ycoef)(y * y)

    At xcoef = 1 and ycoef = 1 you get a perfect circle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get relative mouse coordinates when GLUT return absolute
    By joeprogrammer in forum Game Programming
    Replies: 14
    Last Post: 02-10-2009, 06:35 PM
  2. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 09:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 01:51 PM

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