Computer Graphics Group Assignment 2
Computer Graphics Group Assignment 2
// The constructor
Line() {
// Create a graphics frame
JFrame frame = new JFrame("Clipping");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// The constructor
Poly() {
// Create a graphics frame
JFrame frame = new JFrame("Clipping");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
public Main() {
super("3D Transformation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setVisible(true);
maxx = getWidth();
maxy = getHeight();
midx = maxx / 2;
midy = maxy / 2;
g = getGraphics();
}
public void bar3d(int x1, int y1, int x2, int y2, int z, int c) {
g.setColor(new Color(c, c, c));
int[] x = {x1, x2, x2, x1, x1};
int[] y = {y1, y1, y2, y2, y1};
g.fillPolygon(x, y, 5);
int[] x3 = {x1, x1 + z, x2 + z, x2, x1};
int[] y3 = {y1, y1 - z, y1 - z, y1, y1};
g.fillPolygon(x3, y3, 5);
int[] x4 = {x2, x2 + z, x2 + z, x2, x2};
int[] y4 = {y1, y1 - z, y2 - z, y2, y1};
g.fillPolygon(x4, y4, 5);
}
# Translation
tx = float(input("Enter the translation factor tx: "))
ty = float(input("Enter the translation factor ty: "))
# Rotation
ra = float(input("Enter the radian value ra: "))
# Calculate theta value
theta = (float)(3.14*ra)/180
# Calculate the x,y,p,q values
x = x*math.cos(theta) - y*math.sin(theta)
y = x*math.sin(theta) + y*math.cos(theta)
p = p*math.cos(theta) - q*math.sin(theta)
q = p*math.sin(theta) + q*math.cos(theta)
# Scaling
sx = float(input("Enter the scaled factor sx: "))
sy = float(input("Enter the scaled factor sy: "))
# Transform the coordinates
x = x*sx
y = y*sy
az = az*sx
w = w*sy
p = p*sx
q = q*sy
# Reflection
# Choose the axis of reflection
axis = input("Enter the axis of reflection (x, y or both): ")
if axis == "x":
# Along x-axis
theta = (float)(90*(3.14/180))
elif axis == "y":
# Along y-axis
theta = (float)(270*(3.14/180))
elif axis == "both":
# Along both-axis
theta = (float)(180*(3.14/180))
else:
# Invalid input
print("Invalid axis")
exit()
# Calculate the x,y,p,q values
x = x*math.cos(theta) - y*math.sin(theta)
y = x*math.sin(theta) + y*math.cos(theta)
p = p*math.cos(theta) - q*math.sin(theta)
q = p*math.sin(theta) + q*math.cos(theta)
# Shearing
# Choose the direction of shearing
direction = input("Enter the direction of shearing (x or y): ")
if direction == "x":
# Along x-direction
xls = float(input("Enter the shearing factor xls: "))
# Transform the coordinates
y=y
x = x + (xls*y)
w=w
az = az + (xls*w)
q=q
p = p + (xls*q)
elif direction == "y":
# Along y-direction
yls = float(input("Enter the shearing factor yls: "))
# Transform the coordinates
x=x
y = y + (yls*x)
az = az
w = w + (yls*az)
p=p
q = q + (yls*p)
else:
# Invalid input
print("Invalid direction")
exit()
# Main loop
while True:
# Handle the events
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Update the display only once per frame
pygame.display.flip()
pygame.time.wait(10)
For the above output we used the values below
Enter x coordinate: 1
Enter y coordinate: 1
Enter z coordinate: 1
Enter w coordinate: 1
Enter p coordinate: 1
Enter q coordinate: 1
Enter the translation factor tx: -2
Enter the translation factor ty: -2
Enter the radian value ra: -30
Enter the scaled factor sx: 0.5
Enter the scaled factor sy: 0.5
Enter the axis of reflection (x, y or both): y
Enter the direction of shearing (x or y): x
Enter the shearing factor xls: 0.3
9 3D PROJECTIONS
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import math
for i in range(s):
x = radius * math.cos(math.radians(i * angle))
y = radius * math.sin(math.radians(i * angle))
points.append((x, y, 0))
colors = (
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(1, 1, 0),
(1, 0, 1),
(0, 1, 1),
)
edges = []
for i in range(s):
edges.append((i, (i + 1) % s))
edges.append((i, i + s))
edges.append((i + s, (i + 1) % s + s))
faces = []
for i in range(s):
faces.append((i, (i + 1) % s, (i + 1) % s + s, i + s))
faces.append(tuple(range(s)))
faces.append(tuple(range(s, 2 * s)))
def draw3d():
glBegin(GL_QUADS)
for i, face in enumerate(faces):
color = colors[i % len(colors)]
glColor3fv(color)
for vertex in face:
glVertex3fv(points[vertex])
glEnd()
glBegin(GL_LINES)
glColor3fv((0, 0, 0))
for edge in edges:
for vertex in edge:
glVertex3fv(points[vertex])
glEnd()
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw3d()
pygame.display.flip()
pygame.time.wait(10)
For the above output we used the values below
Enter the number of sides: 4
Enter the depth value: 1