-1

I am using Mid-point Line Drawing Algorithm to draw a shape in Opengl on Pycharm IDE. I have used the functions find_zone() and draw_line() to do that. I have used another function called transform() that will shift the shape by 200 units right and upward. But my aim is to execute the transform() function after taking an input from the user. If the user enters 'up' only then the transform() function will shift the shape.

This is what I have done to achieve that, but when I run the code, my program throws an error.

This is my code:

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import math
import numpy as np

# method to find the zone of the line
def find_zone(x1,y1, x2,y2):
    dx = x2 - x1
    dy = y2 - y1

    if abs(dx) >= abs(dy) and (dx >= 0 and dy >= 0):
        return 0
    elif abs(dy) > abs(dx) and (dx >= 0 and dy >= 0):
        return 1
    elif abs(dy) > abs(dx) and (dx <= 0 and dy >= 0):
        return 2
    elif abs(dx) > abs(dy) and (dx <= 0 and dy >= 0):
        return 3
    elif abs(dx) > abs(dy) and (dx <= 0 and dy <= 0):
        return 4
    elif abs(dy) > abs(dx) and (dx <= 0 and dy <= 0):
        return 5
    elif abs(dy) > abs(dx) and (dx >= 0 and dy <= 0):
        return 6
    elif abs(dx) > abs(dy) and (dx >= 0 and dy <= 0):
        return 7



def draw_line(x1,y1, x2,y2):
    #glLineWidth()
    glBegin(GL_LINES)
    glVertex2f(x1,y1)
    glVertex2f(x2,y2)

    # swap operation to flip the start and end points
    if x1 >= x2 and y1 >= y2:
        temp1, temp2 = x1, y1
        x1, y1 = x2, y2
        x2, y2 = temp1, temp2

    # converting to zone 0 from another zone
    zone = find_zone(x1,y1, x2, y2)
    if zone == 1:
        x1, y1 = y1, x1
        x2, y2 = y2, x2

    elif zone == 2:
        x1, y1 = y1, -x1
        x2, y2 = y2, -x2

    elif zone == 3:
        x1, y1 = -x1, y1
        x2, y2 = -x2, y2

    elif zone == 4:
        x1, y1 = -x1, -y1
        x2, y2 = -x2, -y2

    elif zone == 5:
        x1, y1 = -y1, -x1
        x2, y2 = -y2, -x2

    elif zone == 6:
        x1, y1 = -y1, x1
        x2, y2 = -y2, x2

    elif zone == 7:
        x1, y1 = x1, -y1
        x2, y2 = x2, -y2
    else:
        x1, y1 = x1, y1
        x2, y2 = x2, y2

    # mid-point algorithm for zone 0
    dx = x2 - x1
    dy = y2 - y1
    d = 2 * dy - dx
    dNE = 2 * dy - 2 * dx
    dE = 2 * dy

    x = x1
    y = y1

    while x <= x2 and y <= y2:
        if d > 0:
            d = d + dNE
            x = x + 1
            y = y + 1
        elif d < 0:
            d = d + dE
            x = x + 1

        # converting back to original zone from zone 0
        if zone == 1:
            x1, y1 = y1, x1
            x2, y2 = y2, x2

        elif zone == 2:
            x1, y1 = -y1, x1
            x2, y2 = -y2, x2

        elif zone == 3:
            x1, y1 = -x1, y1
            x2, y2 = -x2, y2

        elif zone == 4:
            x1, y1 = -x1, -y1
            x2, y2 = -x2, -y2

        elif zone == 5:
            x1, y1 = -y1, -x1
            x2, y2 = -y2, -x2

        elif zone == 6:
            x1, y1 = y1, -x1
            x2, y2 = y2, -x2

        elif zone == 7:
            x1, y1 = x1, -y1
            x2, y2 = x2, -y2
        else:
            x1, y1 = x1, y1
            x2, y2 = x2, y2

    glEnd()

def transform():
    v1 = np.array([[70],
                  [235],
                  [1]])
    v2 = np.array([[255],
                  [215],
                   [1]])
    v3 = np.array([[70],
                   [160],
                   [1]])
    v4 = np.array([[140],
                    [235],
                    [1]])
    v5 = np.array([[140],
                    [280],
                    [1]])
    v6 = np.array([[180],
                    [235],
                    [1]])

    t = 200
    trans = np.array([[1, 0, t],
                      [0, 1, t],
                      [0, 0, 1]])

    v11 = np.matmul(trans, v1)
    v22 = np.matmul(trans, v2)
    v33 = np.matmul(trans, v3)
    v44 = np.matmul(trans, v4)
    v55 = np.matmul(trans, v5)
    v66 = np.matmul(trans, v6)

    glBegin(GL_LINES)
    glColor3f(1.0, 1.0, 1.0)

    glVertex2f(v11[0][0], v11[1][0])
    glVertex2f(v22[0][0], v22[1][0])
    glVertex2f(v22[0][0], v22[1][0])
    glVertex2f(v33[0][0], v33[1][0])
    glVertex2f(v11[0][0], v11[1][0])
    glVertex2f(v33[0][0], v33[1][0])

    glVertex2f(v44[0][0], v44[1][0])
    glVertex2f(v55[0][0], v55[1][0])
    glVertex2f(v55[0][0], v55[1][0])
    glVertex2f(v66[0][0], v66[1][0])


    glEnd()


def iterate():
    glViewport(0, 0, 500, 500)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()


def showScreen():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()

    glLineWidth(3)
    glColor3f(1.0, 1.0, 1.0)
    # draw aeroplane
    find_zone(70,235, 70,160)
    draw_line(70,235, 70,160)

    find_zone(70,160, 255, 215)
    draw_line(70,160, 255, 215)

    find_zone(255, 215, 70,235)
    draw_line(255, 215, 70,235)

    find_zone(140,235, 140,280)
    draw_line(140,235, 140,280)

    find_zone(140, 280, 180, 235)
    draw_line(140, 280, 180, 235)

    find_zone(140,180, 165,135)
    draw_line(140,180, 165,135)

    find_zone(165, 135, 165, 180)
    draw_line(165, 135, 165, 180)

    user = input()
    if user == 'up':
        transform()
    else:
        pass

    glutSwapBuffers()




glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(500, 500) #window size
glutInitWindowPosition(0, 0)
wind = glutCreateWindow(b"OpenGL Coding Practice") #window name
glutDisplayFunc(showScreen)


glutMainLoop()
  • you have to be more precise. If you can narrow down the question it will help receive an answer. – Bakudan Aug 18 '23 at 07:51

0 Answers0