C Program to create a house like figure and perform the following operations.
a. Scaling about the origin followed by translation.
b. Scaling with reference to an arbitrary point.
c. Reflect about the line y = mx + c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <stdio.h> #include <graphics.h> #include <stdlib.h> #include <math.h> #include <conio.h> void reset (int h[][2]) { int val[9][2] = { { 50, 50 },{ 75, 50 },{ 75, 75 },{ 100, 75 }, { 100, 50 },{ 125, 50 },{ 125, 100 },{ 87, 125 },{ 50, 100 } }; int i; for (i=0; i<9; i++) { h[i][0] = val[i][0]-50; h[i][1] = val[i][1]-50; } } void draw (int h[][2]) { int i; setlinestyle (DOTTED_LINE, 0, 1); line (320, 0, 320, 480); line (0, 240, 640, 240); setlinestyle (SOLID_LINE, 0, 1); for (i=0; i<8; i++) line (320+h[i][0], 240-h[i][1], 320+h[i+1][0], 240-h[i+1][1]); line (320+h[0][0], 240-h[0][1], 320+h[8][0], 240-h[8][1]); } void rotate (int h[][2], float angle) { int i; for (i=0; i<9; i++) { int xnew, ynew; xnew = h[i][0] * cos (angle) - h[i][1] * sin (angle); ynew = h[i][0] * sin (angle) + h[i][1] * cos (angle); h[i][0] = xnew; h[i][1] = ynew; } } void scale (int h[][2], int sx, int sy) { int i; for (i=0; i<9; i++) { h[i][0] *= sx; h[i][1] *= sy; } } void translate (int h[][2], int dx, int dy) { int i; for (i=0; i<9; i++) { h[i][0] += dx; h[i][1] += dy; } } void reflect (int h[][2], int m, int c) { int i; float angle; for (i=0; i<9; i++) h[i][1] -= c; angle = M_PI/2 - atan (m); rotate (h, angle); for (i=0; i<9; i++) h[i][0] = -h[i][0]; angle = -angle; rotate (h, angle); for (i=0; i<9; i++) h[i][1] += c; } void ini() { int gd=DETECT,gm; initgraph(&gd,&gm,"..\\bgi"); } void dini() { getch(); closegraph(); } void main() { int h[9][2],sx,sy,x,y,m,c,choice; do { clrscr(); printf("1. Scaling about the origin.\n"); printf("2. Scaling about an arbitrary point.\n"); printf("3. Reflection about the line y = mx + c.\n"); printf("4. Exit\n"); printf("Enter the choice: "); scanf("%d",&choice); switch(choice) { case 1: printf ("Enter the x- and y-scaling factors: "); scanf ("%d%d", &sx, &sy); ini(); reset (h); draw (h);getch(); scale (h, sx, sy); cleardevice(); draw (h); dini(); break; case 2: printf ("Enter the x- and y-scaling factors: "); scanf ("%d%d", &sx, &sy); printf ("Enter the x- and y-coordinates of the point: "); scanf ("%d%d", &x, &y); ini(); reset (h); translate (h, x, y);// Go to arbitrary point draw(h); getch();//Show its arbitrary position cleardevice(); translate(h,-x,-y);//Take it back to origin draw(h); getch(); cleardevice(); scale (h, sx, sy);//Now Scale it draw(h); getch(); translate (h, x, y);//Back to Arbitrary point cleardevice(); draw (h); putpixel (320+x, 240-y, WHITE); dini(); break; case 3: printf ("Enter the values of m and c: "); scanf ("%d%d", &m, &c); ini(); reset (h); draw (h); getch(); reflect (h, m, c); cleardevice(); draw (h); dini(); break; case 4: exit(0); } }while(choice!=4); } |
I new