Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an arc between two points pygame

I am trying to create a smooth curve between two straight lines with known gradients, I have found an equation to find theses two point but when i try to find the angle for each of the points the arc doesn't line up.I have done the math and it (should) be right.

def angle(A,B):
    o = abs(B[1]-A[1])
    a = abs(B[0] - A[0])
    angle = math.atan(o/a)
    return angle

a1 = angle((250, 175),(75, 300))
a2 = angle((250, 175),(400, 315))
pygame.draw.arc(gameDisplay, WHITE, (0 ,0 ,500,350), (math.pi) + a2,(2*math.pi) - a1)
pygame.draw.line(gameDisplay, BLUE, [400,0], [400, 500], 1)
pygame.draw.line(gameDisplay, BLUE, [0,315], [1000, 315], 1)   
pygame.draw.line(gameDisplay, RED, [75,0], [75, 500], 1)
pygame.draw.line(gameDisplay, RED, [0,300], [1000, 300], 1)

This is the basic code for finding the start and stop angle, with (250,175) being the center of the ellipse and (75, 300) and (400, 315) being the two points. The lines are where the arc should start and stop.

like image 374
OzzyJosh Avatar asked Apr 29 '26 22:04

OzzyJosh


1 Answers

You do not draw a perfect circular arc, actually you are drawing an elliptical arc. You've to take into account the rectangular area, which is passed to pygame.draw.arc() ((0, 0, 500, 350)), when the start and end angle for the arc is calculated.
As mention in the comments you've to use math.atan2 rather than math.atan to get angles in the range [-pi, pi]. In pygame, the y axis points downwards, so the y component of the direction vector has to be inverted

def angle(A, B, aspectRatio):
    x = B[0] - A[0]
    y = B[1] - A[1]
    angle = math.atan2(-y, x / aspectRatio)
    return angle

a1 = angle((250, 175), (75, 300), 500/350)
a2 = angle((250, 175),(400, 315), 500/350)

pygame.draw.arc(gameDisplay, WHITE, (0, 0, 500, 350), a1, a2)

like image 53
Rabbid76 Avatar answered May 02 '26 10:05

Rabbid76