0

I have a Canvas on which I've drawn a circle / 360 degree arc. I have the arc start drawing from -90 (the top) rather than the right (0) as is default.

I want to place a rectangle at the top of the same canvas and to reduce the sweep of the arc so that the two do not intersect. I've attached an image to illustrate

enter image description here So what I need to do is work out what angle is represented by half of the rectangle so that I can adjust where to start drawing my arc. The centre of the circle is at the centre of my Canvas:

[canvas.width /2, canvas.height /2]

I've read some resources like this question but they haven't helped to do much more than make me feel like I know nothing. I tried a few failing formulas ending with this

double adjustment = Math.atan2(rectangleY - circleY, rectangleX - circleX) - Math.atan2(rectangleY - circleY, (rectangleX + rectangleWidth) - circleX);

Can somebody tell me what is the right way to calculate this in Java? I'd also like to know how to find where the rectangle intersects if that's possible (I.e. the width of the shaded orange part on the image) although this is of lesser importance to me right now

Community
  • 1
  • 1
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124

1 Answers1

1

Let W be the width of the rectangle, R be radius of the circle, and A be the angle you're looking for.

There's a right-angle triangle with angle A at the center, R as the hypotenuse and W/2 as the side opposite angle A, so

W/2R = sin(A)

so

A = Math.asin(0.5*W/R);

Of course you can't use asin(0.5W/R) when W > 2R

EDIT

To answer the second problem (finding the intersection)

Let H be the distance from the center of the circle to the rectangle. When the rectangle is wide enough that the circle intersects the lower side, there's a right angle triangle with A at the center, R as the hypotenuse, and H as the side adjacent to A

H/R = cos(A) and A = Math.acos(H/R). Calculate both angles and use the smaller one.

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Thank you, that made it very simple and I have it working with caveats - firstly I'm having to multiply the result because it gives results in the range 0.2 - 0.6 (E.g R=156.75, 0.5*W=95.0, A=0.651). The other caveat is that to allow me to use any size rectangle I want to calculate where it intersects (like in my image now). Do you have a formula for that? – Nick Cardoso Oct 29 '15 at 04:17
  • Let H be the distance from the center of the circle to the rectangle. When the rectangle is wide enough that the circle intersects the lower side, there's a right angel triangle with A at the center, R as the hypotenuse, and H as the side adjacent to A, so H/R = cos(A) and A = Math.acos(H/R). Calculate both angles and use the smaller one. Of course you can't use asin(0.5W/R) when W > 2R – Matt Timmermans Oct 29 '15 at 04:25
  • Thank you for the explanations – Nick Cardoso Oct 29 '15 at 04:52