def drawLine(win,coords,x0,y0,x1,y1): dx = abs(x1 - x0) dy = abs(y1 - y0) slope = 0 if(dx==0): x2,y2 = transform.transformPoint(coords,x0,y0) x3,y3 = transform.transformPoint(coords,x1,y1) l1 = Line(Point(x2,y2),Point(x3,y3)) l1.setWidth(2) l1.draw(win) time.sleep(2.5) return else: slope = dy/float(dx) x, y = x0, y0 if slope > 1: dx, dy = dy, dx x, y = y, x x0, y0 = y0, x0 x1, y1 = y1, x1 p = 2 * (dy - dx) putPixle(win, x, y) for k in range(2, dx+1): if p > 0: y = y + 1 if y < y1 else y - 1 p = p + 2*(dy - dx) else: p = p + 2*dy x = x + 1 if x < x1 else x - 1 putPixle(win, x, y)
def drawCircle(coords, xc, yc, r): wx1, wy1, wx2, wy2, vx1, vy1, vx2, vy2 = coords win = GraphWin("Circle", vx2 - vx1, vy2 - vy1) win.setCoords(vx1, vy1, vx2, vy2) x_axis1x, x_axis1y = transform.transformPoint(coords, wx2, 0) x_axis2x, x_axis2y = transform.transformPoint(coords, wx1, 0) y_axis1x, y_axis1y = transform.transformPoint(coords, 0, wy2) y_axis2x, y_axis2y = transform.transformPoint(coords, 0, wy1) x_axis = Line(Point(x_axis1x, x_axis1y), Point(x_axis2x, x_axis2y)) x_axis.draw(win) y_axis = Line(Point(y_axis1x, y_axis1y), Point(y_axis2x, y_axis2y)) y_axis.draw(win) x = 0 y = r d = 3 - 2 * r drawP(coords, xc, yc, x, y, win) while (y >= x): x = x + 1 if d > 0: y = y - 1 d = d + 4 * (x - y) + 10 else: d = d + 4 * x + 6 drawP(coords, xc, yc, x, y, win)
def drawPolygon(coords,points): #points is a 2d array. each row contains (x1,y1),(x2,y2) which signifies 1 line of the polygon wx1,wy1,wx2,wy2,vx1,vy1,vx2,vy2 = coords win = GraphWin("Line",vx2-vx1,vy2-vy1) win.setCoords(vx1,vy1,vx2,vy2) x_axis1x,x_axis1y = transform.transformPoint(coords,wx2,0) x_axis2x,x_axis2y = transform.transformPoint(coords,wx1,0) y_axis1x,y_axis1y = transform.transformPoint(coords,0,wy2) y_axis2x,y_axis2y = transform.transformPoint(coords,0,wy1) x_axis = Line(Point(x_axis1x,x_axis1y),Point(x_axis2x,x_axis2y)) x_axis.draw(win) y_axis = Line(Point(y_axis1x,y_axis1y),Point(y_axis2x,y_axis2y)) y_axis.draw(win) for i in range(0,len(points)): x1,y1,x2,y2 = points[i] drawLine(win,coords,x1,y1,x2,y2)
def drawEllipse(coords,xc,yc,rx,ry): wx1,wy1,wx2,wy2,vx1,vy1,vx2,vy2 = coords win = GraphWin("Ellipse",vx2-vx1,vy2-vy1) win.setCoords(vx1,vy1,vx2,vy2) x_axis1x,x_axis1y = transform.transformPoint(coords,wx2,0) x_axis2x,x_axis2y = transform.transformPoint(coords,wx1,0) y_axis1x,y_axis1y = transform.transformPoint(coords,0,wy2) y_axis2x,y_axis2y = transform.transformPoint(coords,0,wy1) x_axis = Line(Point(x_axis1x,x_axis1y),Point(x_axis2x,x_axis2y)) x_axis.draw(win) y_axis = Line(Point(y_axis1x,y_axis1y),Point(y_axis2x,y_axis2y)) y_axis.draw(win) x = 0 y = ry p = (ry*ry) - (rx*rx*ry) + ((rx*rx)/4) while((2*x*ry*ry) < (2*y*rx*rx)): putPixle(win,xc+x,yc-y,coords) putPixle(win,xc-x,yc+y,coords) putPixle(win,xc+x,yc+y,coords) putPixle(win,xc-x,yc-y,coords) if(p<0): x = x+1 p = p + (2*ry*ry*x) + (ry*ry) else: x = x+1 y = y-1 p = p + (2*ry*ry*x + ry*ry) - (2*rx*rx*y) p = (float(x) + 0.5)*(float(x)+0.5)*ry*ry + (y-1)*(y-1)*rx*rx - rx*rx*ry*ry while(y>=0): putPixle(win,xc+x,yc-y,coords) putPixle(win,xc-x,yc+y,coords) putPixle(win,xc+x,yc+y,coords) putPixle(win,xc-x,yc-y,coords) if(p>0): y = y-1 p = p - (2*rx*rx*y) + (rx*rx) else: y = y-1 x = x+1 p = p + (2*ry*ry*x) - (2*rx*rx*y) - (rx*rx)
def drawLine(coords, x0, y0, x1, y1): wx1, wy1, wx2, wy2, vx1, vy1, vx2, vy2 = coords win = GraphWin("Line", vx2 - vx1, vy2 - vy1) win.setCoords(vx1, vy1, vx2, vy2) x_axis1x, x_axis1y = transform.transformPoint(coords, wx2, 0) x_axis2x, x_axis2y = transform.transformPoint(coords, wx1, 0) y_axis1x, y_axis1y = transform.transformPoint(coords, 0, wy2) y_axis2x, y_axis2y = transform.transformPoint(coords, 0, wy1) x_axis = Line(Point(x_axis1x, x_axis1y), Point(x_axis2x, x_axis2y)) x_axis.draw(win) y_axis = Line(Point(y_axis1x, y_axis1y), Point(y_axis2x, y_axis2y)) y_axis.draw(win) dx = abs(x1 - x0) dy = abs(y1 - y0) slope = 0 if (dx == 0): x2, y2 = transform.transformPoint(coords, x0, y0) x3, y3 = transform.transformPoint(coords, x1, y1) l1 = Line(Point(x2, y2), Point(x3, y3)) l1.setWidth(2) l1.draw(win) time.sleep(5) return else: slope = dy / float(dx) x, y = x0, y0 if slope > 1: dx, dy = dy, dx x, y = y, x x0, y0 = y0, x0 x1, y1 = y1, x1 p = 2 * (dy - dx) putPixle(win, x, y) for k in range(2, dx): if p > 0: y = y + 1 if y < y1 else y - 1 p = p + 2 * (dy - dx) else: p = p + 2 * dy x = x + 1 if x < x1 else x - 1 putPixle(win, x, y)
def putPixle(win, x, y): time.sleep(0.1) x1, y1 = transform.transformPoint(coords, x, y) pt = Point(x1, y1) pt.draw(win)
def putPixle(win, x, y, coords): x1, y1 = transform.transformPoint(coords, x, y) pt = Point(x1, y1) pt.draw(win)