Computer Graphics Lab Report
Computer Graphics Lab Report
DAMAK-9, JHAPA
TRIBHUWAN UNIVERSITY
2023
4|Page
LIST OF EXPERIMENTS
4 IMPLEMENTATION OF 2D TRANSFORMATION
7 IMPLEMENTATION OF 3D TRANSFORMATION
9 IMPLEMENTATION OF 3D PROJECTIONS
5|Page
Ex .No - 1 DDA LINE DRAWING ALGORITHM
Question:
ALGORITHM:
Step 4:To determine the slope along find dy=y2-y1 ,dx=x2-x1 and slope =dy/dx.
Step 6:Find the new intermediate points using xᵢ=dx/step and yᵢ=dy/step.
Step 7:Plot pixel using (xᵢ,yᵢ) and repeat this process by incrementing the points (xᵢ,yᵢ) at equal
slope intervals until the end points of the line to be drawn is encountered.
PROGRAM
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int dx = x2 - x1;
int dy = y2 - y1;
int steps = Math.max(Math.abs(dx), Math.abs(dy));
6|Page
float x = x1;
float y = y1;
Output:
7|Page
Ex No.2 BRESENHAM LINE DRAWING ALGORITHM
AIM:
ALGORITHM:
Step 3:Accept the input at starting and endpoint of the line to be drawn.
Step 4: Load (xᵢ,yᵢ) into the frame buffer to be plotted as a first point.
Step 5:Calculate the constants ∆x,∆y,2∆y, 2∆y -2∆x and obtain the first decision parameter
P˳=2∆y -2∆x.
8|Page
Step 6:At each xk along the line starting at k=0 perform the following test
If pk<0,the next point is (xk+1,yK) and pk+1=pk+2∆y,otherwise next point to the plotted
is(xk+1,yk+1),
Step 7 :Repeat the process until the end point of the line to be drawn is encountered.
PROGRAM
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class BresenhamLineDrawing extends JPanel {
private int x1, y1, x2, y2;
public BresenhamLineDrawing(int x1, int y1, int x2, int y2)
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx - dy;
int err2;
int x = x1;
int y = y1;
while (true) {
g.drawRect(x, y, 1, 1);
if (x == x2 && y == y2) break;
err2 = 2 * err;
if (err2 > -dy) {
err -= dy;
x += sx;
}
if (err2 < dx) {
err += dx;
y += sy;
}
}
}
9|Page
public static void main(String[] args) {
int x1 = 50, y1 = 50, x2 = 300, y2 = 200;
JFrame frame = new JFrame("Bresenham Line Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
BresenhamLineDrawing panel = new BresenhamLineDrawing(x1, y1, x2, y2);
frame.add(panel);
frame.setVisible(true);
}
}
Output:
10 | P a g e
Ex No.3 BRESENHAM CIRCLE DRAWING ALGORITHM
AIM:
ALGORITHM:
Step 4: Obtain the first point to be plotted on th circumference of the circle centered on the
origin(o,r).
Step 6: At each xk along the line starting at k=0 perform the following test
If pk<0,the next point is (xk+1,yK) and pk+1=pk+2xk+1+1,otherwise next point to the plotted is
Step 7 : Repeat the above steps until x>=y for this circle drawing one octant is generated and
copied for remaining octants.
PROGRAM
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class BresenhamCircleDrawing extends JPanel {
private int xCenter, yCenter, radius;
public BresenhamCircleDrawing(int xCenter, int yCenter, int radius) {
this.xCenter = xCenter;
this.yCenter = yCenter;
this.radius = radius;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int y = radius;
int d = 3 - 2 * radius;
while (y >= x) {
drawSymmetricPoints(g, x, y);
11 | P a g e
if (d <= 0) {
d = d + 4 * x + 6;
} else {
d = d + 4 * (x - y) + 10;
y--;
}
x++;
}
}
private void drawSymmetricPoints(Graphics g, int x, int y) {
g.drawRect(xCenter + x, yCenter + y, 1, 1);
g.drawRect(xCenter - x, yCenter + y, 1, 1);
g.drawRect(xCenter + x, yCenter - y, 1, 1);
g.drawRect(xCenter - x, yCenter - y, 1, 1);
g.drawRect(xCenter + y, yCenter + x, 1, 1);
g.drawRect(xCenter - y, yCenter + x, 1, 1);
g.drawRect(xCenter + y, yCenter - x, 1, 1);
g.drawRect(xCenter - y, yCenter - x, 1, 1);
}
public static void main(String[] args) {
int xCenter = 200, yCenter = 200, radius = 100;
JFrame frame = new JFrame("Bresenham Circle Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
BresenhamCircleDrawing panel = new BresenhamCircleDrawing(xCenter, yCenter, radius);
frame.add(panel);
frame.setVisible(true);
}
}
Output :
12 | P a g e
Ex No.4 IMPLEMENTATION OF 2D TRANSFORMATION
AIM:
ALGORITHM:
Step 4: Translation
b) Add the translation vector to corresponding co-ordinates of the 2D object to obtain the
transaltion co-ordinates.
Step 5: Scaling
b) Multiply the scaling factor to the corresponding co-ordinates of the 2D object to obtain
the scaling co ordinates.
a) Θ=(float)(3.14xra)/180.
b) X1=x2+abs(x1-x2)cosθ-abs(y1-y2)sinθ
c) Y1=y2+abs(x1-x2)sinθ+abs(y1-y2)cosθ
d) X3=x2+abs(x3-x1)cosθ-abs(y3-y1)sinθ
e) Y3=y2+abs(x3-x1)sinθ+abs(y3-y1)cosθ
Step 7 : Shearing
a) Along x-direction
x=x1+sx*y1, y=y1
13 | P a g e
b) Along y-direction
x=x1, y=y1+sy*x1
Step 8: Reflection
a) Along x-axis
theta=(90*ra)/180
b) Along y-axis
theta=(270*ra)/180
c) Along z-axis
theta=(180*ra)/180
PROGRAM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Transformations2D() {
setPreferredSize(new Dimension(400, 400));
14 | P a g e
shearButton = new JButton("Shear");
translateButton.addActionListener(this);
rotateButton.addActionListener(this);
scaleButton.addActionListener(this);
reflectButton.addActionListener(this);
shearButton.addActionListener(this);
add(translateButton);
add(rotateButton);
add(scaleButton);
add(reflectButton);
add(shearButton);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawPolygon(x, y, numPoints);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == translateButton) {
double tx = 30; // Translate by 30 units in x-direction
double ty = 30; // Translate by 30 units in y-direction
transformationMatrix = new double[][]{{1, 0, tx}, {0, 1, ty}, {0, 0, 1}};
} else if (e.getSource() == rotateButton) {
double angle = Math.toRadians(45); // Rotate by 45 degrees
transformationMatrix = new double[][]{{Math.cos(angle), -Math.sin(angle), 0},
{Math.sin(angle), Math.cos(angle), 0},
{0, 0, 1}};
} else if (e.getSource() == scaleButton) {
double sx = 1.5; // Scale by a factor of 1.5 in x-direction
double sy = 1.5; // Scale by a factor of 1.5 in y-direction
transformationMatrix = new double[][]{{sx, 0, 0}, {0, sy, 0}, {0, 0, 1}};
15 | P a g e
} else if (e.getSource() == reflectButton) {
transformationMatrix = new double[][]{{-1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
} else if (e.getSource() == shearButton) {
double shx = 0.5; // Shear in x-direction by a factor of 0.5
transformationMatrix = new double[][]{{1, shx, 0}, {0, 1, 0}, {0, 0, 1}};
}
applyTransformation(transformationMatrix);
}
16 | P a g e
Ex No.5 LINE CLIPING USING COHEN-SUTHERLAND ALGORITHM
AIM:
To write a JAVA program to clip a line using the line clipping algorithm.
ALGORITHM:
Step 3: Declare the variables and draw a window and a line using rectangle an line function.
Step 4: Using the values of x1,y1,x2,y2.Check whether the line is within the rectangle
coordinates xmin,ymin,xmax,ymax and clip it.
PROGRAM
import java.awt.*;
import javax.swing.*;
public LineClipping(int x1, int y1, int x2, int y2, int xmin, int ymin, int xmax, int ymax) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.xmin = xmin;
this.ymin = ymin;
this.xmax = xmax;
this.ymax = ymax;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawRect(xmin, ymin, xmax - xmin, ymax - ymin);
17 | P a g e
int outCode1 = computeOutCode(x1, y1);
int outCode2 = computeOutCode(x2, y2);
while (true) {
if ((outCode1 & outCode2) != 0) {
break;
}
if ((outCode1 | outCode2) == 0) {
accept = true;
break;
}
int x, y;
int outCode = (outCode1 != 0) ? outCode1 : outCode2;
if ((outCode & 8) != 0) {
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
} else if ((outCode & 4) != 0) {
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
} else if ((outCode & 2) != 0) {
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
} else {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outCode == outCode1) {
x1 = x;
y1 = y;
outCode1 = computeOutCode(x1, y1);
} else {
x2 = x;
y2 = y;
outCode2 = computeOutCode(x2, y2);
}
}
if (accept) {
g.setColor(Color.RED);
g.drawLine(x1, y1, x2, y2);
}
}
18 | P a g e
private int computeOutCode(int x, int y) {
int code = 0;
if (x < xmin) code |= 1; // to the left of clip window
if (x > xmax) code |= 2; // to the right of clip window
if (y < ymin) code |= 4; // below clip window
if (y > ymax) code |= 8; // above clip window
return code;
}
LineClipping panel = new LineClipping(x1, y1, x2, y2, xmin, ymin, xmax, ymax);
frame.add(panel);
frame.setVisible(true);
}
}
Output :
19 | P a g e
Ex No. 6 POLYGON CLIPPING USING COHEN-SUTHERLAND ALGORITHM
AIM:
To write a JAVA program to clip a polygon using the line clipping algorithm.
ALGORITHM:
Step 3: Declare the variables and draw a window and a polygon shape of based on five lines
using window and line function.
Step 4: Using the clipping function,the values of the lines are checked whether the line is within
the rectangle coordinates xmin,ymin,xmax,ymax and clip it.
PROGRAM
public Polygon() {
vertices = new ArrayList<>();
}
20 | P a g e
public ClippingWindow(int xmin, int ymin, int xmax, int ymax) {
this.xmin = xmin;
this.ymin = ymin;
this.xmax = xmax;
this.ymax = ymax;
}
21 | P a g e
} else if (inside(E, edge)) {
outputVertices.add(computeIntersection(S, E, edge));
outputVertices.add(E);
}
S = E;
}
return outputVertices;
}
switch (edge) {
case 1: return x >= window.getXmin();
case 2: return y >= window.getYmin();
case 3: return x <= window.getXmax();
case 4: return y <= window.getYmax();
default: return false;
}
}
switch (edge) {
case 1: // Left Edge (x = xmin)
xi = window.getXmin();
yi = y1 + (x1 - xi) * (y2 - y1) / (x2 - x1);
break;
case 2: // Bottom Edge (y = ymin)
yi = window.getYmin();
xi = x1 + (y1 - yi) * (x2 - x1) / (y2 - y1);
break;
case 3: // Right Edge (x = xmax)
xi = window.getXmax();
yi = y1 + (x1 - xi) * (y2 - y1) / (x2 - x1);
break;
case 4: // Top Edge (y = ymax)
yi = window.getYmax();
xi = x1 + (y1 - yi) * (x2 - x1) / (y2 - y1);
break;
}
22 | P a g e
}
return clippedVertices;
}
}
public Main() {
polygon = new Polygon();
polygon.addVertex(50, 50);
polygon.addVertex(150, 50);
polygon.addVertex(100, 150);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
23 | P a g e
g.drawRect(window.getXmin(), window.getYmin(), window.getXmax() - window.getXmin(),
window.getYmax() - window.getYmin());
}
24 | P a g e
Ex No. 7 3D TRANSFORMATION
AIM:
ALGORITHM:
Step 3: Translation
Step 4: Scaling
Step 5: Rotation
PROGRAM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
25 | P a g e
private int[] x = new int[numVertices];
private int[] y = new int[numVertices];
private int[] z = new int[numVertices];
public ThreeDTransformations() {
setPreferredSize(new Dimension(400, 400));
translateButton.addActionListener(this);
scaleButton.addActionListener(this);
rotateButton.addActionListener(this);
add(translateButton);
add(scaleButton);
add(rotateButton);
System.arraycopy(originalX, 0, x, 0, numVertices);
System.arraycopy(originalY, 0, y, 0, numVertices);
System.arraycopy(originalZ, 0, z, 0, numVertices);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
26 | P a g e
}
System.arraycopy(tempX, 0, x, 0, numVertices);
System.arraycopy(tempY, 0, y, 0, numVertices);
System.arraycopy(tempZ, 0, z, 0, numVertices);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == translateButton) {
double tx = 50; // Translate by 50 units in x-direction
double ty = 50; // Translate by 50 units in y-direction
double tz = 50; // Translate by 50 units in z-direction
transformationMatrix = new double[][]{{1, 0, 0, tx}, {0, 1, 0, ty}, {0, 0, 1, tz}, {0, 0, 0, 1}};
} else if (e.getSource() == scaleButton) {
double sx = 1.5; // Scale by a factor of 1.5 in x-direction
double sy = 1.5; // Scale by a factor of 1.5 in y-direction
double sz = 1.5; // Scale by a factor of 1.5 in z-direction
transformationMatrix = new double[][]{{sx, 0, 0, 0}, {0, sy, 0, 0}, {0, 0, sz, 0}, {0, 0, 0, 1}};
27 | P a g e
} else if (e.getSource() == rotateButton) {
double angle = Math.toRadians(45); // Rotate by 45 degrees around z-axis
transformationMatrix = new double[][]{{Math.cos(angle), -Math.sin(angle), 0, 0},
{Math.sin(angle), Math.cos(angle), 0, 0},
{0, 0, 1, 0}, {0, 0, 0, 1}};
}
applyTransformation(transformationMatrix);
}
28 | P a g e
Ex No. 8 COMPOSITE 2D TRANSFORMATION
AIM:
ALGORITHM:
Step 3: Translation
Step 6: Rotation
Step 5: Scaling
Step 6: Reflection
a) Along x-axis
theta=(float)(90*(3.14/180));
b) Along y-axis
theta=(float)(270*(3.14/180));
c) Along both-axis
theta=(float)(180*(3.14/180));
Step 7 : Shearing
y=y, x=x+(xls*y),
w=w; az=az+(xls*w),
q=q,p=p+(xls*q).
b) Along y-direction
x=x, y=y+(yls*x),
az=az,w=w+(yls*az),
p=p,q=q+(yls*p)
PROGRAM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public Composite2DTransformations() {
setPreferredSize(new Dimension(400, 400));
add(transformButton);
System.arraycopy(originalX, 0, x, 0, numVertices);
System.arraycopy(originalY, 0, y, 0, numVertices);
30 | P a g e
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
// Draw the lines connecting corresponding vertices of the original and transformed squares
for (int i = 0; i < numVertices; i++) {
g.drawLine(x[i], y[i], x[i] + 200, y[i]);
}
}
System.arraycopy(tempX, 0, x, 0, numVertices);
System.arraycopy(tempY, 0, y, 0, numVertices);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
double[][] translationMatrix = {{1, 0, 50}, {0, 1, 50}, {0, 0, 1}};
double[][] rotationMatrix = {{Math.cos(Math.toRadians(45)), -Math.sin(Math.toRadians(45)), 0},
{Math.sin(Math.toRadians(45)), Math.cos(Math.toRadians(45)), 0},
{0, 0, 1}};
double[][] scalingMatrix = {{1.5, 0, 0}, {0, 1.5, 0}, {0, 0, 1}};
31 | P a g e
public static void main(String[] args) {
JFrame frame = new JFrame("Composite 2D Transformations");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
32 | P a g e
Ex No.9 3D PROJECTIONS
AIM:
ALGORITHM:
Step 3: Get the number of sides ‘s’ using the calculate the coordinate points.
Step 4: Get the depth value ‘d’ uing the print the 3d object by draw3d function.
Step 5: Print the top view,side view and bottom view of the 3d object.
PROGRAM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public ThreeDProjection() {
setPreferredSize(new Dimension(400, 400));
add(projectButton);
System.arraycopy(originalX, 0, x, 0, numVertices);
System.arraycopy(originalY, 0, y, 0, numVertices);
}
33 | P a g e
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
// Draw the lines connecting corresponding vertices of the original and projected cubes
for (int i = 0; i < 4; i++) {
int next = (i + 1) % 4;
int next2 = i + 4;
int next2Next = next2 + 1;
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
applyProjection();
}
34 | P a g e
public static void main(String[] args) {
JFrame frame = new JFrame("3D Projection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400);
35 | P a g e
Ex No.10 WINDOW TO VIEWPORT MAPPING
AIM:
ALGORITHM:
PROGRAM
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public WindowToViewportMapping() {
setPreferredSize(new Dimension(400, 400));
36 | P a g e
mapButton = new JButton("Map");
mapButton.addActionListener(this);
add(mapButton);
System.arraycopy(originalX, 0, x, 0, numVertices);
System.arraycopy(originalY, 0, y, 0, numVertices);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
repaint();
}
@Override
public void actionPerformed(ActionEvent e) {
applyMapping();
}
37 | P a g e
Output :
38 | P a g e