Exemplo n.º 1
0
 def angle(l, r):
     if r[0] == l[0]:
         a = math.pi / 2.
         if r[1] < l[1]:
             a += math.pi
     elif r[1] == l[1]:
         a = 2. * math.pi
     else:
         a = arctan(slope(l, r))
     if l[0] > r[0]:
         a += math.pi
     if a < 0.:
         a += 2.0 * math.pi
     elif a > 2.0 * math.pi:
         a -= 2.0 * math.pi
     return Radian(a)
Exemplo n.º 2
0
    def __init__(self):
        self.beats = []
        self.camera = Camera()
        self.brush = Brush((0, 0, 0), 3, 0.33, Radian(math.pi / 4.))
        self.current_beat = None
        self.cached_surface = None
        self.camera_moved = True
        pygame.init()

        ar = 16.0 / 9.0

        size = width, height = 1080, int(1080 / ar)
        self.size = size
        self.width = width
        self.height = height

        self.scale_camera(1.0, 1.0)
        screen = pygame.display.set_mode(size, 0, 32)
        self.screen = screen
        pixels = pygame.surfarray.pixels2d(screen)
        self.cairo_surface = cairo.ImageSurface.create_for_data(
            pixels.data, cairo.FORMAT_RGB24, width, height)
Exemplo n.º 3
0
 def rotate(self, angle):
     if not isinstance(angle, Angle):
         angle = Radian(angle)
     mm = array([[angle.cos(), -angle.sin(), 0.],
                 [angle.sin(), angle.cos(), 0.], [0., 0., 1.]])
     self.matrix = dot(self.matrix, mm)
Exemplo n.º 4
0
    def draw(self, brush, points):
        points = self.points_in(points)

        def dist(l, r):
            return sqrt((r[0] - l[0]) * (r[0] - l[0]) + (r[1] - l[1]) *
                        (r[1] - l[1]))

        def slope(l, r):
            return ((r[1] - l[1]) / (r[0] - l[0]))

        def angle(l, r):
            if r[0] == l[0]:
                a = math.pi / 2.
                if r[1] < l[1]:
                    a += math.pi
            elif r[1] == l[1]:
                a = 2. * math.pi
            else:
                a = arctan(slope(l, r))
            if l[0] > r[0]:
                a += math.pi
            if a < 0.:
                a += 2.0 * math.pi
            elif a > 2.0 * math.pi:
                a -= 2.0 * math.pi
            return Radian(a)

        context = self.context
        context.new_path()

        context.set_source_rgb(*brush.color)
        context.set_line_width(brush.scale * self.matrix[0][0])
        if len(points) == 4:
            d1 = dist(points[0], points[1])
            d2 = dist(points[1], points[2])
            d3 = dist(points[2], points[3])

            a1 = angle(points[0], points[1])
            a2 = angle(points[1], points[2])
            a3 = angle(points[2], points[3])

            while abs(a1 - a2) > 3 * math.pi / 2.:
                if a1 < a2:
                    a1 += 2. * math.pi
                else:
                    a2 += 2. * math.pi
            while abs(a2 - a3) > 3 * math.pi / 2.:
                if a2 < a3:
                    a2 += 2. * math.pi
                else:
                    a3 += 2. * math.pi
            aa1 = Radian(((a1 * d1) + (a2 * d2)) / (d1 + d2))

            aa2 = Radian(((a2 * d2) + (a3 * d3)) / (d2 + d3))

            if abs(a2 - a3) > 3 * math.pi / 2.:
                aa2 = Radian(math.pi - aa2)
                context.set_source_rgb(0, 255, 0)

            cp1 = (points[1][0] + aa1.cos() * d2 / 3.,
                   points[1][1] + aa1.sin() * d2 / 3.)
            cp2 = (points[2][0] - aa2.cos() * d2 / 3.,
                   points[2][1] - aa2.sin() * d2 / 3.)

            args = [coord for p in (cp1, cp2, points[2]) for coord in p]
            if reduce(lambda x, y: {
                    True: True,
                    False: x
            }[math.isnan(y)], args, False):
                context.set_source_rgb(255, 0, 0)
                context.move_to(*(points[1]))
                context.line_to(*(points[2]))
            else:

                context.move_to(*(points[1]))
                context.curve_to(*args)
                """
				context.stroke()

				context.set_line_width(1.5)
				context.new_path()
				context.move_to(*points[1])
				context.line_to(*cp1)
				context.set_source_rgb(0, 0, 200)
				context.stroke()
				context.new_path()
				context.move_to(*points[2])
				context.line_to(*cp2)
				context.set_source_rgb(255, 0, 0)
				"""
            context.stroke()