# How to get the coordinates inside

• 03-20-2009
m3rk
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)
• 03-20-2009
carrotcake1029
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?
• 03-21-2009
fighter92
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.
• 03-21-2009
m3rk
@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.
• 03-21-2009
VirtualAce
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.