Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point inside Pie Piece

I am writing an application in WPF and have come accross a problem. As seen in the picture below, I need an algorithm which determines whether or not a specified point P is inside the shaded region of the circle. The shaded region is just a portion of circle that has an orientation (where the shaded region is looking at) and an angle.

Pie Piece

like image 207
Dave Avatar asked Nov 28 '25 01:11

Dave


1 Answers

Maths first:

let v = p - c let u = (1,0) : Using the geometry shown above

Check |v| < r

Angle = acos(v.u/|v|)

Check the angle is in range.

In WPF:

Vector v = p - (new Point(0,0));
if(v.Length > radius)
    return false;
double angle = -Vector.AngleBetween(v, new Vector(1,0));
...

Here's an untested class

class Pie
{
    public Point Center { get; set; }
    public double Radius { get; set; }
    public Vector ZeroDegrees { get; set; }
    public bool ClockwisePositive { get; set; }

    public double GetAngle(Point p)
    {
        if (ClockwisePositive)
            return (Vector.AngleBetween(p - Center, ZeroDegrees) + 360) % 360;
        else
            return (Vector.AngleBetween(ZeroDegrees, p - Center) + 360) % 360;
    }

    public bool Contains(Point p)
    {
        return (p - Center).Length <= Radius;
    }

    public class Slice
    {
        public Pie Parent { get; set; }
        public double DirectionDegrees { get; set; }
        public double SizeDegrees { get; set; }

        public bool Contains(Point p)
        {
            if (!Parent.Contains(p))
                return false;

            double angle = Parent.GetAngle(p);
            double minAngle = (DirectionDegrees - SizeDegrees / 2 + 360) % 360;
            double maxAngle = (DirectionDegrees + SizeDegrees / 2 + 360) % 360;

            if (minAngle < maxAngle)
                return minAngle <= angle && angle <= maxAngle;
            else
                return angle >= minAngle || angle <= maxAngle;
        }
    }
}
like image 128
Matthew Finlay Avatar answered Nov 30 '25 14:11

Matthew Finlay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!