Skip to content

Commit 6844b56

Browse files
authored
bpo-41528: Use math module in turtle (GH-21837)
Use angle-related functions from math module instead of reinventing the wheel.
1 parent e6905e4 commit 6844b56

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

Lib/turtle.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ def __sub__(self, other):
263263
def __neg__(self):
264264
return Vec2D(-self[0], -self[1])
265265
def __abs__(self):
266-
return (self[0]**2 + self[1]**2)**0.5
266+
return math.hypot(*self)
267267
def rotate(self, angle):
268268
"""rotate self counterclockwise by angle
269269
"""
270270
perp = Vec2D(-self[1], self[0])
271-
angle = angle * math.pi / 180.0
271+
angle = math.radians(angle)
272272
c, s = math.cos(angle), math.sin(angle)
273273
return Vec2D(self[0]*c+perp[0]*s, self[1]*c+perp[1]*s)
274274
def __getnewargs__(self):
@@ -1597,7 +1597,7 @@ def radians(self):
15971597
>>> turtle.heading()
15981598
1.5707963267948966
15991599
"""
1600-
self._setDegreesPerAU(2*math.pi)
1600+
self._setDegreesPerAU(math.tau)
16011601

16021602
def _go(self, distance):
16031603
"""move turtle forward by specified distance"""
@@ -1888,7 +1888,7 @@ def towards(self, x, y=None):
18881888
elif isinstance(x, TNavigator):
18891889
pos = x._position
18901890
x, y = pos - self._position
1891-
result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
1891+
result = round(math.degrees(math.atan2(y, x)), 10) % 360.0
18921892
result /= self._degreesPerAU
18931893
return (self._angleOffset + self._angleOrient*result) % self._fullcircle
18941894

@@ -1903,7 +1903,7 @@ def heading(self):
19031903
67.0
19041904
"""
19051905
x, y = self._orient
1906-
result = round(math.atan2(y, x)*180.0/math.pi, 10) % 360.0
1906+
result = round(math.degrees(math.atan2(y, x)), 10) % 360.0
19071907
result /= self._degreesPerAU
19081908
return (self._angleOffset + self._angleOrient*result) % self._fullcircle
19091909

@@ -1976,7 +1976,7 @@ def circle(self, radius, extent = None, steps = None):
19761976
steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
19771977
w = 1.0 * extent / steps
19781978
w2 = 0.5 * w
1979-
l = 2.0 * radius * math.sin(w2*math.pi/180.0*self._degreesPerAU)
1979+
l = 2.0 * radius * math.sin(math.radians(w2)*self._degreesPerAU)
19801980
if radius < 0:
19811981
l, w, w2 = -l, -w, -w2
19821982
tr = self._tracer()
@@ -2861,7 +2861,7 @@ def settiltangle(self, angle):
28612861
>>> turtle.fd(50)
28622862
"""
28632863
tilt = -angle * self._degreesPerAU * self._angleOrient
2864-
tilt = (tilt * math.pi / 180.0) % (2*math.pi)
2864+
tilt = math.radians(tilt) % math.tau
28652865
self.pen(resizemode="user", tilt=tilt)
28662866

28672867
def tiltangle(self, angle=None):
@@ -2885,7 +2885,7 @@ def tiltangle(self, angle=None):
28852885
>>> turtle.tiltangle()
28862886
"""
28872887
if angle is None:
2888-
tilt = -self._tilt * (180.0/math.pi) * self._angleOrient
2888+
tilt = -math.degrees(self._tilt) * self._angleOrient
28892889
return (tilt / self._degreesPerAU) % self._fullcircle
28902890
else:
28912891
self.settiltangle(angle)
@@ -2939,7 +2939,7 @@ def shapetransform(self, t11=None, t12=None, t21=None, t22=None):
29392939
if t11 * t22 - t12 * t21 == 0:
29402940
raise TurtleGraphicsError("Bad shape transform matrix: must not be singular")
29412941
self._shapetrafo = (m11, m12, m21, m22)
2942-
alfa = math.atan2(-m21, m11) % (2 * math.pi)
2942+
alfa = math.atan2(-m21, m11) % math.tau
29432943
sa, ca = math.sin(alfa), math.cos(alfa)
29442944
a11, a12, a21, a22 = (ca*m11 - sa*m21, ca*m12 - sa*m22,
29452945
sa*m11 + ca*m21, sa*m12 + ca*m22)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
turtle uses math module functions to convert degrees to radians and vice versa and to calculate vector norm

0 commit comments

Comments
 (0)