├── 0-10-in-Hindi(Devnagari) ├── hindiNumbers.cpp ├── hindiNumbers.exe └── hindiNumbers.o ├── 3D-rotation-frustrum ├── frustrumRotate.cpp ├── frustrumRotate.exe └── frustrumRotate.o ├── README.md ├── circle-clipping ├── circleClipping.cpp ├── circleClipping.exe └── circleClipping.o ├── curve-hiding(clipping)-with-shapes ├── curveHiding.cpp ├── curveHiding.exe └── curveHiding.o ├── cyrus-beck-line-clipping ├── cyrusBeck.cpp ├── cyrusBeck.exe └── cyrusBeck.o ├── display-current-time-in-hindi ├── currentTimeHindi.cpp ├── currentTimeHindi.exe └── currentTimeHindi.o ├── draw-on-screen ├── drawOnScreen.cpp ├── drawOnScreen.exe └── drawOnScreen.o ├── hidden-surface ├── Reference figure.png ├── hiddenSurface.cpp ├── hiddenSurface.exe └── hiddenSurface.o ├── line-clipping-with-curve ├── lineCurveClipping.cpp ├── lineCurveClipping.exe └── lineCurveClipping.o ├── name-in-hindi ├── nameHindi.cpp ├── nameHindi.exe └── nameHindi.o ├── rotating-circular-design ├── circularRotateDsgn.cpp ├── circularRotateDsgn.exe └── circularRotateDsgn.o ├── scanline-algorithm ├── scanline.cpp ├── scanline.exe └── scanline.o ├── see-saw-moving-wheel ├── see-saw.cpp ├── see-saw.exe └── see-saw.o ├── skipping-rope ├── skippingRope.cpp ├── skippingRope.exe └── skippingRope.o ├── tetrahedron-with-rollno ├── rollNoTetrahedron.cpp ├── rollNoTetrahedron.exe └── rollNoTetrahedron.o └── the-Indian-national-flag ├── nationalFlag.cpp ├── nationalFlag.exe └── nationalFlag.o /0-10-in-Hindi(Devnagari)/hindiNumbers.cpp: -------------------------------------------------------------------------------- 1 | //Numbers from 0 - 10 in Devnagari 2 | //Drawn using Bezier curves 3 | #include 4 | #include 5 | #define MAX 20 6 | #define PI 3.1416 7 | #define COS(a) cos(((a) * PI) / (float)180) 8 | #define SIN(a) sin(((a) * PI) / (float)180) 9 | #define deg(a) ((float)a * 180) / PI 10 | 11 | using namespace std; 12 | 13 | //for thickness of the curves 14 | const int thick = 5; 15 | 16 | 17 | //Bezier curve is stored in the form of matrix each containing 4 rows and a set of controlling points P1, P2, P3, and P4 for each curve as columns 18 | //each controlling point is stored as [x y z 1] column wise, so that matrix calculation can be carried out for 2D or 3D transformation 19 | 20 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 21 | 22 | //0 23 | void drawHindi0(int xc, int yc, int pixcolor){ 24 | const int crv0 = 2; 25 | int hindi0[4][4*crv0] = { {0,40,40,0, 0,-40,-40,0}, 26 | {30,30,-30,-30, 30,30,-30,-30}, 27 | {0,0,0,0, 0,0,0,0}, 28 | {1,1,1,1, 1,1,1,1} }; 29 | 30 | for (int disp = 0; disp < thick; ++disp){ 31 | for (int col = 0; col < 4 * crv0; col +=4) { 32 | for (float t = 0; t <= 1; t += 0.001) { 33 | float x, y; 34 | x = pow(1-t, 3) * hindi0[0][col] + 3 * pow(1-t, 2) * t * hindi0[0][col+1] + 3 * (1-t) * t * t * hindi0[0][col+2] + pow(t, 3) * hindi0[0][col+3]; 35 | y = pow(1-t, 3) * hindi0[1][col] + 3 * pow(1-t, 2) * t * hindi0[1][col+1] + 3 * (1-t) * t * t * hindi0[1][col+2] + pow(t, 3) * hindi0[1][col+3]; 36 | putpixel(xc + disp + x, yc - y, pixcolor); 37 | } 38 | } 39 | } 40 | } 41 | 42 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 43 | 44 | 45 | //1 46 | void drawHindi1(int xc, int yc, int pixcolor){ 47 | const int crv1 = 2; 48 | int hindi1[4][4*crv1] = { {-5,-90,80,0, 0,-60,50,0}, 49 | {25,70,100,20, 20,-25,-40,-70}, 50 | {0,0,0,0, 0,0,0,0}, 51 | {1,1,1,1, 1,1,1,1} }; 52 | 53 | //for drawing 1 54 | for (int disp = 0; disp < thick; ++disp){ 55 | for (int col = 0; col < 4 * crv1; col +=4) { 56 | for (float t = 0; t <= 1; t += 0.001) { 57 | float x, y; 58 | x = pow(1-t, 3) * hindi1[0][col] + 3 * pow(1-t, 2) * t * hindi1[0][col+1] + 3 * (1-t) * t * t * hindi1[0][col+2] + pow(t, 3) * hindi1[0][col+3]; 59 | y = pow(1-t, 3) * hindi1[1][col] + 3 * pow(1-t, 2) * t * hindi1[1][col+1] + 3 * (1-t) * t * t * hindi1[1][col+2] + pow(t, 3) * hindi1[1][col+3]; 60 | putpixel(xc + disp + x, yc - y, pixcolor); 61 | } 62 | } 63 | } 64 | } 65 | 66 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 67 | 68 | 69 | //2 70 | void drawHindi2(int xc, int yc, int pixcolor){ 71 | const int crv2 = 2; 72 | int hindi2[4][4*crv2] = { {-40,60,60,-30, -30,-60,10,30}, 73 | {55,110,-60,-20, -20,20,-10,-70}, 74 | {0,0,0,0, 0,0,0,0}, 75 | {1,1,1,1, 1,1,1,1} }; 76 | 77 | //for drawing 2 78 | for (int disp = 0; disp < thick; ++disp){ 79 | for (int col = 0; col < 4 * crv2; col +=4) { 80 | for (float t = 0; t <= 1; t += 0.001) { 81 | float x, y; 82 | x = pow(1-t, 3) * hindi2[0][col] + 3 * pow(1-t, 2) * t * hindi2[0][col+1] + 3 * (1-t) * t * t * hindi2[0][col+2] + pow(t, 3) * hindi2[0][col+3]; 83 | y = pow(1-t, 3) * hindi2[1][col] + 3 * pow(1-t, 2) * t * hindi2[1][col+1] + 3 * (1-t) * t * t * hindi2[1][col+2] + pow(t, 3) * hindi2[1][col+3]; 84 | putpixel(xc + disp + x, yc - y, pixcolor); 85 | } 86 | } 87 | } 88 | } 89 | 90 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 91 | 92 | 93 | //3 94 | void drawHindi3(int xc, int yc, int pixcolor){ 95 | const int crv3 = 3; 96 | int hindi3[4][4*crv3] = { {-25,55,55,-5, -5,55,50,-30, -30,-40,0,10}, 97 | {60,100,-10,5, 5,10,-90,-50, -50,-20,-40,-80}, 98 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 99 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 100 | 101 | //for drawing 3 102 | for (int disp = 0; disp < thick; ++disp){ 103 | for (int col = 0; col < 4 * crv3; col +=4) { 104 | for (float t = 0; t <= 1; t += 0.001) { 105 | float x, y; 106 | x = pow(1-t, 3) * hindi3[0][col] + 3 * pow(1-t, 2) * t * hindi3[0][col+1] + 3 * (1-t) * t * t * hindi3[0][col+2] + pow(t, 3) * hindi3[0][col+3]; 107 | y = pow(1-t, 3) * hindi3[1][col] + 3 * pow(1-t, 2) * t * hindi3[1][col+1] + 3 * (1-t) * t * t * hindi3[1][col+2] + pow(t, 3) * hindi3[1][col+3]; 108 | putpixel(xc + disp + x, yc - y, pixcolor); 109 | } 110 | } 111 | } 112 | } 113 | 114 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 115 | 116 | 117 | //4 118 | void drawHindi4(int xc, int yc, int pixcolor){ 119 | const int crv4 = 4; 120 | int hindi4[4][4*crv4] = { {0,40,40,40, 0,-40,-40,-40, 0,-30,-30,0, 0,30,30,0}, 121 | {-5,35,45,70, -5,35,45,70, -5,-35,-65,-70, -5,-35,-65,-70}, 122 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 123 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 124 | 125 | //for drawing 4 126 | for (int disp = 0; disp < thick; ++disp){ 127 | for (int col = 0; col < 4 * crv4; col +=4) { 128 | for (float t = 0; t <= 1; t += 0.001) { 129 | float x, y; 130 | x = pow(1-t, 3) * hindi4[0][col] + 3 * pow(1-t, 2) * t * hindi4[0][col+1] + 3 * (1-t) * t * t * hindi4[0][col+2] + pow(t, 3) * hindi4[0][col+3]; 131 | y = pow(1-t, 3) * hindi4[1][col] + 3 * pow(1-t, 2) * t * hindi4[1][col+1] + 3 * (1-t) * t * t * hindi4[1][col+2] + pow(t, 3) * hindi4[1][col+3]; 132 | putpixel(xc + disp + x, yc - y, pixcolor); 133 | } 134 | } 135 | } 136 | } 137 | 138 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 139 | 140 | 141 | //5 142 | void drawHindi5(int xc, int yc, int pixcolor){ 143 | const int crv5 = 3; 144 | int hindi5[4][4*crv5] = { {-40,-50,40,30, 30,30,40,-20, -20,-30,-10,50}, 145 | {70,-30,-20,70, 70,70,-100,-50, -50,-40,-30,-70}, 146 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 147 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 148 | 149 | //for drawing 5 150 | for (int disp = 0; disp < thick; ++disp){ 151 | for (int col = 0; col < 4 * crv5; col +=4) { 152 | for (float t = 0; t <= 1; t += 0.001) { 153 | float x, y; 154 | x = pow(1-t, 3) * hindi5[0][col] + 3 * pow(1-t, 2) * t * hindi5[0][col+1] + 3 * (1-t) * t * t * hindi5[0][col+2] + pow(t, 3) * hindi5[0][col+3]; 155 | y = pow(1-t, 3) * hindi5[1][col] + 3 * pow(1-t, 2) * t * hindi5[1][col+1] + 3 * (1-t) * t * t * hindi5[1][col+2] + pow(t, 3) * hindi5[1][col+3]; 156 | putpixel(xc + disp + x, yc - y, pixcolor); 157 | } 158 | } 159 | } 160 | } 161 | 162 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 163 | 164 | 165 | //6 166 | void drawHindi6(int xc, int yc, int pixcolor){ 167 | const int crv6 = 3; 168 | int hindi6[4][4*crv6] = { {20,-60,-60,0, 0,-60,-55,30, 30,50,-10,40}, 169 | {60,100,-10,5, 5,10,-90,-50, -50,-10,-30,-80}, 170 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 171 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 172 | 173 | //for drawing 6 174 | for (int disp = 0; disp < thick; ++disp){ 175 | for (int col = 0; col < 4 * crv6; col +=4) { 176 | for (float t = 0; t <= 1; t += 0.001) { 177 | float x, y; 178 | x = pow(1-t, 3) * hindi6[0][col] + 3 * pow(1-t, 2) * t * hindi6[0][col+1] + 3 * (1-t) * t * t * hindi6[0][col+2] + pow(t, 3) * hindi6[0][col+3]; 179 | y = pow(1-t, 3) * hindi6[1][col] + 3 * pow(1-t, 2) * t * hindi6[1][col+1] + 3 * (1-t) * t * t * hindi6[1][col+2] + pow(t, 3) * hindi6[1][col+3]; 180 | putpixel(xc + disp + x, yc - y, pixcolor); 181 | } 182 | } 183 | } 184 | } 185 | 186 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 187 | 188 | 189 | //7 190 | void drawHindi7(int xc, int yc, int pixcolor){ 191 | const int crv7 = 2; 192 | int hindi7[4][4*crv7] = { {-30,-80,80,40, 40,0,0,45}, 193 | {70,-100,-110,35, 35,90,-20,20}, 194 | {0,0,0,0, 0,0,0,0}, 195 | {1,1,1,1, 1,1,1,1} }; 196 | 197 | //for drawing 7 198 | for (int disp = 0; disp < thick; ++disp){ 199 | for (int col = 0; col < 4 * crv7; col +=4) { 200 | for (float t = 0; t <= 1; t += 0.001) { 201 | float x, y; 202 | x = pow(1-t, 3) * hindi7[0][col] + 3 * pow(1-t, 2) * t * hindi7[0][col+1] + 3 * (1-t) * t * t * hindi7[0][col+2] + pow(t, 3) * hindi7[0][col+3]; 203 | y = pow(1-t, 3) * hindi7[1][col] + 3 * pow(1-t, 2) * t * hindi7[1][col+1] + 3 * (1-t) * t * t * hindi7[1][col+2] + pow(t, 3) * hindi7[1][col+3]; 204 | putpixel(xc + disp + x, yc - y, pixcolor); 205 | } 206 | } 207 | } 208 | } 209 | 210 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 211 | 212 | 213 | //8 214 | void drawHindi8(int xc, int yc, int pixcolor){ 215 | const int crv8 = 1; 216 | int hindi8[4][4*crv8] = { {10,-70,-10,40}, 217 | {70,-50,-90,-30}, 218 | {0,0,0,0}, 219 | {1,1,1,1} }; 220 | 221 | //for drawing 8 222 | for (int disp = 0; disp < thick; ++disp){ 223 | for (int col = 0; col < 4 * crv8; col +=4) { 224 | for (float t = 0; t <= 1; t += 0.001) { 225 | float x, y; 226 | x = pow(1-t, 3) * hindi8[0][col] + 3 * pow(1-t, 2) * t * hindi8[0][col+1] + 3 * (1-t) * t * t * hindi8[0][col+2] + pow(t, 3) * hindi8[0][col+3]; 227 | y = pow(1-t, 3) * hindi8[1][col] + 3 * pow(1-t, 2) * t * hindi8[1][col+1] + 3 * (1-t) * t * t * hindi8[1][col+2] + pow(t, 3) * hindi8[1][col+3]; 228 | putpixel(xc + disp + x, yc - y, pixcolor); 229 | } 230 | } 231 | } 232 | } 233 | 234 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 235 | 236 | 237 | //9 238 | void drawHindi9(int xc, int yc, int pixcolor){ 239 | const int crv9 = 2; 240 | int hindi9[4][4*crv9] = { {5,100,-120,0, 0,60,50,20}, 241 | {15,90,90,10, 10,-35,-40,-70}, 242 | {0,0,0,0, 0,0,0,0}, 243 | {1,1,1,1, 1,1,1,1} }; 244 | 245 | //for drawing 9 246 | for (int disp = 0; disp < thick; ++disp){ 247 | for (int col = 0; col < 4 * crv9; col +=4) { 248 | for (float t = 0; t <= 1; t += 0.001) { 249 | float x, y; 250 | x = pow(1-t, 3) * hindi9[0][col] + 3 * pow(1-t, 2) * t * hindi9[0][col+1] + 3 * (1-t) * t * t * hindi9[0][col+2] + pow(t, 3) * hindi9[0][col+3]; 251 | y = pow(1-t, 3) * hindi9[1][col] + 3 * pow(1-t, 2) * t * hindi9[1][col+1] + 3 * (1-t) * t * t * hindi9[1][col+2] + pow(t, 3) * hindi9[1][col+3]; 252 | putpixel(xc + disp + x, yc - y, pixcolor); 253 | } 254 | } 255 | } 256 | } 257 | 258 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 259 | 260 | 261 | int main() 262 | { 263 | initwindow(1400, 900); 264 | int xc = getmaxx() / 2, yc = getmaxy() / 2; 265 | 266 | int pixcolor = YELLOW; 267 | 268 | //Drawing all the Devnagari numbers from 0 - 10 269 | drawHindi0(xc - 500, yc - 200, pixcolor); 270 | drawHindi1(xc - 250, yc - 200, pixcolor); 271 | drawHindi2(xc, yc - 200, pixcolor); 272 | drawHindi3(xc + 250, yc - 200, pixcolor); 273 | drawHindi4(xc + 500, yc - 200, pixcolor); 274 | 275 | drawHindi5(xc - 500, yc + 200, pixcolor); 276 | drawHindi6(xc - 250, yc + 200, pixcolor); 277 | drawHindi7(xc, yc + 200, pixcolor); 278 | drawHindi8(xc + 250, yc + 200, pixcolor); 279 | drawHindi9(xc + 500, yc + 200, pixcolor); 280 | 281 | getch(); 282 | return 0; 283 | } 284 | -------------------------------------------------------------------------------- /0-10-in-Hindi(Devnagari)/hindiNumbers.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/0-10-in-Hindi(Devnagari)/hindiNumbers.exe -------------------------------------------------------------------------------- /0-10-in-Hindi(Devnagari)/hindiNumbers.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/0-10-in-Hindi(Devnagari)/hindiNumbers.o -------------------------------------------------------------------------------- /3D-rotation-frustrum/frustrumRotate.cpp: -------------------------------------------------------------------------------- 1 | //3D rotation along x-, y-, z-axis 2 | #include 3 | #include 4 | #define PI 3.1416 5 | #define COS(a) cos(((a) * PI) / (float)180) 6 | #define SIN(a) sin(((a) * PI) / (float)180) 7 | //COS and SIN uses angle a in radian 8 | 9 | using namespace std; 10 | 11 | int main() 12 | { 13 | initwindow(700, 480); 14 | //vertices of frustrum 15 | int p[4][8] = { {-40, 40, 40, -40, -20, 20, 20, -20}, 16 | {40, 40, -40, -40, 20, 20, -20, -20}, 17 | {0, 0, 0, 0, 60, 60, 60, 60}, 18 | {1, 1, 1, 1, 1, 1, 1, 1} }; 19 | 20 | int i, j, k, page = 0; 21 | int ang = 0; 22 | 23 | while(1) 24 | { 25 | settextstyle(11, HORIZ_DIR, 1); setcolor(LIGHTGREEN); 26 | outtextxy(200, 50, "Press 'a' for \"CW Rotation\" and 'd' for \"CCW Rotation\""); 27 | 28 | //z rotation matrix 29 | float rotz[4][4] = { COS(ang), -SIN(ang), 0, 0, 30 | SIN(ang), COS(ang), 0, 0, 31 | 0, 0, 1, 0, 32 | 0, 0, 0, 1 }; 33 | 34 | float pts[4][8]; 35 | for (i = 0; i <= 3; i++) { 36 | for (j = 0; j <= 8; j++) { 37 | int sum = 0; 38 | for (k = 0; k <= 3; k++) { 39 | sum += rotz[i][k] * p[k][j]; 40 | } 41 | pts[i][j] = sum; 42 | } 43 | } 44 | 45 | //due to symmetry of the surfaces, first front surface is drawn then back surface is drawn 46 | //and finally the edges connecting the front and the back surfaces 47 | 48 | //top view - we are drawing in xy plane 49 | //z coordinate is set to 0 or not taken 50 | setcolor(LIGHTRED); 51 | for(i = 0; i < 4; i++) { 52 | line(140 + pts[0][i], 240 - pts[1][i], 140 + pts[0][(i+1)%4], 240 - pts[1][(i+1)%4]); 53 | line(140 + pts[0][i+4], 240 - pts[1][i+4], 140 + pts[0][(i+1)%4+4], 240 - pts[1][(i+1)%4+4]); 54 | line(140 + pts[0][i], 240 - pts[1][i], 140 + pts[0][i+4], 240 - pts[1][i+4]); 55 | } 56 | 57 | //front view - we are drawing in xz plane 58 | //y coordinate is set to 0 or not taken 59 | for(i = 0; i < 4; i++) { 60 | line(350 + pts[0][i], 260 - pts[2][i], 350 + pts[0][(i+1)%4], 260 - pts[2][(i+1)%4]); 61 | line(350 + pts[0][i+4], 260 - pts[2][i+4], 350 + pts[0][(i+1)%4+4], 260 - pts[2][(i+1)%4+4]); 62 | line(350 + pts[0][i], 260 - pts[2][i], 350 + pts[0][i+4], 260 - pts[2][i+4]); 63 | } 64 | 65 | //side view - we are drawing in yz plane 66 | //x coordinate is set to 0 or not taken 67 | for(i = 0; i < 4; i++) { 68 | line(530 + pts[2][i], 240 - pts[1][i], 530 + pts[2][(i+1)%4], 240 - pts[1][(i+1)%4]); 69 | line(530 + pts[2][i+4], 240 - pts[1][i+4], 530 + pts[2][(i+1)%4+4], 240 - pts[1][(i+1)%4+4]); 70 | line(530 + pts[2][i], 240 - pts[1][i], 530 + pts[2][i+4], 240 - pts[1][i+4]); 71 | } 72 | 73 | char ch = getch(); ch = tolower(ch); 74 | if (ch == 'd') { 75 | ang += 2; 76 | } 77 | else if (ch == 'a') { 78 | ang -= 2; 79 | } 80 | 81 | setactivepage(page); setvisualpage(1 - page); 82 | page = 1 - page; cleardevice(); 83 | } 84 | 85 | getch(); 86 | return 0; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /3D-rotation-frustrum/frustrumRotate.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/3D-rotation-frustrum/frustrumRotate.exe -------------------------------------------------------------------------------- /3D-rotation-frustrum/frustrumRotate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/3D-rotation-frustrum/frustrumRotate.o -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ComputerGraphics_Programs 2 | It contains interesting graphics programs, implemented using graphics.h in C++ 3 | 4 | #### First graphics.h must be set up in your system (code::blocks recommended)! 5 | 6 | Follow these steps: 7 | 1. Uninstall the previous code::blocks 8 | 2. Also don't forget to delete the CodeBlocks folder from appdata ("C:\Users\username\AppData\Roaming\CodeBlocks") 9 | 3. Install the new code::blocks from the link https://sourceforge.net/projects/codeblocks/?source=top3_dlp_t5 (updated) 10 | 4. Download graphics.h files from [here](http://www.mediafire.com/file/kmkx12ikip1rmft/C%2B%2B_Graphics.rar) 11 | 5. Copy 'graphics.h' & 'winbgim.h' to the folder "C:\Program Files (x86)\CodeBlocks\MinGW\include" and 'libbgi.a' to "C:\Program Files (x86)\CodeBlocks\MinGW\lib" 12 | 6. Open CodeBlocks : Settings -> Compiler 13 | 14 | #### Compiler settings tab 15 | 16 | a. Enable c++ 11 17 | 18 | #### Linker settings tab 19 | 20 | a. Add "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libbgi.a" in 'Link libraries' 21 | 22 | b. Add in 'Other Linker Options': 23 | 24 | -lbgi 25 | 26 | -lgdi32 27 | 28 | -lcomdlg32 29 | 30 | -luuid 31 | 32 | -loleaut32 33 | 34 | -lole32 35 | 7. Press OK to exit 36 | 8. Now graphics.h can work with latest c++11 features also 37 | -------------------------------------------------------------------------------- /circle-clipping/circleClipping.cpp: -------------------------------------------------------------------------------- 1 | //clipping of circle is basically a case of either accept or reject on the basis of some condition (here whether it lies inside the clipping window or not) 2 | //the checking condition is done while using the putpixel() function 3 | //the clipping polygon must be convex 4 | #include 5 | #include 6 | #define MAX 10 7 | 8 | using namespace std; 9 | 10 | //x and y coordinate 11 | typedef struct{ 12 | int x; 13 | int y; 14 | }coord; 15 | 16 | //global clipping window 17 | //the reason to declare it here is we are modifying the circle (mid-point) algorithm which is using the checkInside function 18 | //and that needs the vertex of the clipping window 19 | const int n = 6; //no. of vertices/edges 20 | coord vertex[n] = {{400, 350}, {650, 250}, {1000, 350}, {950, 650}, {700, 750}, {480, 650}}; 21 | //coord vertex[n] = {{500, 550}, {600, 350}, {700, 550}}; n = 4 22 | 23 | 24 | bool checkInside(int x, int y) 25 | { 26 | bool check = true; 27 | for (int i = 0; i < n; ++i){ 28 | int dy = vertex[(i+1)%n].y - vertex[i].y; //y2 - y1 29 | int dx = vertex[(i+1)%n].x - vertex[i].x; //x2 - x1 30 | //the coefficients of the line equation 31 | int a = dy; 32 | int b = -dx; 33 | long int c = vertex[i].y * dx - vertex[i].x * dy; //y1*dx - x1*dy 34 | 35 | int x1 = vertex[(i+2)%n].x, y1 = vertex[(i+2)%n].y; //an inside point for the edge equation 36 | long long int eq1 = a*x + b*y + c; //f(x, y) = ax + bx + c; 37 | long long int eq2 = a*x1 + b*y1 + c; //f(x1, y1) = ax1, yb1 + c; 38 | 39 | //point should be inside w.r.t all the edges, if false for any edge then it doesn't lie inside the polygon 40 | if ((eq1 * eq2) < 0) { 41 | return false; 42 | } 43 | } 44 | 45 | //point lies inside the figure 46 | return true; 47 | } 48 | 49 | //for plotting 8 different points of circle using 8-symmetry 50 | void pixel(int xc, int yc, int x, int y, int color, int outColor) 51 | { 52 | //if the pixel lies inside the clipping window then color it with "color" 53 | //otherwise color it with "outColor" 54 | if (checkInside(xc + x, yc + y)) 55 | putpixel(xc + x, yc + y, color); 56 | else 57 | putpixel(xc + x, yc + y, outColor); 58 | 59 | if (checkInside(xc + y, yc + x)) 60 | putpixel(xc + y, yc + x, color); 61 | else 62 | putpixel(xc + y, yc + x, outColor); 63 | 64 | if (checkInside(xc - y, yc + x)) 65 | putpixel(xc - y, yc + x, color); 66 | else 67 | putpixel(xc - y, yc + x, outColor); 68 | 69 | if (checkInside(xc - x, yc + y)) 70 | putpixel(xc - x, yc + y, color); 71 | else 72 | putpixel(xc - x, yc + y, outColor); 73 | 74 | if (checkInside(xc - x, yc - y)) 75 | putpixel(xc - x, yc - y, color); 76 | else 77 | putpixel(xc - x, yc - y, outColor); 78 | 79 | if (checkInside(xc - y, yc - x)) 80 | putpixel(xc - y, yc - x, color); 81 | else 82 | putpixel(xc - y, yc - x, outColor); 83 | 84 | if (checkInside(xc + y, yc - x)) 85 | putpixel(xc + y, yc - x, color); 86 | else 87 | putpixel(xc + y, yc - x, outColor); 88 | 89 | if (checkInside(xc + x, yc - y)) 90 | putpixel(xc + x, yc - y, color); 91 | else 92 | putpixel(xc + x, yc - y, outColor); 93 | } 94 | 95 | //for drawing circle using mid-point algorithm 96 | void circle_mpt(int xc, int yc, int r, int color1 = WHITE, int color2 = WHITE) 97 | { 98 | int x = 0, y = r, d = 1 - r; 99 | pixel(xc, yc, x, y, color1, color2); 100 | 101 | while (x < y) { 102 | if (d < 0) { 103 | x++; 104 | d += (2 * x) + 3; 105 | } 106 | else { 107 | x++; 108 | y--; 109 | d += 2 * (x - y) + 5; 110 | } 111 | pixel(xc, yc, x, y, color1, color2); 112 | } 113 | } 114 | 115 | int main() 116 | { 117 | initwindow(1400, 900); 118 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 119 | POINT cursorPos; 120 | 121 | while(!kbhit()) { 122 | settextstyle(10, HORIZ_DIR, 1); setcolor(LIGHTBLUE); 123 | outtextxy(xc - 60, yc - 350, "Hover the Cursor!!"); 124 | GetCursorPos(&cursorPos); 125 | 126 | //drawing the clipping polygon (it must be convex) 127 | setcolor(LIGHTRED); 128 | for (int i = 0; i < n; ++i) 129 | line(vertex[i].x, vertex[i].y, vertex[(i+1)%n].x, vertex[(i+1)%n].y); 130 | 131 | //drawing the circle which also includes clipping 132 | circle_mpt(cursorPos.x, cursorPos.y, 100, YELLOW, LIGHTGREEN); 133 | circle_mpt(cursorPos.x, cursorPos.y, 99, YELLOW, LIGHTGREEN); 134 | 135 | setactivepage(page); setvisualpage(1 - page); page = 1 - page; 136 | cleardevice(); 137 | } 138 | 139 | getch(); 140 | return 0; 141 | } 142 | -------------------------------------------------------------------------------- /circle-clipping/circleClipping.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/circle-clipping/circleClipping.exe -------------------------------------------------------------------------------- /circle-clipping/circleClipping.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/circle-clipping/circleClipping.o -------------------------------------------------------------------------------- /curve-hiding(clipping)-with-shapes/curveHiding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 10 4 | #define PI 3.1416 5 | #define COS(a) cos(((a) * PI) / (float)180) 6 | #define SIN(a) sin(((a) * PI) / (float)180) 7 | 8 | using namespace std; 9 | 10 | //x- and y- coordinate 11 | typedef struct{ 12 | int x; 13 | int y; 14 | }coord; 15 | 16 | //Test condition of point inside a given convex polygon 17 | bool checkInside(coord vertex[MAX], int n, int x, int y) 18 | { 19 | bool check = true; 20 | for (int i = 0; i < n; ++i){ 21 | int dy = vertex[(i+1)%n].y - vertex[i].y; //y2 - y1 22 | int dx = vertex[(i+1)%n].x - vertex[i].x; //x2 - x1 23 | int a = dy; 24 | int b = -dx; 25 | long long int c = vertex[i].y * dx - vertex[i].x * dy; //y1*dx - x1*dy 26 | 27 | int x1 = vertex[(i+2)%n].x, y1 = vertex[(i+2)%n].y; //an inside point for the edge equation 28 | long long int eq1 = a*x + b*y + c; //f(x, y) = ax + bx + c; 29 | long long int eq2 = a*x1 + b*y1 + c; //f(x1, y1) = ax1, yb1 + c; 30 | 31 | //point should be inside w.r.t all the edges if false for any edge then it doesn't lie inside 32 | if ((eq1 * eq2) < 0) { 33 | return false; 34 | } 35 | } 36 | 37 | //point lies inside the figure 38 | return true; 39 | } 40 | 41 | //Checking whether point lies inside the circle or not 42 | bool insideCircle(int x, int y, int xc, int yc, int r) 43 | { 44 | //using circle equation, we have f(x,y) = (x-h)^2 + (y-k)^2 - r^2 45 | long long sum = 0; 46 | sum = pow((x-xc), 2) + pow((y-yc), 2) - pow(r, 2); 47 | if (sum < 0) 48 | return true; 49 | else 50 | return false; 51 | } 52 | 53 | int main() 54 | { 55 | initwindow(1400, 900); 56 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 57 | 58 | int crv = 6; //no. of curves 59 | //each curve is stored as 4 control points P1, P2, P3, P4 column wise, and each column as [x y z 1] 60 | //oop curve 61 | int curve[4][4*crv] = { {-80,-40,-40,-80, -80,-120,-120,-80, 0,-40,-40,0, 0,40,40,0, 50,50,50,50, 50,120,120,50}, 62 | {40,40,-40,-40, 40,40,-40,-40, 40,40,-40,-40, 40,40,-40,-40, 40,40,-100,-100, 40,60,-60,-40}, 63 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 64 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 65 | 66 | POINT cursorPos; 67 | //initially setting the cursor position to the mid-point of the screen 68 | cursorPos = {xc, yc}; 69 | 70 | long long int a = 0; 71 | while(!kbhit()) { 72 | settextstyle(10, HORIZ_DIR, 1); setcolor(YELLOW); 73 | outtextxy(xc - 220, yc - 300, "Hold Left-Shift key and Hover the Cursor!!"); 74 | 75 | //Update the cursor position only if Left Shift key is pressed 76 | if (GetKeyState(VK_SHIFT) & 0x8000) 77 | GetCursorPos(&cursorPos); 78 | 79 | setcolor(LIGHTGREEN); 80 | //storing the cursor position 81 | int curx = cursorPos.x, cury = cursorPos.y; 82 | 83 | //outer circle 84 | circle(curx, cury, 200); circle(curx, cury, 201); 85 | //d is the distance from the centre of the outer circle, the three shapes are drawn 86 | int d = 60; 87 | 88 | //coordinates of rectangle 89 | coord rect[4] = { {curx + d*COS(a-47), cury - d*SIN(a-47)}, 90 | {curx + d*COS(a+47), cury - d*SIN(a+47)}, 91 | {curx + (d+85)*COS(a+19), cury - (d+85)*SIN(a+19)}, 92 | {curx + (d+85)*COS(a-19), cury - (d+85)*SIN(a-19)} }; 93 | 94 | //coordinates of triangle 95 | coord tri[3] = { {curx + d*COS(a+120-47), cury - d*SIN(a+120-47)}, 96 | {curx + d*COS(a+120+47), cury - d*SIN(a+120+47)}, 97 | {curx + (d+85)*COS(a+120), cury - (d+85)*SIN(a+120)} }; 98 | 99 | //coordinates of inside circle 100 | coord cir = {curx +(d+25)*COS(a+240), cury -(d+25)*SIN(a+240)}; 101 | 102 | setcolor(LIGHTBLUE); 103 | //drawing the rectangle 104 | for(int i = 0; i < 4; ++i) 105 | line(rect[i].x, rect[i].y, rect[(i+1)%4].x, rect[(i+1)%4].y); 106 | 107 | //drawing the triangle 108 | for(int i = 0; i < 3; ++i) 109 | line(tri[i].x, tri[i].y, tri[(i+1)%3].x, tri[(i+1)%3].y); 110 | 111 | //drawing the circle 112 | circle(cir.x, cir.y, 50); 113 | 114 | for (int col = 0; col < 4 * crv; col +=4) { 115 | for (float t = 0; t <= 1; t += 0.001) { 116 | float x, y; 117 | x = pow(1-t, 3) * curve[0][col] + 3 * pow(1-t, 2) * t * curve[0][col+1] + 3 * (1-t) * t * t * curve[0][col+2] + pow(t, 3) * curve[0][col+3]; 118 | y = pow(1-t, 3) * curve[1][col] + 3 * pow(1-t, 2) * t * curve[1][col+1] + 3 * (1-t) * t * t * curve[1][col+2] + pow(t, 3) * curve[1][col+3]; 119 | //clipping is done when the point lies outside the inside rectangle, triangle or circle 120 | //only then they are not displayed 121 | if (checkInside(rect, 4, xc + x, yc - y) or checkInside(tri, 3, xc + x, yc - y) or insideCircle(xc + x, yc - y, cir.x, cir.y, 50) or !insideCircle(xc + x, yc - y, curx, cury, 200)) 122 | putpixel(xc + x, yc - y, LIGHTRED); 123 | } 124 | } 125 | 126 | a--; 127 | delay(10); setactivepage(page); setvisualpage(1-page); 128 | page = 1 - page; cleardevice(); 129 | } 130 | 131 | getch(); 132 | return 0; 133 | } 134 | -------------------------------------------------------------------------------- /curve-hiding(clipping)-with-shapes/curveHiding.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/curve-hiding(clipping)-with-shapes/curveHiding.exe -------------------------------------------------------------------------------- /curve-hiding(clipping)-with-shapes/curveHiding.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/curve-hiding(clipping)-with-shapes/curveHiding.o -------------------------------------------------------------------------------- /cyrus-beck-line-clipping/cyrusBeck.cpp: -------------------------------------------------------------------------------- 1 | //Cyrus Beck algorithm for line clipping, it can handle any polygon : convex or concave 2 | #include 3 | #include 4 | #define MAX 100 5 | #define ff first 6 | #define ss second 7 | 8 | using namespace std; 9 | 10 | //for x and y coordinate 11 | typedef struct{ 12 | int x; 13 | int y; 14 | }coord; 15 | 16 | //used in Normal vector calculation (using CW rotation) 17 | // Edge Normal 18 | // + + - + 19 | // + - + + 20 | // - - + - 21 | // - + - - 22 | void calcSign(int &x, int &y) 23 | { 24 | //using CW wise rotation 25 | if ((x >= 0 and y >= 0) or (x < 0 and y < 0)) 26 | x *= -1; 27 | else 28 | y *= -1; 29 | } 30 | 31 | //returns dot product of two vectors 32 | int dotprod(coord a, coord b) 33 | { 34 | return (a.x * b.x) + (a.y * b.y); 35 | } 36 | 37 | //returns t parameter 38 | float tfunc(coord e1, coord e2, coord l1, coord l2) 39 | { 40 | coord edge = {e2.x - e1.x, e2.y - e1.y}; 41 | //calculating the sign of x and y, 42 | int xsig = (edge.x >= 0) ? 1 : -1; 43 | int ysig = (edge.y >= 0) ? 1 : -1; 44 | //changing it according to the rule for Normal 45 | calcSign(xsig, ysig); 46 | coord norm = {xsig * abs(edge.y), ysig * abs(edge.x)}; 47 | int Nr = dotprod(norm, {l1.x - e1.x, l1.y - e1.y}); // N.(p1 - Ei) 48 | int Dr = - dotprod(norm, {l2.x - l1.x, l2.y - l1.y}); //-N.(p2 - p1) 49 | 50 | float t; 51 | if (Dr != 0) { 52 | t = (float)Nr / (float)Dr; 53 | return t; 54 | } 55 | else 56 | return 10; 57 | } 58 | 59 | //t parameter should lie between 0 and 1 60 | //and it is calculated from both the reference that is edge and line and then edge as line and line as edge 61 | //if they intersect then t value should be between 0 and 1 w.r.t both of them 62 | void cyrusBeck(coord vertex[MAX], int n, coord p1, coord p2, vector &tval) 63 | { 64 | for (int i = 0; i < n; ++i){ 65 | float t1 = tfunc(vertex[i], vertex[(i+1)%n], p1, p2); 66 | float t2 = tfunc(p2, p1, vertex[i], vertex[(i+1)%n]); 67 | //cout << t1 << " " << t2 << "\n\n"; 68 | if (t1 >= 0 and t1 <= 1){ 69 | if (t2 >= 0 and t2 <= 1) 70 | tval.push_back(t1); 71 | } 72 | } 73 | if (tval.size() & 1) 74 | tval.push_back(1); 75 | 76 | sort(tval.begin(), tval.end()); 77 | } 78 | 79 | int main() 80 | { 81 | initwindow(1400, 900); 82 | int xc = getmaxx() / 2, yc = getmaxy() / 2; 83 | POINT cursorPos; 84 | cursorPos = {xc - 200, yc + 300}; 85 | //polygon vertices 86 | coord vertex[MAX] = {{xc - 200, yc - 100}, {xc + 300, yc - 30}, {xc + 50, yc}, {xc + 200, yc + 150}, {xc, yc + 20}, {xc + 10, yc + 200}}; 87 | //1st point of line 88 | coord p1 = {xc + 400, yc - 200}; 89 | int page = 0; 90 | 91 | while(!kbhit()){ 92 | settextstyle(10, HORIZ_DIR, 1); setcolor(YELLOW); 93 | outtextxy(xc - 200, yc - 350, "Hold the Left-Shift key and Hover the Cursor!!"); 94 | 95 | //update the P2 coordinate only if Shift key is pressed 96 | if(GetKeyState(VK_SHIFT) & 0x800) 97 | GetCursorPos(&cursorPos); 98 | coord p2 = {cursorPos.x, cursorPos.y}; 99 | 100 | //for storing the t parameters 101 | vector t; 102 | //n is the total no. of sides/vertices of the polygon 103 | int n = 6; 104 | cyrusBeck(vertex, n, p1, p2, t); 105 | 106 | // cout << "T-values : "; 107 | // for (auto i = t.begin(); i != t.end(); ++i) 108 | // cout << *i << " "; 109 | 110 | setcolor(RED); 111 | //Drawing the edges of polygon 112 | for (int i = 0; i < n; ++i) 113 | line(vertex[i].x, vertex[i].y, vertex[(i+1)%n].x, vertex[(i+1)%n].y); 114 | 115 | setcolor(BLUE); 116 | line(p1.x, p1.y, p2.x, p2.y); 117 | 118 | setcolor(LIGHTGREEN); 119 | //drawing the clipped line (i.e over-drawing) 120 | for (auto i = t.begin(); i != t.end(); ++i){ 121 | coord pt1, pt2; 122 | pt1.x = p1.x + (p2.x - p1.x) * (*i); 123 | pt1.y = p1.y + (p2.y - p1.y) * (*i); 124 | ++i; 125 | pt2.x = p1.x + (p2.x - p1.x) * (*i); 126 | pt2.y = p1.y + (p2.y - p1.y) * (*i); 127 | line(pt1.x, pt1.y, pt2.x, pt2.y); 128 | } 129 | t.clear(); 130 | 131 | setactivepage(page); setvisualpage(1 - page); 132 | cleardevice(); page = 1 - page; 133 | } 134 | 135 | getch(); 136 | return 0; 137 | } 138 | -------------------------------------------------------------------------------- /cyrus-beck-line-clipping/cyrusBeck.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/cyrus-beck-line-clipping/cyrusBeck.exe -------------------------------------------------------------------------------- /cyrus-beck-line-clipping/cyrusBeck.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/cyrus-beck-line-clipping/cyrusBeck.o -------------------------------------------------------------------------------- /display-current-time-in-hindi/currentTimeHindi.cpp: -------------------------------------------------------------------------------- 1 | //Displays current time of your system using Hindi nos. from 0 - 10 2 | //Time is in 24-hours format 3 | #include 4 | #include 5 | #define MAX 20 6 | #define PI 3.1416 7 | #define COS(a) cos(((a) * PI) / (float)180) 8 | #define SIN(a) sin(((a) * PI) / (float)180) 9 | #define deg(a) ((float)a * 180) / PI 10 | 11 | using namespace std; 12 | 13 | const int thick = 5; 14 | 15 | //Bezier curve is stored in the form of matrix each containing 4 rows and a set of controlling points P1, P2, P3, and P4 for each curve as columns 16 | //each controlling point is stored as [x y z 1] column wise, so that matrix calculation can be carried out for 2D or 3D transformation 17 | 18 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 19 | 20 | //0 21 | void drawHindi0(int xc, int yc, int pixcolor){ 22 | const int crv0 = 2; 23 | int hindi0[4][4*crv0] = { {0,40,40,0, 0,-40,-40,0}, 24 | {30,30,-30,-30, 30,30,-30,-30}, 25 | {0,0,0,0, 0,0,0,0}, 26 | {1,1,1,1, 1,1,1,1} }; 27 | 28 | for (int disp = 0; disp < thick; ++disp){ 29 | for (int col = 0; col < 4 * crv0; col +=4) { 30 | for (float t = 0; t <= 1; t += 0.001) { 31 | float x, y; 32 | x = pow(1-t, 3) * hindi0[0][col] + 3 * pow(1-t, 2) * t * hindi0[0][col+1] + 3 * (1-t) * t * t * hindi0[0][col+2] + pow(t, 3) * hindi0[0][col+3]; 33 | y = pow(1-t, 3) * hindi0[1][col] + 3 * pow(1-t, 2) * t * hindi0[1][col+1] + 3 * (1-t) * t * t * hindi0[1][col+2] + pow(t, 3) * hindi0[1][col+3]; 34 | putpixel(xc + disp + x, yc - y, pixcolor); 35 | } 36 | } 37 | } 38 | } 39 | 40 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 41 | 42 | 43 | //1 44 | void drawHindi1(int xc, int yc, int pixcolor){ 45 | const int crv1 = 2; 46 | int hindi1[4][4*crv1] = { {-5,-90,80,0, 0,-60,50,0}, 47 | {25,70,100,20, 20,-25,-40,-70}, 48 | {0,0,0,0, 0,0,0,0}, 49 | {1,1,1,1, 1,1,1,1} }; 50 | 51 | //for drawing 1 52 | for (int disp = 0; disp < thick; ++disp){ 53 | for (int col = 0; col < 4 * crv1; col +=4) { 54 | for (float t = 0; t <= 1; t += 0.001) { 55 | float x, y; 56 | x = pow(1-t, 3) * hindi1[0][col] + 3 * pow(1-t, 2) * t * hindi1[0][col+1] + 3 * (1-t) * t * t * hindi1[0][col+2] + pow(t, 3) * hindi1[0][col+3]; 57 | y = pow(1-t, 3) * hindi1[1][col] + 3 * pow(1-t, 2) * t * hindi1[1][col+1] + 3 * (1-t) * t * t * hindi1[1][col+2] + pow(t, 3) * hindi1[1][col+3]; 58 | putpixel(xc + disp + x, yc - y, pixcolor); 59 | } 60 | } 61 | } 62 | } 63 | 64 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 65 | 66 | 67 | //2 68 | void drawHindi2(int xc, int yc, int pixcolor){ 69 | const int crv2 = 2; 70 | int hindi2[4][4*crv2] = { {-40,60,60,-30, -30,-60,10,30}, 71 | {55,110,-60,-20, -20,20,-10,-70}, 72 | {0,0,0,0, 0,0,0,0}, 73 | {1,1,1,1, 1,1,1,1} }; 74 | 75 | //for drawing 2 76 | for (int disp = 0; disp < thick; ++disp){ 77 | for (int col = 0; col < 4 * crv2; col +=4) { 78 | for (float t = 0; t <= 1; t += 0.001) { 79 | float x, y; 80 | x = pow(1-t, 3) * hindi2[0][col] + 3 * pow(1-t, 2) * t * hindi2[0][col+1] + 3 * (1-t) * t * t * hindi2[0][col+2] + pow(t, 3) * hindi2[0][col+3]; 81 | y = pow(1-t, 3) * hindi2[1][col] + 3 * pow(1-t, 2) * t * hindi2[1][col+1] + 3 * (1-t) * t * t * hindi2[1][col+2] + pow(t, 3) * hindi2[1][col+3]; 82 | putpixel(xc + disp + x, yc - y, pixcolor); 83 | } 84 | } 85 | } 86 | } 87 | 88 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 89 | 90 | 91 | //3 92 | void drawHindi3(int xc, int yc, int pixcolor){ 93 | const int crv3 = 3; 94 | int hindi3[4][4*crv3] = { {-25,55,55,-5, -5,55,50,-30, -30,-40,0,10}, 95 | {60,100,-10,5, 5,10,-90,-50, -50,-20,-40,-80}, 96 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 97 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 98 | 99 | //for drawing 3 100 | for (int disp = 0; disp < thick; ++disp){ 101 | for (int col = 0; col < 4 * crv3; col +=4) { 102 | for (float t = 0; t <= 1; t += 0.001) { 103 | float x, y; 104 | x = pow(1-t, 3) * hindi3[0][col] + 3 * pow(1-t, 2) * t * hindi3[0][col+1] + 3 * (1-t) * t * t * hindi3[0][col+2] + pow(t, 3) * hindi3[0][col+3]; 105 | y = pow(1-t, 3) * hindi3[1][col] + 3 * pow(1-t, 2) * t * hindi3[1][col+1] + 3 * (1-t) * t * t * hindi3[1][col+2] + pow(t, 3) * hindi3[1][col+3]; 106 | putpixel(xc + disp + x, yc - y, pixcolor); 107 | } 108 | } 109 | } 110 | } 111 | 112 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 113 | 114 | 115 | //4 116 | void drawHindi4(int xc, int yc, int pixcolor){ 117 | const int crv4 = 4; 118 | int hindi4[4][4*crv4] = { {0,40,40,40, 0,-40,-40,-40, 0,-30,-30,0, 0,30,30,0}, 119 | {-5,35,45,70, -5,35,45,70, -5,-35,-65,-70, -5,-35,-65,-70}, 120 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 121 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 122 | 123 | //for drawing 4 124 | for (int disp = 0; disp < thick; ++disp){ 125 | for (int col = 0; col < 4 * crv4; col +=4) { 126 | for (float t = 0; t <= 1; t += 0.001) { 127 | float x, y; 128 | x = pow(1-t, 3) * hindi4[0][col] + 3 * pow(1-t, 2) * t * hindi4[0][col+1] + 3 * (1-t) * t * t * hindi4[0][col+2] + pow(t, 3) * hindi4[0][col+3]; 129 | y = pow(1-t, 3) * hindi4[1][col] + 3 * pow(1-t, 2) * t * hindi4[1][col+1] + 3 * (1-t) * t * t * hindi4[1][col+2] + pow(t, 3) * hindi4[1][col+3]; 130 | putpixel(xc + disp + x, yc - y, pixcolor); 131 | } 132 | } 133 | } 134 | } 135 | 136 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 137 | 138 | 139 | //5 140 | void drawHindi5(int xc, int yc, int pixcolor){ 141 | const int crv5 = 3; 142 | int hindi5[4][4*crv5] = { {-40,-50,40,30, 30,30,40,-20, -20,-30,-10,50}, 143 | {70,-30,-20,70, 70,70,-100,-50, -50,-40,-30,-70}, 144 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 145 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 146 | 147 | //for drawing 5 148 | for (int disp = 0; disp < thick; ++disp){ 149 | for (int col = 0; col < 4 * crv5; col +=4) { 150 | for (float t = 0; t <= 1; t += 0.001) { 151 | float x, y; 152 | x = pow(1-t, 3) * hindi5[0][col] + 3 * pow(1-t, 2) * t * hindi5[0][col+1] + 3 * (1-t) * t * t * hindi5[0][col+2] + pow(t, 3) * hindi5[0][col+3]; 153 | y = pow(1-t, 3) * hindi5[1][col] + 3 * pow(1-t, 2) * t * hindi5[1][col+1] + 3 * (1-t) * t * t * hindi5[1][col+2] + pow(t, 3) * hindi5[1][col+3]; 154 | putpixel(xc + disp + x, yc - y, pixcolor); 155 | } 156 | } 157 | } 158 | } 159 | 160 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 161 | 162 | 163 | //6 164 | void drawHindi6(int xc, int yc, int pixcolor){ 165 | const int crv6 = 3; 166 | int hindi6[4][4*crv6] = { {20,-60,-60,0, 0,-60,-55,30, 30,50,-10,40}, 167 | {60,100,-10,5, 5,10,-90,-50, -50,-10,-30,-80}, 168 | {0,0,0,0, 0,0,0,0, 0,0,0,0}, 169 | {1,1,1,1, 1,1,1,1, 1,1,1,1} }; 170 | 171 | //for drawing 6 172 | for (int disp = 0; disp < thick; ++disp){ 173 | for (int col = 0; col < 4 * crv6; col +=4) { 174 | for (float t = 0; t <= 1; t += 0.001) { 175 | float x, y; 176 | x = pow(1-t, 3) * hindi6[0][col] + 3 * pow(1-t, 2) * t * hindi6[0][col+1] + 3 * (1-t) * t * t * hindi6[0][col+2] + pow(t, 3) * hindi6[0][col+3]; 177 | y = pow(1-t, 3) * hindi6[1][col] + 3 * pow(1-t, 2) * t * hindi6[1][col+1] + 3 * (1-t) * t * t * hindi6[1][col+2] + pow(t, 3) * hindi6[1][col+3]; 178 | putpixel(xc + disp + x, yc - y, pixcolor); 179 | } 180 | } 181 | } 182 | } 183 | 184 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 185 | 186 | 187 | //7 188 | void drawHindi7(int xc, int yc, int pixcolor){ 189 | const int crv7 = 2; 190 | int hindi7[4][4*crv7] = { {-30,-80,80,40, 40,0,0,45}, 191 | {70,-100,-110,35, 35,90,-20,20}, 192 | {0,0,0,0, 0,0,0,0}, 193 | {1,1,1,1, 1,1,1,1} }; 194 | 195 | //for drawing 7 196 | for (int disp = 0; disp < thick; ++disp){ 197 | for (int col = 0; col < 4 * crv7; col +=4) { 198 | for (float t = 0; t <= 1; t += 0.001) { 199 | float x, y; 200 | x = pow(1-t, 3) * hindi7[0][col] + 3 * pow(1-t, 2) * t * hindi7[0][col+1] + 3 * (1-t) * t * t * hindi7[0][col+2] + pow(t, 3) * hindi7[0][col+3]; 201 | y = pow(1-t, 3) * hindi7[1][col] + 3 * pow(1-t, 2) * t * hindi7[1][col+1] + 3 * (1-t) * t * t * hindi7[1][col+2] + pow(t, 3) * hindi7[1][col+3]; 202 | putpixel(xc + disp + x, yc - y, pixcolor); 203 | } 204 | } 205 | } 206 | } 207 | 208 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 209 | 210 | 211 | //8 212 | void drawHindi8(int xc, int yc, int pixcolor){ 213 | const int crv8 = 1; 214 | int hindi8[4][4*crv8] = { {10,-70,-10,40}, 215 | {70,-50,-90,-30}, 216 | {0,0,0,0}, 217 | {1,1,1,1} }; 218 | 219 | //for drawing 8 220 | for (int disp = 0; disp < thick; ++disp){ 221 | for (int col = 0; col < 4 * crv8; col +=4) { 222 | for (float t = 0; t <= 1; t += 0.001) { 223 | float x, y; 224 | x = pow(1-t, 3) * hindi8[0][col] + 3 * pow(1-t, 2) * t * hindi8[0][col+1] + 3 * (1-t) * t * t * hindi8[0][col+2] + pow(t, 3) * hindi8[0][col+3]; 225 | y = pow(1-t, 3) * hindi8[1][col] + 3 * pow(1-t, 2) * t * hindi8[1][col+1] + 3 * (1-t) * t * t * hindi8[1][col+2] + pow(t, 3) * hindi8[1][col+3]; 226 | putpixel(xc + disp + x, yc - y, pixcolor); 227 | } 228 | } 229 | } 230 | } 231 | 232 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 233 | 234 | 235 | //9 236 | void drawHindi9(int xc, int yc, int pixcolor){ 237 | const int crv9 = 2; 238 | int hindi9[4][4*crv9] = { {5,100,-120,0, 0,60,50,20}, 239 | {15,90,90,10, 10,-35,-40,-70}, 240 | {0,0,0,0, 0,0,0,0}, 241 | {1,1,1,1, 1,1,1,1} }; 242 | 243 | //for drawing 9 244 | for (int disp = 0; disp < thick; ++disp){ 245 | for (int col = 0; col < 4 * crv9; col +=4) { 246 | for (float t = 0; t <= 1; t += 0.001) { 247 | float x, y; 248 | x = pow(1-t, 3) * hindi9[0][col] + 3 * pow(1-t, 2) * t * hindi9[0][col+1] + 3 * (1-t) * t * t * hindi9[0][col+2] + pow(t, 3) * hindi9[0][col+3]; 249 | y = pow(1-t, 3) * hindi9[1][col] + 3 * pow(1-t, 2) * t * hindi9[1][col+1] + 3 * (1-t) * t * t * hindi9[1][col+2] + pow(t, 3) * hindi9[1][col+3]; 250 | putpixel(xc + disp + x, yc - y, pixcolor); 251 | } 252 | } 253 | } 254 | } 255 | 256 | //------------------------------------------------------------------------------------------------------------------------------------------------------------- 257 | 258 | 259 | int main() 260 | { 261 | initwindow(1400, 900); 262 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 263 | time_t now; 264 | struct tm *now_tm; 265 | int hr, minutes, seconds; 266 | 267 | int pixcolor = LIGHTGREEN; 268 | 269 | //these are the displacement of each digit of hour, minute or second from center of the screen 270 | int hr1disp = -310, hr2disp = -210, min1disp = -57, min2disp = 57, sec1disp = 210, sec2disp = 310; 271 | int hr1, hr2, min1, min2, sec1, sec2; 272 | 273 | while(!kbhit()){ 274 | now = time(NULL); 275 | now_tm = localtime(&now); 276 | hr = now_tm -> tm_hour; minutes = now_tm -> tm_min; seconds = now_tm -> tm_sec; 277 | 278 | hr2 = hr % 10; hr /= 10; hr1 = hr % 10; //stores the digits of hour 279 | min2 = minutes % 10; minutes /= 10; min1 = minutes % 10; //stores the digits of minute 280 | sec2 = seconds % 10; seconds /= 10; sec1 = seconds % 10; //stores the digits of second 281 | 282 | 283 | setcolor(LIGHTBLUE); 284 | setlinestyle(DASHED_LINE, 0xFFFF, 1); rectangle(xc - 390, yc - 110, xc + 390 , yc + 110); 285 | setlinestyle(SOLID_LINE, 0xFFFF, 5); rectangle(xc - 400, yc - 120, xc + 400 , yc + 120); 286 | 287 | //drawing the separating dots 288 | setlinestyle(SOLID_LINE, 0xFFFF, 0); setcolor(YELLOW); setfillstyle(SOLID_FILL, YELLOW); 289 | circle(xc - 130, yc - 20, 10); circle(xc - 130, yc + 20, 10); floodfill(xc - 130, yc - 20, YELLOW); floodfill(xc - 130, yc + 20, YELLOW); 290 | circle(xc + 130, yc - 20, 10); circle(xc + 130, yc + 20, 10); floodfill(xc + 130, yc - 20, YELLOW); floodfill(xc + 130, yc + 20, YELLOW); 291 | 292 | //displaying the 1st digit of the hour 293 | switch(hr1){ 294 | case 0 : drawHindi0(xc + hr1disp, yc, pixcolor); break; 295 | case 1 : drawHindi1(xc + hr1disp, yc, pixcolor); break; 296 | case 2 : drawHindi2(xc + hr1disp, yc, pixcolor); break; 297 | case 3 : drawHindi3(xc + hr1disp, yc, pixcolor); break; 298 | case 4 : drawHindi4(xc + hr1disp, yc, pixcolor); break; 299 | case 5 : drawHindi5(xc + hr1disp, yc, pixcolor); break; 300 | case 6 : drawHindi6(xc + hr1disp, yc, pixcolor); break; 301 | case 7 : drawHindi7(xc + hr1disp, yc, pixcolor); break; 302 | case 8 : drawHindi8(xc + hr1disp, yc, pixcolor); break; 303 | case 9 : drawHindi9(xc + hr1disp, yc, pixcolor); break; 304 | } 305 | 306 | //displaying the 2nd digit of the hour 307 | switch(hr2){ 308 | case 0 : drawHindi0(xc + hr2disp, yc, pixcolor); break; 309 | case 1 : drawHindi1(xc + hr2disp, yc, pixcolor); break; 310 | case 2 : drawHindi2(xc + hr2disp, yc, pixcolor); break; 311 | case 3 : drawHindi3(xc + hr2disp, yc, pixcolor); break; 312 | case 4 : drawHindi4(xc + hr2disp, yc, pixcolor); break; 313 | case 5 : drawHindi5(xc + hr2disp, yc, pixcolor); break; 314 | case 6 : drawHindi6(xc + hr2disp, yc, pixcolor); break; 315 | case 7 : drawHindi7(xc + hr2disp, yc, pixcolor); break; 316 | case 8 : drawHindi8(xc + hr2disp, yc, pixcolor); break; 317 | case 9 : drawHindi9(xc + hr2disp, yc, pixcolor); break; 318 | } 319 | 320 | //displaying the 1st digit of the minute 321 | switch(min1){ 322 | case 0 : drawHindi0(xc + min1disp, yc, pixcolor); break; 323 | case 1 : drawHindi1(xc + min1disp, yc, pixcolor); break; 324 | case 2 : drawHindi2(xc + min1disp, yc, pixcolor); break; 325 | case 3 : drawHindi3(xc + min1disp, yc, pixcolor); break; 326 | case 4 : drawHindi4(xc + min1disp, yc, pixcolor); break; 327 | case 5 : drawHindi5(xc + min1disp, yc, pixcolor); break; 328 | case 6 : drawHindi6(xc + min1disp, yc, pixcolor); break; 329 | case 7 : drawHindi7(xc + min1disp, yc, pixcolor); break; 330 | case 8 : drawHindi8(xc + min1disp, yc, pixcolor); break; 331 | case 9 : drawHindi9(xc + min1disp, yc, pixcolor); break; 332 | } 333 | 334 | //displaying the 2nd digit of the minute 335 | switch(min2){ 336 | case 0 : drawHindi0(xc + min2disp, yc, pixcolor); break; 337 | case 1 : drawHindi1(xc + min2disp, yc, pixcolor); break; 338 | case 2 : drawHindi2(xc + min2disp, yc, pixcolor); break; 339 | case 3 : drawHindi3(xc + min2disp, yc, pixcolor); break; 340 | case 4 : drawHindi4(xc + min2disp, yc, pixcolor); break; 341 | case 5 : drawHindi5(xc + min2disp, yc, pixcolor); break; 342 | case 6 : drawHindi6(xc + min2disp, yc, pixcolor); break; 343 | case 7 : drawHindi7(xc + min2disp, yc, pixcolor); break; 344 | case 8 : drawHindi8(xc + min2disp, yc, pixcolor); break; 345 | case 9 : drawHindi9(xc + min2disp, yc, pixcolor); break; 346 | } 347 | 348 | //displaying the 1st digit of the second 349 | switch(sec1){ 350 | case 0 : drawHindi0(xc + sec1disp, yc, pixcolor); break; 351 | case 1 : drawHindi1(xc + sec1disp, yc, pixcolor); break; 352 | case 2 : drawHindi2(xc + sec1disp, yc, pixcolor); break; 353 | case 3 : drawHindi3(xc + sec1disp, yc, pixcolor); break; 354 | case 4 : drawHindi4(xc + sec1disp, yc, pixcolor); break; 355 | case 5 : drawHindi5(xc + sec1disp, yc, pixcolor); break; 356 | case 6 : drawHindi6(xc + sec1disp, yc, pixcolor); break; 357 | case 7 : drawHindi7(xc + sec1disp, yc, pixcolor); break; 358 | case 8 : drawHindi8(xc + sec1disp, yc, pixcolor); break; 359 | case 9 : drawHindi9(xc + sec1disp, yc, pixcolor); break; 360 | } 361 | 362 | //displaying the 2nd digit of the second 363 | switch(sec2){ 364 | case 0 : drawHindi0(xc + sec2disp, yc, pixcolor); break; 365 | case 1 : drawHindi1(xc + sec2disp, yc, pixcolor); break; 366 | case 2 : drawHindi2(xc + sec2disp, yc, pixcolor); break; 367 | case 3 : drawHindi3(xc + sec2disp, yc, pixcolor); break; 368 | case 4 : drawHindi4(xc + sec2disp, yc, pixcolor); break; 369 | case 5 : drawHindi5(xc + sec2disp, yc, pixcolor); break; 370 | case 6 : drawHindi6(xc + sec2disp, yc, pixcolor); break; 371 | case 7 : drawHindi7(xc + sec2disp, yc, pixcolor); break; 372 | case 8 : drawHindi8(xc + sec2disp, yc, pixcolor); break; 373 | case 9 : drawHindi9(xc + sec2disp, yc, pixcolor); break; 374 | } 375 | 376 | delay(50); 377 | setactivepage(page); setvisualpage(1 - page); 1 - page; 378 | cleardevice(); 379 | } 380 | 381 | getch(); 382 | return 0; 383 | } 384 | -------------------------------------------------------------------------------- /display-current-time-in-hindi/currentTimeHindi.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/display-current-time-in-hindi/currentTimeHindi.exe -------------------------------------------------------------------------------- /display-current-time-in-hindi/currentTimeHindi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/display-current-time-in-hindi/currentTimeHindi.o -------------------------------------------------------------------------------- /draw-on-screen/drawOnScreen.cpp: -------------------------------------------------------------------------------- 1 | // Draw on Screen like paint. 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Press Left shift and move cursor to draw on the screen. 8 | // release shift key to stop drawing. 9 | 10 | int main() 11 | { 12 | initwindow(1000, 600); 13 | 14 | POINT P, L; 15 | 16 | while(!kbhit()){ 17 | L.x = P.x; 18 | L.y = P.y; 19 | if(GetCursorPos(&P) and (GetKeyState(VK_SHIFT) & 0x8000)){ 20 | 21 | line(L.x,L.y, P.x,P.y); 22 | } 23 | } 24 | getch(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /draw-on-screen/drawOnScreen.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/draw-on-screen/drawOnScreen.exe -------------------------------------------------------------------------------- /draw-on-screen/drawOnScreen.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/draw-on-screen/drawOnScreen.o -------------------------------------------------------------------------------- /hidden-surface/Reference figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/hidden-surface/Reference figure.png -------------------------------------------------------------------------------- /hidden-surface/hiddenSurface.cpp: -------------------------------------------------------------------------------- 1 | //Hidden surface implementation using Back-Face Detection 2 | #include 3 | #include 4 | #define MAX 10 5 | #define PI 3.1416 6 | #define COS(a) cos(((a) * PI) / (float)180) 7 | #define SIN(a) sin(((a) * PI) / (float)180) 8 | 9 | using namespace std; 10 | 11 | //x y z coordinate 12 | typedef struct{ 13 | long long int x; 14 | long long int y; 15 | long long int z; 16 | }coord; 17 | 18 | //Calculating normal vector using cross-product formula 19 | coord normalVector(coord edge1, coord edge2) 20 | { 21 | coord normal; 22 | normal.x = edge1.y * edge2.z - edge1.z * edge2.y; 23 | normal.y = edge1.z * edge2.x - edge1.x * edge2.z; 24 | normal.z = edge1.x * edge2.y - edge1.y * edge2.x; 25 | 26 | return normal; 27 | } 28 | 29 | //return dot product of two vectors 30 | long long int dotProduct(coord a, coord b) 31 | { 32 | return (a.x * b.x + a.y * b.y + a.z * b.z); 33 | } 34 | 35 | //setting the vision co-ordinate 36 | coord V = {700,500,1000}; 37 | 38 | //calculating whether the given surface is hidden or not using the Back-face detection technique 39 | bool hidden(coord sfc[MAX], int n) 40 | { 41 | //sfc array contains the surface vertices in order of cross-product 42 | //calculating the midpoint of the surface 43 | coord M = {0,0,0}; 44 | for(int i = 0; i < n; ++i) { 45 | M.x += sfc[i].x; 46 | M.y += sfc[i].y; 47 | M.z += sfc[i].z; 48 | } 49 | M.x /= n; 50 | M.y /= n; 51 | M.z /= n; 52 | 53 | //edge1 and edge2 for calculating normal vector 54 | coord edge1 = {sfc[1].x - sfc[0].x, sfc[1].y - sfc[0].y, sfc[1].z - sfc[0].z}; 55 | coord edge2 = {sfc[2].x - sfc[1].x, sfc[2].y - sfc[1].y, sfc[2].z - sfc[1].z}; 56 | 57 | coord N = normalVector(edge1, edge2); 58 | coord MV = {V.x - M.x, V.y - M.y, V.z - M.z}; 59 | 60 | long long int dp = dotProduct(MV, N); 61 | // cout << "dp : " << dp << "\n"; 62 | // cout << "Mid : " << M.x << " " << M.y << " " << M.z << "\n"; 63 | // cout << "MV : " << MV.x << " " << MV.y << " " << MV.z << "\n"; 64 | // cout << "Normal : " << N.x << " " << N.y << " " << N.z << "\n\n"; 65 | 66 | if (dp < 0) 67 | return false; //surface is not hidden 68 | else 69 | return true; //surface is hidden 70 | } 71 | 72 | int main() 73 | { 74 | initwindow(1400, 900); 75 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 76 | int s = 4, v = 3, crv = 5; 77 | int sz = 200; 78 | 79 | //here we have to use redundant vertices to ease the implementation in for loop 80 | //though we have only 4 vertices for tetrahedron 81 | 82 | //tetrahedron vertices stored as [x y z 1] in each column 83 | //S1 S2 S3 S4 84 | int vertex[4][s*v] = { {sz,0,0, sz,0,0, 0,0,0, 0,sz,0}, 85 | {0,0,sz, 0,0,0, sz,0,0, 0,0,sz}, 86 | {0,0,0, 0,sz,0, 0,0,sz, sz,0,0}, 87 | {1,1,1, 1,1,1, 1,1,1, 1,1,1} }; 88 | 89 | int ang = 0; 90 | 91 | while(1){ 92 | setcolor(LIGHTBLUE); settextstyle(10, HORIZ_DIR, 1); 93 | outtextxy(xc - 300, yc - 350, "Press 'a' for \"CW Rotation\" and 'd' for \"CCW Rotation\""); 94 | //x-rotation matrix 95 | float rotx[4][4] = { {1, 0, 0, 0}, 96 | {0, COS(ang), -SIN(ang), 0}, 97 | {0, SIN(ang), COS(ang), 0}, 98 | {0, 0, 0, 1} }; 99 | 100 | //y-rotation matrix 101 | float roty[4][4] = { {COS(ang), 0, SIN(ang), 0}, 102 | {0, 1, 0, 0}, 103 | {-SIN(ang), 0, COS(ang), 0}, 104 | {0, 0, 0, 1} }; 105 | 106 | //z-rotation matrix 107 | float rotz[4][4] = { {COS(ang), -SIN(ang), 0, 0}, 108 | {SIN(ang), COS(ang), 0, 0}, 109 | {0, 0, 1, 0}, 110 | {0, 0, 0, 1} }; 111 | 112 | 113 | char ch = getch(); ch = tolower(ch); 114 | switch(ch){ 115 | case 'a' : --ang; break; 116 | case 'd' : ++ang; break; 117 | } 118 | 119 | //multiplying the vertex with the y-rotation matrix 120 | float pts[4][s*v]; 121 | for (int i = 0; i < 4; i++) { 122 | for (int j = 0; j < s * v; j++) { 123 | int sum = 0; 124 | for (int k = 0; k < 4; k++) 125 | sum += roty[i][k] * vertex[k][j]; 126 | pts[i][j] = sum; 127 | } 128 | } 129 | 130 | for (int i = 0; i < s * v; i += v){ 131 | //converting the vertex matrix into array of co-ordinates of a given surface 132 | coord sfc[3] = {{xc + pts[0][i], yc + 100 - pts[1][i], pts[2][i]}, {xc + pts[0][i+1], yc + 100 - pts[1][i+1], pts[2][i+1]}, {xc + pts[0][i+2], yc + 100 - pts[1][i+2], pts[2][i+2]}}; 133 | //if surface is not hidden then display it 134 | if (!hidden(sfc, 3)){ 135 | int lcolor; 136 | 137 | switch(i){ 138 | case 0 : 139 | case 3 : 140 | case 6 : lcolor = GREEN; break; 141 | case 9 : lcolor = YELLOW; break; 142 | } 143 | 144 | setcolor(lcolor); 145 | for(int j = 0; j < v; ++j) 146 | line(xc + pts[0][i+j], yc + 100 - pts[1][i+j], xc + pts[0][i+(j+1)%v], yc + 100 - pts[1][i+(j+1)%v]); 147 | } 148 | } 149 | 150 | setactivepage(page); setvisualpage(1-page); delay(20); cleardevice(); 151 | } 152 | 153 | getch(); 154 | return 0; 155 | } 156 | -------------------------------------------------------------------------------- /hidden-surface/hiddenSurface.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/hidden-surface/hiddenSurface.exe -------------------------------------------------------------------------------- /hidden-surface/hiddenSurface.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/hidden-surface/hiddenSurface.o -------------------------------------------------------------------------------- /line-clipping-with-curve/lineCurveClipping.cpp: -------------------------------------------------------------------------------- 1 | //Clipping w.r.t a curve is totally based on either accept or reject case of line drawing 2 | //for this we colored the background of curve so that can be used to check the above case 3 | 4 | //Hover the cursor slowly as rendering with floodfill is quite slow 5 | 6 | #include 7 | #include 8 | #define MAX 10 9 | #define PI 3.1416 10 | #define COS(a) cos(((a) * PI) / (float)180) 11 | #define SIN(a) sin(((a) * PI) / (float)180) 12 | 13 | using namespace std; 14 | 15 | int pixcolor = GREEN; //pixel color of curve 16 | int bgcolor = LIGHTGREEN; //background color of curve 17 | 18 | //flood fill algorithm 19 | void ffill(int x,int y,int fill,int old) 20 | { 21 | if((getpixel(x,y) != old) && (getpixel(x, y) != fill)) { 22 | putpixel(x, y, fill); 23 | ffill(x + 1, y, fill, old); 24 | ffill(x - 1, y, fill, old); 25 | ffill(x, y + 1, fill, old); 26 | ffill(x, y - 1, fill, old); 27 | } 28 | } 29 | 30 | //modifying the mid-point algorithm of line for clipping 31 | void line_mpt (int x1, int y1, int x2, int y2, int color1 = WHITE, int color2 = WHITE) 32 | { 33 | int dx = abs(x2 - x1), dy = abs(y2 - y1), xsign, ysign; 34 | bool compare; 35 | 36 | if (abs (x2 - x1) > abs (y2 - y1)) { //major moment in x 37 | if ((x2 - x1) * (y2 - y1) >= 0) { //slope is +ve: mx - y + c = 0 38 | xsign = -1, ysign = 1, compare = 1; 39 | if ((x1 > x2) or ((x2 - x1) == 0 and (y1 > y2))) { 40 | swap(x1, x2); swap(y1, y2); 41 | } 42 | } 43 | else { //slope is -ve: y + mx - c = 0 44 | xsign = 1, ysign = -1, compare = 0; 45 | if (x1 < x2) { 46 | swap(x1, x2); swap(y1, y2); 47 | } 48 | } 49 | 50 | int x = x1, y = y1; 51 | int del = (dy * ysign) + (dx * xsign) / 2; 52 | //putpixel(x, y, color); 53 | 54 | while (x != x2) { 55 | x -= xsign; 56 | if ((compare ? del < 0 : del > 0)) { 57 | del += (dy * ysign); 58 | } 59 | else { 60 | del += ((dy * ysign) + (dx * xsign)); 61 | y++; 62 | } 63 | if (getpixel(x, y) == bgcolor) //this is the clipping case as line lies inside the curve 64 | putpixel(x, y, color2); 65 | else 66 | putpixel(x, y, color1); //no clipping 67 | } 68 | } 69 | else { //major moment in y 70 | if ((x2 - x1) * (y2 - y1) >= 0) { //slope is +ve: mx - y + c = 0 71 | xsign = -1, ysign = 1, compare = 1; 72 | if ((x1 > x2) or ((x2 - x1) == 0 and (y1 > y2))) { 73 | swap(x1, x2); swap(y1, y2); 74 | } 75 | } 76 | else { //slope is -ve: y + mx - c = 0 77 | xsign = 1, ysign = -1, compare = 0; 78 | if (x1 < x2) { 79 | swap(x1, x2); swap(y1, y2); 80 | } 81 | } 82 | 83 | int x = x1, y = y1; 84 | int del = (dx * xsign) + (dy * ysign) / 2; 85 | //putpixel(x, y, color); 86 | 87 | while (y != y2) { 88 | y ++; 89 | if ((compare ? del > 0 : del < 0)) { 90 | del += (dx * xsign); 91 | } 92 | else { 93 | del += ((dx * xsign) + (dy * ysign)); 94 | x -= xsign; 95 | } 96 | if (getpixel(x, y) == bgcolor) //this is the clipping case as line lies inside the curve 97 | putpixel(x, y, color2); 98 | else 99 | putpixel(x, y, color1); //no clipping 100 | } 101 | } 102 | } 103 | 104 | int main() 105 | { 106 | initwindow(1400, 900); 107 | int xc = getmaxx() / 2, yc = getmaxx() / 2, page = 0; 108 | xc -= 150, yc -= 200; 109 | 110 | //hi of Hindi curve 111 | int hcrv = 13; 112 | int hi_Hindi[4][4*hcrv] = { {0,0,0,0, 0,0,10,10, 10,10,10,10, 60,60,60,60, 70,70,70,70, 60,10,10,60, 70,25,25,70, 60,10,0,90, 70,100,100,60, 70,90,75,60, 70,30,10,90, 0,-30,20,65, 10,-15,20,65}, 113 | {20,20,150,150, 20,20,20,20, 20,20,150,150, 150,150,140,140, 150,150,130,130, 140,140,90,90, 130,130,100,100, 90,90,30,0, 100,90,70,60, 80,75,70,60, 80,80,30,0, 160,215,245,165, 160,205,225,165}, 114 | {0,10,0,0, 0,0,0,0, 0,10,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 115 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 116 | 117 | //n of Hindi curve 118 | int ncrv = 4; 119 | int n_Hindi[4][4*ncrv] = { {100,100,160,160, 125,125,160,160, 100,105,125,125, 160,160,160,160}, 120 | {90,90,90,90, 80,80,80,80, 90,60,70,80, 90,90,80,80}, 121 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 122 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 123 | 124 | //di of Hindi curve 125 | int dcrv = 12; 126 | int d_Hindi[4][4*dcrv] = { {230,230,230,230, 240,240,240,240, 230,148,148,230, 240,158,158,230, 230,225,220,260, 240,235,230,260, 230,230,270,240, 270,270,270,270, 280,280,280,280, 270,270,280,280, 280,310,260,230, 270,295,260,230}, 127 | {150,150,140,140, 150,150,130,130, 140,130,30,40, 130,120,40,50, 40,30,20,10, 40,30,20,10, 50,80,40,40, 20,20,150,150, 20,20,150,150, 20,20,20,20, 160,215,245,165, 160,205,225,165}, 128 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 129 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 130 | 131 | //horizontal bar of Hindi curve 132 | int lcrv = 10; 133 | int l_Hindi[4][4*lcrv] = { {-20,-20,0,0, 10,10,270,270, 280,280,300,300, -20,-20,0,0, 10,10,60,60, 70,70,230,230, 240,240,270,270, 280,280,300,300, -20,-20,-20,-20, 300,300,300,300}, 134 | {160,160,160,160, 160,160,160,160, 160,160,160,160, 150,150,150,150, 150,150,150,150, 150,150,150,150, 150,150,150,150, 150,150,150,150, 150,150,160,160, 150,150,160,160}, 135 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 136 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 137 | 138 | POINT cursorPos; 139 | cursorPos = {xc + 400, yc - 200}; 140 | 141 | while(!kbhit()){ 142 | settextstyle(10, HORIZ_DIR, 1); setcolor(LIGHTRED); 143 | outtextxy(xc - 100, yc - 400, "Hold the Left-Shift key and Hover the CURSOR!!"); 144 | //update the cursor position only if Left Shift is hold 145 | if(GetKeyState(VK_SHIFT) & 0x800) 146 | GetCursorPos(&cursorPos); 147 | 148 | //drawing the hi curve of Hindi 149 | for (int col = 0; col < 4 * hcrv; col +=4) { 150 | for (float t = 0; t <= 1; t += 0.001) { 151 | float x, y; 152 | x = pow(1-t, 3) * hi_Hindi[0][col] + 3 * pow(1-t, 2) * t * hi_Hindi[0][col+1] + 3 * (1-t) * t * t * hi_Hindi[0][col+2] + pow(t, 3) * hi_Hindi[0][col+3]; 153 | y = pow(1-t, 3) * hi_Hindi[1][col] + 3 * pow(1-t, 2) * t * hi_Hindi[1][col+1] + 3 * (1-t) * t * t * hi_Hindi[1][col+2] + pow(t, 3) * hi_Hindi[1][col+3]; 154 | putpixel(xc + x, yc - y, pixcolor); 155 | } 156 | } 157 | 158 | //drawing the n curve of Hindi 159 | for (int col = 0; col < 4 * ncrv; col +=4) { 160 | for (float t = 0; t <= 1; t += 0.001) { 161 | float x, y; 162 | x = pow(1-t, 3) * n_Hindi[0][col] + 3 * pow(1-t, 2) * t * n_Hindi[0][col+1] + 3 * (1-t) * t * t * n_Hindi[0][col+2] + pow(t, 3) * n_Hindi[0][col+3]; 163 | y = pow(1-t, 3) * n_Hindi[1][col] + 3 * pow(1-t, 2) * t * n_Hindi[1][col+1] + 3 * (1-t) * t * t * n_Hindi[1][col+2] + pow(t, 3) * n_Hindi[1][col+3]; 164 | putpixel(xc + x, yc - y, pixcolor); 165 | } 166 | } 167 | 168 | //drawing the di curve of Hindi 169 | for (int col = 0; col < 4 * dcrv; col +=4) { 170 | for (float t = 0; t <= 1; t += 0.001) { 171 | float x, y; 172 | x = pow(1-t, 3) * d_Hindi[0][col] + 3 * pow(1-t, 2) * t * d_Hindi[0][col+1] + 3 * (1-t) * t * t * d_Hindi[0][col+2] + pow(t, 3) * d_Hindi[0][col+3]; 173 | y = pow(1-t, 3) * d_Hindi[1][col] + 3 * pow(1-t, 2) * t * d_Hindi[1][col+1] + 3 * (1-t) * t * t * d_Hindi[1][col+2] + pow(t, 3) * d_Hindi[1][col+3]; 174 | putpixel(xc + x, yc - y, pixcolor); 175 | } 176 | } 177 | 178 | //drawing the horizontal bar of Hindi 179 | for (int col = 0; col < 4 * lcrv; col +=4) { 180 | for (float t = 0; t <= 1; t += 0.001) { 181 | float x, y; 182 | x = pow(1-t, 3) * l_Hindi[0][col] + 3 * pow(1-t, 2) * t * l_Hindi[0][col+1] + 3 * (1-t) * t * t * l_Hindi[0][col+2] + pow(t, 3) * l_Hindi[0][col+3]; 183 | y = pow(1-t, 3) * l_Hindi[1][col] + 3 * pow(1-t, 2) * t * l_Hindi[1][col+1] + 3 * (1-t) * t * t * l_Hindi[1][col+2] + pow(t, 3) * l_Hindi[1][col+3]; 184 | putpixel(xc + x, yc - y, pixcolor); 185 | } 186 | } 187 | 188 | //filling the background of the curve 189 | ffill(xc + 5, yc - 25, bgcolor, pixcolor); 190 | ffill(xc + 110, yc - 85, bgcolor, pixcolor); 191 | //drawing the line and also performing the clipping 192 | line_mpt(xc - 200, yc + 50, cursorPos.x, cursorPos.y, YELLOW, BLACK); 193 | line_mpt(xc - 201, yc + 49, cursorPos.x-1, cursorPos.y-1, YELLOW, BLACK); 194 | 195 | setactivepage(page); setvisualpage(1-page); 196 | page = 1 - page; cleardevice(); 197 | } 198 | 199 | getch(); 200 | return 0; 201 | } 202 | -------------------------------------------------------------------------------- /line-clipping-with-curve/lineCurveClipping.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/line-clipping-with-curve/lineCurveClipping.exe -------------------------------------------------------------------------------- /line-clipping-with-curve/lineCurveClipping.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/line-clipping-with-curve/lineCurveClipping.o -------------------------------------------------------------------------------- /name-in-hindi/nameHindi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define PI 3.1416 4 | #define COS(a) cos(((a) * PI) / (float)180) 5 | #define SIN(a) sin(((a) * PI) / (float)180) 6 | 7 | using namespace std; 8 | 9 | int main() 10 | { 11 | initwindow(1200, 900); 12 | int xc = getmaxx() / 2, yc = getmaxy() / 2; 13 | 14 | //it defines the no. of curves to be used in curve matrix 15 | int crv = 11; 16 | 17 | //each curve matrix is written as: 18 | //a group of P1, P2, P3, P4 (bezier curve) points as column and each column as [x y z 1] 19 | //so first column is [P1.x, P1.y, P1.z, 1] 20 | int curve[4][4*crv] = { {-200,-200,-200,-200, -100,-200,-200,-100, -200,-280,-210,-110, -100,-100,-100,-100, 0,-100,-100,0, 0,-50,-150,-80, 0,0,0,0, 100,0,0,100, 100,100,100,100, 100,200,200,120, -240,-240,200,200}, 21 | {70,70,-70,-70, 5,150,-150,-5, 70,150,150,70, 70,70,-70,-70, 5,150,-150,-5, 70,150,140,120, 70,70,-70,-70, 5,150,-150,-5, 70,70,-70,-70, 5,140,-100,-30, 70,70,70,70}, 22 | {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 23 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 24 | 25 | //circle drawn using 2 curves 26 | int page = 0; 27 | for (int ang = 0; !kbhit(); ++ang) { 28 | //rotation matrix about x-axis 29 | float rotx[4][4] = { {1, 0, 0, 0}, 30 | {0, COS(ang), -SIN(ang), 0}, 31 | {0, SIN(ang), COS(ang), 0}, 32 | {0, 0, 0, 1} }; 33 | 34 | //rotation matrix about y-axis 35 | float roty[4][4] = { {COS(ang), 0, SIN(ang), 0}, 36 | {0, 1, 0, 0}, 37 | {-SIN(ang), 0, COS(ang), 0}, 38 | {0, 0, 0, 1} }; 39 | 40 | //rotation matrix about z-axis 41 | float rotz[4][4] = { {COS(ang), -SIN(ang), 0, 0}, 42 | {SIN(ang), COS(ang), 0, 0}, 43 | {0, 0, 1, 0}, 44 | {0, 0, 0, 1} }; 45 | 46 | //using the x-rotation matrix and implementing it 47 | float pts[4][4*crv]; 48 | //multiplying the rotation matrix with the curve points 49 | for (int i = 0; i < 4; i++) { 50 | for (int j = 0; j < 4 * crv; j++) { 51 | int sum = 0; 52 | for (int k = 0; k < 4; k++) { 53 | sum += rotx[i][k] * curve[k][j]; 54 | } 55 | pts[i][j] = sum; 56 | } 57 | } 58 | 59 | //drawing the bezier curve using the binomial formula 60 | for (int col = 0; col < 4 * crv; col +=4) { 61 | //we increment col by 4 because we have 4 columns group as a 1 bezier curve control points 62 | for (float t = 0; t <= 1; t += 0.001) { 63 | float x, y; 64 | x = pow(1-t, 3) * pts[0][col] + 3 * pow(1-t, 2) * t * pts[0][col+1] + 3 * (1-t) * t * t * pts[0][col+2] + pow(t, 3) * pts[0][col+3]; 65 | y = pow(1-t, 3) * pts[1][col] + 3 * pow(1-t, 2) * t * pts[1][col+1] + 3 * (1-t) * t * t * pts[1][col+2] + pow(t, 3) * pts[1][col+3]; 66 | putpixel(xc + x, yc - y, LIGHTRED); 67 | } 68 | } 69 | 70 | delay(5); setvisualpage(1 - page), setactivepage(page); 71 | page = 1 - page; cleardevice(); 72 | } 73 | 74 | getch(); 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /name-in-hindi/nameHindi.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/name-in-hindi/nameHindi.exe -------------------------------------------------------------------------------- /name-in-hindi/nameHindi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/name-in-hindi/nameHindi.o -------------------------------------------------------------------------------- /rotating-circular-design/circularRotateDsgn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 10 4 | #define PI 3.1416 5 | #define COS(a) cos(((a) * PI) / (float)180) 6 | #define SIN(a) sin(((a) * PI) / (float)180) 7 | #define deg(a) ((float)a * 180) / PI 8 | 9 | using namespace std; 10 | 11 | void pixel(int xc,int yc,int x,int y, int color) 12 | { 13 | putpixel(xc + x, yc + y, color); 14 | putpixel(xc + y, yc + x, color); 15 | putpixel(xc - y, yc + x, color); 16 | putpixel(xc - x, yc + y, color); 17 | putpixel(xc - x, yc - y, color); 18 | putpixel(xc - y, yc - x, color); 19 | putpixel(xc + y, yc - x, color); 20 | putpixel(xc + x, yc - y, color); 21 | } 22 | 23 | //for drawing circle using mid-point algorithm 24 | void circle_mpt(int xc, int yc, int r, int color = WHITE) 25 | { 26 | int x = 0, y = r, d = 1 - r; 27 | pixel(xc, yc, x, y, color); 28 | 29 | while (x < y) 30 | { 31 | if (d < 0) 32 | { 33 | x++; 34 | d += (2 * x) + 3; 35 | } 36 | else 37 | { 38 | x++; 39 | y--; 40 | d += 2 * (x - y) + 5; 41 | } 42 | pixel(xc, yc, x, y, color); 43 | } 44 | } 45 | 46 | int main() 47 | { 48 | initwindow(1100, 900); 49 | int xc = getmaxx() / 2, yc = getmaxy() / 2, R = 150, page = 0; 50 | 51 | //Program stops when any key is pressed on the keyboard 52 | for (int rot = 0; !kbhit(); rot ++){ 53 | for (int a = -30 + rot; a <= 270 + rot; a += 60) { 54 | int x = xc + R * COS(a), y = yc + R * SIN(a), n = 13, ang = 180 - (a) - 10, r = 5; 55 | circle_mpt(xc, yc, 15, RED); 56 | 57 | //Each cirlce design is drawn on the bigger circle circumference up to its 3/4th distance 58 | while (n > 0){ 59 | //calculating centres of such circles 60 | int xx = x + R * COS(ang), yy = y - R * SIN(ang); 61 | circle_mpt(xx, yy, r, YELLOW); 62 | //upto 7 circles, their radius increases 63 | if (n > 7) { 64 | //ang calculates the centre of the next circle 65 | r += 5; ang -= deg((2 * r + 1) / R); 66 | } 67 | else { 68 | ang -= deg((2 * r + 1) / R); r -= 5; 69 | } 70 | n--; 71 | } 72 | } 73 | delay(10); setactivepage(page); setvisualpage(1-page); 74 | page = 1 - page; cleardevice(); 75 | } 76 | 77 | getch(); 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /rotating-circular-design/circularRotateDsgn.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/rotating-circular-design/circularRotateDsgn.exe -------------------------------------------------------------------------------- /rotating-circular-design/circularRotateDsgn.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/rotating-circular-design/circularRotateDsgn.o -------------------------------------------------------------------------------- /scanline-algorithm/scanline.cpp: -------------------------------------------------------------------------------- 1 | //Polygon filling through Scan-line approach 2 | #include 3 | #include 4 | #define MAX 10 5 | 6 | using namespace std; 7 | 8 | typedef struct{ 9 | int x; 10 | int y; 11 | }coord; 12 | 13 | void scanline(coord pts[MAX], int n, int color) 14 | { 15 | int ymin = INT_MAX, ymax = -INT_MAX; 16 | int xi[MAX]; 17 | float slope[MAX]; 18 | 19 | for (int i = 0; i < n; i++) { 20 | ymin = min(ymin, pts[i].y); 21 | ymax = max(ymax, pts[i].y); 22 | } 23 | 24 | for (int i = 0; i < n; i++) { 25 | int dy = pts[(i+1)%n].y - pts[i].y; 26 | int dx = pts[(i+1)%n].x - pts[i].x; 27 | if (dy == 0) 28 | slope[i] = 1.0; 29 | if (dx == 0) 30 | slope[i] = 0.0; 31 | if ((dy != 0) && (dx != 0)) // calculate inverse slope 32 | slope[i] = (float) dx / dy; 33 | } 34 | 35 | for(int y = ymin + 1; y < ymax; y++) { 36 | int k = 0; 37 | for (int i = 0; i < n; i++) { 38 | if (((pts[i].y <= y) && (pts[(i+1)%n].y > y)) || ((pts[i].y > y) && (pts[(i+1)%n].y <= y))) { 39 | xi[k] = (int)(pts[i].x + slope[i] * (y - pts[i].y)); 40 | k++; 41 | } 42 | } 43 | 44 | for(int j = 0; j < k - 1; j++) { //Arrange x-intersections in order 45 | for(int i = 0; i < k - 1; i++) { 46 | if(xi[i] > xi[i+1]) { 47 | swap(xi[i], xi[i+1]); 48 | } 49 | } 50 | 51 | setcolor(color); 52 | for(int i = 0; i < k; i += 2) 53 | line(xi[i]+1, y, xi[i+1], y); 54 | } 55 | } 56 | } 57 | 58 | int main() 59 | { 60 | initwindow(1400, 900); 61 | int n, color; 62 | coord pts[MAX]; 63 | 64 | cout << "Enter no. of vertices of polygon : "; cin >> n; 65 | cout << "\nEnter co-ordinates in cyclic order :\n"; 66 | 67 | for (int i = 0; i < n; ++i){ 68 | int x, y; 69 | cout << "Enter (x, y) of vertex no. " << i + 1 << " : "; 70 | cin >> x >> y; 71 | pts[i] = {x, y}; 72 | } 73 | 74 | cout << "\nEnter color (1 - 15) : "; cin >> color; 75 | 76 | for (int i = 0; i < n; i++) 77 | line(pts[i].x, pts[i].y, pts[(i+1)%n].x, pts[(i+1)%n].y); 78 | 79 | scanline(pts, n, color); 80 | 81 | getch(); 82 | return 0; 83 | } 84 | 85 | /*Test Case: 86 | 4 87 | 200 200 88 | 200 800 89 | 800 800 90 | 800 200 91 | 10 92 | */ 93 | -------------------------------------------------------------------------------- /scanline-algorithm/scanline.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/scanline-algorithm/scanline.exe -------------------------------------------------------------------------------- /scanline-algorithm/scanline.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/scanline-algorithm/scanline.o -------------------------------------------------------------------------------- /see-saw-moving-wheel/see-saw.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 10 4 | #define PI 3.1416 5 | #define COS(a) cos(((a) * PI) / (float)180) 6 | #define SIN(a) sin(((a) * PI) / (float)180) 7 | 8 | //COS and SIN calculate the angle given in degree 9 | 10 | using namespace std; 11 | 12 | //for plotting 8 different points of circle using 8-symmetry 13 | void pixel(int xc,int yc,int x,int y, int color) 14 | { 15 | putpixel(xc + x, yc + y, color); 16 | putpixel(xc + y, yc + x, color); 17 | putpixel(xc - y, yc + x, color); 18 | putpixel(xc - x, yc + y, color); 19 | putpixel(xc - x, yc - y, color); 20 | putpixel(xc - y, yc - x, color); 21 | putpixel(xc + y, yc - x, color); 22 | putpixel(xc + x, yc - y, color); 23 | } 24 | 25 | //for drawing circle using mid-point algorithm 26 | void circle_mpt(int xc, int yc, int r, int color = WHITE) 27 | { 28 | int x = 0, y = r, d = 1 - r; 29 | pixel(xc, yc, x, y, color); 30 | 31 | while (x < y) 32 | { 33 | if (d < 0) 34 | { 35 | x++; 36 | d += (2 * x) + 3; 37 | } 38 | else 39 | { 40 | x++; 41 | y--; 42 | d += 2 * (x - y) + 5; 43 | } 44 | pixel(xc, yc, x, y, color); 45 | } 46 | } 47 | 48 | //for drawing line using mid-point algorithm which handles all the cases 49 | void line_mpt (int x1, int y1, int x2, int y2, int color = WHITE) 50 | { 51 | int dx = abs(x2 - x1), dy = abs(y2 - y1), xsign, ysign; 52 | bool compare; 53 | 54 | if (abs (x2 - x1) > abs (y2 - y1)) { //major moment in x 55 | if ((x2 - x1) * (y2 - y1) > 0) { //slope is +ve: mx - y + c = 0 56 | xsign = -1, ysign = 1, compare = 1; 57 | if (x1 > x2) { 58 | swap(x1, x2); swap(y1, y2); 59 | } 60 | } 61 | else { //slope is -ve: y + mx - c = 0 62 | xsign = 1, ysign = -1, compare = 0; 63 | if (x1 < x2) { 64 | swap(x1, x2); swap(y1, y2); 65 | } 66 | } 67 | 68 | int x = x1, y = y1; 69 | int del = (dy * ysign) + (dx * xsign) / 2; 70 | putpixel(x, y, color); 71 | 72 | while (x != x2) { 73 | x -= xsign; 74 | if ((compare ? del < 0 : del > 0)) { 75 | del += (dy * ysign); 76 | } 77 | else { 78 | del += ((dy * ysign) + (dx * xsign)); 79 | y++; 80 | } 81 | putpixel(x, y, color); 82 | } 83 | } 84 | else { //major moment in y 85 | if ((x2 - x1) * (y2 - y1) > 0) { //slope is +ve: mx - y + c = 0 86 | xsign = -1, ysign = 1, compare = 1; 87 | if (x1 > x2) { 88 | swap(x1, x2); swap(y1, y2); 89 | } 90 | } 91 | else { //slope is -ve: y + mx - c = 0 92 | xsign = 1, ysign = -1, compare = 0; 93 | if (x1 < x2) { 94 | swap(x1, x2); swap(y1, y2); 95 | } 96 | } 97 | 98 | int x = x1, y = y1; 99 | int del = (dx * xsign) + (dy * ysign) / 2; 100 | putpixel(x, y, color); 101 | 102 | while (y != y2) { 103 | y ++; 104 | if ((compare ? del > 0 : del < 0)) { 105 | del += (dx * xsign); 106 | } 107 | else { 108 | del += ((dx * xsign) + (dy * ysign)); 109 | x -= xsign; 110 | } 111 | putpixel(x, y, color); 112 | } 113 | } 114 | } 115 | 116 | int main() 117 | { 118 | initwindow(1400, 900); 119 | int xc = getmaxx() / 2, yc = getmaxy() / 2, r = 300, degcnt = 50, a = 25, sec = 65, disp = 50, page = 0, sa = 0, sign = 1, k = 0; 120 | //disp is used for the displacement of the circle from the extreme point of the plank as it can't go till the end 121 | 122 | for (int rot = 0; !kbhit(); a -= sign, k += sign * 10, rot -= sign * 20, degcnt--) { 123 | //drawing the triangle 124 | line_mpt(xc, yc, xc + 50, yc + 80, LIGHTRED), line_mpt(xc, yc, xc - 50, yc + 80, LIGHTRED), line_mpt(xc - 50, yc + 80, xc + 50, yc + 80, LIGHTRED); 125 | 126 | //the sea-saw plank 127 | for (int j = 0; j < 5; ++j) 128 | line_mpt(xc + r * COS(a), yc - r * SIN(a) - j, xc - r * COS(a), yc + r * SIN(a) - j, BROWN); 129 | 130 | //centre of the moving circle 131 | int cirx = xc - (r - disp - k) * COS(a), ciry = yc - 40 + (r - disp - k) * SIN(a), cirR = 36; 132 | circle_mpt(cirx, ciry, cirR, GREEN), circle_mpt(cirx, ciry, cirR - 1, GREEN), circle_mpt(cirx, ciry, cirR - 2, GREEN); 133 | circle_mpt(cirx, ciry, 4, YELLOW), circle_mpt(cirx, ciry, 5, YELLOW), circle_mpt(cirx, ciry, 30, YELLOW); 134 | 135 | //drawing the spokes 136 | for (int cnt = 0; cnt < 4; sa += sign * 45, cnt++) 137 | line_mpt(cirx + cirR * COS(sa + rot), ciry - cirR * SIN(sa + rot), cirx - cirR * COS(sa + rot), ciry + cirR * SIN(sa + rot), YELLOW); 138 | 139 | //reseting the degree count and changing the nature of rotation 140 | if (degcnt == 0) 141 | degcnt = 50, sign *= -1; 142 | 143 | //using setactivepage() and setvisualpage() to avoid flickering 144 | delay(sec); setactivepage(page); setvisualpage(1-page); 145 | page = 1 - page; cleardevice(); 146 | } 147 | 148 | getch(); 149 | return 0; 150 | } 151 | -------------------------------------------------------------------------------- /see-saw-moving-wheel/see-saw.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/see-saw-moving-wheel/see-saw.exe -------------------------------------------------------------------------------- /see-saw-moving-wheel/see-saw.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/see-saw-moving-wheel/see-saw.o -------------------------------------------------------------------------------- /skipping-rope/skippingRope.cpp: -------------------------------------------------------------------------------- 1 | //for hiding the rope behind the person during skipping, z-coordinate of the rope has been compared 2 | //and point is checked whether it lies inside a given figure or not 3 | #include 4 | #include 5 | #define PI 3.1416 6 | #define COS(a) cos(((a) * PI) / (float)180) 7 | #define SIN(a) sin(((a) * PI) / (float)180) 8 | #define MAX 10 9 | 10 | using namespace std; 11 | 12 | //x and y coordinate 13 | typedef struct{ 14 | int x; 15 | int y; 16 | }coord; 17 | 18 | //using checkInside function for clipping purpose 19 | bool checkInside(coord vertex[MAX], int n, int x, int y) 20 | { 21 | bool check = true; 22 | for (int i = 0; i < n; ++i){ 23 | int dy = vertex[(i+1)%n].y - vertex[i].y; //y2 - y1 24 | int dx = vertex[(i+1)%n].x - vertex[i].x; //x2 - x1 25 | int a = dy; 26 | int b = -dx; 27 | long long int c = vertex[i].y * dx - vertex[i].x * dy; //y1*dx - x1*dy 28 | 29 | int x1 = vertex[(i+2)%n].x, y1 = vertex[(i+2)%n].y; //an inside point for the edge equation 30 | long long int eq1 = a*x + b*y + c; //f(x, y) = ax + bx + c; 31 | long long int eq2 = a*x1 + b*y1 + c; //f(x1, y1) = ax1, yb1 + c; 32 | 33 | //point should be inside w.r.t all the edges if false for any edge then it doesn't lie inside 34 | if ((eq1 * eq2) < 0) { 35 | return false; 36 | } 37 | } 38 | 39 | //point lies inside the figure 40 | return true; 41 | } 42 | 43 | bool insideCircle(int x, int y, int xc, int yc, int r) 44 | { 45 | //inside test of circle is checked using the circle equation 46 | //f(x,y) = (x-h)^2 + (y-k)^2 - r^2 = 0 47 | long long sum = 0; 48 | sum = pow((x-xc), 2) + pow((y-yc), 2) - pow(r, 2); 49 | if (sum < 0) 50 | return true; //point lies inside the circle 51 | else 52 | return false; //point lies outside the circle 53 | } 54 | 55 | int main() 56 | { 57 | initwindow(1200, 900); 58 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0, ang = 0; 59 | int YC = yc; //used in hand movement 60 | 61 | //we require only one curve for drawing the skipping rope (use of bezier curve) 62 | //second curve is used for thickening the curve 63 | //each curve is stored as 4 control points P1, P2, P3, P4 column wise, and each column as [x y z 1] 64 | int crv = 2; 65 | int rope[4][4*crv] = { {-75,-75,75,75, -74,-74,74,74}, 66 | {-10,248,248,-10, -9,247,247,-9}, 67 | {0,0,0,0, 0,0,0,0}, 68 | {1,1,1,1, 1,1,1,1} }; 69 | 70 | //yinc is used for person jumping 71 | int yinc = 1, last = 0; 72 | float k = 0; 73 | for (int ang = 0; !kbhit(); ang+=3) { 74 | last = yc; 75 | //setting condition for going up or down 76 | if(yc > YC + 30) { 77 | yinc *= -1; 78 | } 79 | if (yc < YC - 30) { 80 | yinc *= -1; 81 | } 82 | yc += yinc; 83 | 84 | //k is used for the hand movement relative to the body 85 | if (yc > last){ 86 | k += 0.3; 87 | } 88 | else{ 89 | k -= 0.3; 90 | } 91 | 92 | //Drawing person 93 | setcolor(LIGHTGRAY); 94 | line(xc - 25, yc + 50, xc + 25, yc + 50); line(xc - 25, yc + 49, xc + 25, yc + 49); line(xc - 25, yc + 48, xc + 25, yc + 48); //used for main body 95 | line(xc - 25, yc + 50, xc - 25, yc - 50); line(xc - 24, yc + 50, xc - 24, yc - 50); line(xc - 23, yc + 50, xc - 23, yc - 50); //~ 96 | line(xc + 25, yc + 50, xc + 25, yc - 50); line(xc + 24, yc + 50, xc + 24, yc - 50); line(xc + 23, yc + 50, xc + 23, yc - 50); //~ 97 | line(xc - 28, yc - 60, xc + 28, yc - 60); line(xc - 28, yc - 59, xc + 28, yc - 59); line(xc - 28, yc - 58, xc + 28, yc - 58); //~ 98 | line(xc - 45, yc - 20, xc - 28, yc - 60); line(xc - 44, yc - 19, xc - 27, yc - 59); line(xc - 43, yc - 19, xc - 27, yc - 58); //left arm 99 | line(xc + 45, yc - 20, xc + 28, yc - 60); line(xc + 44, yc - 19, xc + 27, yc - 59); line(xc + 43, yc - 19, xc + 27, yc - 58); //right arm 100 | 101 | line(xc - 75, yc + k - 10, xc - 45, yc - 20); line(xc - 74, yc + k - 9, xc - 44, yc - 19); line(xc - 73, yc + k - 8, xc - 43, yc - 18); //left hand 102 | line(xc + 75, yc + k - 10, xc + 45, yc - 20); line(xc + 74, yc + k - 9, xc + 44, yc - 19); line(xc + 73, yc + k - 8, xc + 43, yc - 18); //right hand 103 | 104 | line(xc, yc - 60, xc, yc - 70); line(xc - 1, yc - 60, xc - 1, yc - 70); line(xc + 1, yc - 60, xc + 1, yc - 70); //neck 105 | circle(xc - 7 , yc - 95, 5); circle(xc + 7 , yc - 95, 5); line(xc - 5, yc - 83, xc + 5, yc - 83); //eyes and mouth 106 | circle(xc, yc - 90, 20); circle(xc, yc - 90, 21); circle(xc, yc - 90, 19); //head 107 | line(xc - 15, yc + 50, xc - 15, yc + 100); line(xc - 16, yc + 50, xc - 16, yc + 100); line(xc - 14, yc + 50, xc - 14, yc + 100); //left leg 108 | line(xc + 15, yc + 50, xc + 15, yc + 100); line(xc + 16, yc + 50, xc + 16, yc + 100); line(xc + 14, yc + 50, xc + 14, yc + 100); //right leg 109 | line(xc - 15, yc + 100, xc - 10, yc + 100); line(xc - 15, yc + 99, xc - 11, yc + 99); line(xc - 15, yc + 98, xc - 10, yc + 99); //left foot 110 | line(xc + 15, yc + 100, xc + 10, yc + 100); line(xc + 15, yc + 99, xc + 11, yc + 99); line(xc + 15, yc + 98, xc + 10, yc + 99); //right foot 111 | 112 | setcolor(LIGHTRED); 113 | line(0, YC + 136, getmaxx(), YC + 136); line(0, YC + 135, getmaxx(), YC + 135); line(0, YC + 134, getmaxx(), YC + 134); //ground line 114 | 115 | //rotation matrix about x-axis 116 | float rotx[4][4] = { {1, 0, 0, 0}, 117 | {0, COS(ang), -SIN(ang), 0}, 118 | {0, SIN(ang), COS(ang), 0}, 119 | {0, 0, 0, 1} }; 120 | 121 | //multiplying the curve points with the rotation matrix 122 | float pts[4][4*crv]; 123 | for (int i = 0; i < 4; i++) { 124 | for (int j = 0; j < 4 * crv; j++) { 125 | int sum = 0; 126 | for (int k = 0; k < 4; k++) { 127 | sum += rotx[i][k] * rope[k][j]; 128 | } 129 | pts[i][j] = sum; 130 | } 131 | } 132 | 133 | //defining the clipping window, using it for hiding the rope 134 | coord rect[4] = {{xc - 25, yc + 50}, {xc - 25, yc - 60}, {xc + 25, yc - 60}, {xc + 25, yc + 50}}; 135 | 136 | //plotting using Bezier method 137 | for (int col = 0; col < 4 * crv; col +=4) { 138 | for (float t = 0; t <= 1; t += 0.001) { 139 | float x, y; 140 | x = pow(1-t, 3) * pts[0][col] + 3 * pow(1-t, 2) * t * pts[0][col+1] + 3 * (1-t) * t * t * pts[0][col+2] + pow(t, 3) * pts[0][col+3]; 141 | y = pow(1-t, 3) * pts[1][col] + 3 * pow(1-t, 2) * t * pts[1][col+1] + 3 * (1-t) * t * t * pts[1][col+2] + pow(t, 3) * pts[1][col+3]; 142 | 143 | //we check the z value of the skipping rope 144 | //if z<=0, then we draw the rope over the body 145 | //if z>0, then point should not lie inside rectangle or circle(head) and it should not be drawn on white pixels(i.e the edges used for drawing person) 146 | if ((pts[2][col] <= 0) or (!checkInside(rect, 4, xc + x, yc + k - y) and !insideCircle(xc + x, yc + k - y, xc, yc - 90, 20) and getpixel(xc + x, yc + k - y) == BLACK)) 147 | putpixel(xc + x, yc + k - y, LIGHTGREEN); 148 | 149 | } 150 | } 151 | 152 | delay(10); setvisualpage(1 - page); setactivepage(page); 153 | page = 1 - page; cleardevice(); 154 | } 155 | 156 | getch(); 157 | return 0; 158 | } 159 | -------------------------------------------------------------------------------- /skipping-rope/skippingRope.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/skipping-rope/skippingRope.exe -------------------------------------------------------------------------------- /skipping-rope/skippingRope.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/skipping-rope/skippingRope.o -------------------------------------------------------------------------------- /tetrahedron-with-rollno/rollNoTetrahedron.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 10 4 | #define PI 3.1416 5 | #define COS(a) cos(((a) * PI) / (float)180) 6 | #define SIN(a) sin(((a) * PI) / (float)180) 7 | 8 | using namespace std; 9 | 10 | //x y z coordinate 11 | typedef struct{ 12 | long long int x; 13 | long long int y; 14 | long long int z; 15 | }coord; 16 | 17 | 18 | //Calculating normal vector using cross-product formula 19 | coord normalVector(coord edge1, coord edge2) 20 | { 21 | coord normal; 22 | normal.x = edge1.y * edge2.z - edge1.z * edge2.y; 23 | normal.y = edge1.z * edge2.x - edge1.x * edge2.z; 24 | normal.z = edge1.x * edge2.y - edge1.y * edge2.x; 25 | 26 | return normal; 27 | } 28 | 29 | //return dot product of two vectors 30 | long long int dotProduct(coord a, coord b) 31 | { 32 | return (a.x * b.x + a.y * b.y + a.z * b.z); 33 | } 34 | 35 | //setting the vision co-ordinate 36 | coord V = {700,300,1000}; 37 | 38 | //calculating whether the given surface is hidden or not using the Back-face detection technique 39 | bool hidden(coord sfc[MAX], int n) 40 | { 41 | //sfc array contains the surface vertices in order of cross-product 42 | //calculating the midpoint of the surface 43 | coord M = {0,0,0}; 44 | for(int i = 0; i < n; ++i) { 45 | M.x += sfc[i].x; 46 | M.y += sfc[i].y; 47 | M.z += sfc[i].z; 48 | } 49 | M.x /= n; 50 | M.y /= n; 51 | M.z /= n; 52 | 53 | //edge1 and edge2 for calculating normal vector 54 | coord edge1 = {sfc[1].x - sfc[0].x, sfc[1].y - sfc[0].y, sfc[1].z - sfc[0].z}; 55 | coord edge2 = {sfc[2].x - sfc[1].x, sfc[2].y - sfc[1].y, sfc[2].z - sfc[1].z};; 56 | coord N = normalVector(edge1, edge2); 57 | coord MV = {V.x - M.x, V.y - M.y, V.z - M.z}; 58 | 59 | long long int dp = dotProduct(MV, N); 60 | 61 | if (dp < 0) 62 | return false; //surface is not hidden 63 | else 64 | return true; //surface is hidden 65 | } 66 | 67 | int main() 68 | { 69 | initwindow(1400, 900); 70 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 71 | int s = 4, v = 3, crv = 7; 72 | int sz = 200; 73 | 74 | //here we have to use redundant vertices to ease the implementation in for loop 75 | //though we have only 4 vertices for tetrahedron 76 | 77 | //tetrahedron vertices stored as [x y z 1] in each column 78 | //S1 S2 S3 S4 79 | int vertex[4][s*v] = { {sz,0,0, sz,0,0, 0,0,0, 0,sz,0}, 80 | {0,0,sz, 0,0,0, sz,0,0, 0,0,sz}, 81 | {0,0,0, 0,sz,0, 0,0,sz, sz,0,0}, 82 | {1,1,1, 1,1,1, 1,1,1, 1,1,1} }; 83 | 84 | //coordinates of roll no. are set according to the surfaces of tetrahedron 85 | ///reference figure is provided in the folder 86 | 87 | int crv4 = 4; 88 | int hindi4[4][4*crv4] = { {0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}, 89 | {65,105,115,140, 65,105,115,140, 65,35,5,0, 65,35,5,0}, 90 | {70,110,110,110, 70,30,30,30, 70,40,40,70, 70,110,110,70}, 91 | {1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1} }; 92 | 93 | int crv0 = 2; 94 | int hindi0[4][4*crv0] = { {50,10,10,100, 50,80,150,100}, 95 | {140,150,50,50, 140,150,50,50}, 96 | {50,80,150,100, 50,10,10,100}, 97 | {1,1,1,1, 1,1,1,1} }; 98 | 99 | int crv8 = 1; 100 | int hindi8[4][4*crv8] = { {60,130,130,30}, 101 | {130,20,-20,40}, 102 | {0,0,0,0}, 103 | {1,1,1,1} }; 104 | 105 | 106 | int ang = 0; 107 | 108 | while(1){ 109 | setcolor(LIGHTBLUE); settextstyle(10, HORIZ_DIR, 1); 110 | outtextxy(xc - 300, yc - 350, "Press 'a' for \"CW Rotation\" and 'd' for \"CCW Rotation\""); 111 | 112 | //x-rotation matrix 113 | float rotx[4][4] = { {1, 0, 0, 0}, 114 | {0, COS(ang), -SIN(ang), 0}, 115 | {0, SIN(ang), COS(ang), 0}, 116 | {0, 0, 0, 1} }; 117 | 118 | //y-rotation matrix 119 | float roty[4][4] = { {COS(ang), 0, SIN(ang), 0}, 120 | {0, 1, 0, 0}, 121 | {-SIN(ang), 0, COS(ang), 0}, 122 | {0, 0, 0, 1} }; 123 | 124 | //z-rotation matrix 125 | float rotz[4][4] = { {COS(ang), -SIN(ang), 0, 0}, 126 | {SIN(ang), COS(ang), 0, 0}, 127 | {0, 0, 1, 0}, 128 | {0, 0, 0, 1} }; 129 | 130 | 131 | char ch = getch(); ch = tolower(ch); 132 | switch(ch){ 133 | case 'a' : --ang; break; 134 | case 'd' : ++ang; break; 135 | } 136 | 137 | 138 | //multiplying the vertex with the y-rotation matrix 139 | float pts[4][s*v]; 140 | for (int i = 0; i < 4; i++) { 141 | for (int j = 0; j < s * v; j++) { 142 | int sum = 0; 143 | for (int k = 0; k < 4; k++) 144 | sum += roty[i][k] * vertex[k][j]; 145 | pts[i][j] = sum; 146 | } 147 | } 148 | 149 | //multiplying the roll no. with the y-rotation matrix 150 | float pts4[4][4*crv4]; 151 | for (int i = 0; i < 4; i++) { 152 | for (int j = 0; j < 4 * crv4; j++) { 153 | int sum = 0; 154 | for (int k = 0; k < 4; k++) { 155 | sum += 0.8 * roty[i][k] * hindi4[k][j]; 156 | } 157 | pts4[i][j] = sum; 158 | } 159 | } 160 | 161 | //multiplying the roll no. with the y-rotation matrix 162 | float pts0[4][4*crv0]; 163 | for (int i = 0; i < 4; i++) { 164 | for (int j = 0; j < 4 * crv0; j++) { 165 | int sum = 0; 166 | for (int k = 0; k < 4; k++) { 167 | sum += 0.8 * roty[i][k] * hindi0[k][j]; 168 | } 169 | pts0[i][j] = sum; 170 | } 171 | } 172 | 173 | //multiplying the roll no. with the y-rotation matrix 174 | float pts8[4][4*crv8]; 175 | for (int i = 0; i < 4; i++) { 176 | for (int j = 0; j < 4 * crv8; j++) { 177 | int sum = 0; 178 | for (int k = 0; k < 4; k++) { 179 | sum += 0.8 * roty[i][k] * hindi8[k][j]; 180 | } 181 | pts8[i][j] = sum; 182 | } 183 | } 184 | 185 | for (int i = 0; i < s * v; i += v){ 186 | //converting the vertex matrix into array of co-ordinates of a given surface 187 | coord sfc[3] = {{xc + pts[0][i], yc - pts[1][i], pts[2][i]}, {xc + pts[0][i+1], yc - pts[1][i+1], pts[2][i+1]}, {xc + pts[0][i+2], yc - pts[1][i+2], pts[2][i+2]}}; 188 | //if surface is not hidden then display it 189 | if (!hidden(sfc, 3)){ 190 | int lcolor; 191 | 192 | switch(i){ 193 | case 0 : lcolor = LIGHTMAGENTA; break; 194 | case 3 : lcolor = LIGHTCYAN; break; 195 | case 6 : lcolor = LIGHTGREEN; break; 196 | case 9 : lcolor = YELLOW; break; 197 | } 198 | 199 | //Calculating the mid-point 200 | coord M; 201 | M.x = (sfc[0].x + sfc[1].x + sfc[2].x) / 3; 202 | M.y = (sfc[0].y + sfc[1].y + sfc[2].y) / 3; 203 | 204 | setcolor(lcolor); 205 | for(int j = 0; j < v; ++j) 206 | line(sfc[j].x, sfc[j].y, sfc[(j+1)%v].x, sfc[(j+1)%v].y); 207 | 208 | //filling color in the corresponding surfaces 209 | setfillstyle(SOLID_FILL, lcolor); 210 | floodfill(M.x, M.y, lcolor); 211 | 212 | 213 | if (i == 6) { //4 is present on S3 214 | for (int col = 0; col < 4 * crv4; col +=4) { //for hindi 4 215 | for (float t = 0; t <= 1; t += 0.001) { 216 | float x, y; 217 | x = pow(1-t, 3) * pts4[0][col] + 3 * pow(1-t, 2) * t * pts4[0][col+1] + 3 * (1-t) * t * t * pts4[0][col+2] + pow(t, 3) * pts4[0][col+3]; 218 | y = pow(1-t, 3) * pts4[1][col] + 3 * pow(1-t, 2) * t * pts4[1][col+1] + 3 * (1-t) * t * t * pts4[1][col+2] + pow(t, 3) * pts4[1][col+3]; 219 | putpixel(xc + x, yc - y, RED); 220 | } 221 | } 222 | } 223 | else if (i == 0) { //8 is present on S1 224 | for (int col = 0; col < 4 * crv8; col +=4) { //for hindi 8 225 | for (float t = 0; t <= 1; t += 0.001) { 226 | float x, y; 227 | x = pow(1-t, 3) * pts8[0][col] + 3 * pow(1-t, 2) * t * pts8[0][col+1] + 3 * (1-t) * t * t * pts8[0][col+2] + pow(t, 3) * pts8[0][col+3]; 228 | y = pow(1-t, 3) * pts8[1][col] + 3 * pow(1-t, 2) * t * pts8[1][col+1] + 3 * (1-t) * t * t * pts8[1][col+2] + pow(t, 3) * pts8[1][col+3]; 229 | putpixel(xc + x, yc - y, RED); 230 | } 231 | } 232 | } 233 | else if (i == 9) { //0 is present on S4 234 | for (int col = 0; col < 4 * crv0; col +=4) { //for hindi 0 235 | for (float t = 0; t <= 1; t += 0.001) { 236 | float x, y; 237 | x = pow(1-t, 3) * pts0[0][col] + 3 * pow(1-t, 2) * t * pts0[0][col+1] + 3 * (1-t) * t * t * pts0[0][col+2] + pow(t, 3) * pts0[0][col+3]; 238 | y = pow(1-t, 3) * pts0[1][col] + 3 * pow(1-t, 2) * t * pts0[1][col+1] + 3 * (1-t) * t * t * pts0[1][col+2] + pow(t, 3) * pts0[1][col+3]; 239 | putpixel(xc + x, yc - y, RED); 240 | } 241 | } 242 | } 243 | } 244 | } 245 | 246 | setactivepage(page); setvisualpage(1-page); cleardevice(); 247 | } 248 | 249 | getch(); 250 | return 0; 251 | } 252 | -------------------------------------------------------------------------------- /tetrahedron-with-rollno/rollNoTetrahedron.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/tetrahedron-with-rollno/rollNoTetrahedron.exe -------------------------------------------------------------------------------- /tetrahedron-with-rollno/rollNoTetrahedron.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/tetrahedron-with-rollno/rollNoTetrahedron.o -------------------------------------------------------------------------------- /the-Indian-national-flag/nationalFlag.cpp: -------------------------------------------------------------------------------- 1 | //The Indian National Flag drawn using standing wave 2 | //Sorry for the saffron color as graphics.h doesn't support it; this code may be implemented in openGL also for the required color and animation 3 | #include 4 | #include 5 | #define MAX 20 6 | #define PI (float)3.1416 7 | #define rad(a) ((a) * PI) / (float)180 8 | #define COS(a) cos(rad(a)) 9 | #define SIN(a) sin(rad(a)) 10 | #define deg(a) ((float)a * 180) / PI 11 | 12 | 13 | using namespace std; 14 | 15 | 16 | int lambda = 180; //length of a wave (in pixels) 17 | int gap = 60; //width of each color in flag 18 | float t, f = 10, n1 = 1.8, n2 = 1; //t is for time and f is frequency; n1 and n2 controls the scaling of the wave 19 | float delta = 0.01; //used for comparing the float difference 20 | float k = 2 * PI / lambda; //wave number 21 | float w = 2 * PI * f; //angular wave frequency 22 | 23 | int y1, y2, x1, x2; //these variables are used for the starting/ending points of each horizontal/vertical wave 24 | 25 | 26 | //for drawing the horizontal standing wave 27 | void drawWaveH(int xc, int yc, int color, int s = 0) 28 | { 29 | //xc and yc is the point from where the wave is drawn 30 | int amp = 10; 31 | //s is used for the starting/ending point of the wave 32 | for(float x = 20 + s; x < n1 * lambda + s; x += 0.01){ 33 | //formula of standing wave in terms of y 34 | int y = amp * sin(k*x) * cos(w*t); 35 | if(abs(x - 20) <= delta) 36 | y1 = y; 37 | if(abs(x - n1 * lambda) <= delta) 38 | y2 = y; 39 | 40 | putpixel(xc + x, yc - y, color); 41 | } 42 | } 43 | 44 | 45 | //for drawing the vertical standing wave 46 | void drawWaveV(int xc, int yc, int color, int s) 47 | { 48 | //xc and yc is the point from where the wave is drawn 49 | int amp = 10; 50 | //s is used for the starting/ending point of the wave 51 | for(float y = s - 2; y < n2 * lambda + s + 2; y += 0.01){ 52 | //formula of standing wave in terms of x 53 | int x = amp * sin(k*y) * cos(w*t); 54 | if(abs(y - gap - 5) <= delta) 55 | x1 = x; 56 | if(abs(y - 2 * gap - 5) <= delta) 57 | x2 = x; 58 | 59 | putpixel(xc + x, yc + y, color); 60 | } 61 | } 62 | 63 | 64 | int main() 65 | { 66 | initwindow(1400, 1000); 67 | int xc = getmaxx() / 2, yc = getmaxy() / 2, page = 0; 68 | 69 | for(t = 0; !kbhit(); t+= 0.01){ 70 | //Drawing the flag and coloring it 71 | drawWaveH(xc - 155, 120, WHITE); 72 | drawWaveH(xc - 155, 120 + 3 * gap, WHITE); 73 | drawWaveV(xc - 135, 120, WHITE, -y1); 74 | drawWaveV(xc - 157 + n1 * lambda, 120, WHITE, -y2); 75 | drawWaveH(xc - 155, 120 + gap, WHITE, x1); 76 | drawWaveH(xc - 155, 120 + 2 * gap, WHITE, x2); 77 | setcolor(WHITE); 78 | line(xc - 155, 115, xc - 135, 120 - y1); line(xc - 155, 125 + 3 * gap, xc - 135, 120 - y1 + 3 * gap); 79 | setfillstyle(SOLID_FILL, LIGHTRED); floodfill(xc - 75, 145, WHITE); 80 | setfillstyle(SOLID_FILL, WHITE); floodfill(xc - 75, 145 + gap, WHITE); 81 | setfillstyle(SOLID_FILL, GREEN); floodfill(xc - 75, 145 + 2 * gap, WHITE); 82 | 83 | //Drawing the Ashoka Chakra 84 | int r = gap / 2 - 2, xw = xc + 27, yw = 120 + 1.5 * gap + (2 * cos(w*t)); 85 | setlinestyle(SOLID_LINE, 0, 4); setcolor(BLUE); circle(xw, yw, r); 86 | setlinestyle(SOLID_LINE, 0, 2); 87 | for(int a = 0; a < 180; a += 15) 88 | line(xw + r * COS(a), yw - r * SIN(a), xw - r * COS(a), yw + r * SIN(a)); 89 | 90 | //Drawing the pole and the stand 91 | setlinestyle(SOLID_LINE, 0, 1); 92 | setcolor(LIGHTGRAY); setfillstyle(SOLID_FILL, LIGHTGRAY); 93 | circle(xc - 160, 88, 13); rectangle(xc - 165, 100, xc - 155, 850); 94 | fillellipse(xc - 160, 850, 40, 10); fillellipse(xc - 160, 880, 40, 10); 95 | line(xc - 200, 850, xc - 200, 880); line(xc - 120, 850, xc - 120, 880); 96 | fillellipse(xc - 160, 885, 70, 15); fillellipse(xc - 160, 920, 70, 15); 97 | line(xc - 230, 885, xc - 230, 920); line(xc - 90, 885, xc - 90, 920); 98 | floodfill(xc - 160, 140, LIGHTGRAY); floodfill(xc - 160, 845, LIGHTGRAY); 99 | floodfill(xc - 160, 862, LIGHTGRAY); floodfill(xc - 160, 902, LIGHTGRAY); 100 | setfillstyle(SOLID_FILL, YELLOW); floodfill(xc - 160, 88, LIGHTGRAY); 101 | 102 | setcolor(YELLOW); settextstyle(9, HORIZ_DIR, 1); 103 | outtextxy(xc + 100, yc, "\"THE INDIAN NATIONAL FLAG\""); 104 | 105 | //double buffering 106 | setactivepage(page); setvisualpage(1 - page); 107 | page = 1 - page; cleardevice(); 108 | } 109 | 110 | getch(); 111 | return 0; 112 | } 113 | -------------------------------------------------------------------------------- /the-Indian-national-flag/nationalFlag.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/the-Indian-national-flag/nationalFlag.exe -------------------------------------------------------------------------------- /the-Indian-national-flag/nationalFlag.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/k3v007/ComputerGraphics_Programs/380862df6d21504c004a812711d96f0255e3be8a/the-Indian-national-flag/nationalFlag.o --------------------------------------------------------------------------------