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)
def main(): console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue') motion.start_updates() sleep(0.2) print('Capturing motion data...') w = 1000 h = 1200 while True: sleep(0.01) current = motion.get_gravity() newMotion = [0,0,0] for i in range(len(current)): newMotion[i] = (current[i]*(10**3))//1 #print(newMotion) x = newMotion[0]+500 y = newMotion[1]+500 z = newMotion[2]+1000 goalX = w/2 goalY = h/2 canvas.set_size(w, h) canvas.set_fill_color(0, 0, 0) canvas.fill_ellipse(goalX, goalY, 30, 30) if (abs(goalX-x) < 10 and abs(goalY-y) < 10): canvas.set_fill_color(0, 1, 0) canvas.fill_ellipse(x, y, 30, 30) else: canvas.set_fill_color(1, 0, 0) canvas.fill_ellipse(x, y, 30, 30) motion.stop_updates() print('Capture finished, plotting...')
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
def show_render(image): canvas.set_aa_enabled(False) canvas.set_size(image.width, image.height) for y, pixel in enumerate(image.pixels): for x, pixel in enumerate(image.pixels[y]): try: canvas.set_fill_color(pixel.x, pixel.y, pixel.z) canvas.fill_rect(x, y, 1, 1) except: pass
def main(): import canvas canvas.set_size(1000,1000) x=0 y=0 colors=distinct_colors(1,[(0,0,0),(1,1,1)]) for c in colors: canvas.set_fill_color(*c) canvas.fill_rect(x,y,50,50) x=x+60 if x>900: x=0 y=y+60
def __init__(self): self.num_spectra = 0 self.x_set = [] self.y_set = [] self.spectrum_colour = [] self.style = [] self.x_min = 1000000.0 self.x_max = 0 self.y_min = 1000000.0 self.y_max = 0 canvas.set_size(688, 688) self.x_offset = 50 self.y_offset = 30 self.x_axis_format = '{:.1f}' self.y_axis_format = '{:.1f}'
def main(): console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue') motion.start_updates() sleep(0.2) print('Capturing motion data...') num_samples = 100 w = 512 h = 512 for nums in range(num_samples): canvas.set_size(w, h) sleep(0.05) current = motion.get_gravity() newMotion = [0,0,0] for i in range(len(current)): newMotion[i] = (current[i]*(10**3))//1 print(newMotion) canvas.set_fill_color(1, 0, 0) canvas.fill_ellipse(0, 0, newMotion[1], newMotion[2]) motion.stop_updates() print('Capture finished, plotting...')
def main(): console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue') motion.start_updates() sleep(0.2) print('Capturing motion data...') w = 1000 h = 1200 goalX = w / 2 goalY = h / 2 moalX = w / 2 - 100 moalY = h / 2 - 100 while True: sleep(0.01) current = motion.get_gravity() newMotion = [0, 0, 0] for i in range(len(current)): newMotion[i] = (current[i] * (10**2)) // 1 #print(newMotion) x = newMotion[0] + 100 y = newMotion[1] + 100 z = newMotion[2] + 100 goalX += (x - 100) goalY += (y - 100) moalX += (x - 102) * 2 moalY += (y - 102) * 2 if goalX <= 0 or goalY <= 0 or goalX >= w or goalY >= h: goalX -= (x - 100) goalY -= (y - 100) if moalX <= 0 or moalY <= 0 or moalX >= w or moalY >= h: moalX -= (x - 102) * 2 moalY -= (y - 102) * 2 canvas.set_size(w, h) canvas.set_fill_color(0, 0, 0) canvas.fill_ellipse(goalX, goalY, 30, 30) canvas.set_fill_color(0, 0, 1) canvas.fill_ellipse(moalX, moalY, 30, 30) motion.stop_updates() print('Capture finished, plotting...')
def main(): console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue') motion.start_updates() sleep(0.2) print('Capturing motion data...') w = 512 h = 512 while True: sleep(0.1) current = motion.get_gravity() newMotion = [0, 0, 0] for i in range(len(current)): newMotion[i] = (current[i] * (10**3)) // 1 #print(newMotion) if newMotion[0] < 1001 and newMotion[0] > 900 and newMotion[ 1] > -50 and newMotion[1] < 50 and newMotion[ 2] > -50 and newMotion[2] < 50: canvas.set_size(w, h) canvas.set_fill_color(1, 0, 0) canvas.fill_ellipse(0, 0, w, h) if newMotion[0] > -1001 and newMotion[0] < -900 and newMotion[ 1] > -50 and newMotion[1] < 50 and newMotion[ 2] > -50 and newMotion[2] < 50: canvas.set_size(w, h) canvas.set_fill_color(0, 1, 0) canvas.fill_ellipse(0, 0, w, h) if newMotion[0] > -50 and newMotion[0] < 50 and newMotion[ 1] > -50 and newMotion[1] < 50 and newMotion[ 2] > -1001 and newMotion[2] < -900: canvas.set_size(w, h) canvas.set_fill_color(0, 0, 1) canvas.fill_ellipse(0, 0, w, h) motion.stop_updates() print('Capture finished, plotting...')
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()
def render(self, scene, samples): width = scene.width height = scene.height canvas.set_size(width, height) aspect_ratio = float(width) / height x0 = -1.0 x1 = +1.0 xstep = (x1 - x0) / (width - 1) y0 = -1.0 / aspect_ratio y1 = +1.0 / aspect_ratio ystep = (y1 - y0) / (height - 1) camera = scene.camera pixels = Image(width, height) heightRange = list(range(height)) widthRange = list(range(width)) #random.shuffle(heightRange) #random.shuffle(widthRange) for j in heightRange: y = y0 + j * ystep for i in widthRange: x = x0 + i * xstep renderedSamples = [] for _ in range(samples): if samples > 1: ray = Ray(camera, Point(x+random.uniform(xstep*-1,xstep),y+random.uniform(xstep*-1,ystep)) - camera) else: ray = Ray(camera, Point(x,y) - camera) raytracedColorSample = self.ray_trace(ray, scene) renderedSamples.append(raytracedColorSample) finalPixel = sum(renderedSamples, Color(0,0,0)) / len(renderedSamples) pixels.set_pixel(i, j, finalPixel) canvas.set_fill_color(finalPixel.x, finalPixel.y, finalPixel.z) canvas.fill_rect(i, j, 1, 1) return pixels
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)
import math import random import canvas f = 200 h = 0 canvas.set_size(f, f) canvas.set_fill_color(1, 1, 1) canvas.fill_rect(0, 0, f, f) k = int(input('Number of dots: ')) for i in range(0, k): x = (random.randint(0, f) / f) y = (random.randint(0, f) / f) if math.sqrt(math.pow(x, 2) + math.pow(y, 2)) <= 1: h += 1 canvas.set_fill_color(0, 1, 0) canvas.fill_pixel(x * f, y * f) else: canvas.set_fill_color(1, 0, 0) canvas.fill_pixel(x * f, y * f) if i % 5000 == 0: print(i) print(h / k)
import canvas import random from math import pi canvasWidth = canvasHeight = 500 canvas.set_size(canvasWidth, canvasHeight) x = 10 y = 10 w = 50 h = 50 r = b = g = 1.0 for i in range(8): for j in range(8): r -= 0.01 b = 0.0 g = 0.0 canvas.set_fill_color(r, b, g) canvas.fill_rect(x, y, w, h) y += 60 x += 60 y = 10 x = 15 y = 15 w = 40 h = 40 colors = [(0.545, 0.271, 0.075), (0.545, 0.000, 0.000), (1.000, 0.647, 0.000), (0.933, 0.910, 0.667), (0.000, 0.392, 0.000), (0.529, 0.808, 0.922),
#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() #Set up the canvas size and clear any text output: console.clear() canvas.set_size(688, 688) #Draw the grid: area = (-pi, pi, -pi, pi) draw_grid(*area) #Draw 4 different graphs (sin(x), cos(x), x^2, x^3): plot_function(sin, (1, 0, 0), *area) plot_function(cos, (0, 0, 1), *area) plot_function(lambda x: x ** 2, (0, 1, 1), *area) plot_function(lambda x: x ** 3, (1.0, 0.5, 0), *area)
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()
print('touch began at:%s org=%s' % (touch.location, self.xyUser)) def touch_moved(self, touch): moved = touch.location-self.xyStart wh = self.context.whUser(*moved) #moved= self.context.whUser(*map(sum, zip(xy, self.xyStart))) moved= self.context.user.offset(*wh) print('touch moved:%s %s' % (moved,self.xyUser)) #moved = tuple(map(operator.__sub__, self.xyUser, moved)) self.context.user.move(*moved) # #.set_origin(*moved) print('new org:%s %s ' % self.context.user.get_origin()) self.draw(autoscale=False) if __name__ == "__main__": #import ui canvas.set_size(*ui.get_screen_size()) view = touch_view() # Plot a quadratic and a cubic curve in the same plot. xs = [(t-40.0)/30 for t in range(0,101)] y1 = [x**2-0.3 for x in xs] y2 = [x**3 for x in xs] view.graph.add_plot(xs, y1, color=(0.00, 0.00, 1.00)) view.graph.add_plot(xs, y2, color=(0.00, 0.50, 0.00)) #view.graph.draw() view.present('sheet')
finally: canvas.restore_gstate() screenHeight = 512 circleHeight = 128 colorBlue = (0, 0, 1) colorGreen = (0, 1, 0) colorRed = (1, 0, 0) def coloredCircle(inColor, inX, inY): canvas.set_fill_color(*inColor) canvas.fill_ellipse(inX, inY, circleHeight, circleHeight) canvas.clear() canvas.set_size(screenHeight * 1.42, screenHeight) with privateGstate(): # Save and then restore the canvas.gstate canvas.rotate(89) # Text on an angle. canvas.draw_text('Green on top -->', 154, 0, 'Helvetica', 36) canvas.draw_text('<-- Red and Blue on bottom', 282, 52, 'Helvetica', 36) x = 20 + circleHeight coloredCircle(colorRed, 10, 10) # Red circle appears at bottom. with flippedDisplay(): # Change origin to topLeft instead of bottomLeft. coloredCircle(colorGreen, x, 10) # Green circle appears at top. coloredCircle(colorBlue, x, 10) # Blue circle appears at bottom.
import math from urbanape import common_devices from kuler import * random.seed() width, height = common_devices['ipad_r'] circle_size = 192.0 step = math.sqrt(circle_size**2 - (circle_size / 2.0)**2) palette = let_the_rays_fall_on_the_earth canvas.begin_updates() canvas.set_size(width, height) canvas.set_fill_color(*random.choice(palette)) canvas.fill_rect(0, 0, width, height) for x in range(100): r, g, b = random.choice(palette) a = random.random() * 0.5 + 0.25 canvas.set_fill_color(r, g, b, a) origin_x = random.random() * width origin_y = random.random() * height csize = random.random() * (width / 8.0) + (width / 16.0) canvas.fill_ellipse(origin_x, origin_y, csize, csize) def rstrokedline(start_x, start_y, end_x, end_y): line_width = random.random() * 0.75 + 0.25
import canvas import random import math 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):
paramfile = dialogs.pick_document() with open(paramfile, 'r') as f: params = json.load(f) inz = parseparams(params) print() print('Found parameter file ' + paramfile + ' with description: ' + inz.description) print() setupnum = query except: print('\nValid parameter file not found, so will use setup 1.') inz = grabsetup(setupnum) # start showing stuff on screen canvas.set_size(winwidth, winheight) canvas.set_aa_enabled(False) # important to make thin lines visible canvas.set_fill_color(0, 0, .15) # make space almost black canvas.fill_rect(0, 0, winwidth, winheight) # plot some random white stars canvas.set_fill_color(1, 1, 1) for i in range(53): x = randint(0, winwidth) y = randint(0, winheight) canvas.fill_pixel(x, y) # use MKS units: meter, kg, sec # use average Earth-Moon distance for view scaling moondistance = 3.84399e8
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()
import canvas 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) canvas.set_size(1000, 600) #for i in range(9): draw_tree(500, 100, 30, 300, 150, 120)
#Spirograph.py from math import * import canvas canvas.set_size(1024, 768) import time COLOR = "#000000" #root = ui.View() def rotate(point, angle, center=(0, 0)): counterangle = 360 - angle while counterangle > 0: counterangle -= 360 while counterangle < 0: counterangle += 360 theta = radians(counterangle) #Translate point to rotate around center translated = point[0] - center[0], point[1] - center[1] #Rotate point rotated = (translated[0] * cos(theta) - translated[1] * sin(theta), translated[0] * sin(theta) + translated[1] * cos(theta)) #Translate point back newcoords = (round(rotated[0] + center[0], 1), round(rotated[1] + center[1], 1)) return newcoords def createSpiral(arm1, arm2, color): """arm1 and arm2 are pairs of (length, velocity)"""
#Spirograph.py from math import * import canvas canvas.set_size(1024, 768) import time COLOR = "#000000" #root = ui.View() def rotate(point, angle, center=(0, 0)): counterangle = 360 - angle while counterangle > 0: counterangle -= 360 while counterangle < 0: counterangle += 360 theta = radians(counterangle) #Translate point to rotate around center translated = point[0]-center[0] , point[1]-center[1] #Rotate point rotated = (translated[0]*cos(theta)-translated[1]*sin(theta),translated[0]*sin(theta)+translated[1]*cos(theta)) #Translate point back newcoords = (round(rotated[0]+center[0], 1),round(rotated[1]+center[1], 1)) return newcoords def createSpiral(arm1, arm2, color): """arm1 and arm2 are pairs of (length, velocity)""" canvas.begin_path()
def add_curve(cp1x, cp1y, cp2x, cp2y, x, y): canvas.add_curve(*self.xyWorld(cp1x, cp1y), *self.xyWorld(cp2x, cp2y), *self.xyWorld(x, y)) def get_text_size(self, text, font_name=None, font_size=None): ''' size of text string in user coordinates ''' if font_size is None: font_size = self.font_size if font_name is None: font_name = self.font_name return self.whUser(*canvas.get_text_size(text, font_name, font_size)) if __name__ == "__main__": from time import sleep canvas.set_size(1000, 700) world = plottls.rect_area(100, 100, 300, 300) user = plottls.rect_area(0, 0, 60, 30) #ctx = uiView_context(world ,user) ctx1 = canvas_context(world, user) ctx2 = canvas_context(world, user.sub_area(xofs=-1.2)) # left of world frame ctx1.draw_line(2, 1, 30, 15) ctx1.add_rect(4, 2, 52, 26) ctx1.draw_text('org', 4, 2) ctx1.draw_text('mid1', 30, 15) ctx1.draw_text('top', 30, 28) ctx2.add_rect(4, 2, 52, 26) ctx2.draw_text('mid2', 30, 15)
# https://forum.omz-software.com/topic/3238/the-color-of-the-text-that-is-drawn-with-draw_text import canvas canvas.set_size(600, 600) canvas.set_fill_color(1, 0, 0) #red text = 'OK' x = 0 y = 400 font_family = 'Helvetica-Bold' font_size = 40 canvas.draw_text(text, x, y, font_family, font_size)
# coding: utf-8 # https://forum.omz-software.com/topic/3191/can-t-fill-path # use the canvas to draw a trapezoid import canvas b = 10 canvas.set_size(500, 500 + b) canvas.draw_rect(0, 0 + b, 500, 500) canvas.set_fill_color(1, 0.1, 0.1) canvas.set_stroke_color(1, 0, 0) canvas.set_line_width(4) canvas.begin_path() canvas.add_line(150, 100 + b) canvas.add_line(350, 100 + b) canvas.add_line(400, 200 + b) canvas.add_line(100, 200 + b) canvas.add_line(150, 100 + b) canvas.draw_path() canvas.fill_path() canvas.close_path()
#Draw a red circle filling the entire canvas import canvas w = h = 512 canvas.set_size(w, h) canvas.set_fill_color(1, 0, 0) canvas.fill_ellipse(0, 0, w/2, h/2)
def main(): console.alert('Motion Experiment 2', 'yo gang gang, we gonna measure this motion', 'Continue') motion.start_updates() sleep(0.2) print('Capturing motion data...') w = 1000 h = 1400 while True: sleep(0.01) canvas.set_size(w, h) current = motion.get_gravity() newMotion = [0, 0, 0] for i in range(len(current)): newMotion[i] = (current[i] * (10**3)) // 1 #print(newMotion) x = newMotion[0] + 1000 y = newMotion[1] + 1000 z = newMotion[2] + 1000 goalX = w / 2 goalY = h / 2 #bottom if (abs(x - 1000) < 500 and abs(y - 1000) < 600 and abs(z - 0) < 500): canvas.set_fill_color(1, 0, 0) canvas.draw_text("bottom", x - 500, y - 400, font_name='Courier New', font_size=50.0) #top if (abs(x - 1000) < 500 and abs(y - 1000) < 600 and abs(z - 2000) < 500): canvas.set_fill_color(0, 0, 1) canvas.draw_text("top", 1500 - x, 1700 - y, font_name='Courier New', font_size=50.0) #right if (abs(x - 0) < 500 and abs(y - 1000) < 600 and abs(z - 1000) < 500): canvas.set_fill_color(0, 0, 1) canvas.draw_text("right", 500 - x, 1500 - y, font_name='Courier New', font_size=50.0) #left if (abs(x - 2000) < 500 and abs(y - 1000) < 600 and abs(z - 1000) < 500): canvas.set_fill_color(0, 0, 1) canvas.draw_text("left", x - 1500, 1500 - y, font_name='Courier New', font_size=50.0) #front if (abs(x - 1000) < 500 and abs(y - 0) < 600 and abs(z - 1000) < 500): canvas.set_fill_color(0, 0, 1) canvas.draw_text("front", 500 - y, 1500 - z, font_name='Courier New', font_size=50.0) #back if (abs(x - 1000) < 500 and abs(y - 2000) < 600 and abs(z - 1000) < 500): canvas.set_fill_color(0, 0, 1) canvas.draw_text("back", y - 1500, z - 500, font_name='Courier New', font_size=50.0) # if (z > 1000): # canvas.set_fill_color(0, 0, 1) # canvas.draw_text("top", 1500-x, 1700-y, font_name='Courier New', font_size=50.0) motion.stop_updates() print('Capture finished, plotting...')
# 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)