def cohen_Sutherland_clip(x0, y0, x1, y1, x_min, y_min, x_max, y_max, win): outcode0 = cal_outcode(x0, y0, x_min, y_min, x_max, y_max) outcode1 = cal_outcode(x1, y1, x_min, y_min, x_max, y_max) if (x1 != x0): slope = (y1 - y0) / (x1 - x0) else: slope = None done = accept = False while (done is False): if (outcode0 | outcode1) == 0: # Trivially Accepted done = True accept = True elif (outcode0 & outcode1) != 0: # Trivially Rejected done = True else: if (outcode0): tmp_outcode = outcode0 flag = 0 else: tmp_outcode = outcode1 flag = 1 # Find intersections with edges if (tmp_outcode & 8): if (slope is None): tx = x0 else: tx = x0 + (y_max - y0) / slope ty = y_max elif (tmp_outcode & 4): if (slope is None): tx = x0 else: tx = x0 + (y_min - y0) / slope ty = y_min elif (tmp_outcode & 2): tx = x_max ty = y0 + (slope) * (x_max - x0) else: tx = x_min ty = y0 + (slope) * (x_min - x0) # Now we move outside point to intersection point and update outcode if (flag == 0): x0 = tx y0 = ty outcode0 = cal_outcode(x0, y0, x_min, y_min, x_max, y_max) else: x1 = tx y1 = ty outcode1 = cal_outcode(x1, y1, x_min, y_min, x_max, y_max) if (accept is True): x0 = int(round(x0)) y0 = int(round(y0)) x1 = int(round(x1)) y1 = int(round(y1)) plotLine(x0, y0, x1, y1, window, viewport, win, "red") return 0
def main(): global viewport, window print("Enter length of viewport : ", end="") x = int(input()) print("Enter width of viewport : ", end="") y = int(input()) # Notation : viewport = (xv_min,yv_min,xv_max,yv_max) viewport = (0, 0, x, y) win = GraphWin("Cohen-Sutherland Line Clipping", x, y) win.setBackground(color_rgb(92, 219, 149)) print("Enter xw_min : ", end="") xw_min = int(input()) print("Enter yw_min : ", end="") yw_min = int(input()) print("Enter xw_max : ", end="") xw_max = int(input()) print("Enter yw_max : ", end="") yw_max = int(input()) # Some Sanity Checks for window corners if (xw_min > xw_max): (xt, yt) = (xw_min, yw_min) (xw_min, yw_min) = (xw_max, yw_max) (xw_max, yw_max) = (xt, yt) if (yw_min > yw_max): (yw_min, yw_max) = (yw_max, yw_min) win.setCoords(xw_min, yw_min, xw_max, yw_max) # Notation : window = (xw_min,yw_min,xw_max,yw_max) window = (xw_min, yw_min, xw_max, yw_max) ################################################### print("Enter the coordinates of clipping rectangle") print("Enter x_min : ", end="") x_min = int(input()) print("Enter y_min : ", end="") y_min = int(input()) print("Enter x_max : ", end="") x_max = int(input()) print("Enter y_max : ", end="") y_max = int(input()) print("Enter point 1 for line(x space y) : ", end="") (x0, y0) = (map(int, input().split())) print("Enter point 2 for line(x space y) : ", end="") (x1, y1) = (map(int, input().split())) ################################################### plotLine(x_min, y_min, x_min, y_max, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_min, y_min, x_max, y_min, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_min, y_max, x_max, y_max, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_max, y_max, x_max, y_min, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x0, y0, x1, y1, window, viewport, win, color_rgb(5, 56, 107)) cohen_Sutherland_clip(x0, y0, x1, y1, x_min, y_min, x_max, y_max, win) win.getMouse() win.close()
def plotPolygon(vertices, window, viewport, win, color): length = len(vertices) i = 0 while (i <= length - 2): x0 = vertices[i][0][0] y0 = vertices[i][1][0] x1 = vertices[i + 1][0][0] y1 = vertices[i + 1][1][0] plotLine(x0, y0, x1, y1, window, viewport, win, color) i += 1 x0 = vertices[0][0][0] y0 = vertices[0][1][0] x1 = vertices[length - 1][0][0] y1 = vertices[length - 1][1][0] plotLine(x0, y0, x1, y1, window, viewport, win, color)
def plotPolygon(vertices,win): length = len(vertices) ; i=0 edges = [] while(i <= length-2): x0 = vertices[i][0] ; y0 = vertices[i][1] x1 = vertices[i+1][0] ; y1 = vertices[i+1][1] tt1 = tuple([x0,y0,x1,y1]) plotLine(x0,y0,x1,y1,window,viewport,win,"black") edges.append(tt1) i += 1 x0 = vertices[0][0] ; y0 = vertices[0][1] x1 = vertices[length-1][0] ; y1 = vertices[length-1][1] tt1 = tuple([x0,y0,x1,y1]) edges.append(tt1) plotLine(x0,y0,x1,y1,window,viewport,win,"black") polyFill(edges,win)
def reflect(A, win): print("Enter point 1 for line (x space y): ", end='') (x0, y0) = (map(int, input().split())) print("Enter point 2 for line (x space y): ", end='') (x1, y1) = (map(int, input().split())) if (x0 != x1): slope = (y1 - y0) / (x1 - x0) angle = math.atan(slope) angle = angle * (180 / math.pi) else: angle = 90 plotLine(x0, y0, x1, y1, window, viewport, win, "blue") tmp = translate(-x0, -y0, A) tmp = rotate(-angle, [0, 0], tmp) mat = [[1, 0, 0], [0, -1, 0], [0, 0, 1]] tmp = matrix_multiply(mat, tmp) tmp = rotate(angle, [0, 0], tmp) tmp = translate(x0, y0, tmp) return tmp
def main(): global viewport,window print("Enter length of viewport : ",end="") ; x = int(input()) print("Enter width of viewport : ",end="") ; y = int(input()) # Notation : viewport = (xv_min,yv_min,xv_max,yv_max) viewport = (0,0,x,y) win = GraphWin("Sutherland Hodgman Polygon Clipping",x,y) win.setBackground(color_rgb(92,219,149)) print("Enter xw_min : ",end="") ; xw_min = int(input()) print("Enter yw_min : ",end="") ; yw_min = int(input()) print("Enter xw_max : ",end="") ; xw_max = int(input()) print("Enter yw_max : ",end="") ; yw_max = int(input()) # Some Sanity Checks for window corners if(xw_min>xw_max): (xt,yt) = (xw_min,yw_min) (xw_min,yw_min) = (xw_max,yw_max) (xw_max,yw_max) = (xt,yt) if(yw_min>yw_max): (yw_min,yw_max) = (yw_max,yw_min) win.setCoords(xw_min,yw_min,xw_max,yw_max) # Notation : window = (xw_min,yw_min,xw_max,yw_max) window = (xw_min,yw_min,xw_max,yw_max) ################################################### print("Enter coordinates of polygon to be clipped in clockwise order") print("Number of vertices : ",end="") V = int(input()) vertices = [] for i in range(V): print("Enter V",i+1,"(x space y) : ",end="") (x,y) = (map(int,input().split())) vertices.append([x,y]) for i in range(V-1): x1 = vertices[i][0] ; y1 = vertices[i][1] x2 = vertices[i+1][0] ; y2 = vertices[i+1][1] plotLine(x1,y1,x2,y2,window,viewport,win,color_rgb(5,56,107)) x1 = vertices[V-1][0] ; y1 = vertices[V-1][1] x2 = vertices[0][0] ; y2 = vertices[0][1] plotLine(x1,y1,x2,y2,window,viewport,win,color_rgb(5,56,107)) ################################################### print("Enter coordinates of clipping polygon(convex)in clockwise order") print("Number of vertices : ",end="") VC = int(input()) vertices_c = [] for i in range(VC): print("Enter V",i+1,"(x space y) : ",end="") (x,y) = (map(int,input().split())) vertices_c.append([x,y]) for i in range(VC-1): x1 = vertices_c[i][0] ; y1 = vertices_c[i][1] x2 = vertices_c[i+1][0] ; y2 = vertices_c[i+1][1] plotLine(x1,y1,x2,y2,window,viewport,win,"black") x1 = vertices_c[VC-1][0] ; y1 = vertices_c[VC-1][1] x2 = vertices_c[0][0] ; y2 = vertices_c[0][1] plotLine(x1,y1,x2,y2,window,viewport,win,"black") ################################################## new_vertices = sutherland_Hodgman(vertices,vertices_c) V = len(new_vertices) for i in range(V-1): x1 = new_vertices[i][0] ; y1 = new_vertices[i][1] x2 = new_vertices[i+1][0] ; y2 = new_vertices[i+1][1] plotLine(x1,y1,x2,y2,window,viewport,win,"red") x1 = new_vertices[V-1][0] ; y1 = new_vertices[V-1][1] x2 = new_vertices[0][0] ; y2 = new_vertices[0][1] plotLine(x1,y1,x2,y2,window,viewport,win,"red") ################################################## win.getMouse() win.close()
def main(): global viewport, window print("Enter length of viewport : ", end="") x = int(input()) print("Enter width of viewport : ", end="") y = int(input()) # Notation : viewport = (xv_min,yv_min,xv_max,yv_max) viewport = (0, 0, x, y) win = GraphWin("Projections", x, y) win.setBackground(color_rgb(60, 179, 113)) print("Enter xw_min : ", end="") xw_min = int(input()) print("Enter yw_min : ", end="") yw_min = int(input()) print("Enter xw_max : ", end="") xw_max = int(input()) print("Enter yw_max : ", end="") yw_max = int(input()) # Some Sanity Checks for window corners if (xw_min > xw_max): (xt, yt) = (xw_min, yw_min) (xw_min, yw_min) = (xw_max, yw_max) (xw_max, yw_max) = (xt, yt) if (yw_min > yw_max): (yw_min, yw_max) = (yw_max, yw_min) win.setCoords(xw_min, yw_min, xw_max, yw_max) # Notation : window = (xw_min,yw_min,xw_max,yw_max) window = (xw_min, yw_min, xw_max, yw_max) # CUBE(SIDE = 300) - 12 Edges, Homogeneous Coordinates points = [[[0, 300, 300, 1], [0, 300, 0, 1]], [[0, 300, 0, 1], [300, 300, 0, 1]], [[300, 300, 0, 1], [300, 300, 300, 1]], [[300, 300, 300, 1], [0, 300, 300, 1]], [[0, 0, 300, 1], [0, 0, 0, 1]], [[0, 0, 0, 1], [300, 0, 0, 1]], [[300, 0, 0, 1], [300, 0, 300, 1]], [[300, 0, 300, 1], [0, 0, 300, 1]], [[0, 0, 300, 1], [0, 300, 300, 1]], [[0, 0, 0, 1], [0, 300, 0, 1]], [[300, 0, 300, 1], [300, 300, 300, 1]], [[300, 0, 0, 1], [300, 300, 0, 1]]] print(" 1. Orthographic - Top View") print(" 2. Orthographic - Front View") print(" 3. Orthographic - Side View") print(" 4. Orthographic - Isometric") print(" 5. Oblique - General Parallel") print(" 6. General Perspective") choice = int(input(" Enter choice : ")) if (choice == 1): p_mat = [[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] ref_point = (0, 0, 0) normal = dop = (0, 1, 0) elif (choice == 2): p_mat = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 1]] ref_point = (0, 0, 0) normal = dop = (0, 0, 1) elif (choice == 3): p_mat = [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] ref_point = (0, 0, 0) normal = dop = (1, 0, 0) elif (choice == 4): print("Enter reference point of PP(x space y space z) : ", end='') ref_point = tuple(map(int, input().split())) normal = dop = (1, 1, 1) p_mat = project_matrix(ref_point, normal, dop) elif (choice == 5): print("Enter reference point of PP(x space y space z) : ", end='') ref_point = tuple(map(int, input().split())) print("Enter normal to the plane(n1 space n2 space n3) : ", end="") normal = tuple(map(int, input().split())) print("Enter the Direction of Projection(a space b space c) : ", end="") dop = tuple(map(int, input().split())) p_mat = project_matrix(ref_point, normal, dop) elif (choice == 6): print("Enter reference point of PP(x space y space z) : ", end='') ref_point = tuple(map(int, input().split())) print("Enter normal to the plane(n1 space n2 space n3) : ", end="") normal = tuple(map(int, input().split())) print("Enter the COP(x space y space z) : ", end="") cop = tuple(map(int, input().split())) p_mat = project_matrix_perspective(ref_point, normal, cop) q_mat = make_viewplane_xy(ref_point, normal) for i in range(12): point1 = points[i][0] point2 = points[i][1] point1 = matrix_multiply(point1, p_mat) point2 = matrix_multiply(point2, p_mat) # Homogenize the points again if (point1[3] != 1): point1[0] = point1[0] / point1[3] point1[1] = point1[1] / point1[3] point1[2] = point1[2] / point1[3] point1[3] = 1 if (point2[3] != 1): point2[0] = point2[0] / point2[3] point2[1] = point2[1] / point2[3] point2[2] = point2[2] / point2[3] point2[3] = 1 point1 = matrix_multiply(point1, q_mat) point2 = matrix_multiply(point2, q_mat) points[i][0] = point1 points[i][1] = point2 for i in range(12): point1 = points[i][0] point2 = points[i][1] x0 = point1[0] y0 = point1[1] x1 = point2[0] y1 = point2[1] plotLine(x0, y0, x1, y1, window, viewport, win, "black") win.getMouse() win.close()
def main(): global viewport, window print("Enter length of viewport : ", end="") x = int(input()) print("Enter width of viewport : ", end="") y = int(input()) # Notation : viewport = (xv_min,yv_min,xv_max,yv_max) viewport = (0, 0, x, y) win = GraphWin("Parametric Line Clipping", x, y) win.setBackground(color_rgb(92, 219, 149)) print("Enter xw_min : ", end="") xw_min = int(input()) print("Enter yw_min : ", end="") yw_min = int(input()) print("Enter xw_max : ", end="") xw_max = int(input()) print("Enter yw_max : ", end="") yw_max = int(input()) # Some Sanity Checks for window corners if (xw_min > xw_max): (xt, yt) = (xw_min, yw_min) (xw_min, yw_min) = (xw_max, yw_max) (xw_max, yw_max) = (xt, yt) if (yw_min > yw_max): (yw_min, yw_max) = (yw_max, yw_min) win.setCoords(xw_min, yw_min, xw_max, yw_max) # Notation : window = (xw_min,yw_min,xw_max,yw_max) window = (xw_min, yw_min, xw_max, yw_max) ################################################### print("Enter the coordinates of clipping rectangle") print("Enter x_min : ", end="") x_min = int(input()) print("Enter y_min : ", end="") y_min = int(input()) print("Enter x_max : ", end="") x_max = int(input()) print("Enter y_max : ", end="") y_max = int(input()) print("Enter point 1 for line(x space y) : ", end="") (x0, y0) = (map(int, input().split())) print("Enter point 2 for line(x space y) : ", end="") (x1, y1) = (map(int, input().split())) ################################################### plotLine(x_min, y_min, x_min, y_max, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_min, y_min, x_max, y_min, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_min, y_max, x_max, y_max, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x_max, y_max, x_max, y_min, window, viewport, win, color_rgb(5, 56, 107)) plotLine(x0, y0, x1, y1, window, viewport, win, color_rgb(5, 56, 107)) ################################################### tt = cyrus_beck_line_clip(x0, y0, x1, y1, x_min, y_min, x_max, y_max) if (tt is not None): x0_new = int(round(x0 + tt[0] * (x1 - x0))) y0_new = int(round(y0 + tt[0] * (y1 - y0))) x1_new = int(round(x0 + tt[1] * (x1 - x0))) y1_new = int(round(y0 + tt[1] * (y1 - y0))) plotLine(x0_new, y0_new, x1_new, y1_new, window, viewport, win, "red") win.getMouse() win.close()