Debian Codegraph @

src/igraph/drawing/cairo/coord.py

raw
1 """
2 Coordinate systems and related plotting routines
3 """
4
5 from abc import abstractmethod
6
7 from igraph.drawing.cairo.base import AbstractCairoDrawer
8 from igraph.drawing.utils import BoundingBox
9
10 #####################################################################
11
12
13 class CoordinateSystem(AbstractCairoDrawer):
14 """Class implementing a coordinate system object.
15
16 Coordinate system objects are used when drawing plots which
17 2D or 3D coordinate system axes. This is an abstract class
18 which must be extended in order to use it. In general, you'll
19 only need the documentation of this class if you intend to
20 implement an own coordinate system not present in igraph yet.
21 """
22
23 @abstractmethod
24 def draw(self):
25 """Draws the coordinate system.
26
27 This method must be overridden in derived classes.
28 """
29 raise NotImplementedError
30
31 @abstractmethod
32 def local_to_context(self, x, y):
33 """Converts local coordinates to the context coordinate system (given
34 by the bounding box).
35
36 This method must be overridden in derived classes."""
37 raise NotImplementedError
38
39
40 class DescartesCoordinateSystem(CoordinateSystem):
41 """Class implementing a 2D Descartes coordinate system object."""
42
43 def __init__(self, context, bbox, bounds):
44 """Initializes the coordinate system.
45
46 @param context: the context on which the coordinate system will
47 be drawn.
48 @param bbox: the bounding box that will contain the coordinate
49 system.
50 @param bounds: minimum and maximum X and Y values in a 4-tuple.
51 """
52 self._bounds, self._bbox = None, None
53 self._sx, self._sy = None, None
54 self._ox, self._oy, self._ox2, self._oy2 = None, None, None, None
55
56 super().__init__(context, bbox)
57
58 self.bbox = bbox
59 self.bounds = bounds
60
61 @property
62 def bbox(self):
63 """Returns the bounding box of the coordinate system"""
64 return BoundingBox(self._bbox.coords)
65
66 @bbox.setter
67 def bbox(self, bbox):
68 """Sets the bounding box of the coordinate system"""
69 self._bbox = bbox
70 self._recalc_scale_factors()
71
72 @property
73 def bounds(self):
74 """Returns the lower and upper bounds of the X and Y values"""
75 return self._bounds.coords
76
77 @bounds.setter
78 def bounds(self, bounds):
79 """Sets the lower and upper bounds of the X and Y values"""
80 self._bounds = BoundingBox(bounds)
81 self._recalc_scale_factors()
82
83 def _recalc_scale_factors(self):
84 """Recalculates some cached scale factors used within the class"""
85 if self._bounds is None:
86 return
87 self._sx = self._bbox.width / self._bounds.width
88 self._sy = self._bbox.height / self._bounds.height
89 self._ox = self._bounds.left
90 self._oy = self._bounds.top
91 self._ox2 = self._bbox.left
92 self._oy2 = self._bbox.bottom
93
94 def draw(self):
95 """Draws the coordinate system."""
96 # Draw the frame
97 coords = self.bbox.coords
98 self.context.set_source_rgb(0.0, 0.0, 0.0)
99 self.context.set_line_width(1)
100 self.context.rectangle(
101 coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]
102 )
103 self.context.stroke()
104
105 def local_to_context(self, x, y):
106 """Converts local coordinates to the context coordinate system (given
107 by the bounding box).
108 """
109 return (x - self._ox) * self._sx + self._ox2, self._oy2 - (
110 y - self._oy
111 ) * self._sy