Skip to content

Commit b064d9a

Browse files
committed
Update Pygame Angles to use standard angles
1 parent f2cd690 commit b064d9a

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

compile

-4
This file was deleted.

fives.pyc

134 Bytes
Binary file not shown.

pygame_angles.py

+23-28
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44
55
In pygame, +y is down. Python's arctangent functions expect +y to be up.
66
This wreaks havoc with the unit circle if you want to find the angle between
7-
two points (for, say, collision detection or aiming a gun).
7+
two points (for, say, collision detection or aiming a gun). You can avoid the
8+
problem entirely by calling atan2(-y, x) and adding 2*pi to the result if it's
9+
negative.
810
911
Note that math.atan2(numerator, denominator) does the division for you.
1012
11-
This applet demonstrates the confusing result. The range of the arctangent
12-
(-pi, pi], which is mathematically accurate but hard to work with. Much worse
13-
is that the direction of increasing angular measure is reversed!
14-
15-
You can avoid the problem entirely by calling atan2(-y, x) and adding 2*pi to
16-
the result if it's negative.
17-
1813
Controls: move the mouse near the axes.
1914
2015
"""
2116

2217
import pygame, sys, os
2318
from pygame.locals import *
2419
from math import atan2, degrees, pi
25-
halfpi = pi/2.0
2620

2721
def quit():
2822
pygame.quit()
@@ -67,35 +61,36 @@ def quit():
6761
if 0 < event.pos[0] < 400 and 0 < event.pos[1] < 400:
6862
pos = event.pos
6963

70-
x = pos[0] - origin[0]
71-
y = pos[1] - origin[1]
72-
theta = atan2(y,x)
64+
#Angle logic
65+
dx = pos[0] - origin[0]
66+
dy = pos[1] - origin[1]
67+
rads = atan2(-dy,dx)
68+
rads %= 2*pi
69+
degs = degrees(rads)
7370

7471
screen.fill(white)
7572

73+
#Draw coordinate axes and labels
7674
pygame.draw.circle(screen, black, origin, 5)
7775
pygame.draw.line(screen, black, (0, 440), (440, 440), 3)
7876
pygame.draw.line(screen, black, (200, 15), (200, 380))
79-
screen.blit(font.render("-pi/2", True, black), (180,10))
80-
screen.blit(font.render( "pi/2", True, black), (187, 380))
81-
pygame.draw.line(screen, black, (10, 200), (355, 200))
82-
screen.blit(font.render("-pi", True, black), (5, 180))
83-
screen.blit(font.render( "pi", True, black), (12, 200))
77+
screen.blit(font.render( "pi/2", True, black), (178, 10))
78+
screen.blit(font.render("3pi/2", True, black), (175, 380))
79+
pygame.draw.line(screen, black, (28, 200), (355, 200))
80+
screen.blit(font.render("pi", True, black), (8, 190))
8481
screen.blit(font.render("0", True, black), (360, 190))
8582

83+
#Draw lines to cursor
8684
pygame.draw.line(screen, blue, origin, (pos[0], origin[1]), 2)
8785
pygame.draw.line(screen, red, (pos[0], origin[1]), (pos[0], pos[1]), 2)
8886
pygame.draw.line(screen, purple, origin, pos, 2)
89-
if theta < 0:
90-
pygame.draw.arc(screen, green, arcRect, 0, -theta, 4)
91-
else:
92-
pygame.draw.arc(screen, green, arcRect, -theta, 0, 4)
93-
94-
screen.blit(font.render(str(x)+" x", True, blue), (10, 450))
95-
screen.blit(font.render(str(y)+" y", True, red), (100, 450))
96-
screen.blit(font.render(str((x**2 + y**2)**.5)+" d", True, purple), (10, 480))
97-
screen.blit(font.render("arctangent(y/x) =", True, black), (10, 510))
98-
screen.blit(font.render(str(degrees(theta))+" deg", True, green), (30, 540))
99-
screen.blit(font.render(str(theta/pi)+"*pi rad", True, green), (30, 570))
87+
pygame.draw.arc(screen, green, arcRect, 0, rads, 4)
88+
#Note that the function expects angles in radians
89+
90+
#Draw numeric readings
91+
screen.blit(font.render(str(dx)+" x", True, blue), (10, 450))
92+
screen.blit(font.render(str(dy)+" y", True, red), (100, 450))
93+
screen.blit(font.render(str((dx**2 + dy**2)**.5)+" d", True, purple), (10, 480))
94+
screen.blit(font.render(str(degs)+" degrees", True, green), (10, 510))
10095

10196
pygame.display.flip()

0 commit comments

Comments
 (0)