Example #1
0
    def on_draw(self, wd, cr: cairo.Context, sc=None, lw=None, al=None):
        # setup scale
        if sc is None:
            sc = self.sc
            lw = self.lw
            al = self.al
        cr.identity_matrix()
        cr.scale(self.width * sc, self.height * sc)
        tx = (sc - 1) / (sc * 2)
        cr.translate(-tx, 0)

        self.background(cr)
        # triangle
        p = [0.0, 1.0]
        for r0, r1 in self.reverse_line():  # iterate thought rows
            pc = p.copy()
            for n, _ in enumerate(r0):
                if n % 2:
                    continue
                if (r0[n] and r1[n]) and (r0[n + 1] and r1[n]):
                    self.draw_triag(cr, pc)
                pc[0] += 2 * self.dx
            p = [p[0] + self.dx, p[1] - self.dy]

        cr.set_line_width(lw)
        cr.set_source_rgb(0, 0, 0)
        cr.stroke_preserve()
        cr.set_source_rgba(*colors[4], al)
        cr.fill()
Example #2
0
    def on_draw(self, wd, cr: cairo.Context):
        cr.identity_matrix()
        cr.set_matrix(self.matrix)
        self.draw_line_grid(cr)

        if np.any(self.M):  # Test if at least on is zero
            self.draw_squares(cr)
Example #3
0
    def draw_base(self, cr: cairo.Context):
        cr.identity_matrix()
        cr.set_matrix(self.matrix)

        cr.rectangle(-1, -1, 2, 2)
        cr.set_source_rgb(*colors[0])
        cr.fill()

        if self.gtk_mode:
            cr.rectangle(-1, .65, .15, .35)
            cr.set_source_rgb(*colors[1])
            cr.fill()

        cr.set_source_rgb(*colors[1])
        cr.set_line_width(9e-3)

        cr.arc(0, 0, self.radius, 0, 2 * PI)
        # cr.stroke()
        for theta in np.linspace(0, 2 * PI, self.radio_button + 1):
            cr.move_to(0, 0)
            x, y = self.radius * np.cos(theta), self.radius * np.sin(theta)
            cr.line_to(x, y)
            if (x, y) not in self.inter_sections:
                self.inter_sections.append((x, y))
        cr.stroke()
Example #4
0
    def draw(self, cr: cairo.Context) -> None:
        if self._extents is None:
            return

        extents = self._extents
        stroke_color = self._stroke_color
        stroke_width = self._stroke_width

        cr.rectangle(extents.x, extents.y, extents.w, extents.h)

        dx = 1 / cr.get_matrix().xx
        dy = 1 / cr.get_matrix().yy

        cr.save()

        if self._scale_strokes:
            stroke_scale = max(dx, dy)
            cr.identity_matrix()
        else:
            stroke_scale = 1.0

        cr.set_line_width(stroke_width)
        cr.set_source_rgba(*stroke_color)
        cr.stroke()

        cr.restore()

        self._last_drawn_region = rectangle_stroke_region(
            extents, max(stroke_width * stroke_scale, dx, dy))
        self._last_drawn_stroke_scale = stroke_scale
Example #5
0
    def draw(self, cr: cairo.Context) -> None:
        line = self._line
        stroke_color = self._stroke_color
        stroke_width = self._stroke_width
        scale_strokes = self._scale_strokes

        if line is None:
            return

        if line.pt0 == line.pt1:
            return

        clip_extents = Rect2(cr.clip_extents())

        start = line.eval(x=clip_extents.x0)
        end = line.eval(x=clip_extents.x1)

        if not clip_extents.contains(start):
            start = line.eval(y=clip_extents.y0 if start.y < clip_extents.y0
                              else clip_extents.y1)

        if not clip_extents.contains(end):
            end = line.eval(y=clip_extents.
                            y0 if end.y < clip_extents.y0 else clip_extents.y1)

        cr.move_to(*start)
        cr.line_to(*end)

        cr.save()
        if scale_strokes:
            cr.identity_matrix()
        cr.set_source_rgb(*stroke_color)
        cr.set_line_width(stroke_width)
        cr.stroke()
        cr.restore()
Example #6
0
    def on_draw(self, wd, cr: cairo.Context):
        cr.identity_matrix()

        mat = cairo.Matrix(self.width, 0, 0, -self.height, self.width / 2,
                           self.height / 2)
        cr.set_matrix(mat)

        cr.rectangle(-1, -1, 3, 2)
        cr.set_source_rgb(*colors[0])
        cr.fill()
        cr.set_source_rgb(*colors[1])
        cr.set_line_width(5e-3)
        cr.move_to(0, 0)
        cr.line_to(1, 1)
        cr.stroke()
Example #7
0
    def draw(self, cr: cairo.Context) -> None:
        polyline = self._polyline
        stroke_width = self._stroke_width
        stroke_color = self._stroke_color

        if polyline is None or len(polyline) == 0:
            self._last_drawn_region = None
            return

        if self._path_cache is not None:
            cr.append_path(self._path_cache)
        else:
            self._show_polyline(cr, polyline)
            self._path_cache = cr.copy_path()
        extents = Rect2(cr.path_extents())

        dx = 1 / cr.get_matrix().xx
        dy = 1 / cr.get_matrix().yy

        cr.save()

        if self._scale_strokes:
            stroke_scale = max(dx, dy)
            cr.identity_matrix()
        else:
            stroke_scale = 1.0

        cr.set_line_width(stroke_width)
        cr.set_source_rgba(*stroke_color)
        cr.stroke()

        cr.restore()

        extents = expand_rect(extents, max(stroke_width * stroke_scale, dx,
                                           dy))
        self._last_drawn_region = cairo.Region(
            cairo.RectangleInt(
                int(math.floor(extents.x)),
                int(math.floor(extents.y)),
                int(math.ceil(extents.w)),
                int(math.ceil(extents.h)),
            ))
Example #8
0
    def on_draw(self, wd, cr: cairo.Context):
        cr.identity_matrix()

        mat = cairo.Matrix(self.width, 0, 0, -self.height, 0, self.height)
        cr.set_matrix(mat)
        # Background
        cr.rectangle(0, 0, 1, 1)
        cr.set_source_rgb(*hex2rgb(colors[-1]))
        cr.fill()
        # Set line colors
        cr.set_line_width(5e-3)
        self.x, self.y = 0, 0.05
        if self.delta is None:
            cr.set_source_rgb(*self.line_color)
            for i, c in zip(linspace(1, 0, 4), colors[:4]):
                self.x, self.y = 0, 0.05
                cr.set_source_rgba(*hex2rgb(c))
                self.turtle_draw(cr, delta=i)
        else:
            self.movie(cr)
Example #9
0
    def on_draw(self, wd, cr: cairo.Context, d_an=None):
        cr.identity_matrix()
        # cr.scale(self.width*sc, self.height*sc)
        # tx = (sc-1)/(sc*2)
        # mat = cairo.Matrix(1, 0, 0, 1, .5*(1-self.width), .5*(self.height-1))
        mat = cairo.Matrix(self.width, 0, 0, -self.height, self.width / 2,
                           self.height / 2)
        cr.set_matrix(mat)
        # cr.scale(self.width, self.height)
        # cr.translate(.5, .5)

        cr.rectangle(-1, -1, 3, 2)
        cr.set_source_rgb(*colors[1])
        cr.fill()

        cr.set_line_width(.5e-2)
        cr.set_source_rgb(0, 0, 0)
        # self.hypotrochoid(cr, .005, .2, self.d*2, theta=2 * np.pi, n=1000, c=(1, 2))

        if d_an is None:
            d = self.d
        else:
            d = d_an
        #
        if d < .7:
            self.hypotrochoid(cr, .005, .2, d, theta=2 * np.pi, n=1000)

        cr.set_line_width(.8e-2)
        if .25 < d < .8:
            if d_an is None:
                self.hypo_scale(cr, 1.9, .21)
            else:
                self.hypo_scale(cr, 1.9, .21, d)
            self.hypotrochoid(cr,
                              .005,
                              .2,
                              d - .25,
                              theta=2 * np.pi,
                              n=1000,
                              c=(1, 5))
        elif d >= .8:
            self.hypo_scale(cr, 1.9, .21, .8)
            a = self.line(d, 0.8, 1.2, 1, 0)
            self.hypotrochoid(cr,
                              .005,
                              .2,
                              .8 - .25,
                              theta=2 * np.pi,
                              n=1000,
                              c=(1, 5),
                              alpha=a)

        if .459 < d < .8:
            cr.set_line_width(.2e-2)
            if d_an is None:
                self.hypo_scale(cr, 3.2921, 1.53)
            else:
                self.hypo_scale(cr, 3.2921, 1.53, d)
            self.hypotrochoid(cr,
                              .005,
                              .2,
                              .2 - d / 6.3015,
                              theta=2 * np.pi,
                              n=1000,
                              c=(0, 4))
            # self.hypotrochoid(cr, .005, .2, .2 + 8e-4 + d / 1000, theta=4 * np.pi, n=2000, c=(0, 4))
        elif d >= .8:
            cr.set_line_width(self.line(d, 0.8, 1.2, .2e-2, 0.5e-2))
            self.hypo_scale(cr, 3.2921, 1.53, .8)
            # self.hypotrochoid(cr, .005+8e-4+d/1000, .2, .2, theta=4 * np.pi, n=2000, c=(0, 4))
            self.hypotrochoid(cr,
                              .005,
                              .2,
                              .2 - d / 6.3015,
                              theta=2 * np.pi,
                              n=1000,
                              c=(0, 4))
Example #10
0
    def draw(self, cr: cairo.Context) -> None:
        start_angle = self._start_angle
        delta_angle = self._delta_angle
        clockwise = self._clockwise
        arc_radius = self._arc_radius
        start_line_radius = self._start_line_radius
        end_line_radius = self._end_line_radius
        x = self._x
        y = self._y
        stroke_color = self._stroke_color
        stroke_width = self._stroke_width
        text_radius = self._text_radius
        font_size = self._font_size
        text_color = self._text_color
        scale_radii = self._scale_radii
        scale_text = self._scale_text
        scale_strokes = self._scale_strokes

        if not (math.isfinite(start_angle) and math.isfinite(delta_angle)):
            return

        end_angle = start_angle + delta_angle

        matrix = cr.get_matrix()
        if scale_radii:
            radius_scale = 1/(0.5 * (matrix.xx + matrix.yy))
        else:
            radius_scale = 1.0

        self._show_hand(cr, x, y, -start_angle, radius_scale*start_line_radius)
        self._show_hand(cr, x, y, -end_angle, radius_scale*end_line_radius)

        if clockwise:
            cr.arc(x, y, radius_scale*arc_radius, -start_angle, -end_angle)
        else:
            cr.arc_negative(x, y, radius_scale*arc_radius, -start_angle, -end_angle)

        cr.save()
        if scale_strokes:
            cr.identity_matrix()
        cr.set_source_rgba(*stroke_color)
        cr.set_line_width(stroke_width)
        cr.stroke()
        cr.restore()

        half_angle = (start_angle + end_angle) / 2
        cr.move_to(x, y)
        cr.rel_move_to(
            radius_scale*text_radius *  math.cos(half_angle),
            radius_scale*text_radius * -math.sin(half_angle),
        )

        cr.save()

        cr.set_source_rgba(*text_color)
        cr.set_font_size(font_size)

        if scale_text:
            cr.identity_matrix()

        text = '{:.1f}°'.format(math.degrees(math.fabs(delta_angle)))
        text_extents = cr.text_extents(text)

        cr.rel_move_to(-text_extents.x_bearing, -text_extents.y_bearing)
        cr.rel_move_to(-text_extents.width/2, -text_extents.height/2)
        cr.show_text(text)
        cr.fill()

        cr.restore()