1
$\begingroup$

Given an arc that is a function of Rectangle that forms the total ellipse and then the start and sweep angle to finally get the arc , i need to test if a point P(x,y) lies inside the segment formed by this arc or not.

Basically here's some more details about it :

/// Draw an arc scaled to fit inside the given rectangle. It starts from
  /// startAngle radians around the oval up to startAngle + sweepAngle
  /// radians around the oval, with zero radians being the point on
  /// the right hand side of the oval that crosses the horizontal line
  /// that intersects the center of the rectangle and with positive
  /// angles going clockwise around the oval. If useCenter is true, the arc is
  /// closed back to the center, forming a circle sector. Otherwise, the arc is
  /// not closed, forming a circle segment.
  ///
  drawArc(Rect rect, double startAngle, double sweepAngle, bool useCenter)

Things that i want to know :

  • What's the equation of the arc that I get above
  • How to test if P(x,y) lies inside the segment formed by the arc
  • How to test if P(x,y) lies inside the sector formed by the arc
  • How to test if P(x,y) lies on the arc itself ( on the curved line )
  • How to test if P(x,y) lies on the arc segment or sector border (not inside but on the segment or sector boundary line)
  • How to test if P(x,y) lies on the arc segment or sector border given that the border is thick and has some width.

What data I have :

  • Full information about the rectangle ( its center , top , left , right , bottom)
  • the start angle (in radian say pi/2)
  • the end angle (in radian say pi)

Update :

  • The center (focus) of the arc is not assumed to be origin and could be anywhere.
  • The start and end angle are the actual angles and not part of some parametric equation. The angle could be anything and not just multiples of pi or pi/2 or anything like that.

enter image description here

$\endgroup$
4
  • $\begingroup$ Is a "start angle" or "end angle" the $\theta$ input of the parametric equations $x = a\cos\theta,$ $y = b\sin\theta,$ or is it the actual angle from the origin to the start or end of the arc? If the angle is a multiple of $\frac\pi2$ this makes no difference, but every other angle will give two different arcs depending on how you answer that question. $\endgroup$
    – David K
    Apr 12 '20 at 19:45
  • $\begingroup$ For "the equation" of the arc, do you want a set of parametric equations or a set of equations and inequalities using just $x$ and $y$, like the equation $x^2+y^2=1$ for the unit circle? $\endgroup$
    – David K
    Apr 13 '20 at 2:34
  • $\begingroup$ Suggest you add a picture that clarifies to you are seeking and its relevance to the rectangle. Usually the tests are straightforward. $\endgroup$
    – Moti
    Apr 13 '20 at 5:13
  • $\begingroup$ I have updated my question. @DavidK $\endgroup$ Apr 13 '20 at 7:37
1
$\begingroup$

Equations of the ellipse

If you have a rectangle with center $(h,k),$ width $2a$ parallel to the $x$ axis, and height $2b$ parallel to the $y$ axis, then the equation of the ellipse is

$$ \frac{(x - h)^2}{a^2} + \frac{(y-k)^2}{b^2} = 1. \tag1$$

That is, a point is on the ellipse if it satisfies Equation $(1)$. The condition for a point to be inside the ellipse is

$$ \frac{(x - h)^2}{a^2} + \frac{(y-k)^2}{b^2} < 1. \tag2$$

And of course a point is on or inside the ellipse if it satisfies $$ \frac{(x - h)^2}{a^2} + \frac{(y-k)^2}{b^2} \leq 1. \tag3$$

There is also a set of parametric equations for the ellipse:

\begin{align} x &= a \cos \phi \\ y &= b \sin \phi, \end{align}

which look somewhat like the parametric equations for the unit circle, except that we have introduced new factors ($a$ and $b$) and the parameter $\phi$ is not the angular direction to the point $(x,y)$ from the origin. Instead, if the angular direction to $(x,y)$ is $\theta,$ then

$$ a \tan\theta = b \tan\phi, $$

which enables conversion of any angle $\theta$ to the parameter $\phi$ and vice versa.

Finding the endpoints of the arc

Let $\theta$ be the angular direction from the origin to one endpoint of the arc (either the start or the end, take your pick). There are at least two ways to get the coordinates $(x,y)$ of this endpoint. One is to solve a system of two equations: Equation $(1)$ together with the equation

$$ (x - h) \sin\theta = (y - k) \cos\theta. $$

Depending on $\theta,$ this gives either $x=0,$ $y=0,$ or $y$ as a linear function of $x$; you can make this substitution into Equation $(1)$ and solve using the quadratic formula. You will get two answers; choose the one in which the signs of $x$ and $y$ agree with the signs of $\cos\theta$ and $\sin\theta,$ respectively.

Another way to find the coordinates $(x,y)$ of the endpoint is to set

$$ \phi = \mathrm{atan2}(a \sin\theta, b \cos\theta) $$

where $\mathrm{atan2}(p,q)$ is the function that gives $\pm\arctan\frac pq$ with the sign chosen so that the sine and cosine of the result have the same signs as $p$ and $q,$ respectively. In other words, $\mathrm{atan2}(p,q)$ gives the direction to $(q,p).$ If you must use the ordinary arc tangent, then take $\phi = \pm\arctan\left(\frac{a}{b} \tan\theta\right),$ choosing the sign to be the same as the sign of $\cos\theta.$

Testing if $(x,y)$ is inside the segment of the arc

Find the endpoints of the segment. Find the equation of the line through those two points. Find an inequality satisfied by all points on the same side of that line as the arc. (There are several ways to do this.)

The points in the segment are the points that satisfy the inequality in the previous paragraph and also satisfy Inequality $(2)$ above.

Testing if $(x,y)$ is inside the sector of the arc

For starting angle $\theta_1$ and ending angle $\theta_2,$ write an inequality for being on the "correct" side of the line $y = k + (x - h) \tan\theta_1$ and one for being on the "correct" side of the line $y = k + (x - h) \tan\theta_2.$

In the example in the question, the selected arc is inside a $30$ degree angle between lines from the center of the ellipse and is less than half of the entire ellipse. But if the arc goes counterclockwise from a start angle of $20$ degrees to an end angle of $220$ degrees, for example, the arc is more than half of the ellipse; the remaining part of the ellipse that is not part of the arc is smaller.

If the arc is less than half of the ellipse, $(x,y)$ is in the sector only if the inequality for $\theta_1$ is true and the inequality for $\theta_2$ also is true. If the arc is more than half of the ellipse then the required condition is that at least one of the inequalities is true; it is not necessary for both to be true. In either case (less than half the ellipse or more than half), $(x,y)$ is inside the sector only if Inequality $(2)$ is true.

Testing if $(x,y)$ is on the arc

This is the same as testing for "inside the segment" or "inside the sector" (your choice), except that instead of using Inequality $(2)$ you use Equation $(1)$.

Testing if $(x,y)$ is on the segment or sector border

In addition to the arc, the boundary of the segment consists of the line segment between the endpoints of the arc, and the boundary of the sector consists of the two line segments between $(h,k)$ and the endpoints of the arc.

There are well-known ways to determine whether a point $(x,y)$ is on the line segment between two given points. For the segment of the ellipse, however, and alternative is to check whether $(x,y)$ satisfies the equation of the line through the two endpoints of the arc and also satisfies Inequality $(3)$.

For the sector you can check whether $(x,y)$ is in one of the correct directions from $(h,k)$ (either $\theta_1$ or $\theta_2$) and also satisfies Inequality $(3)$. But this has disadvantages for practical computing, since it is hard to set tolerances on an angle to cover numerical errors in the distance from a line.

Testing if $(x,y)$ is on a thick segment or sector border

In most cases this comes down to whether $(x,y)$ is within half a border's thickness of the nearest curve or line segment bounding the ellipse. There is an extra complication at the "corners" of the segment or sector; is the border rounded at those points, mitered, or finished some other way?

Distance from a line is relatively easy to compute, but distance from an ellipse is relatively messy. In general it involves a quartic (fourth-degree) polynomial. See the answers of Distance of point $P$ from an ellipse or Calculating Distance of a Point from an Ellipse Border for some clues.

$\endgroup$
4
  • $\begingroup$ This has been of great help. thanks :D $\endgroup$ Apr 15 '20 at 0:17
  • $\begingroup$ in testing if the point is inside sector : what do you mean by "If the arc is less than half of the ellipse, both inequalities must be true;" $\endgroup$ Apr 15 '20 at 0:51
  • $\begingroup$ "Less than half of the ellipse": start $20^\circ$ and end $50^circ$ is less than half; start $20^\circ$ and end $190^circ$ is less than half; start $20^\circ$ and end $210^circ$ is more than half. "Both inequalities must be true": if the point is on the "correct" side of the line at the start angle and is on the "correct" side of the line for the end angle, it is in the sector; otherwise it is not. $\endgroup$
    – David K
    Apr 15 '20 at 1:25
  • $\begingroup$ Thanks again :) u have been immensely helpful man 🙏🏻 $\endgroup$ Apr 18 '20 at 5:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.