Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Cga Till Lab 9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

LAB 1

POINTS AND CO-ORDINATE SYSTEM

AIM:
This experiment explains how points and co-ordinate systems are used together to represent two- and three-
dimensional shapes.

THEORY:
The two-dimensional points are represented or located on an x-y plane. Thex-y plane is a plane that
contains an orthogonal axis which are x and y axis. The point is represented as the distance from the
orthogonal axes. So (4,5) represents point on the x-y plane which is 4 units from y-axis and 5 units
from x-axis. If the coordinate axes are non-orthogonal then the point is represented by the parallel
distances form the axes.
Whereas the three dimensional points are located on an x-y-z plane similarto x-y plane and includes z-
axis along with the x and y axis and all three axes areorthogonal to each other. The points are represented
as the distances from each ofthe three planes formed by the axis. So (1,2,3) represents a point which is
1 unit from y-z plane, 2 units from x-z plane and 3 units from x-y plane.
We also can homogenize the coordinates by adding an extra value to the tuple or the point
representation. So in 3 dimensions points are represented as (x,y, z, w) which is similar to (x/w, y/w,
z/w) in ordinary coordinates.

OBJECTIVE:
The objective of the experiment is to understand the representation of points in 2D or 3D space with
orthogonal as well non-orthogonal coordinate systems.

PROCEDURE:
This experiment helps us to learn how the points and co-ordinate systems are represented in computer
graphics. The display on the left shows the world with each shape and co-ordinate system that we create.
The tree at the right showsa point and the associated co-ordinate system.

21124094 SAKSHAM BASSI 1


Each node is an instance where instance is a point or any shape respectivelywhich has 3 sections, that are
Coordinate system, Shape, Transformation. Coordinate system is the values of the distance of the
instance along with homogenize value. Shape is the geometry or a child instance of the current instance.
Transformation is the set of Transformations applied to the shape or instance with respect to local
coordinate system.

We can edit the co-ordinates of the point and comparing how the point getsdisplayed. The co-ordinates
can be edited by clicking on the node under instance – shape – vertices.

We can modify the co-ordinate system’s origin and axis directions. This can be done by clicking on
the nodes under instance-coordinate system. We can notice that the point is redrawn using the modified
co-ordinate system. Also we can notice how the absolute co-ordinates are displayed.

21124094 SAKSHAM BASSI 2


We can observe that all the points shown within ‘[‘ and ‘]’ use homogeneous co-ordinates that is we
have the fourth component w acts as a scalerfactor for calculating the cartesian co-ordinates. If we
modify this component wecan notice how the point’s co-ordinates get scaled.
We can also modify the absolute co-ordinates of the point and see how therelative co-ordinates get
calculated with respect to the co-ordinate system. We can also change the dimension of the scene form
2D to 3D under the display tab and perform all the actions which we have discussed before. We can
also get theview from every side by just right clicking the area with mouse and can rotate it which
helps us to view from any direction.

21124094 SAKSHAM BASSI 3


LAB 2

BRESENHAM’s LINE DRAWING ALGORITHM

AIM:
This experiment explains implementation of a line using Bresenham’s Line Algorithm.

THEORY:
Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional
raster that should be selected in order to form a close approximation to a straight line between two points.

CODE:

#include <bits/stdc++.h>
using namespace std;
// function for line generation
void bresenham(int x1, int y1, int x2, int y2)
{
int m_new = 2 * (y2 - y1);
int slope_error_new = m_new - (x2 - x1);
for (int x = x1, y = y1; x <= x2; x++) {
cout << "(" << x << "," << y << ")\n";
// Add slope to increment angle formed
slope_error_new += m_new;
// Slope error reached limit, time to
// increment y and update slope error.
if (slope_error_new >= 0) {
y++;
slope_error_new -= 2 * (x2 - x1);
}
}
}
// driver code
int main()
{
int x1 = 3, y1 = 2, x2 = 15, y2 = 5;
// Function call
bresenham(x1, y1, x2, y2);
return 0;
}

21124094 SAKSHAM BASSI 4


OUTPUT :

21124094 SAKSHAM BASSI 5


LAB 3

DDA LINE GENERATION ALGORITHM

AIM:
This experiment explains implementation of a line using DDA line generation Algorithm.

THEORY:
DDA (Digital Differential Analyzer) is a line drawing algorithm used in computer graphics to generate a
line segment between two specified endpoints.

CODE:

import matplotlib.pyplot as plt

def dda_line(x1, y1, x2, y2):


dx = x2 - x1
dy = y2 - y1
steps = abs(dx) if abs(dx) > abs(dy) else abs(dy)
x_increment = dx / steps
y_increment = dy / steps

x = x1
y = y1

points = [(round(x), round(y))]

for _ in range(steps):
x += x_increment
y += y_increment
points.append((round(x), round(y)))

return points

# Define the endpoints of the line


x1, y1 = 2, 3
x2, y2 = 9, 8
points = dda_line(x1, y1, x2, y2)
x_vals = [point[0] for point in points]
y_vals = [point[1] for point in points]

21124094 SAKSHAM BASSI 6


plt.plot(x_vals, y_vals, marker='o')
plt.title("DDA Line Generation Algorithm")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid()
plt.show()

OUTPUT :

21124094 SAKSHAM BASSI 7


LAB 4

BRESENHAM’s CIRCLE ALGORITHM

AIM:
This experiment explains implementation of a line using Bresenham’s Circle Algorithm.

THEORY:
Bresenham’s Circle Drawing Algorithm is a circle drawing algorithm that selects the nearest pixel position
to complete the arc. The unique part of this algorithm is that is uses only integer arithmetic which makes it,
significantly, faster than other algorithms using floating point arithmetic in classical processors.

CODE:
import matplotlib.pyplot as plt

def draw_circle(x_center, y_center, radius):


x=0
y = radius
p = 3 - 2 * radius

circle_points = []

while x <= y:
# Octant 1
circle_points.append((x_center + x, y_center + y))
# Octant 2
circle_points.append((x_center + y, y_center + x))
# Octant 3
circle_points.append((x_center - y, y_center + x))
# Octant 4
circle_points.append((x_center - x, y_center + y))
# Octant 5
circle_points.append((x_center - x, y_center - y))
# Octant 6
circle_points.append((x_center - y, y_center - x))
# Octant 7
circle_points.append((x_center + y, y_center - x))
# Octant 8
circle_points.append((x_center + x, y_center - y))

if p < 0:
p += 4 * x + 6
else:
p += 4 * (x - y) + 10
y -= 1
x += 1

return circle_points

21124094 SAKSHAM BASSI 8


# Center and radius of the circle
center_x = 50
center_y = 50
radius = 25

circle_points = draw_circle(center_x, center_y, radius)

# Plotting the circle


plt.figure(figsize=(6, 6))
plt.plot(*zip(*circle_points), marker='o', linestyle='', color='b')
plt.gca().set_aspect('equal', adjustable='box')
plt.title('Bresenham Circle Drawing Algorithm')
plt.xlabel('X')
plt.ylabel('Y')
plt.grid(True)
plt.show()

OUTPUT :

21124094 SAKSHAM BASSI 9


LAB 5

CIRCLE USING MID-POINT ALGORITHM

AIM:
This experiment explains implementation of a circle using mid-point Algorithm.

THEORY:
One of the most efficient and easiest to drive of the circle algorithms is due to MidPoint. To begin, note that
only one octant of the circle need be generated. The other partscan be obtained by successive reflections. If
the first octant ( 0 to 45 ccw ) is generated, the second octant can be obtained by reflection through the line
y=x to yield the first quadrant. The results in the first quadrant are reflected through the line x=0 to obtain
those in the second quadrant. The combined result in the upper semicircle are reflected through the line y=0
to complete the circle. Mid Point’s Algorithm is consider the first quadrant of an origin- centered circle. If
the algorithm begins at x=0 , y=r, then for clockwise generation of the circle y is a monotonically decreasing
function 49 of x in the first quadrant. Here the clockwise generation starting at x=0, y=r is chosen. The
center of the circle is ( 0,0 ). We use the mid-point algorithm to calculate all the perimeter points of the
circle in the first octant and then print them along with their mirror points in the other octants. This will
work because a circle is symmetric about its centre.

CODE:
#include<iostream>
using namespace std;
void midPointCircleDraw(int x_centre, int y_centre, int r)
{
int x = r, y = 0;
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
if (r > 0)
{
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
21124094 SAKSHAM BASSI 10
cout << "(" << -y + x_centre << ", " << x + y_centre << ")\n";
}
int P = 1 - r;
while (x > y)
{
y++;
if (P <= 0){
P = P + 2*y + 1;}
else{
x--;
P = P + 2*y - 2*x + 1;
}
if (x < y){
break;}
cout << "(" << x + x_centre << ", " << y + y_centre << ") ";
cout << "(" << -x + x_centre << ", " << y + y_centre << ") ";
cout << "(" << x + x_centre << ", " << -y + y_centre << ") ";
cout << "(" << -x + x_centre << ", " << -y + y_centre << ")\n";
if (x != y)
{
cout << "(" << y + x_centre << ", " << x + y_centre << ") ";
cout << "(" << -y + x_centre << ", " << x + y_centre << ") ";
cout << "(" << y + x_centre << ", " << -x + y_centre << ") ";
cout << "(" << -y + x_centre << ", " << -x + y_centre << ")\n";
}
}
}
int main()
{
int xc,yc;
cout<<"Enter the coordinates of the center of the circle :"<<endl;
cin>>xc>>yc;
cout<<"Enter the radius of the circle :"<<endl;
int r;
cin>>r;
midPointCircleDraw(xc, yc, r);
return 0;
}

21124094 SAKSHAM BASSI 11


OUTPUT :

21124094 SAKSHAM BASSI 12


LAB 6

ELLIPSE USING MID-POINT ALGORITHM

AIM:
This experiment explains implementation of a ellipse using mid-point Algorithm.

THEORY:
One of the most efficient and easiest to drive of the ellipse algorithms is due to Midpoint. Midpoint ellipse
algorithm plots(finds) points of an ellipse on the first quadrant by dividing the quadrant into two regions.
Each point (x, y) is then projected into other three quadrants (-x, y), (x, -y), (-x, -y) i.e. it uses 4-way
symmetry. Initially, we have two decision parameters p10 in region 1 and p20 in region 2.

CODE:
#include<iostream>
using namespace std;
void drawEllipse(int xc, int yc, int x, int y) {
cout << "(" << xc+x << "," << yc+y << ")\n";
cout << "(" << xc-x << "," << yc+y << ")\n";
cout << "(" << xc+x << "," << yc-y << ")\n";
cout << "(" << xc-x << "," << yc-y << ")\n";
}
void midPointEllipseDraw(int xc, int yc ,int rx, int ry)
{
int x=0,y=ry;
int rx2=rx*rx;
int ry2=ry*ry;
int fx=0;
int fy=2*rx2*y;
int p=ry2-(rx2*ry)*(0.25*rx2);
while(fx<fy)
{
x++;
fx=fx+(2*ry2);
if(p<0)
21124094 SAKSHAM BASSI 13
{
p=p+fx+ry2;
}
else{
y--;
fy=fy-(2-rx2);
p=p+fx-fy+ry2;
}
drawEllipse(xc,yc,x,y);
}
p=ry2*((x+0.5)*(x+0.5))+rx2*((y-1)*(y-1))-rx2*ry2;
while(y>0)
{
y--;
fy=fy-(2*rx2);
if(p>0)
{
p=p-fy+rx2;
}
else{
x++;
fx=fx+(2*ry2);
p=p+fx-fy+rx2;
}
drawEllipse(xc,yc,x,y);
}}
int main()
{
int x,y;
cout<<"Enter the coordinates of center of Ellipse :"<<endl;
cin>>x>>y;
int a,b;
cout<<"Enter the length of major and minor axes of the ellipse :"<<endl;
cin>>a>>b;
midPointEllipseDraw(x,y,a,b);
return 0;
}

21124094 SAKSHAM BASSI 14


OUTPUT :

21124094 SAKSHAM BASSI 15


LAB 7

2-D TRANSFORMATIONS

AIM:
To perform various 2D transformations of an object.

THEORY:
2D Transformation:
We can use a 2 × 2 matrix to change or transform, a 2D vector. This kind of operation, which takes in a 2-
vector and produces another 2-vector by a simple matrix multiplication, is a linear transformation. By this
simple formula, we can achieve a variety of useful transformations, depending on what we put in the entries
of the matrix. For our purposes, consider moving along the x-axis a horizontal move and along the y-axis, a
vertical move.
Translation:
A translation process moves every point a constant distance in a specified direction. It can be described as a
rigid motion. A translation can also be interpreted as the addition of a constant vector to every point, or as
shifting the origin of the coordinate system.

Rotation:
Rotation is one of the part of computer graphic’s transformation, Transformation means to change some
graphics into something else with the help of rules. There are various types of transformations like
translation, scaling, rotation, shearing, reflection etc. it helps to change the object’s position, size,
orientation, shape, etc. When a transformation takes place on a 2D plane, it is called 2D transformation.

x=rotation angle (in degree)


Scaling:
A scaling transformation alters size of an object. In the scaling process, we either compress or expand the
dimension of the object. Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of
the polygon by scaling factors Sx and Sy to produce the transformed coordinates as (x’,y’).

21124094 SAKSHAM BASSI 16


PROCEDURE:

This experiment helps us to learn how the transformation of objects take place in computer graphics. The
display on the left shows the world with each shape and co-ordinate system that we create. The tree at the
right shows a point and the associated coordinate system.

Now we have to create new instance with shape as cube.

Now we will setup the translation parameters (Tx,Ty) as this is a 2D transformation to translate the in instance
created.

21124094 SAKSHAM BASSI 17


Now we will setup the rotation angle and axis about which we want to rotate our object.

21124094 SAKSHAM BASSI 18


Now we will setup the scaling parameters (Sx,Sy) of the object to scale the object from its original size.

Now we will add all the three transformation (translation, rotation ,scaling) that we have created to the
transformation window of our object.
21124094 SAKSHAM BASSI 19
This will be the final Output of the object after all 3 transformations (Translation, Rotation, Scaling)
performed in given order.

21124094 SAKSHAM BASSI 20


LAB 8

3-D TRANSFORMATIONS

AIM:
To perform various 3D transformations of an object.

THEORY:
3D Transformation:
In very general terms a 3D model is a mathematical representation of a physical entity that occupies space.
In more practical terms, a 3D model is made of a description of its shape and a description of its color
appearance.3-D Transformation is the process of manipulating the view of a three-D object with respect to
its original position by modifying its physical attributes through various methods of transformation like
Translation, Scaling, Rotation, Shear, etc.

Properties of 3D transformation:

• Lines are preserved,


• Parallelism is preserved,
• Proportional distances are preserved.

Translation:
It is the process of changing the relative location of a 3-D object with respect to the original position by
changing its coordinates.

Rotation:
Rotation is one of the part of computer graphic’s transformation, Transformation means to change some
graphics into something else with the help of rules. There are various types of transformations like
translation, scaling, rotation, shearing, reflection etc. It helps to change the object’s position, size,
orientation, shape, etc. When a transformation takes place on a 3D plane, it is called 3D transformation.
1. Rotation about x axis:
In this kind of rotation, the object is rotated parallel to the x-axis (principal axis), where the x coordinate
remains unchanged and the rest of the two coordinates y and z only change

21124094 SAKSHAM BASSI 21


2. Rotation about y axis:
In this kind of rotation, the object is rotated parallel to the y-axis (principal axis), where the y coordinate
remains unchanged and the rest of the two coordinates x and z only change.

3. Rotation about z axis:


In this kind of rotation, the object is rotated parallel to the z-axis (principal axis), where the z coordinate
remains unchanged and the rest of the two coordinates x and y only change.

Scaling :
It is performed to resize the 3D-object that is the dimension of the object can be scaled(alter) in any of the x,
y, z direction through Sx, Sy, Sz scaling factors. In scaling we either compress or expand the dimension of
the object.

PROCEDURE:
This experiment helps us to learn how the transformation of objects take place in computer graphics. The
display on the left shows the world with each shape and co-ordinate system that we create. The tree at the
right shows a point and the associated coordinate system.

21124094 SAKSHAM BASSI 22


Now we have to create new instance with shape as cube.

Setting the display to 3D

Now we will setup the translation parameter (Tx, Ty, Tz) as this is a 3D transformation to translate the object
in instance created.

21124094 SAKSHAM BASSI 23


Now we will set the rotation angle and the axis about which we want to rotate our object.

21124094 SAKSHAM BASSI 24


Now we will setup the scaling parameters (Sx,Sy,Sz) of the object to scale the object from its original size.

Now we will add all the three transformation (translation, rotation, scaling) that we have created to the
transformation window of our object.

21124094 SAKSHAM BASSI 25


This will be the final output of the object after all 3 transformations (Translation, Rotation, Scaling) performed
in the given order.

21124094 SAKSHAM BASSI 26


LAB 9

AIM: Implementation of Liang Barskey algorithm.

CODE:
#include <bits/stdc++.h>
using namespace std;
bool clip(double x1, double y1, double x2, double y2, double &xmin, double &ymin, double &xmax,
double &ymax)
{
double p1 = -(x2 - x1);
double p2 = -p1;
double p3 = -(y2 - y1);
double p4 = -p3;
double q1 = x1 - xmin;
double q2 = xmax - x1;
double q3 = y1 - ymin;
double q4 = ymax - y1;
double t1 = 0, t2 = 1;
if (p1 == 0 && q1 < 0)
return false;
if (p2 == 0 && q2 < 0)
return false;
if (p3 == 0 && q3 < 0)
return false;
if (p4 == 0 && q4 < 0)
return false;
if (p1 != 0)
{
double r1 = q1 / p1;
if (p1 < 0)
{
if (r1 > t2)
return false;
else if (r1 > t1)
t1 = r1;
}
else
{
if (r1 < t1)
return false;
else if (r1 < t2)
t2 = r1;
}
}
if (p2 != 0)
21124094 SAKSHAM BASSI 27
{
double r2 = q2 / p2;
if (p2 < 0)
{
if (r2 > t2)
return false;
else if (r2 > t1)
t1 = r2;
}
else
{
if (r2 < t1)
return false;
else if (r2 < t2)
t2 = r2;
}
}
if (t1 >= t2)
return false;
xmin = x1 + t1 * (x2 - x1);
ymin = y1 + t1 * (y2 - y1);
xmax = x1 + t2 * (x2 - x1);
ymax = y1 + t2 * (y2 - y1);
return true;
}
int main()
{
double x1, y1, x2, y2;
double xmin, ymin, xmax, ymax;
cout << "Enter the coordinates of the line (x1 y1 x2 y2): ";
cin >> x1 >> y1 >> x2 >> y2;
cout << "Enter x-min: ";
cin >> xmin;
cout << "Enter x-max: ";
cin >> xmax;
cout << "Enter y-min: ";
cin >> ymin;
cout << "Enter y-max: ";
cin >> ymax;
bool clipped = clip(x1, y1, x2, y2, xmin, ymin, xmax, ymax);
if (clipped)
cout << "Clipped line: (" << xmin << ", " << ymin << ") to ("<<xmax <<", "<<ymax<<") << endl;
else cout<< "Line is completely outside the window." << endl;
return 0;
}

21124094 SAKSHAM BASSI 28


OUTPUT:

21124094 SAKSHAM BASSI 29

You might also like