projects
/
experiments
/
RadialSymmetry.git
/ blobdiff
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Diagram.py: simplify setting bounding box fill and stroke when drawing text
[experiments/RadialSymmetry.git]
/
Diagram.py
diff --git
a/Diagram.py
b/Diagram.py
index
3a03949
..
4e14b1c
100755
(executable)
--- a/
Diagram.py
+++ b/
Diagram.py
@@
-97,30
+97,29
@@
class Diagram(object):
else:
return None
else:
return None
- def
draw_polygon(self, verts, stroke_color=[0, 0, 0], fill_color=None
):
+ def
_draw_polygon(self, verts
):
cr = self.cr
cr = self.cr
- if fill_color:
- v = verts[0]
- cr.move_to(v[0], v[1])
- for v in verts[1:]:
- cr.line_to(v[0], v[1])
- cr.close_path()
+ v = verts[0]
+ cr.move_to(v[0], v[1])
+ for v in verts[1:]:
+ cr.line_to(v[0], v[1])
+ cr.close_path()
+
+ def draw_polygon(self, verts, fill_color=None, stroke_color=[0, 0, 0]):
+ cr = self.cr
+ if fill_color:
+ self._draw_polygon(verts)
r, g, b, a = self.color_to_rgba(fill_color)
cr.set_source_rgba(r, g, b, a)
cr.fill()
r, g, b, a = self.color_to_rgba(fill_color)
cr.set_source_rgba(r, g, b, a)
cr.fill()
- n = len(verts)
- for i in range(0, n):
- v1 = verts[i]
- v2 = verts[(i + 1) % n]
- cr.move_to(v1[0], v1[1])
- cr.line_to(v2[0], v2[1])
-
- r, g, b, a = self.color_to_rgba(stroke_color)
- cr.set_source_rgba(r, g, b, a)
- cr.stroke()
+ if stroke_color:
+ self._draw_polygon(verts)
+ r, g, b, a = self.color_to_rgba(stroke_color)
+ cr.set_source_rgba(r, g, b, a)
+ cr.stroke()
def draw_star(self, cx, cy, verts, stroke_color=[0, 0, 0]):
cr = self.cr
def draw_star(self, cx, cy, verts, stroke_color=[0, 0, 0]):
cr = self.cr
@@
-133,14
+132,23
@@
class Diagram(object):
cr.set_source_rgba(r, g, b, a)
cr.stroke()
cr.set_source_rgba(r, g, b, a)
cr.stroke()
- def draw_dot(self, cx, cy, size=10.0, fill_color=[0, 0, 0, 0.5]):
+ def draw_circle(self, cx, cy, size=10.0, fill_color=[0, 0, 0, 0.5],
+ stroke_color=None):
cr = self.cr
cr.save()
cr = self.cr
cr.save()
- r, g, b, a = self.color_to_rgba(fill_color)
- cr.set_source_rgba(r, g, b, a)
cr.arc(cx, cy, size, 0, 2 * pi)
cr.arc(cx, cy, size, 0, 2 * pi)
- cr.fill()
+
+ if fill_color:
+ r, g, b, a = self.color_to_rgba(fill_color)
+ cr.set_source_rgba(r, g, b, a)
+ cr.fill()
+
+ if stroke_color:
+ r, g, b, a = self.color_to_rgba(stroke_color)
+ cr.set_source_rgba(r, g, b, a)
+ cr.stroke()
+
cr.restore()
def normalized_angle_01(self, theta):
cr.restore()
def normalized_angle_01(self, theta):
@@
-148,15
+156,14
@@
class Diagram(object):
def draw_line(self, x1, y1, x2, y2, stroke_color=[0, 0, 0, 1]):
cr = self.cr
def draw_line(self, x1, y1, x2, y2, stroke_color=[0, 0, 0, 1]):
cr = self.cr
- r, g, b, a = self.color_to_rgba(stroke_color)
- cr.set_source_rgba(r, g, b, a)
cr.move_to(x1, y1)
cr.line_to(x2, y2)
cr.move_to(x1, y1)
cr.line_to(x2, y2)
+ r, g, b, a = self.color_to_rgba(stroke_color)
+ cr.set_source_rgba(r, g, b, a)
cr.stroke()
def draw_rect_from_center(self, cx, cy, width, height, theta=0,
cr.stroke()
def draw_rect_from_center(self, cx, cy, width, height, theta=0,
- fill=True, fill_color=[1, 1, 1, 0.8],
- stroke=False, stroke_color=[0, 0, 0, 0.5]):
+ fill_color=[1, 1, 1, 0.8], stroke_color=None):
cr = self.cr
# the position of the center of a rectangle at (0,0)
cr = self.cr
# the position of the center of a rectangle at (0,0)
@@
-168,37
+175,35
@@
class Diagram(object):
rx = cx - (mx * cos(theta) - my * sin(theta))
ry = cy - (mx * sin(theta) + my * cos(theta))
rx = cx - (mx * cos(theta) - my * sin(theta))
ry = cy - (mx * sin(theta) + my * cos(theta))
- self.draw_rect(rx, ry, width, height, theta, fill, fill_color, stroke,
- stroke_color)
+ self.draw_rect(rx, ry, width, height, theta, fill_color, stroke_color)
- def draw_rect(self, x, y, width, height, theta=0,
fill=True, fill_color=[1, 1, 1, 0.8],
-
stroke=False, stroke_color=[0, 0, 0, 0.5]
):
+ def draw_rect(self, x, y, width, height, theta=0,
+
fill_color=[1, 1, 1, 0.8], stroke_color=None
):
cr = self.cr
cr.save()
cr.translate(x, y)
cr.rotate(theta)
cr = self.cr
cr.save()
cr.translate(x, y)
cr.rotate(theta)
- if fill:
+ if fill
_color
:
cr.rectangle(0, 0, width, height)
r, g, b, a = self.color_to_rgba(fill_color)
cr.set_source_rgba(r, g, b, a)
cr.fill()
cr.rectangle(0, 0, width, height)
r, g, b, a = self.color_to_rgba(fill_color)
cr.set_source_rgba(r, g, b, a)
cr.fill()
- if stroke:
+ if stroke
_color
:
cr.rectangle(0, 0, width, height)
r, g, b, a = self.color_to_rgba(stroke_color)
cr.set_source_rgba(r, g, b, a)
cr.stroke()
cr.rectangle(0, 0, width, height)
r, g, b, a = self.color_to_rgba(stroke_color)
cr.set_source_rgba(r, g, b, a)
cr.stroke()
- self.draw_dot(0, 0, 3.0, stroke_color)
cr.restore()
def draw_centered_text(self, cx, cy, text, theta=0,
color=[0, 0, 0],
align_baseline=False,
cr.restore()
def draw_centered_text(self, cx, cy, text, theta=0,
color=[0, 0, 0],
align_baseline=False,
- bb_fill
=True, bb_fill
_color=[1, 1, 1, 0.8],
- bb_stroke
=False, bb_stroke_color=[0, 0, 0, 0.5]
):
+ bb_fill_color=[1, 1, 1, 0.8],
+ bb_stroke
_color=None
):
cr = self.cr
x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
cr = self.cr
x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
@@
-229,8
+234,8
@@
class Diagram(object):
cr.translate(rx, ry)
cr.rotate(theta)
cr.translate(rx, ry)
cr.rotate(theta)
- if bb_fill
or bb_stroke
:
- self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0, bb_fill
, bb_fill_color, bb_stroke
, bb_stroke_color)
+ if bb_fill
_color or bb_stroke_color
:
+ self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0, bb_fill
_color
, bb_stroke_color)
r, g, b, a = self.color_to_rgba(color)
cr.set_source_rgba(r, g, b, a)
r, g, b, a = self.color_to_rgba(color)
cr.set_source_rgba(r, g, b, a)
@@
-254,36
+259,36
@@
if __name__ == "__main__":
theta = 0
theta = 0
- advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
x_offset += advance
- advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, align_baseline=True, bb_stroke
=True
)
+ advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, align_baseline=True, bb_stroke
_color=[0, 0, 0, 0.5]
)
x_offset += advance
diagram.draw_line(0, y, 400, y, [0, 0, 1, 0.2])
x_offset += advance
diagram.draw_line(0, y, 400, y, [0, 0, 1, 0.2])
- diagram.draw_rect(40, 40, 300, 100, stroke
=True
)
- diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke
=True
)
+ diagram.draw_rect(40, 40, 300, 100, stroke
_color=[0, 0, 0, 0.8]
)
+ diagram.draw_rect(40, 40, 300, 100, pi / 30, stroke
_color=[0, 0, 0, 0.8]
)
- diagram.draw_rect(40, 250, 300, 100, stroke
=True
)
- diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=
pi / 40, stroke=True, stroke_color=[1, 0, 0], fill=Fals
e)
+ diagram.draw_rect(40, 250, 300, 100, stroke
_color=[0, 0, 0, 0.8]
)
+ diagram.draw_rect_from_center(40 + 150, 250 + 50, 300, 100, theta=
(pi / 40), stroke_color=[1, 0, 0], fill_color=Non
e)
diagram.show()
diagram.show()