Penn - 11 months ago 45

Python Question

I need to draw an arc in pygame, through three points, however pygame's arc function doesn't seem to support this. I have researched three point arcs, both in and out of pygame, however cannot find anything that I understand and is useful.

I would ideally like a full code however any hints will be useful

Penn

Answer

Here are some hints, which will require a little mathematics to finish. If you need more help, show more of your work and ask.

Let's say you want to draw a circular arc from point `a=(ax, ay)`

through point `b=(bx, by)`

to point `c=(cx, cy)`

. You want to use the function

`pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)`

So you need to find `Rect`

, the bounding rectangle (which will be a square) of the circle, and the start and stop angles. Here is an outline.

- Use your point coordinates to calculate the center of the relevant circle, giving you point
`u=(ux, uy)`

. If that calculation raises a divide-by-zero or overflow error, the points are (nearly) in a straight line, so just draw a line segment from point`a`

to point`c`

and you are done. - Calculate the distance from point
`u`

to point`a`

(or`b`

or`c`

, it doesn't matter). That will be the radius of the desired circle. - Use point
`u`

and the radius to calculate the parameters of the bounding rectangle`Rect`

. The rectangle will actually be a square, and point`u`

will be its center. - Use a little trigonometry to calculate the direction angle from point
`u`

to point`a`

: this (almost) will be`start_angle`

. Be careful, since Cartesian coordinates have increasing y go up, while in pygame increasing y goes down. Also calculate the direction angle from point`u`

to point`c`

: this (almost) will be`stop_angle`

. Then calculate the direction angle from point`u`

to point`b`

. - Examine those three direction angles. If necessary, swap the start and stop angles and/or add 2*pi to the stop angle to ensure that the start angle is less than the stop angle and that the arc goes through point
`b`

.

You now have `Rect`

, `start_angle`

, and `stop_angle`

. This outline ignores some subtle issues such as rounding to integers, but I'll leave those to you.