1.
Develop a program to draw a line using Bresenham’s line drawing technique
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def LineBres(X1, Y1, X2, Y2):
    glClear(GL_COLOR_BUFFER_BIT)
    dx, dy = abs(X2 - X1), abs(Y2 - Y1)
    p, twoDy, twoDyDx = 2 * dy - dx, 2 * dy, 2 * (dy - dx)
    x, y = (X2, Y2) if X1 > X2 else (X1, Y1)
    X2 = max(X1, X2)
    glBegin(GL_POINTS)
    glVertex2i(x, y)
    while x < X2:
        x, p = x + 1, p + (twoDy if p < 0 else twoDyDx)
        if p >= 0: y += 1
        glVertex2i(x, y)
    glEnd()
    glFlush()
def Init():
    glClearColor(1.0, 1.0, 1.0, 0)
    glColor3f(0.0, 0.0, 0.0)
    glPointSize(4.0)
    glViewport(0, 0, 300, 400)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0, 300, 0, 400)
def main():
    pygame.init()
    pygame.display.set_mode((700, 700), DOUBLEBUF | OPENGL)
    Init()
    X1, Y1 = map(int, input("Enter X1, Y1: ").split())
    X2, Y2 = map(int, input("Enter X2, Y2: ").split())
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
         LineBres(X1, Y1, X2, Y2)
         pygame.display.flip()
         pygame.time.wait(100)
    pygame.quit()
if __name__ == "__main__":
    main()
Output
2.Develop a program to demonstrate basic geometric operations on the 2D object
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
# Initial transformation values
tx, ty = 0, 0 # Translation
def init():
    glClearColor(0.0, 0.0, 0.0, 1.0)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0)
    glMatrixMode(GL_MODELVIEW)
def draw_square():
    glBegin(GL_QUADS)
    glColor3f(1.0, 0.0, 0.0)
    glVertex2f(-1.0, -1.0)
    glVertex2f(1.0, -1.0)
    glVertex2f(1.0, 1.0)
    glVertex2f(-1.0, 1.0)
    glEnd()
def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(tx, ty, 0)
    draw_square()
    pygame.display.flip()
def main():
    global tx, ty
    pygame.init()
    screen = pygame.display.set_mode((500, 500), DOUBLEBUF | OPENGL)
    pygame.display.set_caption("2D Geometric Operations")
    init()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == K_w:                 # Move up
                    ty += 2
                elif event.key == K_s:               # Move down
                     ty -= 1
                 elif event.key == K_a:   # Move left
                     tx -= 1
                 elif event.key == K_d:   # Move right
                     tx += 1
          display()
    pygame.quit()
if __name__ == "__main__":
    main()
Output:
3.Develop a program to demonstrate basic geometric operations on the 3D object
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
# Vertices of a cube
vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
)
# Edges   of a cube
edges =   (
    (0,   1),
    (0,   3),
    (0,   4),
    (2,   1),
    (2,   3),
    (2,   7),
    (6,   3),
    (6,   4),
    (6,   7),
    (5,   1),
    (5,   4),
    (5,   7)
)
def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()
def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
         glRotatef(1,1,1,1)
         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
         Cube()
         pygame.display.flip()
         pygame.time.wait(10)
if __name__ == "__main__":
    main()
Output
4.Develop a program to demonstrate 2D transformation on basic objects
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
# Initial transformation values
tx, ty = 0, 0 # Translation
angle = 0      # Rotation angle
scale = 1      # Scale factor
def init():
    glClearColor(0.0, 0.0, 0.0, 1.0)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(-10.0, 10.0, -10.0, 10.0)
    glMatrixMode(GL_MODELVIEW)
def draw_square():
    glBegin(GL_QUADS)
    glColor3f(1.0, 0.0, 0.0)
    glVertex2f(-1.0, -1.0)
    glVertex2f(1.0, -1.0)
    glVertex2f(1.0, 1.0)
    glVertex2f(-1.0, 1.0)
    glEnd()
def display():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(tx, ty, 0)
    glRotatef(angle, 0, 0, 1)
    glScalef(scale, scale, 1)
    draw_square()
    pygame.display.flip()
def main():
    global tx, ty, angle, scale
    pygame.init()
    screen = pygame.display.set_mode((500, 500), DOUBLEBUF | OPENGL)
    pygame.display.set_caption("2D Geometric Operations")
    init()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == K_w:       #   Move up
                    ty += 2
                elif event.key == K_s:     #   Move down
                    ty -= 1
                elif event.key == K_a:     #   Move left
                    tx -= 1
                elif event.key == K_d:     #   Move right
                    tx += 1
                elif event.key == K_r:     #   Rotate clockwise
                    angle -= 45
                elif event.key == K_e:     #   Rotate counterclockwise
                    angle += 45
                elif event.key == K_EQUALS:    # Scale up
                    scale += 0.1
                elif event.key == K_MINUS:     # Scale down
                    scale -= 0.1
         display()
    pygame.quit()
if __name__ == "__main__":
    main()
Output
5.Develop a program to demonstrate 3D transformation on 3D objects
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
# Initial transformation values
tx, ty, tz = 0, 0, -5 # Translation
angle_x, angle_y, angle_z = 0, 0, 0 # Rotation angles
scale = 1 # Scale factor
def init():
    glClearColor(0.0, 0.0, 0.0, 1.0)
    glEnable(GL_DEPTH_TEST)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1.0, 0.1, 50.0)
    glMatrixMode(GL_MODELVIEW)
def draw_cube():
    vertices = [
        [-1, -1, -1],
        [1, -1, -1],
        [1, 1, -1],
        [-1, 1, -1],
        [-1, -1, 1],
        [1, -1, 1],
        [1, 1, 1],
        [-1, 1, 1]
    ]
    edges =   [
        [0,   1],
        [1,   2],
        [2,   3],
        [3,   0],
        [4,   5],
        [5,   6],
        [6,   7],
        [7,   4],
        [0,   4],
        [1,   5],
        [2,   6],
        [3,   7]
    ]
    glBegin(GL_LINES)
   for edge in edges:
       for vertex in edge:
           glVertex3fv(vertices[vertex])
   glEnd()
def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
   glLoadIdentity()
   glTranslatef(tx, ty, tz)
   glRotatef(angle_x, 1, 0, 0)
   glRotatef(angle_y, 0, 1, 0)
   glRotatef(angle_z, 0, 0, 1)
   glScalef(scale, scale, scale)
   draw_cube()
   pygame.display.flip()
def main():
    global tx, ty, tz, angle_x, angle_y, angle_z, scale
   pygame.init()
   screen = pygame.display.set_mode((800, 600), DOUBLEBUF | OPENGL)
   pygame.display.set_caption("3D Transformations")
   init()
   running = True
   while running:
       for event in pygame.event.get():
           if event.type == pygame.QUIT:
               running = False
           elif event.type == KEYDOWN:
               if event.key == K_w: # Move up
                   ty += 0.1
               elif event.key == K_s: # Move down
                   ty -= 0.1
               elif event.key == K_a: # Move left
                   tx -= 0.1
               elif event.key == K_d: # Move right
                   tx += 0.1
               elif event.key == K_q: # Move forward
                   tz += 0.1
               elif event.key == K_e: # Move backward
                   tz -= 0.1
               elif event.key == K_i: # Rotate up
                   angle_x += 5
               elif event.key == K_k: # Rotate down
                    angle_x -= 5
                elif event.key ==   K_j:   # Rotate left
                    angle_y -= 5
                elif event.key ==   K_l:   # Rotate right
                    angle_y += 5
                elif event.key ==   K_u:   # Rotate clockwise
                    angle_z += 5
                elif event.key ==   K_o:   # Rotate counterclockwise
                    angle_z -= 5
                elif event.key ==   K_EQUALS:   # Scale up
                    scale += 0.1
                elif event.key ==   K_MINUS:    # Scale down
                    scale -= 0.1
         display()
    pygame.quit()
if __name__ == "__main__":
    main()
Output
6. Develop a program to demonstrate Animation effects on simple objects.
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up the display
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Moving Ball")
# Define colors
RED = (255, 0, 0)
# Ball properties
ball_radius = 20
ball_x = random.randint(ball_radius, screen_width - ball_radius)
ball_y = random.randint(ball_radius, screen_height - ball_radius)
speed_x = random.randint(-5, 5)
speed_y = random.randint(-5, 5)
# Main loop
running = True
clock = pygame.time.Clock()
while running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # Move the ball
    ball_x += speed_x
    ball_y += speed_y
    # Bounce off the edges
    if ball_x - ball_radius < 0 or ball_x + ball_radius > screen_width:
        speed_x = -speed_x
    if ball_y - ball_radius < 0 or ball_y + ball_radius > screen_height:
        speed_y = -speed_y
    # Clear the screen
    screen.fill((0, 0, 0))
    # Draw the ball
    pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)
    # Update the display
    pygame.display.flip()
    clock.tick(150) # Limit the frame rate to 60 FPS
# Quit Pygame
pygame.quit()
Output