AlishahNovin
AlishahNovin

Reputation: 1932

How to determine whether a point (X,Y) is contained within an arc section of a circle (i.e. a Pie slice)?

Imagine a circle. Imagine a pie. Imagine trying to return a bool that determines whether the provided parameters of X, Y are contained within one of those pie pieces.

What I know about the arc:

I have the CenterX, CenterY, Radius, StartingAngle, EndingAngle, StartingPoint (point on circumference), EndingPoint (point on circumference).

Given a coordinate of X,Y, I'd like to determine if this coordinate is contained anywhere within the pie slide.

Upvotes: 23

Views: 23365

Answers (4)

tada007
tada007

Reputation: 31

You have to convert atan2() to into 0-360 before making comparisons with starting and ending angles.

(A > 0 ? A : (2PI + A)) * 360 / (2PI)

Upvotes: 0

user7048690
user7048690

Reputation: 346

I know this question is old but none of the answers consider the placement of the arc on the circle.

This algorithm considers that all angles are between 0 and 360, and the arcs are drawn in positive mathematical direction (counter-clockwise)

First you can transform to polar coordinates: radius (R) and angle (A). Note: use Atan2 function if available. wiki

R = sqrt ((X - CenterX)^2 + (Y - CenterY)^2)

A = atan2 (Y - CenterY, X - CenterX)

Now if R < Radius the point is inside the circle.

To check if the angle is between StartingAngle (S) and EndingAngle (E) you need to consider two possibilities:

1) if S < E then if S < A < E the point lies inside the slice

image 1

2) if S > E then there are 2 possible scenarios

image 2

  • if A > S

then the point lies inside the slice

image 3

  • if A < E

then the point lies inside the slice

image 4

In all other cases the point lies outside the slice.

Upvotes: 23

Dr. Snoopy
Dr. Snoopy

Reputation: 56417

Convert X,Y to polar coordinates using this:

Angle = arctan(y/x); Radius = sqrt(x * x + y * y);

Then Angle must be between StartingAngle and EndingAngle, and Radius between 0 and your Radius.

Upvotes: 17

Wrikken
Wrikken

Reputation: 70540

Check:

  1. The angle from the centerX,centerY through X,Y should be between start&endangle.
  2. The distance from centerX,centerY to X,Y should be less then the Radius

And you'll have your answer.

Upvotes: 38

Related Questions