determine if a point is an an ellipse

I

illusion.admins

I am trying to code something to tell me if a selected point is in a
particular ellipse. For the ellipse I know how it was constructed
(know the x,y, and width, height). But if I just check to see if the
point is in the rectangle making up the ellipse wouldn't that possibly
give me a false answer? Eg if the point is the upper left coordinate
of the rectangle...this point is in the rectangle making up ellipse
but NOT the ellipse itself.

Thanks for and advice.
 
J

Jon Skeet [C# MVP]

I am trying to code something to tell me if a selected point is in a
particular ellipse. For the ellipse I know how it was constructed
(know the x,y, and width, height). But if I just check to see if the
point is in the rectangle making up the ellipse wouldn't that possibly
give me a false answer? Eg if the point is the upper left coordinate
of the rectangle...this point is in the rectangle making up ellipse
but NOT the ellipse itself.

I won't do all of the maths right now, but consider how you would write
a hit test for a circle (considering the distance from the centre of
the circle to the point, and the radius of the circle). Then think of
an ellipse as a circle which has different scaling factors in the X and
Y directions.
 
P

Peter Duniho

I am trying to code something to tell me if a selected point is in a
particular ellipse. For the ellipse I know how it was constructed
(know the x,y, and width, height). But if I just check to see if the
point is in the rectangle making up the ellipse wouldn't that possibly
give me a false answer? Eg if the point is the upper left coordinate
of the rectangle...this point is in the rectangle making up ellipse
but NOT the ellipse itself.

Well, you could just use the mathematical equations for an ellipse, where
you've defined your foci and radius and then just test for the point being
within the radius or outside it.

However, in .NET you can use a Region, created from a GraphicsPath that
was in turn created using the GraphicsPath.AddEllipse() method. Then,
create a single-pixel Rectangle based on the point you're trying to test,
and pass that to Region.Intersect(Rectangle). If the result is empty
(Region.IsEmpty()), the point was outside the ellipse. If the result is
not empty, it was inside.

For simple ellipses, I think that the mathematical approach is nicer. In
some ways it's clearer, and it certainly should perform better. But it's
only applicable to ellipses. You'd need a completely different algorithm
for any other shapes. The latter method, using regions, will work with
any arbitrary shape you can define using various combinations of the GDI
vector descriptions (line, rectangle, polygon, ellipse, etc.). And since,
at least with some effort, you could in fact create a region based on a
raster mask, it would actually work with any well-defined area you can
create.

Pete
 
I

illusion.admins

I won't do all of the maths right now, but consider how you would write
a hit test for a circle (considering the distance from the centre of
the circle to the point, and the radius of the circle). Then think of
an ellipse as a circle which has different scaling factors in the X and
Y directions.

In the instance of what I am using the ellipse is actually a circle.
 
P

Peter Duniho

In the instance of what I am using the ellipse is actually a circle.

Well, then the math is trivial. Even if you're using a square to define
the circle, the center is just the average of the top left and bottom
right corner point coordinates, the radius is half the width of the
square, and a point is inside the circle if the distance from the center
to the point is less than the radius.

Pete
 
P

Paul E Collins

[find point inside a circle]

This is really a maths question, not a programming question, but I have
the C# code kicking around from a simple shape-based drawing application
a couple of years ago. It's for circles only, not ellipses.

public bool ContainsPoint(Point centre, int radius, Point toTest)
{
// Is the point inside the circle? Sum the squares of the
x-difference and
// y-difference from the centre, square-root it, and compare with the
radius.
// (This is Pythagoras' theorem.)

int dX = Math.Abs(toTest.X - centre.X);
int dY = Math.Abs(toTest.Y - centre.Y);

int sumOfSquares = dX * dX + dY * dY;

int distance = (int) Math.Sqrt(sumOfSquares);

return (radius >= distance);
}

Eq.
 
I

illusion.admins

[find point inside a circle]

This is really a maths question, not a programming question, but I have
the C# code kicking around from a simple shape-based drawing application
a couple of years ago. It's for circles only, not ellipses.

public bool ContainsPoint(Point centre, int radius, Point toTest)
{
// Is the point inside the circle? Sum the squares of the
x-difference and
// y-difference from the centre, square-root it, and compare with the
radius.
// (This is Pythagoras' theorem.)

int dX = Math.Abs(toTest.X - centre.X);
int dY = Math.Abs(toTest.Y - centre.Y);

int sumOfSquares = dX * dX + dY * dY;

int distance = (int) Math.Sqrt(sumOfSquares);

return (radius >= distance);

}

Eq.

Thanks!!
 
C

christery

Jon Skeet - said:
In the instance of what I am using the ellipse is actually a circle.- Dölj citerad text -

- Visa citerad text -

well, 1024/768 will do that to a perfect circle.. ;)

//CY
 
M

Martin Bonner

[find point inside a circle]

This is really a maths question, not a programming question, but I have
the C# code kicking around from a simple shape-based drawing application
a couple of years ago. It's for circles only, not ellipses.

public bool ContainsPoint(Point centre, int radius, Point toTest)
{
// Is the point inside the circle? Sum the squares of the
x-difference and
// y-difference from the centre, square-root it, and compare with the
radius.
// (This is Pythagoras' theorem.)

int dX = Math.Abs(toTest.X - centre.X);
int dY = Math.Abs(toTest.Y - centre.Y);
Why the call to Math.Abs?
int sumOfSquares = dX * dX + dY * dY;

int distance = (int) Math.Sqrt(sumOfSquares);

return (radius >= distance);

I can see you weren't brought up with sqrt as an impossibly expensive
operation :). In my day, that would have been written as:

return radius*radius >= sumOfSquares;

HOWEVER, beware that such an optimization only works if nothing
overflows. Of course:
1. you've already got overflow problems with sumOfSquares
2. Overflow is only a problem for dX,dY,radius in the order of 40k.
If the arguments are screen coordinates, that shouldn't be a problem
for several years.

Function becomes
public bool ContainsPoint(Point centre, int radius, Point toTest)
{
// Is the point inside the circle?)

int dX = toTest.X - centre.X;
int dY = toTest.Y - centre.Y;

int sumOfSquares = dX * dX + dY * dY;

return radius*radius >= sumOfSquares;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top