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()
def __init__(self, xmin=0.0, ymin=0.0, xsize=None, ysize=None): ''' uses canvas size as default ''' self.xmin = xmin self.ymin = ymin if xsize is None or ysize is None: w, h = canvas.get_size() if xsize is None: xsize = w if ysize is None: ysize = h self.xsize = xsize self.ysize = ysize
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_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()
originX = w / 2 originY = h / 2 for i in range(1, 300, 5): x1 = originX y1 = originY x2 = 100 + 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] canvas.draw_line(x1, y1, x2, y2) canvas.set_fill_color(0, 0.7, 0) for i in range(40, 460, 20): x = i + 5 y = originY - 5 canvas.fill_rect(x, y, 10, 10) for i in range(40, 460, 20): x = originX - 5 y = i + 5 canvas.fill_rect(x, y, 10, 10) canvas.set_fill_color(0.5, 0, 0, 0.5)
def coordinates(radius): w, h = canvas.get_size() x = random.uniform(0, w - radius * 2) y = random.uniform(0, h - radius * 2) return x, y
def radius(): return random.uniform(0, min(*canvas.get_size()) / 5)