Need help getting a point on a circle edge using an axis

Hello everyone, I’m having trouble solving this issue with my circular grid system. I’ve calculated the corner positions of the rectangle so that I can calculate the distance that the rectangle needs to move inwards so that the corners are contained inside the circle. The issue occurs when I need to get the position on the circle edge where the corner needs to be. Right now, It’s calculating the red points by getting the circle edge position using the center of the circle, but the position we need is straight down. I’m sure there’s some sort of math to be done here, if anyone can point me in the right direction it would be greatly appreciated!

All I need is that length between the corner and the circle edge on the left.

In this screenshot, you can see how the corners have not been calculated correctly.

Is the rectangle always going to be this size? Is the circle always going to be this size? Is the circle a shadow, an actual part, or neither? Either way, from the center of the circle you should be able to determine an angle that would give you that point on the circle you’re looking for.

(I know this response might’ve not been super helpful, currently figuring out the math on pen and paper). The answers to these questions could just provide other possible solutions.

1 Like

Thanks for the help! The rectangle is the items boundary box so the top left corner will be calculated each time. The circle canvas is just for looks but it’s exactly 40 studs in radius and the angles are being calculated using cos, sin and an angle.

The red line is how it’s being calculated, but it needs to be calculated like the green line. I can get the coordinates from the shifted center to the corner but how do I calculate a third point where it intersected my original circle?

You’ll find the trigonometry you need here: Chord (geometry) - Wikipedia

Basically, you take your circle radius and chord length (30 for the example), and calculate the angle theta for the chord using the formula on the wiki page. Then you have a triangle where two sides have the radius for their length and the base is the chord (30). The offset amount you need is the circle radius minus the height of this isoceles triangle. Finding the height is easy, because the triangle can be divided into two right triangles with hypotenuse r, base of chord/2 (15), and angles that are theta/2, 90 and 90-theta/2. So the height of that triangle that you need is just r * cos(theta/2).

So the offset amount you indicate with the “?” is:

offset = r - r * math.cos( math.asin ( c / (2 *r) ))

Where c is the chord length, i.e. the width of your rectangle, 30, and r is the circle radius.

EDIT: I realized as I wrote the first answer, that there is another simpler way. You have a right triangle with hypotenuse that’s the radius of the circle r, and one other side that is half the length of the chord you’re after (so 15). So Pythagorean theorem can give you the length of the missing side just as readily as the chord trigonometry. Let’s call the length of that side h, h = math.sqrt( r^2 - (c/2)^2) where c is the full chord length of 30 in your example. The offset you need is r - h.

Suppose for example that your circle also has radius 30. Then the offset you calculate by either means above will come out to ~4.0192

4 Likes

Thank you so much! Amazing how simple you made that seem, I’m inspired! Even solved it 2 different ways lol

1 Like