remove shebang -- see comment 3 on https://bugzilla.redhat.com/bugzilla/show_bug...
[PyX/mjg.git] / pyx / graph / axis / positioner.py
blob7ab89a7faba39e2a71886770b31037215d93b0a0
1 # -*- coding: ISO-8859-1 -*-
4 # Copyright (C) 2005 André Wobst <wobsta@users.sourceforge.net>
6 # This file is part of PyX (http://pyx.sourceforge.net/).
8 # PyX is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # PyX is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with PyX; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import math
23 from pyx import path, unit
26 class _positioner:
27 """interface definition of axis tick position methods
28 - these methods are used for the postitioning of the ticks
29 when painting an axis"""
30 # TODO: should we add a local transformation (for label text etc?)
31 # (this might replace tickdirection (and even tickposition?))
33 def vbasepath(self, v1=None, v2=None):
34 """return the basepath as a path
35 - like basepath, but for graph coordinates"""
37 def vgridpath(self, v):
38 """return the gridpath as a path for a given position v
39 in graph coordinates
40 - might return None when no gridpath is available"""
41 return None
43 def vtickpoint_pt(self, v):
44 """return tick position (x, y) in pts for a tick at position v in graph coordinates"""
46 def vtickdirection(self, v):
47 """return direction tuple (dx, dy) for a tick at position v in graph coordinates"""
50 class pathpositioner(_positioner):
51 """axis tick position methods along an arbitrary path"""
53 def __init__(self, p, direction=1):
54 self.path = p
55 self.normpath = p.normpath()
56 self.arclen_pt = self.normpath.arclen_pt()
57 self.arclen = self.arclen_pt * unit.t_pt
58 self.direction = direction
60 def vbasepath(self, v1=None, v2=None):
61 if v1 is None:
62 if v2 is None:
63 return self.path
64 else:
65 return self.normpath.split(self.normpath.arclentoparam(v2 * self.arclen))[0]
66 else:
67 if v2 is None:
68 return self.normpath.split(self.normpath.arclentoparam(v1 * self.arclen))[1]
69 else:
70 return self.normpath.split(*self.normpath.arclentoparam([v1 * self.arclen, v2 * self.arclen]))[1]
72 def vtickpoint_pt(self, v):
73 return self.normpath.at_pt(self.normpath.arclentoparam(v * self.arclen))
75 def vtickdirection(self, v):
76 return self.normpath.rotation(self.normpath.arclentoparam(v * self.arclen)).apply_pt(0, self.direction)
79 class lineaxispos_pt:
80 """an axispos linear along a line with a fix direction for the ticks"""
82 def __init__(self, x1_pt, y1_pt, x2_pt, y2_pt, fixtickdirection, vgridpathfunction):
83 self.x1_pt = x1_pt
84 self.y1_pt = y1_pt
85 self.x2_pt = x2_pt
86 self.y2_pt = y2_pt
87 self.fixtickdirection = fixtickdirection
88 self.vgridpathfunction = vgridpathfunction
90 def vbasepath(self, v1=None, v2=None):
91 if v1 is None:
92 v1 = 0
93 if v2 is None:
94 v2 = 1
95 return path.line_pt((1-v1)*self.x1_pt+v1*self.x2_pt,
96 (1-v1)*self.y1_pt+v1*self.y2_pt,
97 (1-v2)*self.x1_pt+v2*self.x2_pt,
98 (1-v2)*self.y1_pt+v2*self.y2_pt)
100 def vgridpath(self, v):
101 return self.vgridpathfunction(v)
103 def vtickpoint_pt(self, v):
104 return (1-v)*self.x1_pt+v*self.x2_pt, (1-v)*self.y1_pt+v*self.y2_pt
106 def vtickdirection(self, v):
107 return self.fixtickdirection