X-Git-Url: https://git.ao2.it/experiments/RadialSymmetry.git/blobdiff_plain/3ad27a2c463cbaeb6989828ed1925869e55e193a..3286ef9194ac827cfa6d93aac6036214631f69c9:/Diagram.py?ds=sidebyside diff --git a/Diagram.py b/Diagram.py index 8d958f9..7d6348b 100755 --- a/Diagram.py +++ b/Diagram.py @@ -23,7 +23,7 @@ from math import * class Diagram(object): - def __init__(self, width, height, background=[1, 1, 1]): + def __init__(self, width, height, background=[1, 1, 1], font_size=20): self.width = width self.height = height self.background = background @@ -38,7 +38,7 @@ class Diagram(object): cr.select_font_face("Georgia", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) - cr.set_font_size(20) + cr.set_font_size(font_size) # Adjust the font matrix to left-bottom origin M = cr.get_font_matrix() @@ -161,37 +161,25 @@ class Diagram(object): stroke=False, stroke_color=[0, 0, 0, 0.5]): cr = self.cr + # the position of the center of a rectangle at (0,0) mx = width / 2.0 my = height / 2.0 + # calculate the position of the bottom-left corner after rotating the + # rectangle around the center rx = cx - (mx * cos(theta) - my * sin(theta)) ry = cy - (mx * sin(theta) + my * cos(theta)) - cr.save() - cr.translate(rx, ry) - cr.rotate(theta) + self.draw_rect(rx, ry, width, height, theta, fill, fill_color, stroke, + stroke_color) - if 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: - 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_rect(self, x, y, width, height, fill=True, fill_color=[1, 1, 1, 0.8], + 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]): cr = self.cr cr.save() cr.translate(x, y) + cr.rotate(theta) if fill: cr.rectangle(0, 0, width, height) @@ -244,7 +232,7 @@ class Diagram(object): cr.rotate(theta) if bb_fill or bb_stroke: - self.draw_rect(bb[0], bb[1], bb[2], bb[3], bb_fill, bb_fill_color, bb_stroke, bb_stroke_color) + self.draw_rect(bb[0], bb[1], bb[2], bb[3], 0, bb_fill, bb_fill_color, bb_stroke, bb_stroke_color) r, g, b, a = self.color_to_rgba(color) cr.set_source_rgba(r, g, b, a) @@ -294,4 +282,10 @@ if __name__ == "__main__": 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, 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=False) + diagram.show()