Final 2d Report
Final 2d Report
Final 2d Report
1
Circle
Definition: A circle is the set of all the points that are equidistant
from a given fixed point.
mathematical equation:
Translating the definition into an equation, we get,
where (x, y) represents all the points on the circle and, hence, it varies. is the
fixed point from which the distance is measured. The coordinates of the fixed
point mentioned earlier are of the Centre of the circle from which the
distance to all the points is measured. The coordinates are the variables here
since they describe the position of each point on the circle relative to the
origin.
2
Using the distance formula between two points, we can calculate the
distance between and as follows:
Which is none other than the equation we started with, using the
definition of a circle. The equation obtained is the standard
equation of a circle with center and radius. The above form is
particularly useful when the coordinates of the center are given
straightaway.
Algorithm
Now, we will see how to calculate the next pixel location from a previously
known pixel location (x, y). In Bradenham’s algorithm at any point (x, y) we have
two option either to choose the next pixel in the east i.e. (x+1, y) or in the
3
And this can be decided by using the decision parameter d as:
code
// C-program for circle drawing
// using Bresenham’s Algorithm
// in computer-graphics
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
5
x++;
// Driver code
int main()
{
int xc = 50, yc = 50, r = 30;
6
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Output:
7
Draw an rectangle:
Algorithm
1)Define the width of the rectangle.
2)Define the Height of the rectangle.
3)Define Area of the rectangle.
4)Calculate the area of the rectangle by multiplying the width and height of the
rectangle.
5)Assign the area of the rectangle to the area variable.
6)print the area of the rectangle.
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
8
// Driver Code
// white background
// successfully or not
if (!image.data) {
return 0;
9
Point p2(255, 255);
int thickness = 2;
Scalar(255, 0, 0),
thickness, LINE_8);
imshow("Output", image);
waitKey(0);
return 0;
10
Line:
Below are some assumptions to keep the algorithm simple.
1. We draw lines from left to right.
2. x1 < x2 and y1< y2
3. Slope of the line is between 0 and 1. We draw a line from lower left to
upper right.
Code:
// A naive way of drawing line
print(x, y);
}
}
11
Output
12
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
Code:
// C Implementation for Boundary Filling Algorithm
#include <graphics.h>
13
{
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
}
}
//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
14
int x = 250, y = 200, radius = 50;
// circle function
circle(x, y, radius);
// Function calling
boundaryFill4(x, y, 6, 15);
delay(10000);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
15
rectangle
8-connected pixels : More complex figures are filled using this approach. The
pixels to be tested are the 8 neighbouring pixels, the pixel on the right, left,
above, below and the 4 diagonal pixels. Areas filled by this method are called 8-
connected. Below given is the algorithm :
Algorithm :
void boundaryFill8(int x, int y, int fill_color,int boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
{
putpixel(x, y, fill_color);
boundaryFill8(x + 1, y, fill_color, boundary_color);
boundaryFill8(x, y + 1, fill_color, boundary_color);
boundaryFill8(x - 1, y, fill_color, boundary_color);
boundaryFill8(x, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y - 1, fill_color, boundary_color);
boundaryFill8(x - 1, y + 1, fill_color, boundary_color);
boundaryFill8(x + 1, y - 1, fill_color, boundary_color);
boundaryFill8(x + 1, y + 1, fill_color, boundary_color);
}
}
Code:
// C Implementation for Boundary Filling Algorithm
16
#include <graphics.h>
17
//driver code
int main()
{
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
// Rectangle function
rectangle(50, 50, 100, 100);
// Function calling
boundaryFill8(55, 55, 4, 15);
delay(10000);
18
getch();
return 0;
}
Output:
19
translation
Point Translation P(X, Y) : Here we only translate the x and y coordinates of
given point as per given translation factor dx and dy respectively.
Below is the C++ program to translate a point:
Code:
// C++ program for translation
// of a single coordinate
#include<bits/stdc++.h>
#include<graphics.h>
// driver program
int main()
{
int P[2] = {5, 8}; // coordinates of point
20
int T[] = {2, 1}; // translation factor
translatePoint (P, T);
return 0;
}
Line Translation: The idea to translate a line is to translate both of the end
points of the line by the given translation factor(dx, dy) and then draw a new line
with inbuilt graphics function.
Below is the C++ implementation of above idea:
• CPP
21
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
// driver program
int main()
{
int P[2][2] = {5, 8, 12, 18}; // coordinates of point
int T[] = {2, 1}; // translation factor
translateLine (P, T);
return 0;
}
Ouput:
22
1. Rectangle Translation : Here we translate the x and
y coordinates of both given points A(top left ) and B(bottom right) as
per given translation factor dx and dy respectively and then draw a
rectangle with inbuilt graphics function
• CPP
// driver program
int main()
{
// Xmin, Ymin, Xmax, Ymax as rectangle
// coordinates of top left and bottom right points
23
int P[2][2] = {5, 8, 12, 18};
int T[] = {2, 1}; // translation factor
translateRectangle (P, T);
return 0;
}
output
scaling transformation
24
= x * sx and y’ y * sy. The scaling factor sx, sy scales the object in X and Y
direction respectively. So, the above equation can be represented in matrix
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
26
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][0];
}
27
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}
// Driven Program
int main()
{
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
scale(x, y, sx,sy);
getch();
return 0;
}
28
Shearing
Shearing deals with changing the shape and size of the 2D object along x-axis
and y-axis. It is similar to sliding the layers in one direction to change the shape
of the 2D object.It is an ideal technique to change the shape of an existing
object in a two dimensional plane. In a two dimensional plane, the object size
can be changed along X direction as well as Y direction.
x-Shear :
In x shear, the y co-ordinates remain the same but the x co-ordinates changes.
If P x, y is the point then the new points will be P’ x’, y’ given as –
29
Matrix Form:
y-Shear :
In y shear, the x co-ordinates remain the same but the y co-ordinates changes.
If P x, y is the point then the new points will be P’ x’, y’ given as –
30
Matrix Form:
x-y Shear :
In x-y shear, both the x and y co-ordinates changes. If P(x, y) is the point then
the new points will be P’ x’, y’ given as –
31