Reputation: 7603
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.
Upvotes: 3
Views: 1357
Reputation: 3464
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;
}
}
}
Upvotes: 5
Reputation: 89129
assuming you've first tested that P is inside the circle, and that you know the start and end angles for the pie piece
a. find the Orientation of the line segment CP (CPa) - this should be fairly basic trig
b. check if CPa is between the start and end angles for the pie piece
Upvotes: 0