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

Final 2d Report

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

Final 2d report

Name: Badawy Ali Badawy


Ahmed
Course 2d
Code 190032
Department MM

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:

We can hereby introduce the term ‘radius’ as the distance


between (x, y) and the center of the circle and denote it by . Now,
with the new symbol r for the radius of the circle, squaring both sides
of the above equation, the square root is eliminated:

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

southeast i.e. (x+1, y-1).

3
And this can be decided by using the decision parameter d as:

• If d > 0, then (x+1, y-1) is to be chosen as the next pixel as it will be


closer to the arc.
• else (x+1, y) is to be chosen as next pixel.
Now to draw the circle for a given radius ‘r’ and centre xc, yc We will start from
(0, r) and move in first quadrant till x=y (i.e. 45 degree). We should start from
listed initial condition:
d = 3 - (2 * r)
x = 0
y = r
Now for each pixel, we will do the following operations:
1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).

3. call draw Circle(int xc, int yc, int x, int y) function.


4. Repeat steps 5 to 8 until x < = y
5. Increment value of x.
6. If d < 0, set d = d + (4*x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call draw Circle(int xc, int yc, int x, int y) function

code
// C-program for circle drawing
// using Bresenham’s Algorithm
// in computer-graphics
#include <stdio.h>
#include <dos.h>
#include <graphics.h>

// Function to put pixels


// at subsequence points
4
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

// Function for circle-generation


// using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
// for each pixel we will
// draw all eight pixels

5
x++;

// check for decision parameter


// and correspondingly
// update d, x, y
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
}

// 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.

// C++ program to demonstrate rectangle

// over a self-formed background image

#include <iostream>

#include <opencv2/core/core.hpp>

// Drawing shapes

#include <opencv2/imgproc.hpp>

#include <opencv2/highgui/highgui.hpp>

using namespace cv;

using namespace std;

8
// Driver Code

int main(int argc, char** argv)

// Creating a blank image with

// white background

Mat image(500, 500, CV_8UC3,

Scalar(255, 255, 255));

// Check if the image is created

// successfully or not

if (!image.data) {

std::cout << "Could not open or "

<< "find the image\n";

return 0;

// Top Left Corner

Point p1(30, 30);

// Bottom Right Corner

9
Point p2(255, 255);

int thickness = 2;

// Drawing the Rectangle

rectangle(image, p1, p2,

Scalar(255, 0, 0),

thickness, LINE_8);

// Show our image inside a window

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

void naiveDrawLine(x1, x2, y1, y2)

m = (y2 - y1) / (x2 - x1);

for (x = x1; x <= x2; x++) {


// Assuming that the round function finds
// closest integer to a given float.
y = round(mx + c);

print(x, y);
}
}

11
Output

Filling circle with a color


Boundary Fill Algorithm is recursive in nature. It takes an interior point(x, y),
a fill color, and a boundary color as the input. The algorithm starts by checking
the color of x, y . If it’s color is not equal to the fill color and the boundary color,
then it is painted with the fill color and the function is called for all the neighbors
of (x, y). If a point is found to be of fill color or of boundary color, the function
does not call its neighbors and returns. This process continues until all points
up to the boundary color for the region have been tested.
The boundary fill algorithm can be implemented by 4-connected pixels or 8-
connected pixels.
4-connected pixels: After painting a pixel, the function is called for four
neighboring points. These are the pixel positions that are right, left, above, and
below the current pixel. Areas filled by this method are called 4-connected.
Below given is the algorithm:
Algorithm:

void boundaryFill4(int x, int y, int fill_color,int boundary_color)


{

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>

// Function for 4 connected Pixels


void boundaryFill4(int x, int y, int fill_color,int
boundary_color)
{
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)

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;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &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>

// Function for 8 connected Pixels


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);
}
}

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;

// initgraph initializes the


// graphics system by loading a
// graphics driver from disk
initgraph(&gd, &gm, "");

// Rectangle function
rectangle(50, 50, 100, 100);

// Function calling
boundaryFill8(55, 55, 4, 15);

delay(10000);

18
getch();

// closegraph function closes the


// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();

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>

using namespace std;

// function to translate point


void translatePoint ( int P[], int T[])
{
/* init graph and putpixel are used for
representing coordinates through graphical
functions
*/
int gd = DETECT, gm, errorcode;
initgraph (&gd, &gm, "c:\\tc\\bgi");

cout<<"Original Coordinates :"<<P[0]<<","<<P[1];

putpixel (P[0], P[1], 1);

// calculating translated coordinates


P[0] = P[0] + T[0];
P[1] = P[1] + T[1];

cout<<"\nTranslated Coordinates :"<< P[0]<<","<< P[1];

// Draw new coordinates


putpixel (P[0], P[1], 3);
closegraph();
}

// 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

// cpp program for translation


// of a single line
#include<bits/stdc++.h>
#include<graphics.h>

using namespace std;

// function to translate line


void translateLine ( int P[][2], int T[])
{
/* init graph and line() are used for
representing line through graphical
functions
*/
int gd = DETECT, gm, errorcode;
initgraph (&gd, &gm, "c:\\tc\\bgi");

// drawing original line using graphics functions


setcolor (2);
line(P[0][0], P[0][1], P[1][0], P[1][1]);

// calculating translated coordinates


P[0][0] = P[0][0] + T[0];

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];

// drawing translated line using graphics functions


setcolor(3);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
closegraph();
}

// 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

// C++ program for translation


// of a rectangle
#include<bits/stdc++.h>
#include<graphics.h>
using namespace std;

// function to translate rectangle


void translateRectangle ( int P[][2], int T[])
{
/* init graph and rectangle() are used for
representing rectangle through graphical functions */
int gd = DETECT, gm, errorcode;
initgraph (&gd, &gm, "c:\\tc\\bgi");
setcolor (2);
// rectangle (Xmin, Ymin, Xmax, Ymax)
// original rectangle
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);

// calculating translated coordinates


P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];

// translated rectangle (Xmin, Ymin, Xmax, Ymax)


// setcolor(3);
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
// closegraph();
}

// 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

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.
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 factor sx and sy to produce the transformed coordinates as x’, y’ . So, x’

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

form: Or P’ S . P Scaling process:

1. Make a 2x2 scaling matrix S as:


Sx 0
0 Sy
2. For each point of the polygon.
(i) Make a 2x1 matrix P, where P[0][0] equals
to x coordinate of the point and P[1][0]
equals to y coordinate of the point.
(ii) Multiply scaling matrix S with point
matrix P to get the new coordinate.
3. Draw the polygon using new coordinates.
// C program to demonstrate scaling of abjects
#include<stdio.h>
25
#include<graphics.h>

// Matrix Multiplication to find new Coordinates.


// s[][] is scaling matrix. p[][] is to store
// points that needs to be scaled.
// p[0][0] is x coordinate of point.
// p[1][0] is y coordinate of given point.
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };

for (int i = 0; i < 2; i++)


for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

// Scaling the Polygon


void scale(int x[], int y[], int sx, int sy)
{

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]);

// Initializing the Scaling Matrix.


int s[2][2] = { sx, 0, 0, sy };
int p[2][1];

// Scaling the triangle


for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];

findNewCoordinate(s, p);

x[i] = p[0][0];
y[i] = p[1][0];
}

// Triangle after Scaling


line(x[0], y[0], x[1], y[1]);

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;

int gd, gm;


detectgraph(&gd, &gm);
initgraph(&gd, &gm," ");

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

You might also like