Cga Till Lab 9
Cga Till Lab 9
Cga Till Lab 9
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.
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.
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;
}
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:
x = x1
y = y1
for _ in range(steps):
x += x_increment
y += y_increment
points.append((round(x), round(y)))
return points
OUTPUT :
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
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
OUTPUT :
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;
}
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;
}
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.
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 will setup the translation parameters (Tx,Ty) as this is a 2D transformation to translate the in instance
created.
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.
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:
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
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.
Now we will setup the translation parameter (Tx, Ty, Tz) as this is a 3D transformation to translate the object
in instance created.
Now we will add all the three transformation (translation, rotation, scaling) that we have created to the
transformation window of our object.
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;
}