Пример #1
0
def draw_triangle(x, y, size, num_remaining):
    if num_remaining > 0:
        canvas.set_fill_color(*shade_of(random.choice(palette.colors)))
        canvas.set_stroke_color(*shade_of(random.choice(palette.colors)))
        canvas.set_line_width(random.random() * 0.5 + 0.5)
        step = math.sqrt(size**2 - (size / 2.0)**2)
        canvas.move_to(x - step, y - (size / 2.0))
        canvas.add_line(x, y + size)
        canvas.add_line(x + step, y - (size / 2.0))
        canvas.add_line(x - step, y - (size / 2.0))
        canvas.fill_path()
        canvas.draw_line(x - step, y - (size / 2.0), x, y + size)
        canvas.draw_line(x, y + size, x + step, y - (size / 2.0))
        canvas.draw_line(x + step, y - (size / 2.0), x - step,
                         y - (size / 2.0))
        canvas.draw_line(x, y, x - (step / 2.0), y + (size / 4.0))
        canvas.draw_line(x, y, x + (step / 2.0), y + (size / 4.0))
        canvas.draw_line(x, y, x, y - (size / 2.0))
        canvas.draw_line(x - (step / 2.0), y + (size / 4.0), x + (step / 2.0),
                         y + (size / 4.0))
        canvas.draw_line(x + (step / 2.0), y + (size / 4.0), x,
                         y - (size / 2.0))
        canvas.draw_line(x, y - (size / 2.0), x - (step / 2.0),
                         y + (size / 4.0))
        draw_triangle(random.random() * width,
                      random.random() * height,
                      random.random() * triangle_side, num_remaining - 1)
Пример #2
0
def hexacircle(start_x, start_y):
	r, g, b = random.choice(palette)

	canvas.set_fill_color(r, g, b, random.random() * 0.75 + 0.25)
	canvas.set_stroke_color(1.0, 1.0, 1.0, random.random() * 0.50 + 0.50)
	canvas.set_line_width(random.random() * 0.75 + 0.25)
	circle = (start_x - (0.5 * circle_size),
	          start_y - (0.5 * circle_size),
	          circle_size, circle_size)
	canvas.draw_ellipse(*circle)
	canvas.fill_ellipse(*circle)

	# draw lines
	rstrokedline(start_x, start_y - circle_size, start_x, start_y + circle_size)
	rstrokedline(start_x - step, start_y, start_x + step, start_y)

	rstrokedline(start_x - step, start_y - (0.5 * circle_size), start_x + step, start_y + (0.5 * circle_size))
	rstrokedline(start_x - step, start_y + (0.5 * circle_size), start_x + step, start_y - (0.5 * circle_size))

	rstrokedline(start_x - step, start_y + (1.5 * circle_size), start_x + step, start_y - (1.5 * circle_size))
	rstrokedline(start_x - step, start_y - (1.5 * circle_size), start_x + step, start_y + (1.5 * circle_size))
    
	rstrokedline(start_x - step, start_y + (2.5 * circle_size), start_x + step, start_y - (2.5 * circle_size))
	rstrokedline(start_x - step, start_y - (2.5 * circle_size), start_x + step, start_y + (2.5 * circle_size))
    
	rstrokedline(start_x - (3.0 * step), start_y + (0.5 * circle_size), start_x + (3.0 * step), start_y - (0.5 * circle_size))
	rstrokedline(start_x - (3.0 * step), start_y - (0.5 * circle_size), start_x + (3.0 * step), start_y + (0.5 * circle_size))
def draw_heart(outline = False):
	first = True
	for t in xrange(int(2*pi * detail)):
		t = t * detail
		# heart equation
		x = 16*(sin(t) ** 3)
		y = 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t)
		# scale result
		x = origin + x * scale
		y = origin + y * scale + scale*2
		# hide first line
		if first:
			canvas.move_to(x, y)
			first = False
		else:
			canvas.add_line(x, y)
	# set color
	canvas.set_fill_color(1,0.5,0.5)
	canvas.set_stroke_color(0.5,0,0)
	canvas.set_line_width(detail/2)
	# draw heart
	if outline:
		canvas.draw_path()
	else:
		canvas.close_path()
		canvas.fill_path()
Пример #4
0
def hexacircle(start_x, start_y):
    r, g, b = random.choice(palette)

    canvas.set_fill_color(r, g, b, random.random() * 0.75 + 0.25)
    canvas.set_stroke_color(1.0, 1.0, 1.0, random.random() * 0.50 + 0.50)
    canvas.set_line_width(random.random() * 0.75 + 0.25)
    circle = (start_x - (0.5 * circle_size), start_y - (0.5 * circle_size),
              circle_size, circle_size)
    canvas.draw_ellipse(*circle)
    canvas.fill_ellipse(*circle)

    # draw lines
    rstrokedline(start_x, start_y - circle_size, start_x,
                 start_y + circle_size)
    rstrokedline(start_x - step, start_y, start_x + step, start_y)

    rstrokedline(start_x - step, start_y - (0.5 * circle_size), start_x + step,
                 start_y + (0.5 * circle_size))
    rstrokedline(start_x - step, start_y + (0.5 * circle_size), start_x + step,
                 start_y - (0.5 * circle_size))

    rstrokedline(start_x - step, start_y + (1.5 * circle_size), start_x + step,
                 start_y - (1.5 * circle_size))
    rstrokedline(start_x - step, start_y - (1.5 * circle_size), start_x + step,
                 start_y + (1.5 * circle_size))

    rstrokedline(start_x - step, start_y + (2.5 * circle_size), start_x + step,
                 start_y - (2.5 * circle_size))
    rstrokedline(start_x - step, start_y - (2.5 * circle_size), start_x + step,
                 start_y + (2.5 * circle_size))

    rstrokedline(start_x - (3.0 * step), start_y + (0.5 * circle_size),
                 start_x + (3.0 * step), start_y - (0.5 * circle_size))
    rstrokedline(start_x - (3.0 * step), start_y - (0.5 * circle_size),
                 start_x + (3.0 * step), start_y + (0.5 * circle_size))
Пример #5
0
def rstrokedline(start_x, start_y, end_x, end_y):
    line_width = random.random() * 0.25 + 0.25
    canvas.set_line_width(line_width)
    r, g, b = palette.lightest
    a = random.random() * 0.25 + 0.05
    canvas.set_stroke_color(r, g, b, a)
    canvas.draw_line(start_x, start_y, end_x, end_y)
Пример #6
0
def draw_grid(min_x, max_x, min_y, max_y):
	w, h = canvas.get_size()
	scale_x = w / (max_x - min_x)
	scale_y = h / (max_y - min_y)
	min_x, max_x = round(min_x), round(max_x)
	min_y, max_y = round(min_y), round(max_y)
	canvas.begin_updates()
	canvas.set_line_width(1)
	canvas.set_stroke_color(0.7, 0.7, 0.7)
	#Draw vertical grid lines:
	x = min_x
	while x <= max_x:
		if x != 0:
			draw_x = round(w / 2 + x * scale_x) + 0.5
			canvas.draw_line(draw_x, 0, draw_x, h)
		x += 0.5
	#Draw horizontal grid lines:
	y = min_y
	while y <= max_y:
		if y != 0:
			draw_y = round(h/2 + y * scale_y) + 0.5
			canvas.draw_line(0, draw_y, w, draw_y)
		y += 0.5
	#Draw x and y axis:
	canvas.set_stroke_color(0, 0, 0)
	canvas.draw_line(0, h/2, w, h/2)
	canvas.draw_line(w/2, 0, w/2, h)
	canvas.end_updates()
Пример #7
0
    def plot_spectra(self):
        # calculate scale and axis label steps:
        scale_x = (688.0 - 2 * self.x_offset) / (self.x_max - self.x_min)
        scale_y = (688.0 - 2 * self.y_offset) / (self.y_max - self.y_min)
        step_x = scale_x * (self.x_max - self.x_min) / 10
        step_y = scale_y * (self.y_max - self.y_min) / 10

        # Draw x and y axix:
        canvas.set_stroke_color(0, 0, 0)
        # x-axis
        canvas.draw_line(self.x_offset, self.y_offset, 688, self.y_offset)
        # y-axis
        canvas.draw_line(self.x_offset, self.y_offset, self.x_offset, 688)

        # label and mark the axes..
        for i in range(11):
            canvas.set_fill_color(0, 0, 0)
            # x-axis...
            label = self.x_axis_format.format(self.x_min + i *
                                              (self.x_max - self.x_min) / 10)
            canvas.draw_text(label,
                             self.x_offset + step_x * i,
                             0,
                             font_name='Helvetica',
                             font_size=16)
            canvas.draw_line(self.x_offset + step_x * i, self.y_offset - 5,
                             self.x_offset + step_x * i, self.y_offset)

            # y-axis...
            label = self.y_axis_format.format(self.y_min + i *
                                              (self.y_max - self.y_min) / 10)
            canvas.draw_text(label,
                             0,
                             self.y_offset + step_y * i,
                             font_name='Helvetica',
                             font_size=16)
            canvas.draw_line(self.x_offset - 5, self.y_offset + step_y * i,
                             self.x_offset, self.y_offset + step_y * i)

        # draw each dataset...
        for j in range(len(self.x_set)):
            temp_x = []
            temp_y = []
            temp_colour = []
            temp_x = self.x_set[j]
            temp_y = self.y_set[j]

            canvas.set_stroke_color(*self.spectrum_colour[j])
            canvas.set_line_width(2)
            canvas.move_to(self.x_offset + scale_x * (temp_x[0] - self.x_min),
                           self.y_offset + scale_y * (temp_y[0] - self.y_min))

            for i in range(len(temp_x)):
                draw_x = self.x_offset + scale_x * (temp_x[i] - self.x_min)
                draw_y = self.y_offset + scale_y * (temp_y[i] - self.y_min)
                if (self.style[j] == '-'):
                    canvas.add_line(draw_x, draw_y)
                if (self.style[j] == 'o'):
                    canvas.add_ellipse(draw_x - 3, draw_y - 3, 6, 6)
            canvas.draw_path()
Пример #8
0
def testidea():
    j = what()
    a = time.time()
    print j.nearestjunction()
    print j.prev
    print time.time() - a
    import canvas
    canvas.set_size(256, 256)
    x = []
    for n in j.junctions:
        x.append(j.junctions[n])

    mn = [min(x)[0], min(x, key=lambda a: a[1])[1]]
    mx = [max(x)[0], max(x, key=lambda a: a[1])[1]]
    ml = (mx[0] - mn[0]) / 256.0
    mf = (mx[1] - mn[1]) / 256.0
    md = max([ml, mf])
    print mn, mx
    print ml, mf
    for n in x:
        canvas.draw_ellipse((n[0] - mn[0]) / md, 256 - (n[1] - mn[1]) / md, 2,
                            2)
    canvas.set_stroke_color(255, 0, 0)
    canvas.draw_ellipse((j.loc[0] - mn[0]) / md, 256 - (j.loc[1] - mn[1]) / md,
                        2, 2)

    canvas.set_stroke_color(0, 255, 255)
    d = j.nearestjunction()[0]
    x = []
    for n in d:
        x.append(n[4])
    for n in x:
        canvas.draw_ellipse((n[0] - mn[0]) / md, 256 - (n[1] - mn[1]) / md, 2,
                            2)
Пример #9
0
def rstrokedline(start_x, start_y, end_x, end_y):
	line_width = random.random() * 0.25 + 0.25
	canvas.set_line_width(line_width)
	r, g, b = palette.lightest
	a = random.random() * 0.25 + 0.05
	canvas.set_stroke_color(r, g, b, a)
	canvas.draw_line(start_x, start_y, end_x, end_y)
Пример #10
0
def reset():
	set_size(512, 512)
	set_line_width(3)
	set_stroke_color(0,0,0)
	draw_rect(0, 0, 512, 512)
	set_line_width(1)
	set_stroke_color(0, 0, 1)
	return True
Пример #11
0
	def backward(self, distance):
		sleep(DELAY)
		to = (self.pos[0] - sin(self.heading) * distance, self.pos[1] - cos(self.heading) * distance)
		if self.pen_is_down:
			set_line_width(self.pen_width)
			set_stroke_color(self.r_color, self.g_color, self.b_color)
			draw_line(self.pos[0], self.pos[1], to[0], to[1])
		self.pos = to
Пример #12
0
def drawHand(width, length, angle):
	rads = radians(angle)
	endX = int(cos(angle) * length) + centerX
	endY = int(sin(angle) * length) + centerY
	canvas.save_gstate()
	canvas.set_stroke_color(0.0, 0.0, 0.0, 0.6)
	canvas.set_line_width(width)
	canvas.draw_line(centerX, centerY, endX, endY)			
	canvas.restore_gstate()
Пример #13
0
def draw_tree(x, y, trunk_thickness, leaf_h, tree_w, trunk_h):
    canvas.begin_path()
    canvas.move_to(x - tree_w / 2, y + trunk_h)
    canvas.add_line(x + tree_w / 2, y + trunk_h)
    canvas.add_line(x, y + trunk_h + leaf_h)
    canvas.close_path()
    canvas.set_fill_color(0.25, 0.50, 0.00)
    canvas.fill_path()
    canvas.set_stroke_color(0.50, 0.25, 0.00)
    canvas.set_line_width(trunk_thickness)
    canvas.draw_line(x, y + trunk_h, x, y)
Пример #14
0
def plot_function(func, color, min_x, max_x, min_y, max_y):
	#Calculate scale, set line width and color:
	w, h = canvas.get_size()
	origin_x, origin_y = w * 0.5, h * 0.5
	scale_x = w / (max_x - min_x)
	scale_y = h / (max_y - min_y)
	canvas.set_stroke_color(*color)
	canvas.set_line_width(2)
	canvas.move_to(origin_x + scale_x * min_x, 
	               origin_y + func(min_x) * scale_y)
	#Draw the graph line:
	x = min_x
	while x <= max_x:
		x += 0.05
		draw_x = origin_x + scale_x * x
		draw_y = origin_y + func(x) * scale_y
		canvas.add_line(draw_x, draw_y)
	canvas.set_fill_color(*color)
	canvas.draw_path()
Пример #15
0
def draw_triangle(x, y, size, num_remaining):
	r, g, b = random.choice(palette)
	canvas.set_fill_color(r, g, b, random.random() * 0.75 + 0.25)
	r, g, b = random.choice(palette)
	canvas.set_stroke_color(r, g, b, random.random() * 0.5 + 0.5)
	canvas.set_line_width(random.random() * 2.5 + 0.5)
	step = math.sqrt(size**2 - (size / 2.0)**2)
	canvas.move_to(x - step, y - (size / 2.0))
	canvas.add_line(x, y + size)
	canvas.add_line(x + step, y - (size / 2.0))
	canvas.add_line(x - step, y - (size / 2.0))
	canvas.fill_path()
	canvas.draw_line(x - step, y - (size / 2.0), x, y + size)
	canvas.draw_line(x, y + size, x + step, y - (size / 2.0))
	canvas.draw_line(x + step, y - (size / 2.0), x - step, y - (size / 2.0))
	canvas.draw_line(x, y, x - (step / 2.0), y + (size / 4.0))
	canvas.draw_line(x, y, x + (step / 2.0), y + (size / 4.0))
	canvas.draw_line(x, y, x, y - (size / 2.0))
	canvas.draw_line(x - (step / 2.0), y + (size / 4.0), x + (step / 2.0), y + (size / 4.0))
	canvas.draw_line(x + (step / 2.0), y + (size / 4.0), x, y - (size / 2.0))
	canvas.draw_line(x, y - (size / 2.0), x - (step / 2.0), y + (size / 4.0))
Пример #16
0
def draw_triangle(x, y, size, num_remaining):
	if num_remaining > 0:
		canvas.set_fill_color(*shade_of(random.choice(palette.colors)))
		canvas.set_stroke_color(*shade_of(random.choice(palette.colors)))
		canvas.set_line_width(random.random() * 0.5 + 0.5)
		step = math.sqrt(size**2 - (size / 2.0)**2)
		canvas.move_to(x - step, y - (size / 2.0))
		canvas.add_line(x, y + size)
		canvas.add_line(x + step, y - (size / 2.0))
		canvas.add_line(x - step, y - (size / 2.0))
		canvas.fill_path()
		canvas.draw_line(x - step, y - (size / 2.0), x, y + size)
		canvas.draw_line(x, y + size, x + step, y - (size / 2.0))
		canvas.draw_line(x + step, y - (size / 2.0), x - step, y - (size / 2.0))
		canvas.draw_line(x, y, x - (step / 2.0), y + (size / 4.0))
		canvas.draw_line(x, y, x + (step / 2.0), y + (size / 4.0))
		canvas.draw_line(x, y, x, y - (size / 2.0))
		canvas.draw_line(x - (step / 2.0), y + (size / 4.0), x + (step / 2.0), y + (size / 4.0))
		canvas.draw_line(x + (step / 2.0), y + (size / 4.0), x, y - (size / 2.0))
		canvas.draw_line(x, y - (size / 2.0), x - (step / 2.0), y + (size / 4.0))
		draw_triangle(random.random() * width, random.random() * height, random.random() * triangle_side, num_remaining - 1)
Пример #17
0
def draw_triangle(x, y, size, num_remaining):
    r, g, b = random.choice(palette)
    canvas.set_fill_color(r, g, b, random.random() * 0.75 + 0.25)
    r, g, b = random.choice(palette)
    canvas.set_stroke_color(r, g, b, random.random() * 0.5 + 0.5)
    canvas.set_line_width(random.random() * 2.5 + 0.5)
    step = math.sqrt(size**2 - (size / 2.0)**2)
    canvas.move_to(x - step, y - (size / 2.0))
    canvas.add_line(x, y + size)
    canvas.add_line(x + step, y - (size / 2.0))
    canvas.add_line(x - step, y - (size / 2.0))
    canvas.fill_path()
    canvas.draw_line(x - step, y - (size / 2.0), x, y + size)
    canvas.draw_line(x, y + size, x + step, y - (size / 2.0))
    canvas.draw_line(x + step, y - (size / 2.0), x - step, y - (size / 2.0))
    canvas.draw_line(x, y, x - (step / 2.0), y + (size / 4.0))
    canvas.draw_line(x, y, x + (step / 2.0), y + (size / 4.0))
    canvas.draw_line(x, y, x, y - (size / 2.0))
    canvas.draw_line(x - (step / 2.0), y + (size / 4.0), x + (step / 2.0),
                     y + (size / 4.0))
    canvas.draw_line(x + (step / 2.0), y + (size / 4.0), x, y - (size / 2.0))
    canvas.draw_line(x, y - (size / 2.0), x - (step / 2.0), y + (size / 4.0))
Пример #18
0
def draw_grid():
    console.clear()
    canvas.set_size(size_x, size_y)
    canvas.begin_updates()
    canvas.set_line_width(1)
    canvas.set_stroke_color(0.7, 0.7, 0.7)
    #Draw vertical grid lines:
    x = grid
    while x <= max(abs(min_x), abs(max_x)):
        cx = t_c_x(x)
        if cx > 0 and cx < size_x:
            canvas.draw_line(cx, 0, cx, size_y)

        cx = t_c_x(-x)
        if cx > 0 and cx < size_x:
            canvas.draw_line(cx, 0, cx, size_y)

        x += grid

    y = grid
    while y <= max(abs(min_y), abs(max_y)):
        cy = t_c_y(y)
        if cy > 0 and cy < size_y:
            canvas.draw_line(0, cy, size_x, cy)

        cy = t_c_y(-y)
        if cy > 0 and cy < size_y:
            canvas.draw_line(0, cy, size_x, cy)

        y += grid

    canvas.set_stroke_color(0, 0, 0)
    cx = t_c_x(0)
    if cx > 0 and cx < size_x:
        canvas.draw_line(cx, 0, cx, size_y)
    cy = t_c_y(0)
    if cy > 0 and cy < size_y:
        canvas.draw_line(0, cy, size_x, cy)
    canvas.end_updates()
Пример #19
0
def plot_function(t_, color, min_x,max_x,min_y,max_y):
    #Calculate scale, set line width and color:
    w, h = canvas.get_size()
    scale_x = w / (max_x - min_x)
    scale_y = h / (max_y - min_y)
    scale_x = min(scale_x,scale_y)
    scale_y=scale_x
    origin_x, origin_y = -scale_x*min_x,-scale_y*min_y
    canvas.set_stroke_color(*color)
    canvas.set_line_width(2)
    #Draw the graph line:
    x = t_[0][0]
    y = t_[0][1]
    canvas.move_to(origin_x + scale_x * x, 
                   origin_y + scale_y * y)
    for p in t_[1:]:
        x=p[0]
        y=p[1]
        draw_x = origin_x + scale_x * x
        draw_y = origin_y + scale_y * y
        canvas.add_line(*(draw_x, draw_y))
    canvas.set_fill_color(*color)
    canvas.draw_path()
Пример #20
0
        canvas.set_fill_color(randomColor, randomColor, randomColor, 0.2)
        canvas.fill_ellipse(x, y, 4, 4)
    canvas.restore_gstate()
canvas.end_updates()

#overlaping rectangles in circle
for i in range(12):
    canvas.save_gstate()
    canvas.set_fill_color(0.4, 0.4, 0.8, 0.1)
    canvas.translate(250, 250)
    canvas.rotate(-2 * pi / 12.0 * i)
    canvas.fill_rect(0, 0, 40, 40)
    canvas.restore_gstate()

#fanning-out
canvas.set_stroke_color(0.3, 0.3, 0.3, 0.6)
originX = w / 2
originY = h / 2

for i in range(1, 500, 5):
    x1 = originX
    y1 = originY
    x2 = i
    y2 = 0
    canvas.draw_line(x1, y1, x2, y2)

for i in range(1, 500, 5):
    x1 = originX
    y1 = originY
    x2 = i
    y2 = canvas.get_size()[0]
Пример #21
0
def setstyle(w, gray=0):
    canvas.set_line_width(w)
    canvas.set_stroke_color(gray, gray, gray)
Пример #22
0
def draw_circle(x, y, r):
	canvas.set_line_width(3)
	canvas.set_stroke_color(0, 0, 0)
	canvas.set_fill_color(255,255,255)
	canvas.fill_ellipse(x - r, y - r, 2 * r, 2 * r)
	canvas.draw_ellipse(x - r, y - r, 2 * r, 2 * r)
Пример #23
0
from urbanape import common_devices
from kuler import *

random.seed()

width, height = common_devices['ipad_r']
palette = robots_are_cool

canvas.begin_updates()

canvas.set_size(width, height)
canvas.set_fill_color(*palette.darkest)
canvas.fill_rect(0, 0, width, height)

canvas.set_fill_color(*random.choice(palette))
canvas.set_stroke_color(*palette.lightest)
start_x, start_y = (width / 2.0, height / 2.0)

lollipop_points = []

for x in xrange(64):
	end_x, end_y = (random.random() * (width * 0.8) + (width * 0.1), random.random() * (height * 0.8) + (height * 0.1))
	lollipop_points.append((end_x, end_y))
	canvas.set_line_width(random.random() * 0.75 + 0.25)
	canvas.draw_line(start_x, start_y, end_x, end_y)
	
for x in xrange(64):
	end_x, end_y = lollipop_points[x]
	size = random.random() * 128.0 + 32.0
	canvas.fill_ellipse(end_x - (size / 2.0), end_y - (size / 2.0), size, size)
	canvas.set_line_width(1.25)
Пример #24
0
def main():
    draw_grid()
    canvas.set_stroke_color(0.7, 0.3, 0.3)
    canvas.set_line_width(5)
    x = 5.0
    y = -3.0
    path = []
    last_a = 0
    for l, delta_a in [(10, 30), (10, 20), (10, 150), (10, -90), (10, 20), (10, 45), (10, 160)]:
        last_a += delta_a
        a1 = last_a / 180.0 * pi
        dx = l * cos(a1)
        dy = l * sin(a1)
        dt = l / speed
        #print dx,dy, sin(a1), cos(a1)
        line(x, y, x + dx, y + dy)
        path.append([x, y, x + dx, y + dy, dt, 'i'])
        x += dx
        y += dy

    path.append([x, y, x, y, 1, 'l'])

    path[0][5] = 'f'

    print path
    x, y = path[0][:2]
    vx = 0.0
    vy = 0.0
    need_slow = 1
    k_slow = 1.0

    while need_slow:
        need_slow = 0
        points = []
        last_dt = 0
        for (x1, y1, x2, y2, dt, fli) in path:
            dt = dt * k_slow
            nvx = (x2 - x1) / dt
            nvy = (y2 - y1) / dt
            dvx = nvx - vx
            dvy = nvy - vy
            dta = max(abs(dvx / ax_max), abs(dvy / ay_max))

            if fli == "i":
                dta = dta / 2
                ka = dta / dt
            else:
                ka = dta / dt / 2

            #print fli, dvx, dvy, dt, dta, dta/dt
            last_ka = dta / (last_dt or 1)

            if fli == "f":
                points.append((x1, y1, 0, 0, 0))
                points.append((x1 + (x2 - x1) * ka, y1 + (y2 - y1) * ka, nvx, nvy, dta))
                dta = dta / 2
            elif fli == "i":
                points.append((last_x2 - (last_x2 - last_x1) * last_ka,
                               last_y2 - (last_y2 - last_y1) * last_ka,
                               vx, vy, last_dt - last_dta - dta))
                points.append((x1 + (x2 - x1) * ka,
                               y1 + (y2 - y1) * ka,
                               nvx, nvy, dta * 2))
            elif fli == "l":
                points.append((last_x2 - (last_x2 - last_x1) * last_ka / 2,
                               last_y2 - (last_y2 - last_y1) * last_ka / 2,
                               vx, vy, last_dt - last_dta - dta / 2))
                points.append((x2, y2,
                               0, 0, dta))

            if fli == "i":
                if ka > part_max:
                    need_slow = 1
                if last_dt and last_ka > part_max:
                    need_slow = 1

                x0, y0, vx0, vy0, _ = points[-2]
                xn, yn, vxn, vyn, sdt = points[-1]
                coefs_x = cubic_coeff(x0, vx0, xn, vxn, sdt)
                coefs_y = cubic_coeff(y0, vy0, yn, vyn, sdt)

                xm = cubic_x(coefs_x, sdt / 2)
                ym = cubic_x(coefs_y, sdt / 2)
                delta = sqrt((xm - x1) * (xm - x1) + (ym - y1) * (ym - y1))
                print delta

                if delta > delta_max:
                    need_slow = 1
            else:
                if ka > 0.5:
                    need_slow = 1
                if last_dt and last_ka > 0.5:
                    need_slow = 1

            last_x1 = x1
            last_y1 = y1
            last_x2 = x2
            last_y2 = y2
            last_dt = dt
            last_dta = dta
            vx = nvx
            vy = nvy

        #need_slow = 0
        if need_slow:
            k_slow = k_slow * 1.1
            print "need slowdown, new k =", k_slow, "speed = ", speed / k_slow

    print points

    last_p = points[0]
    for p in points[1:]:
        x0, y0, vx0, vy0, _ = last_p
        x1, y1, vx1, vy1, dt = p

        canvas.set_stroke_color(0.3, 0.7, 0.3)
        canvas.set_line_width(3)

        line(x0, y0, x1, y1)
        circle(x1, y1)

        coefs_x = cubic_coeff(x0, vx0, x1, vx1, dt)
        coefs_y = cubic_coeff(y0, vy0, y1, vy1, dt)
        print "x:", coefs_x, cubic_a(coefs_x, 0), cubic_a(coefs_x, dt)
        print "y:", coefs_y, cubic_a(coefs_y, 0), cubic_a(coefs_y, dt)
        t = 0.0
        step = dt / 10
        canvas.set_stroke_color(0, 0, 0)
        canvas.set_line_width(3)
        while 1:
            x = cubic_x(coefs_x, t)
            y = cubic_x(coefs_y, t)
            vx = cubic_v(coefs_x, t)
            vy = cubic_v(coefs_y, t)
            print sqrt(vx * vx + vy * vy)
            circle(x, y, 2)
            t += step
            if t > dt:
                break

        last_p = p
Пример #25
0
    def paint(self, width, height):
        # start with unit diagonal
        self.unit_diagonal(self.view_tm)

        # rotate object
        self.rotx(self.xinc, self.rotx_tm)
        self.am4x4(self.view_tm, self.rotx_tm, self.view_tm)

        self.roty(self.yinc, self.roty_tm)
        self.am4x4(self.view_tm, self.roty_tm, self.view_tm)

        self.rotz(self.zinc, self.rotz_tm)
        self.am4x4(self.view_tm, self.rotz_tm, self.view_tm)

        # scale
        self.scaleit(self.scale, self.scale, self.scale, self.scale_tm)
        self.am4x4(self.view_tm, self.scale_tm, self.view_tm)

        # move coordinates to origin
        self.trans(-self.xe, -self.ye, -self.ze, self.trans_tm)
        self.am4x4(self.view_tm, self.trans_tm, self.view_tm)

        # rotate -90 deg. about x axis
        self.rotx(1.5708, self.rotx_tm)
        self.am4x4(self.view_tm, self.rotx_tm, self.view_tm)

        # rotate -theta about y axis
        theta = math.atan2(-(self.xe - self.xt), -(self.ye - self.yt))
        self.roty(-theta, self.roty_tm)
        self.am4x4(self.view_tm, self.roty_tm, self.view_tm)

        # rotate -phi around x axis
        phi = math.atan2((self.ze - self.zt),
                         math.sqrt((self.xe - self.xt) * (self.xe - self.xt) +
                                   (self.ye - self.yt) * (self.ye - self.yt)))
        self.rotx(-phi, self.rotx_tm)
        self.am4x4(self.view_tm, self.rotx_tm, self.view_tm)

        # change rh to lh coordinates
        self.scaleit(1, -1, -1, self.scale_tm)
        self.am4x4(self.view_tm, self.scale_tm, self.view_tm)

        # add perspective
        if self.persp:
            self.perspect(self.s, self.d, self.persp_tm)
            self.am4x4(self.view_tm, self.persp_tm, self.view_tm)

        # transform the view
        self.am20x4(self.view_tm, self.view)

        # draw the view in width/height
        orgx = width / 2
        orgy = height / 2
        if self.persp:
            for i in range(self.vertnum):
                self.view[i][0] = (orgx * self.view[i][0] /
                                   self.view[i][2]) + orgx
                self.view[i][1] = (orgy * self.view[i][1] /
                                   self.view[i][2]) + orgy
        canvas.begin_updates()
        canvas.set_size(width, height)
        canvas.set_stroke_color(0, 1, 0)
        for i in range(self.facenum):
            for j in range(self.edgenum[i] - 1):
                canvas.draw_line(self.view[self.face[i][j]][0],
                                 self.view[self.face[i][j]][1],
                                 self.view[self.face[i][j + 1]][0],
                                 self.view[self.face[i][j + 1]][1])
            canvas.draw_line(self.view[self.face[i][j + 1]][0],
                             self.view[self.face[i][j + 1]][1],
                             self.view[self.face[i][0]][0],
                             self.view[self.face[i][0]][1])
            canvas.set_fill_color(0, 1, 0)
        self.frames += 1
        canvas.draw_text(
            'Object: ' + self.name + '   Frames: ' + str(self.frames) +
            '   Angle: ' + str(self.yinc), 10, 10)
        canvas.end_updates()
Пример #26
0
from devices import *
from kuler import *

random.seed()

width, height = ipad_r_ios7
palette = random.choice(themes)

canvas.begin_updates()

canvas.set_size(width, height)
canvas.set_fill_color(*palette.darkest)
canvas.fill_rect(0, 0, width, height)

canvas.set_fill_color(*random.choice(palette.colors))
canvas.set_stroke_color(*palette.lightest)
start_x, start_y = (width / 2.0, height / 2.0)

lollipop_points = []

for x in xrange(64):
    end_x, end_y = (random.random() * (width * 0.8) + (width * 0.1),
                    random.random() * (height * 0.8) + (height * 0.1))
    lollipop_points.append((end_x, end_y))
    canvas.set_line_width(random.random() * 0.75 + 0.25)
    canvas.draw_line(start_x, start_y, end_x, end_y)

size = random.random() * (width * 0.10)
canvas.fill_ellipse(start_x - (size / 2.0), start_y - (size / 2.0), size, size)
canvas.set_line_width(1.25)
canvas.draw_ellipse(start_x - (size / 2.0), start_y - (size / 2.0), size, size)
Пример #27
0
def rstrokedline(start_x, start_y, end_x, end_y):
	line_width = random.random() * 0.75 + 0.25
	canvas.set_line_width(line_width)
	a = random.random() * 0.25 + 0.05
	canvas.set_stroke_color(1.0, 1.0, 1.0, a)
	canvas.draw_line(start_x, start_y, end_x, end_y)
Пример #28
0
def reset():
	set_size(512, 512)
	home()
	set_stroke_color(0, 0, 0)
	draw_rect(0, 0, 512, 512)
	set_stroke_color(0, 0, 1)
Пример #29
0
from urbanape import common_devices
from kuler import *

random.seed()

width, height = common_devices['ipad_r']
palette = robots_are_cool

canvas.begin_updates()

canvas.set_size(width, height)
canvas.set_fill_color(*palette['darkest'])
canvas.fill_rect(0, 0, width, height)

canvas.set_fill_color(*random.choice(palette['palette']))
canvas.set_stroke_color(*palette['lightest'])
start_x, start_y = (width / 2.0, height / 2.0)

lollipop_points = []

for x in xrange(64):
	end_x, end_y = (random.random() * (width * 0.8) + (width * 0.1), random.random() * (height * 0.8) + (height * 0.1))
	lollipop_points.append((end_x, end_y))
	canvas.set_line_width(random.random() * 0.75 + 0.25)
	canvas.draw_line(start_x, start_y, end_x, end_y)
	
for x in xrange(64):
	end_x, end_y = lollipop_points[x]
	size = random.random() * 128.0 + 32.0
	canvas.fill_ellipse(end_x - (size / 2.0), end_y - (size / 2.0), size, size)
	canvas.set_line_width(1.25)
Пример #30
0
 def set_draw_color(self, color):
     super().set_draw_color(color)
     if type(color) is tuple:
         canvas.set_stroke_color(*color)
     else:
         canvas.set_stroke_color(*ImageColor.getrgb(color))
Пример #31
0
def rstrokedline(start_x, start_y, end_x, end_y):
    line_width = random.random() * 0.75 + 0.25
    canvas.set_line_width(line_width)
    a = random.random() * 0.25 + 0.05
    canvas.set_stroke_color(1.0, 1.0, 1.0, a)
    canvas.draw_line(start_x, start_y, end_x, end_y)
Пример #32
0
import canvas

canvas.set_size(512, 512)

from_point = (10, 10)
cp1 = (40, 200)  #control point 1
cp2 = (350, 50)  #control point 2
to_point = (300, 300)

# Draw the actual curve:
canvas.begin_path()
canvas.move_to(from_point[0], from_point[1])
canvas.add_curve(cp1[0], cp1[1], cp2[0], cp2[1], to_point[0], to_point[1])
canvas.set_line_width(2)
canvas.draw_path()

# Draw the red dots and lines to illustrate what's happening:
canvas.set_stroke_color(1, 0, 0)
canvas.set_fill_color(1, 0, 0)

# Draw straight lines between the control points and the end points:
canvas.draw_line(from_point[0], from_point[1], cp1[0], cp1[1])
canvas.draw_line(to_point[0], to_point[1], cp2[0], cp2[1])

# Draw red squares on all the points:
canvas.fill_rect(from_point[0] - 4, from_point[1] - 4, 8, 8)
canvas.fill_rect(to_point[0] - 4, to_point[1] - 4, 8, 8)
canvas.fill_rect(cp1[0] - 4, cp1[1] - 4, 8, 8)
canvas.fill_rect(cp2[0] - 4, cp2[1] - 4, 8, 8)
Пример #33
0
def reset():
    set_size(512, 512)
    home()
    set_stroke_color(0, 0, 0)
    draw_rect(0, 0, 512, 512)
    set_stroke_color(0, 0, 1)
Пример #34
0
# https://gist.github.com/omz/5087533

import canvas

canvas.set_size(512, 512)

from_point = (10, 10)
cp1 = (40, 200) #control point 1
cp2 = (350, 50) #control point 2
to_point = (300, 300)

# Draw the actual curve:
canvas.begin_path()
canvas.move_to(from_point[0], from_point[1])
canvas.add_curve(cp1[0], cp1[1], cp2[0], cp2[1], to_point[0], to_point[1])
canvas.set_line_width(2)
canvas.draw_path()

# Draw the red dots and lines to illustrate what's happening:
canvas.set_stroke_color(1, 0, 0)
canvas.set_fill_color(1, 0, 0)

# Draw straight lines between the control points and the end points:
canvas.draw_line(from_point[0], from_point[1], cp1[0], cp1[1])
canvas.draw_line(to_point[0], to_point[1], cp2[0], cp2[1])

# Draw red squares on all the points:
canvas.fill_rect(from_point[0]-4, from_point[1]-4, 8, 8)
canvas.fill_rect(to_point[0]-4, to_point[1]-4, 8, 8)
canvas.fill_rect(cp1[0]-4, cp1[1]-4, 8, 8)
canvas.fill_rect(cp2[0]-4, cp2[1]-4, 8, 8)