From 05bf6674ecd5c3266d5a52c2729f53eaca4c03c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=B6rg=20Lehmann?= Date: Sat, 17 Jan 2004 19:27:27 +0000 Subject: [PATCH] use radians and degrees for Python 2.2 and above git-svn-id: https://pyx.svn.sourceforge.net/svnroot/pyx/trunk/pyx@1248 069f4177-920e-0410-937b-c2a4a81bcd90 --- CHANGES | 7 ------- pyx/connector.py | 38 +++++++++++++++++++------------------- pyx/path.py | 38 ++++++++++++++++++++++---------------- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/CHANGES b/CHANGES index 0b5aab56..ada34d48 100644 --- a/CHANGES +++ b/CHANGES @@ -30,16 +30,12 @@ TODO: - path module: - reverse needs to be fixed - there should be a path._at, path._begin, path._end - - use math.degrees and math.radians - data module: - data.data should add line number like data.datafile - use csv module (new in 2.3) - canvas module: - multi page PostScript output - connnector module: - - use math.degrees and math.radians instead of self-written functions - (try to be compatible with old python versions, we should consider - support down to python2.0!) DEFERRED - _xxx -> xxx_pt renaming 0.5 (????/??/??): @@ -119,9 +115,6 @@ TODO: - _xxx -> xxx_pt renaming TODO for PyX 0.5 (please feel free to move items up to the general TODO list) - - path module: - - path.split takes several points (as *args) while lentopar takes a list of points (done, but fix docu) - - path.tangent is missing in the documentation - color module: - should colors and palettes exclude each other? - graph module: diff --git a/pyx/connector.py b/pyx/connector.py index 0be8d237..3ac0a1dc 100644 --- a/pyx/connector.py +++ b/pyx/connector.py @@ -1,6 +1,12 @@ import math from math import sin, cos, atan2, tan, hypot, acos, sqrt import path, trafo, unit, helper +try: + from math import radians, degrees +except ImportError: + # fallback implementation for Python 2.1 and below + def radians(x): return x*pi/180 + def degrees(x): return x*180/pi ######################### @@ -14,12 +20,6 @@ def _topt(length, default_type=None): else: return unit.topt(unit.length(length)) -def _torad(deg): - return deg * math.pi / 180 - -def _todeg(rad): - return rad * 180.0 / math.pi - class _connector(path.normpath): def omitends(self, box1, box2): @@ -96,9 +96,9 @@ class _arc(_connector): # otherwise use relangle else: bulge=None - try: radius = 0.5 * distance / abs(cos(0.5*math.pi - _torad(relangle))) + try: radius = 0.5 * distance / abs(cos(0.5*math.pi - radians(relangle))) except: radius = 10 * distance - try: center = tan(0.5*math.pi - _torad(relangle)) + try: center = tan(0.5*math.pi - radians(relangle)) except: center = 0 # up to here center is only @@ -113,11 +113,11 @@ class _arc(_connector): if (relangle is not None and relangle < 0) or (bulge is not None and bulge < 0): _connector.__init__(self, path.moveto_pt(*self.box1.center), - path.arcn_pt(center[0], center[1], radius, _todeg(angle1), _todeg(angle2))) + path.arcn_pt(center[0], center[1], radius, degrees(angle1), degrees(angle2))) else: _connector.__init__(self, path.moveto_pt(*self.box1.center), - path.arc_pt(center[0], center[1], radius, _todeg(angle1), _todeg(angle2))) + path.arc_pt(center[0], center[1], radius, degrees(angle1), degrees(angle2))) self.omitends(box1, box2) self.shortenpath(boxdists) @@ -144,13 +144,13 @@ class _curve(_connector): bulge = abs(distance*relbulge + absbulge) if absangle1 is not None: - angle1 = _torad(absangle1) + angle1 = radians(absangle1) else: - angle1 = dangle - _torad(relangle1) + angle1 = dangle - radians(relangle1) if absangle2 is not None: - angle2 = _torad(absangle2) + angle2 = radians(absangle2) else: - angle2 = dangle + _torad(relangle2) + angle2 = dangle + radians(relangle2) control1 = [cos(angle1), sin(angle1)] control2 = [cos(angle2), sin(angle2)] @@ -185,12 +185,12 @@ class _twolines(_connector): distance = hypot(*rel) dangle = atan2(rel[1], rel[0]) - if relangle1 is not None: relangle1 = _torad(relangle1) - if relangle2 is not None: relangle2 = _torad(relangle2) - if relangleM is not None: relangleM = _torad(relangleM) + if relangle1 is not None: relangle1 = radians(relangle1) + if relangle2 is not None: relangle2 = radians(relangle2) + if relangleM is not None: relangleM = radians(relangleM) # absangle has priority over relangle: - if absangle1 is not None: relangle1 = dangle - _torad(absangle1) - if absangle2 is not None: relangle2 = math.pi - dangle + _torad(absangle2) + if absangle1 is not None: relangle1 = dangle - radians(absangle1) + if absangle2 is not None: relangle2 = math.pi - dangle + radians(absangle2) # check integrity of arguments no_angles, no_lengths=0,0 diff --git a/pyx/path.py b/pyx/path.py index fc7f4945..8408c72e 100755 --- a/pyx/path.py +++ b/pyx/path.py @@ -31,6 +31,12 @@ import copy, math, string, bisect from math import cos, sin, pi +try: + from math import radians, degrees +except ImportError: + # fallback implementation for Python 2.1 and below + def radians(x): return x*pi/180 + def degrees(x): return x*180/pi import base, bbox, trafo, unit, helper ################################################################################ @@ -301,9 +307,9 @@ def _arctobcurve(x, y, r, phi1, phi2): def _arctobezierpath(x, y, r, phi1, phi2, dphimax=45): apath = [] - phi1 = phi1*pi/180 - phi2 = phi2*pi/180 - dphimax = dphimax*pi/180 + phi1 = radians(phi1) + phi2 = radians(phi2) + dphimax = radians(dphimax) if phi2phi1 ... @@ -1112,13 +1118,13 @@ class arc_pt(pathel): def _sarc(self): """Return starting point of arc segment""" - return (self.x+self.r*cos(pi*self.angle1/180), - self.y+self.r*sin(pi*self.angle1/180)) + return (self.x+self.r*cos(radians(self.angle1)), + self.y+self.r*sin(radians(self.angle1))) def _earc(self): """Return end point of arc segment""" - return (self.x+self.r*cos(pi*self.angle2/180), - self.y+self.r*sin(pi*self.angle2/180)) + return (self.x+self.r*cos(radians(self.angle2)), + self.y+self.r*sin(radians(self.angle2))) def _updatecontext(self, context): if context.currentpoint: @@ -1130,8 +1136,8 @@ class arc_pt(pathel): context.currentpoint = self._earc() def _bbox(self, context): - phi1=pi*self.angle1/180 - phi2=pi*self.angle2/180 + phi1 = radians(self.angle1) + phi2 = radians(self.angle2) # starting end end point of arc segment sarcx, sarcy = self._sarc() @@ -1237,13 +1243,13 @@ class arcn_pt(pathel): def _sarc(self): """Return starting point of arc segment""" - return (self.x+self.r*cos(pi*self.angle1/180), - self.y+self.r*sin(pi*self.angle1/180)) + return (self.x+self.r*cos(radians(self.angle1)), + self.y+self.r*sin(radians(self.angle1))) def _earc(self): """Return end point of arc segment""" - return (self.x+self.r*cos(pi*self.angle2/180), - self.y+self.r*sin(pi*self.angle2/180)) + return (self.x+self.r*cos(radians(self.angle2)), + self.y+self.r*sin(radians(self.angle2))) def _updatecontext(self, context): if context.currentpoint: @@ -1368,12 +1374,12 @@ class arct_pt(pathel): if rx==0: phi=90 elif rx>0: - phi = math.atan(ry/rx)/math.pi*180 + phi = degrees(math.atan(ry/rx)) else: - phi = math.atan(rx/ry)/math.pi*180+180 + phi = degrees(math.atan(rx/ry))+180 # half angular width of arc - deltaphi = 90*(1-alpha/math.pi) + deltaphi = 90*(1-alpha/pi) # center position of arc mx = self.x1-rx*self.r/(lr*sin(alpha/2)) -- 2.11.4.GIT