コード例 #1
0
def draw_triangle(P, Q, R):
    P_Q = line_algorithm(P, Q)
    Q_R = line_algorithm(Q, R)
    R_P = line_algorithm(R, P)
    P_Q.dda_line()
    Q_R.dda_line()
    R_P.dda_line()
コード例 #2
0
def draw_figure(P, Q, R):
    P_Q = line_algorithm(P, Q)
    Q_R = line_algorithm(Q, R)
    R_P = line_algorithm(R, P)

    lines = [P_Q, Q_R, R_P]
    for line in lines:
        line.dda_line()
def plot_points():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 0.0, 0.0)
    glBegin(GL_POINTS)
    targPts = [(50, 150), (200, 50), (350, 150), (350, 300), (250, 300),
               (200, 250), (150, 350), (100, 250), (100, 200)]
    clipPts = [(100, 100), (300, 100), (300, 300), (100, 300)]
    # printing target polygon
    glColor3f(0, 1, 0)
    for index, each_coord in enumerate(targPts[:-1]):
        line = line_algorithm(each_coord, targPts[index + 1])
        line.dda_line()
    line = line_algorithm(targPts[-1], targPts[0])
    line.dda_line()
    # printing clipper
    glColor3f(1, 0, 0)
    for index, each_coord in enumerate(clipPts[:-1]):
        line = line_algorithm(each_coord, clipPts[index + 1])
        line.dda_line()
    line = line_algorithm(clipPts[-1], clipPts[0])
    line.dda_line()

    # doing the clip
    glColor3f(0, 0, 1)
    output_clip = clip(targPts, clipPts)
    for index, coord in enumerate(output_clip[:-1]):
        line = line_algorithm(coord, output_clip[index + 1])
        line.dda_line()
    line = line_algorithm(output_clip[-1], output_clip[0])
    line.dda_line()

    glEnd()
    glFlush()
コード例 #4
0
def plot_points():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 0.0, 0.0)
    glBegin(GL_POINTS)
    min = [x_min, y_min]
    max = [x_max, y_max]
    X = [x_max, y_min]
    Y = [x_min, y_max]
    min_X = line_algorithm(min, X)
    min_Y = line_algorithm(min, Y)
    max_X = line_algorithm(max, X)
    max_Y = line_algorithm(max, Y)

    # min_X.dda_line()
    # min_Y.dda_line()
    # max_X.dda_line()
    # max_Y.dda_line()
    param_circle = circle_algorithms(radius, 0, 0)
    param_circle.parameteric_circle()
    glEnd()
    glFlush()
    flood_fill(0, 0, [0, 1, 0], old_color)
コード例 #5
0
def plot_points():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0.0, 0.0, 1.0)
    glBegin(GL_POINTS)
    P = [80, 50]
    Q = [60, 10]
    R = [100, 10]
    draw_triangle(P, Q, R)

    # reflection about y_axis + delta change in x
    # x_new = -1 * x + delta
    # draw line x = 30
    delta = 30
    axis = line_algorithm([delta / 2, 200], [delta / 2, -200])
    glColor3f(1.0, 0.0, 1.0)
    axis.dda_line()

    P_ref = [-1 * P[0] + delta, P[1]]
    Q_ref = [-1 * Q[0] + delta, Q[1]]
    R_ref = [-1 * R[0] + delta, R[1]]
    glColor3f(0.0, 1.0, 1.0)
    draw_triangle(P_ref, Q_ref, R_ref)
    glEnd()
    glFlush()
コード例 #6
0
def plot_points():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 0.0, 0.0)
    glBegin(GL_POINTS)
    line_3 = line_algorithm([0, 0], [100, 0])
    line_2 = line_algorithm([100, 0], [100, 100])
    line_1 = line_algorithm([100, 100], [0, 100])
    line_0 = line_algorithm([0, 100], [0, 0])
    P_0 = [0, 0]
    P_1 = [0, 100]
    P_2 = [100, 100]
    P_3 = [100, 0]
    clipper = [P_0, P_1, P_2, P_3]

    # implementating a triangle to cut-off
    edge_2 = line_algorithm([150, -50], [-50, 150])
    edge_1 = line_algorithm([-50, 150], [-50, -50])
    edge_0 = line_algorithm([-50, -50], [150, -50])
    t_0 = [-50, -50]
    t_1 = [-50, 150]
    t_2 = [150, -50]

    # the edges are declared in clockwise direction
    polygon = [t_0, t_1, t_2]

    # line_0.dda_line()
    # line_1.dda_line()
    # line_2.dda_line()
    # line_3.dda_line()

    # edge_0.dda_line()
    # edge_1.dda_line()
    # edge_2.dda_line()
    cohen_hodge_clip(polygon, clipper)
    glEnd()
    glFlush()
def draw_triangle():
    global x0, y0, x1, y1, x2, y2, x_orig, y_orig
    # glBegin(GL_LINE_LOOP)
    # glVertex2f(x0, y0)
    # glVertex2f(x1, y1)
    # glVertex2f(x2, y2)
    glBegin(GL_POINTS)
    P_0 = line_algorithm([x0, y0], [x1, y1])
    P_1 = line_algorithm([x1, y1], [x2, y2])
    P_2 = line_algorithm([x2, y2], [x0, y0])
    P_0.dda_line()
    P_1.dda_line()
    P_2.dda_line()

    a = P_0.return_length()
    b = P_1.return_length()
    c = P_2.return_length()
    s = (a + b + c) / 2
    mid_a = P_0.return_midpoint()
    mid_b = P_1.return_midpoint()
    mid_c = P_2.return_midpoint()

    m_a = P_0.return_slope()
    a_x, a_y = mid_a
    # declare and initailize x_center and y_center for the center of the circumcircle
    x_center, y_center = 0, 0

    if (m_a == 0 or m_a == 10000):
        if m_a == 0:

            # line is x = mid_a
            normal_a = line_algorithm(mid_a, [a_x, a_y + 100])
            x_center = a_x
            a_normal_m = 'Inf'
            # normal_a_n = line_algorithm(mid_a, [a_x, a_y - 100])
        elif m_a == 10000:
            # line is y = mid_a
            y_center = a_y
            a_normal_m = 'Zero'
            normal_a = line_algorithm(mid_a, [a_x + 100, a_y])
            # normal_a_n = line_algorithm(mid_a, [a_x - 100, a_y])

    else:
        # line is y = f(x)
        # y = -1 / ma * x + const_a
        a_normal_m = -1 / m_a
        const_a = a_y - a_normal_m * a_x
        normal_a = line_algorithm(mid_a, [0, const_a])

    m_b = P_1.return_slope()
    b_x, b_y = mid_b

    if (m_b == 0 or m_b == 10000):
        if m_b == 0:

            # line is x = mid_a
            b_normal_m = 'Inf'
            normal_b = line_algorithm(mid_b, [b_x, b_y + 100])
            x_center = b_x
        elif m_b == 10000:
            b_normal_m = 'Zero'
            normal_b = line_algorithm(mid_b, [b_x + 100, b_y])
            y_center = b_y

    else:
        b_normal_m = -1 / m_b
        const_b = b_y - b_normal_m * b_x
        normal_b = line_algorithm(mid_b, [0, const_b])

    m_c = P_2.return_slope()
    c_x, c_y = mid_c

    if (m_c == 0 or m_c == 10000):
        if m_c == 0:

            # line is x = mid_a
            normal_c = line_algorithm(mid_c, [c_x, c_y + 100])
        elif m_c == 10000:
            normal_c = line_algorithm(mid_c, [c_x + 100, c_y])

    else:
        c_normal_m = -1 / m_c
        const_c = c_y - c_normal_m * c_x
        normal_c = line_algorithm(mid_c, [0, const_c])

    glColor3f(1.0, 0.0, 1.0)
    # normal_a.dda_line()
    # normal_b.dda_line()
    # normal_c.dda_line()
    # take the intersection of mid_a and mid_b and find the coordinates for the point
    # that will serve as the center for the circumcircle
    if isinstance(a_normal_m, float) and isinstance(b_normal_m, float):
        # do x_center and y_center calculation
        x_center = (const_b - const_a) / (a_normal_m - b_normal_m)
        y_center = a_normal_m * x_center + const_a

    if (a_normal_m == 'Inf' or a_normal_m == 'Zero') and isinstance(
            b_normal_m, float):
        # calculate x_c and y_cs
        if a_normal_m == 'Inf':
            x_center = a_x
            y_center = b_normal_m * x_center + const_b
        elif a_normal_m == 'Zero':
            y_center = a_y
            x_center = 1 / b_normal_m * (y_center - const_b)
    elif (b_normal_m == 'Inf' or b_normal_m == 'Zero') and isinstance(
            a_normal_m, float):
        # calculate x_c and y_cs
        if b_normal_m == 'Inf':
            x_center = b_x
            y_center = a_normal_m * x_center + const_a
        elif b_normal_m == 'Zero':
            y_center = b_y
            x_center = 1 / a_normal_m * (y_center - const_a)

    print("Central Coordinates for the circumcircle are ", x_center, " ",
          y_center)

    # m_b = P_1.return_slope()
    # m_c = P_2.return_slope()

    # b_normal_m = -1 / m_b
    # c_normal_m = -1 / m_c

    # # for line a
    # # y = m_new * x + c
    centre = [x_center, y_center]
    glEnd()
    glFlush()

    x_orig, y_orig = x_center, y_center
コード例 #8
0
def plot_points():
    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(1.0, 0.0, 0.0)
    glBegin(GL_POINTS)
    # draw the triangle first
    P_0 = line_algorithm([0, 0], [100, 100])
    P_1 = line_algorithm([100, 100], [200, 0])
    P_2 = line_algorithm([0, 0], [200, 0])
    draw_triangle(P_0, P_1, P_2)
    a = P_0.return_length()
    b = P_1.return_length()
    c = P_2.return_length()
    s = (a + b + c) / 2
    mid_a = P_0.return_midpoint()
    mid_b = P_1.return_midpoint()
    mid_c = P_2.return_midpoint()

    m_a = P_0.return_slope()
    a_x, a_y = mid_a
    # declare and initailize x_center and y_center for the center of the circumcircle
    x_center, y_center = 0, 0

    if (m_a == 0 or m_a == 10000):
        if m_a == 0:

            # line is x = mid_a
            normal_a = line_algorithm(mid_a, [a_x, a_y + 100])
            x_center = a_x
            a_normal_m = 'Inf'
            # normal_a_n = line_algorithm(mid_a, [a_x, a_y - 100])
        elif m_a == 10000:
            # line is y = mid_a
            y_center = a_y
            a_normal_m = 'Zero'
            normal_a = line_algorithm(mid_a, [a_x + 100, a_y])
            # normal_a_n = line_algorithm(mid_a, [a_x - 100, a_y])

    else:
        # line is y = f(x)
        # y = -1 / ma * x + const_a
        a_normal_m = -1 / m_a
        const_a = a_y - a_normal_m * a_x
        normal_a = line_algorithm(mid_a, [0, const_a])

    m_b = P_1.return_slope()
    b_x, b_y = mid_b

    if (m_b == 0 or m_b == 10000):
        if m_b == 0:

            # line is x = mid_a
            b_normal_m = 'Inf'
            normal_b = line_algorithm(mid_b, [b_x, b_y + 100])
            x_center = b_x
        elif m_b == 10000:
            b_normal_m = 'Zero'
            normal_b = line_algorithm(mid_b, [b_x + 100, b_y])
            y_center = b_y

    else:
        b_normal_m = -1 / m_b
        const_b = b_y - b_normal_m * b_x
        normal_b = line_algorithm(mid_b, [0, const_b])

    m_c = P_2.return_slope()
    c_x, c_y = mid_c

    if (m_c == 0 or m_c == 10000):
        if m_c == 0:

            # line is x = mid_a
            normal_c = line_algorithm(mid_c, [c_x, c_y + 100])
        elif m_c == 10000:
            normal_c = line_algorithm(mid_c, [c_x + 100, c_y])

    else:
        c_normal_m = -1 / m_c
        const_c = c_y - c_normal_m * c_x
        normal_c = line_algorithm(mid_c, [0, const_c])

    glColor3f(1.0, 0.0, 1.0)
    # normal_a.dda_line()
    # normal_b.dda_line()
    # normal_c.dda_line()
    # take the intersection of mid_a and mid_b and find the coordinates for the point
    # that will serve as the center for the circumcircle
    if isinstance(a_normal_m, float) and isinstance(b_normal_m, float):
        # do x_center and y_center calculation
        x_center = (const_b - const_a) / (a_normal_m - b_normal_m)
        y_center = a_normal_m * x_center + const_a

    if (a_normal_m == 'Inf' or a_normal_m == 'Zero') and isinstance(
            b_normal_m, float):
        # calculate x_c and y_cs
        if a_normal_m == 'Inf':
            x_center = a_x
            y_center = b_normal_m * x_center + const_b
        elif a_normal_m == 'Zero':
            y_center = a_y
            x_center = 1 / b_normal_m * (y_center - const_b)
    elif (b_normal_m == 'Inf' or b_normal_m == 'Zero') and isinstance(
            a_normal_m, float):
        # calculate x_c and y_cs
        if b_normal_m == 'Inf':
            x_center = b_x
            y_center = a_normal_m * x_center + const_a
        elif b_normal_m == 'Zero':
            y_center = b_y
            x_center = 1 / a_normal_m * (y_center - const_a)

    print("Central Coordinates for the circumcircle are ", x_center, " ",
          y_center)

    # m_b = P_1.return_slope()
    # m_c = P_2.return_slope()

    # b_normal_m = -1 / m_b
    # c_normal_m = -1 / m_c

    # # for line a
    # # y = m_new * x + c
    centre = [x_center, y_center]
    radius = (a * b * c) / (math.sqrt(
        (a + b + c) * (b + c - a) * (c + a - b) * (a + b - c)))
    circumcircle = circle_algorithms(radius, x_center, y_center)
    glColor3f(0.0, 1.0, 1.0)
    circumcircle.parameteric_circle()

    # # for line b
    # # y = m_new * x + c
    # b_x, b_y = mid_b
    # const_b = b_y - b_normal_m * b_x
    # normal_b = line_algorithm(mid_b, [0, const_b])

    # # for line x
    # # y = m_new * x + c
    # c_x, c_y = mid_c
    # const_c = c_y - c_normal_m * c_x
    # normal_c = line_algorithm(mid_c, [0, const_c])

    glEnd()
    glFlush()
コード例 #9
0
def draw_polygon(Points):
    for index, point in enumerate(Points[:-1]):
        line = line_algorithm(point, Points[index + 1])
        line.dda_line()
    line = line_algorithm(Points[0], Points[-1])
    line.dda_line()
コード例 #10
0
def square(x,y):
    line = line_algorithm([x - 20, y - 20], [x + 20, y + 20])
    line.dda_line()