Yanick Rochon
Yanick Rochon

Reputation: 53636

Calculate angles that are in degrees (clockwise) to radians (counter clockwise)

First, I am aware of how converting degrees in radians.

I need to draw an arc given a coordinate, radius, and two angles in degrees clockwise in pygame. For example:

point = (50, 50)
startAngle = 320
endAngle = 140

should draw something like

enter image description here

and

point = (50, 50)
startAngle = 90
endAngle = 180

should draw something like

enter image description here

etc.

I have tried reverting the angle (i.e. 360 - angle) but pygame draws the arc in reverse; instead of a 45⁹ arc in the last image, I get a 270⁹ arc that is the complement of what I want.

I guess I'm having a brain fart day because I can't figure this out. Thank you!


Edit : I may have an answer, though I'm not sure if it is a good one. If I reverse the angles and negate them, it seems to draw them correctly clockwise. For example, given the first example :

point = (50, 50)
startAngle = 360 - 140
endAngle = 360 - 320

seems to correctly draws the arc where expected.

Upvotes: 2

Views: 802

Answers (1)

Rabbid76
Rabbid76

Reputation: 211277

If you want to draw an clockwise arc, then you've to invert the angles and you've to swap the start and end angle:

def clockwiseArc(surface, color, point, radius, startAngle, endAngle):
    rect = pygame.Rect(0, 0, radius*2, radius*2)
    rect.center = point

    endRad   = math.radians(-startAngle)
    startRad = math.radians(-endAngle)

    pygame.draw.arc(surface, color, rect, startRad, endRad)

e.g.:

clockwiseArc(window, (255, 0, 0), (150, 70), 50, 300, 140) 
clockwiseArc(window, (255, 0, 0), (300, 70), 50, 90, 180) 

Upvotes: 1

Related Questions