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

My 2D Final Report

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

Name : Abdelrahman Mohamed Abbas

Code :190086
Department : MM
( Final Report)
( Content )
-My info ..
-point class
-Drawing Shapes
-line
- Circle
-Rectangle
-Triangle
-Filling color
- Circle
-Rectangle
-Scaling
- Rotation
- collision
[point]
Point is a location in space drawed as a dot. A point didn’t have length,
width,
shape or size, it only has a position (x,y)

Pseudocode:
[Circle]
1-mathematical Representation :
It is based on the following function for testing the spatial relationship between the arbitrary point (x, y)
and a circle of radius r centered at the origin:
Now, consider the coordinates of the point halfway between pixel T and pixel S

This is called midpoint (xi+1,yi- ) and we use it to define a decision parameter:

Pi=f (xi+1,yi- ) = (xi+1)2+(yi- )2-r2 ...............equation 2

If Pi is -ve ⟹midpoint is inside the circle and we choose pixel T

If Pi is+ve ⟹midpoint is outside the circle (or on the circle)and we choose pixel S.

The decision parameter for the next step is:

Pi+1=(xi+1+1)2+(yi+1- )2- r2............equation 3

Since xi+1=xi+1, we have

If pixel T is choosen ⟹Pi<0

We have yi+1=yi

If pixel S is choosen ⟹Pi≥0

We have yi+1=yi-1

We can continue to simplify this in n terms of (xi,yi) and get

Now, initial value of Pi (0,r)from equation 2


We can put ≅1
∴r is an integer
So, P1=1-r

- Algorithm :
- Step1: Put x =0, y =r in equation 2
We have p=1-r
- Step2: Repeat steps while x ≤ y
Plot (x, y)
If (p<0)
Then set p = p + 2x + 3
Else
p = p + 2(x-y)+5
y =y - 1 (end if)
x =x+1 (end loop)
- Step3: End

[Code] :
1. #include <graphics.h>
2. #include <stdlib.h>
3. #include <math.h>
4. #include <stdio.h>
5. #include <conio.h>
6. #include <iostream.h>
7.
8. class bresen
9. {
10. float x, y,a, b, r, p;
11. public:
12. void get ();
13. void cal ();
14. };
15. void main ()
16. {
17. bresen b;
18. b.get ();
19. b.cal ();
20. getch ();
21. }
22. Void bresen :: get ()
23. {
24. cout<<"ENTER CENTER AND RADIUS";
25. cout<< "ENTER (a, b)";
26. cin>>a>>b;
27. cout<<"ENTER r";
28. cin>>r;
29. }
30. void bresen ::cal ()
31. {
32. /* request auto detection */
33. int gdriver = DETECT,gmode, errorcode;
34. int midx, midy, i;
35. /* initialize graphics and local variables */
36. initgraph (&gdriver, &gmode, " ");
37. /* read result of initialization */
38. errorcode = graphresult ();
39. if (errorcode ! = grOK) /*an error occurred */
40. {
41. printf("Graphics error: %s \n", grapherrormsg (errorcod
e);
42. printf ("Press any key to halt:");
43. getch ();
44. exit (1); /* terminate with an error code */
45. }
46. x=0;
47. y=r;
48. putpixel (a, b+r, RED);
49. putpixel (a, b-r, RED);
50. putpixel (a-r, b, RED);
51. putpixel (a+r, b, RED);
52. p=5/4)-r;
53. while (x<=y)
54. {
55. If (p<0)
56. p+= (4*x)+6;
57. else
58. {
59. p+=(2*(x-y))+5;
60. y--;
61. }
62. x++;
63. putpixel (a+x, b+y, RED);
64. putpixel (a-x, b+y, RED);
65. putpixel (a+x, b-y, RED);
66. putpixel (a+x, b-y, RED);
67. putpixel (a+x, b+y, RED);
68. putpixel (a+x, b-y, RED);
69. putpixel (a-x, b+y, RED);
70. putpixel (a-x, b-y, RED);
71. }
72. }
Example and OUTPUT:

[ Rectangle ]
1 -Mathematical Representation
Rectangle() is used to draw a rectangle. Coordinates
of left top and right bottom corner are required to
draw the rectangle. left specifies the X-coordinate
of top left corner, top specifies the Y-coordinate of
top left corner, right specifies the X-coordinate of
right bottom corner, bottom specifies the Y-
coordinate of right bottom corner.

[Code]
#include <graphics.h>

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

// location of left, top, right, bottom


int left = 150, top = 150;
int right = 450, bottom = 450;

// initgraph initializes the graphics system


// by loading a graphics driver from disk
initgraph(&gd, &gm, "");
// rectangle function
rectangle(left, top, right, bottom);

getch();

// closegraph function closes the graphics


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

return 0;
}

Numerical Example :
rectangle(int left, int top, int right, int bottom)

Input : left = 150, top = 250, right = 450, bottom = 350;

OUTPUT :

[Line]
Mathematical Representation :
We already know that the general equation of a straight line is
y = mx + c, where m is the slope of the line and c is the y-
intercept. It is the most common form of the equation of a
straight line that is mainly used in geometry. So, We use this
straight line equation to draw a line in C Program without using
graphics

[Code] :
• #include<iostream.h>
• #include<conio.h>
• #include<math.h>
• using namespace std;
• int pth (int x) {
• return 2*x-3;
• }
• void main()
• {
• int i,j,y;
• for (int x=-10;x <= 10;x++)
• {
• for ( i=-10;i <= x;i++)
• {
• if (i==x)
• {
• y=pth(x);
• cout<<"x=";
• cout<<x;
• cout<<"y=";
• cout<<y;

• for(j=-10;j<=y;j++)
• {
• if(j==y)
• cout << "*";
• else
• cout << " ";
• }
• }
• }
• cout << "\n";
• }
• cin.get();
• return 0;
• getch();
• }

[Triangle]
Mathematical representation :
The following code shows a C# pseudo code
implementation of the triangle drawing routine. Given a
bitmap, the array of pixels (integers), then the following
short piece of code will draw the bitmap efficiently. It
also makes use of a swap function that swaps 2 variables
(integers, not included).
[code] :
public static void FillTriangleSimple
(this WriteableBitmap bitmap, int x0, int y0, int x1, int y1, int x2, int y2,
int color)
{
int[] pixels = bitmap.Pixels;
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
// sort the points vertically
if (y1 > y2)
{
swap(ref x1, ref x2);
swap(ref y1, ref y2);
}
if (y0 > y1)
{
swap(ref x0, ref x1);
swap(ref y0, ref y1);
}
if (y1 > y2)
{
swap(ref x1, ref x2);
swap(ref y1, ref y2);
}

double dx_far = Convert.ToDouble(x2 - x0) / (y2 - y0 + 1);


double dx_upper = Convert.ToDouble(x1 - x0) / (y1 - y0 + 1);
double dx_low = Convert.ToDouble(x2 - x1) / (y2 - y1 + 1);
double xf = x0;
double xt = x0 + dx_upper; // if y0 == y1, special case
for (int y = y0; y <= (y2 > height-1 ? height-1 : y2); y++)
{
if (y >= 0)
{
for (int x = (xf > 0 ? Convert.ToInt32(xf) : 0);
x <= (xt < width ? xt : width-1) ; x++)
pixels[Convert.ToInt32(x + y * width)] = color;
for (int x = (xf < width ? Convert.ToInt32(xf) : width-1);
x >= (xt > 0 ? xt : 0); x--)
pixels[Convert.ToInt32(x + y * width)] = color;
}
xf += dx_far;
if (y < y1)
xt += dx_upper;
else
xt += dx_low;
}
}
Filling Shapes with color :

1- [Circle]
Boundary Fill Algorithm starts at a pixel inside the polygon to
be filled and paints the interior proceeding outwards towards the
boundary. This algorithm works only if the color with which the
region has to be filled and the color of the boundary of the
region are different. If the boundary is of one single color, this
approach proceeds outwards pixel by pixel until it hits the
boundary of the region.
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
neighbours of (x, y). If a point is found to be of fill color or of
boundary color, the function does not call its neighbours 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 neighbouring 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 :
2- void boundaryFill4(int x, int y, int fill_color,int
boundary_color)
3- {
4- if(getpixel(x, y) != boundary_color &&
5- getpixel(x, y) != fill_color)
6- {
7- putpixel(x, y, fill_color);
8- boundaryFill4(x + 1, y, fill_color,
boundary_color);
9- boundaryFill4(x, y + 1, fill_color,
boundary_color);
10- boundaryFill4(x - 1, y, fill_color,
boundary_color);
11- boundaryFill4(x, y - 1, fill_color,
boundary_color);
12- }
13- }
output:

2-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);
}
}
Output:
Scaling :

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’ = 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: P’ = S . P Scaling
the object. Algorithm:

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.

Codee :
// C program to demonstrate scaling of abjects
#include<stdio.h>
#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)
{
// 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]);
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;
}
Output :
Rotation :
Now, we have to rotate the point P not about origin but about a
general point Q. This can be easily understood by the method of
translation which is quite a common technique adopted in geometric
analysis. What is Translation? In Euclidean geometry, translation is
a geometric transformation that moves every point of a figure or a
space by the same amount in a given direction. How to Perform
Translation? Translation can also be interpreted as the addition of a
constant vector to every point, or as shifting the origin of
the coordinate system. After the translation, required computations are
made and the translation is nullified by subtracting the constant vector
to every point or shifting the origin back.
So, for rotating P about Q, we shift the origin at Q i.e. we subtract the
vector equivalent of Q from every point of the coordinate plane. Now
the new point P – Q has to be rotated about the origin and then
translation has to be nullified. These steps can be described as under:

1. Translation (Shifting origin at Q): Subtract Q from all points.


Thus, P becomes P – Q
2. Rotation of (P – Q) about origin: (P – Q) * polar(1.0, θ)
3. Restoring back the Origin: Add Q to all the points.
Code :
/ CPP example to illustrate the rotation
// of a point about another point
#include <iostream>
#include <complex>

using namespace std;

typedef complex<double> point;


#define x real()
#define y imag()

// Constant PI for providing angles in radians


#define PI 3.1415926535897932384626

// Function used to display X and Y coordinates of a point


void displayPoint(point P)
{
cout << "(" << P.x << ", " << P.y << ")" << endl;
}

//Function for Rotation of P about Q by angle theta


point rotate(point P, point Q, double theta)
{
return (P-Q) * polar(1.0, theta) + Q;
}

int main()
{
// Rotate P about Q
point P(4.0, 3.0);
point Q(2.0, 2.0);

// Angle of rotation = 90 degrees


double theta = PI/2;

point P_rotated = rotate(P, Q, theta);


cout << "The point P on rotating 90 degrees anti-clockwise about Q
becomes:";
cout << "P_rotated"; displayPoint(P_rotated);

return 0;
}

You might also like