def draw_bezier(p0, p1, p2, p3, tolerance, img, img_n): # unpack points (p0x, p0y) = p0 (p1x, p1y) = p1 (p2x, p2y) = p2 (p3x, p3y) = p3 # check to see if we can approximate with a flat line if is_flat(p0, p3, p1, tolerance) and is_flat(p0, p3, p2, tolerance): line_drawing.midpoint_line_drawing(p0x, p0y, p3x, p3y, img, img_n) else: # split into two bezier curves # # DO DERIVATION FOR THESE # Basically need three conditions: # First q3 = P(1/2) = r(3) # first_half(2t) = P(t) for t = [0, 0.5] # second_half(2t + 0.5) = P(t) for t = [0.5, 1] q0 = p0 q1 = ((0.5 * p0x + 0.5 * p1x), (0.5 * p0y + 0.5 * p1y)) q2 = ((0.25 * p0x + 0.5 * p1x + 0.25 * p2x), (0.25 * p0y + 0.5 * p1y + 0.25 * p2y)) q3 = ((0.125 * p0x + 0.375 * p1x + 0.375 * p2x + 0.125 * p3x), (0.125 * p0y + 0.375 * p1y + 0.375 * p2y + 0.125 * p3y)) draw_bezier(q0, q1, q2, q3, tolerance, img, img_n) r0 = q3 r1 = ((0.25 * p1x + 0.5 * p2x + 0.25 * p3x), (0.25 * p1y + 0.5 * p2y + 0.25 * p3y)) r2 = ((0.5 * p2x + 0.5 * p3x), (0.5 * p2y + 0.5 * p3y)) r3 = p3 draw_bezier(r0, r1, r2, r3, tolerance, img, img_n)
def draw_bezier(p0, p1, p2, p3, tolerance, img, img_n): # unpack points (p0x, p0y) = p0 (p1x, p1y) = p1 (p2x, p2y) = p2 (p3x, p3y) = p3 # check to see if we can approximate with a flat line if is_flat(p0, p3, p1, tolerance) and is_flat(p0, p3, p2, tolerance): line_drawing.midpoint_line_drawing(p0x, p0y, p3x, p3y, img, img_n) else: # split into two bezier curves # # DO DERIVATION FOR THESE # Basically need three conditions: # First q3 = P(1/2) = r(3) # first_half(2t) = P(t) for t = [0, 0.5] # second_half(2t + 0.5) = P(t) for t = [0.5, 1] q0 = p0 q1 = ((0.5*p0x+0.5*p1x), (0.5*p0y+0.5*p1y)) q2 = ((0.25*p0x + 0.5*p1x + 0.25*p2x), (0.25*p0y + 0.5*p1y + 0.25*p2y)) q3 = ((0.125*p0x + 0.375*p1x + 0.375*p2x + 0.125*p3x), (0.125*p0y + 0.375*p1y + 0.375*p2y + 0.125*p3y)) draw_bezier(q0, q1, q2, q3, tolerance, img, img_n) r0 = q3 r1 = ((0.25*p1x + 0.5*p2x + 0.25*p3x), (0.25*p1y + 0.5*p2y + 0.25*p3y)) r2 = ((0.5*p2x + 0.5*p3x), (0.5*p2y + 0.5*p3y)) r3 = p3 draw_bezier(r0, r1, r2, r3, tolerance, img, img_n)
def cohen_sutherland(xl, xr, yt, yb, start_pt, end_pt, img, img_n): (sx, sy) = start_pt (ex, ey) = end_pt m = (ey-sy)/float(ex-sx) # Q1 = ABCD q_start = (sx < xl, sx > xr, sy < yb, sy > yt) # Q2 = ABCD q_end = (ex < xl, ex > xr, ey < yb, ey > yt) if (not q_start[0] and not q_start[1] and not q_start[2] and not q_start[3]) and \ (not q_end[0] and not q_end[1] and not q_end[2] and not q_end[3]): # accept line_drawing.midpoint_line_drawing(sx, sy, ex, ey, img, img_n) elif (q_start[0] and q_end[0]) or (q_start[1] and q_end[1]) or (q_start[2] and q_end[2]) or \ (q_start[3] and q_end[3]): # reject return None ### CASES # START POINT elif q_start[0]: # clip against left boundary sx_new = xl sy_new = sy + (sx_new-sx)*m return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[1]: # clip against right boundary sx_new = xr sy_new = sy + (sx_new-sx)*m return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[2]: # clip against bottom boundary sy_new = yb sx_new = sx + (1/m)*(sy_new-sy) return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[3]: # clip aggainst top boundary sy_new = yt sx_new = sx + (1/m)*(sy_new-sy) return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) # END POINT elif q_end[0]: # clip against left boundary ex_new = xl ey_new = ey + (ex_new-ex)*m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[1]: # clip against right boundary ex_new = xr ey_new = ey + (ex_new-ex)*m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[2]: # clip against bottom boundary ey_new = yb ex_new = ex + (1/m)*(ey_new-ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[3]: # clip aggainst top boundary ey_new = yt ex_new = ex + (1/m)*(ey_new-ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) else: raise 'Error'
ey_new = ey + (ex_new-ex)*m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[2]: # clip against bottom boundary ey_new = yb ex_new = ex + (1/m)*(ey_new-ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[3]: # clip aggainst top boundary ey_new = yt ex_new = ex + (1/m)*(ey_new-ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) else: raise 'Error' IMAGE_N = 200 IMAGE = np.zeros((IMAGE_N, IMAGE_N)) cohen_sutherland(-25, 25, 25, -25, (-50, -90), (90, 90), IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(-25, -25, 25.1, -25.1, IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(-25, 25, -24.95, -25, IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(25, 25, -25, 25,IMAGE,IMAGE_N) line_drawing.midpoint_line_drawing(25, 25, 25.05, -25.05, IMAGE,IMAGE_N) imgplot = plt.imshow(IMAGE, cmap = cm.Greys_r, interpolation="nearest") plt.show()
def cohen_sutherland(xl, xr, yt, yb, start_pt, end_pt, img, img_n): (sx, sy) = start_pt (ex, ey) = end_pt m = (ey - sy) / float(ex - sx) # Q1 = ABCD q_start = (sx < xl, sx > xr, sy < yb, sy > yt) # Q2 = ABCD q_end = (ex < xl, ex > xr, ey < yb, ey > yt) if (not q_start[0] and not q_start[1] and not q_start[2] and not q_start[3]) and \ (not q_end[0] and not q_end[1] and not q_end[2] and not q_end[3]): # accept line_drawing.midpoint_line_drawing(sx, sy, ex, ey, img, img_n) elif (q_start[0] and q_end[0]) or (q_start[1] and q_end[1]) or (q_start[2] and q_end[2]) or \ (q_start[3] and q_end[3]): # reject return None ### CASES # START POINT elif q_start[0]: # clip against left boundary sx_new = xl sy_new = sy + (sx_new - sx) * m return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[1]: # clip against right boundary sx_new = xr sy_new = sy + (sx_new - sx) * m return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[2]: # clip against bottom boundary sy_new = yb sx_new = sx + (1 / m) * (sy_new - sy) return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) elif q_start[3]: # clip aggainst top boundary sy_new = yt sx_new = sx + (1 / m) * (sy_new - sy) return cohen_sutherland(xl, xr, yt, yb, (sx_new, sy_new), end_pt, img, img_n) # END POINT elif q_end[0]: # clip against left boundary ex_new = xl ey_new = ey + (ex_new - ex) * m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[1]: # clip against right boundary ex_new = xr ey_new = ey + (ex_new - ex) * m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[2]: # clip against bottom boundary ey_new = yb ex_new = ex + (1 / m) * (ey_new - ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[3]: # clip aggainst top boundary ey_new = yt ex_new = ex + (1 / m) * (ey_new - ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) else: raise 'Error'
ey_new = ey + (ex_new - ex) * m return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[2]: # clip against bottom boundary ey_new = yb ex_new = ex + (1 / m) * (ey_new - ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) elif q_end[3]: # clip aggainst top boundary ey_new = yt ex_new = ex + (1 / m) * (ey_new - ey) return cohen_sutherland(xl, xr, yt, yb, start_pt, (ex_new, ey_new), img, img_n) else: raise 'Error' IMAGE_N = 200 IMAGE = np.zeros((IMAGE_N, IMAGE_N)) cohen_sutherland(-25, 25, 25, -25, (-50, -90), (90, 90), IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(-25, -25, 25.1, -25.1, IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(-25, 25, -24.95, -25, IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(25, 25, -25, 25, IMAGE, IMAGE_N) line_drawing.midpoint_line_drawing(25, 25, 25.05, -25.05, IMAGE, IMAGE_N) imgplot = plt.imshow(IMAGE, cmap=cm.Greys_r, interpolation="nearest") plt.show()