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()
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()
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)
def createSpiral(arm1, arm2, color): """arm1 and arm2 are pairs of (length, velocity)""" canvas.begin_path() canvas.move_to(512, 684) x, y = 0, 0 len1, step1 = arm1 len2, step2 = arm2 global lines lines = [] previousPositions = [] while step1 > 10 or step2 > 10: step1 /= 2 step2 /= 2 global run run = 1 iteration = 1 inarow = 0 while run: iteration += 10 point1 = rotate((0,len1), x) point2 = map(sum,zip(rotate((0, len2), y), point1)) p2 = map(sum, zip(point2, (512, 384))) #Detection of whether pattern is repeating itself if point2 not in previousPositions: previousPositions.append(point2) inarow = 0 else: inarow += 1 if inarow >= 5: print "Pattern is detected to be repeating itself" run = 0 if x == 0: oldpoint2 = point2 else: canvas.add_line(p2[0], p2[1]) #lines.append( canvas.add_line(point1[0], point1[1]) ) #lines.append( canvas.add_line(point2[0], point2[1]) ) oldpoint2 = point2 x += step1 if x > 360: x -= 360 y += step2 if y > 360: y -= 360 #for line in lines: # canvas.delete(line) lines = [] time.sleep(0.005) canvas.close_path() canvas.set_line_width(1) canvas.draw_path()
def createSpiral(arm1, arm2, color): """arm1 and arm2 are pairs of (length, velocity)""" canvas.begin_path() canvas.move_to(512, 684) x, y = 0, 0 len1, step1 = arm1 len2, step2 = arm2 global lines lines = [] previousPositions = [] while step1 > 10 or step2 > 10: step1 /= 2 step2 /= 2 global run run = 1 iteration = 1 inarow = 0 while run: iteration += 10 point1 = rotate((0,len1), x) point2 = map(sum,zip(rotate((0, len2), y), point1)) p2 = map(sum, zip(point2, (512, 384))) #Detection of whether pattern is repeating itself if point2 not in previousPositions: previousPositions.append(point2) inarow = 0 else: inarow += 1 if inarow >= 5: print("Pattern is detected to be repeating itself") run = 0 if x == 0: oldpoint2 = point2 else: canvas.add_line(p2[0], p2[1]) #lines.append( canvas.add_line(point1[0], point1[1]) ) #lines.append( canvas.add_line(point2[0], point2[1]) ) oldpoint2 = point2 x += step1 if x > 360: x -= 360 y += step2 if y > 360: y -= 360 #for line in lines: # canvas.delete(line) lines = [] time.sleep(0.005) canvas.close_path() canvas.set_line_width(1) canvas.draw_path()
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)
def draw_state(statusList: list, x_pos=50, y_pos=50, width=250): canvas.clear() height = width # высота поля равно ширине widKl = width // 3 # ширина клетки x = widKl // 2 + x_pos # начальные координата середины клетки y = x index = 0 # индекс списка # отрисовка линий canvas.line_width(2) canvas.set_color('Grey') for n in [1, 2]: canvas.move_to(n * widKl + x_pos, 0 + y_pos) canvas.line_to(n * widKl + x_pos, height + y_pos) canvas.move_to(0 + x_pos, n * widKl + y_pos) canvas.line_to(width + x_pos, n * widKl + y_pos) canvas.line_width(5) for i in range(3): for j in range(3): el = statusList[index] index += 1 # расчет координат середины текущей клетки _x = x + widKl * j _y = y + widKl * i if el is not None: if el == 'o': canvas.set_color('Green') canvas.circle(_x, _y, width * 0.1) if el == 'x': canvas.set_color('Blue') wNol = width * 0.1 canvas.move_to(_x - wNol, _y + wNol) canvas.line_to(_x + wNol, _y - wNol) canvas.move_to(_x - wNol, _y - wNol) canvas.line_to(_x + wNol, _y + wNol)
def fill_triangles(x, y, size, theme): # fill upper triangle canvas.set_fill_color(*shade_of(random.choice(theme.colors))) canvas.begin_path() canvas.move_to(x, y) canvas.add_line(x, y + size) canvas.add_line(x + size, y + size) canvas.add_line(x, y) canvas.fill_path() # fill lower triangle canvas.set_fill_color(*shade_of(random.choice(theme.colors))) canvas.begin_path() canvas.move_to(x, y) canvas.add_line(x + size, y + size) canvas.add_line(x + size, y) canvas.add_line(x, y) canvas.fill_path()
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()
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))
def draw_heart(scale = 18): # 18 = full canvas #print(scale) # useful for debugging first_time = True (xorigin, yorigin) = canvas.get_size() xorigin *= 0.5 # in the center yorigin *= 0.588 # north of center detail = 100 canvas.begin_path() for t in range(int(2 * math.pi * detail)): t *= detail x = scale * (16 * math.sin(t) ** 3) y = scale * (13 * math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4 * t)) if first_time: # hide the seams canvas.move_to(x + xorigin, y + yorigin) first_time = False canvas.add_line(x + xorigin, y + yorigin) canvas.set_line_width(1) canvas.close_path() canvas.draw_path() # try commenting out this line... canvas.set_fill_color(1, 0, 0) canvas.fill_path() # how do I fill just the inner part?
def draw_heart(scale = 18): # 18 = full canvas #print(scale) # useful for debugging first_time = True (xorigin, yorigin) = canvas.get_size() xorigin *= 0.5 # in the center yorigin *= 0.588 # north of center detail = 100 canvas.begin_path() for t in xrange(int(2 * math.pi * detail)): t *= detail x = scale * (16 * math.sin(t) ** 3) y = scale * (13 * math.cos(t) - 5*math.cos(2*t) - 2*math.cos(3*t) - math.cos(4 * t)) if first_time: # hide the seams canvas.move_to(x + xorigin, y + yorigin) first_time = False canvas.add_line(x + xorigin, y + yorigin) canvas.set_line_width(1) canvas.close_path() canvas.draw_path() # try commenting out this line... canvas.set_fill_color(1, 0, 0) canvas.fill_path() # how do I fill just the inner part?
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()
def paint_draw(): global paint_obj for el in paint_obj: if el[0] == 'point': canvas.fill_style(el[3]) canvas.fill_circle(el[1], el[2], 2) if el[0] == 'line': canvas.set_color(el[5]) canvas.move_to(el[1], el[2]) canvas.line_to(el[3], el[4]) if el[0] == 'rect': if el[6]: canvas.fill_style(el[5]) canvas.fill_rect(el[1], el[2], el[3], el[4]) else: canvas.set_color(el[5]) canvas.stroke_rect(el[1], el[2], el[3], el[4]) if el[0] == 'circle': if el[5]: canvas.fill_style(el[4]) canvas.fill_circle(el[1], el[2], el[3]) else: canvas.set_color(el[4]) canvas.circle(el[1], el[2], el[3])
import canvas w = h = 512 canvas.set_size(w, h) canvas.move_to(w * 0.45, h * 0.1) canvas.add_line(w * 0.8, h * 0.55) canvas.add_line(w * 0.55, h * 0.65) canvas.add_line(w * 0.65, h * 0.85) canvas.add_line(w * 0.3, h * 0.55) canvas.add_line(w * 0.55, h * 0.45) canvas.close_path() canvas.set_line_width(3) canvas.draw_path() canvas.fill_path()
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)
import canvas w = h = 512 canvas.set_size(w, h) canvas.move_to(w*0.45, h*0.1) canvas.add_line(w*0.8, h*0.55) canvas.add_line(w*0.55, h*0.65) canvas.add_line(w*0.65, h*0.85) canvas.add_line(w*0.3, h*0.55) canvas.add_line(w*0.55, h*0.45) canvas.close_path() canvas.set_line_width(3) canvas.draw_path() canvas.fill_path()
def move_to(self, x, y): canvas.move_to(*self.xyWorld(x, y))
# 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)