My 2D Final Report
My 2D Final Report
My 2D Final Report
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
If Pi is+ve ⟹midpoint is outside the circle (or on the circle)and we choose pixel S.
We have yi+1=yi
We have yi+1=yi-1
- 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;
getch();
return 0;
}
Numerical Example :
rectangle(int left, int top, int right, int bottom)
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);
}
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 :
form: P’ = S . P Scaling
the object. Algorithm:
Codee :
// C program to demonstrate scaling of abjects
#include<stdio.h>
#include<graphics.h>
p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}
findNewCoordinate(s, p);
x[i] = p[0][0];
y[i] = p[1][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;
}
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:
int main()
{
// Rotate P about Q
point P(4.0, 3.0);
point Q(2.0, 2.0);
return 0;
}