> 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
[quoted text clipped - 3 lines]
> 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.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
illusion.admins@gmail.com - 20 Feb 2008 20:47 GMT
> <illusion.adm...@gmail.com> wrote:
> > I am trying to code something to tell me if a selected point is in a
[quoted text clipped - 14 lines]
> Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> World class .NET training in the UK:http://iterativetraining.co.uk
In the instance of what I am using the ellipse is actually a circle.
Peter Duniho - 20 Feb 2008 20:57 GMT
> 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
Paul E Collins - 20 Feb 2008 20:59 GMT
> [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.
illusion.admins@gmail.com - 20 Feb 2008 21:04 GMT
> <illusion.adm...@gmail.com> wrote:
> > [find point inside a circle]
[quoted text clipped - 23 lines]
>
> Eq.
Thanks!!
Martin Bonner - 22 Feb 2008 12:47 GMT
> <illusion.adm...@gmail.com> wrote:
> > [find point inside a circle]
[quoted text clipped - 13 lines]
> 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;
}
christery@gmail.com - 20 Feb 2008 21:27 GMT
> > Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
> > World class .NET training in the UK:http://iterativetraining.co.uk
>
> 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
> 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
[quoted text clipped - 3 lines]
> 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