C के Graphics Mini Projects
In this page:
Classic Screensaver
एक bouncing-shapes screensaver कई shapes combine करता है हर एक अपनी position और direction variables track करते हुए, screen edge तक पहुँचने पर हर shape की direction independently reverse करते हुए, जो animation basics से same bounce logic है एक साथ कई objects पर apply किया गया।
उदाहरण: Classic Screensaver
#include <graphics.h>
// Program execution starts in main()
int main() {
initgraph(0, 0, "");
int x = 0, direction = 1;
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
circle(x, 100, 10);
if (x >= 200 || x <= 0) direction = -direction;
x += direction * 10;
delay(50);
}
closegraph();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Interactive Paint Brush
एक interactive paint program mouse clicks (या drags) सुनता है और, हर detected click पर, current cursor coordinates पर एक छोटा shape draw करता है, hardcoded shape calls की बजाय user के अपने input से screen पर एक picture बनाते हुए।
उदाहरण: Interactive Paint Brush
#include <graphics.h>
#include <dos.h>
int main() {
initgraph(0, 0, "");
union REGS in, out;
in.x.ax = 3;
int86(0x33, &in, &out);
int x = out.x.cx, y = out.x.dx;
circle(x, y, 3);
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Analog Clock
एक analog clock face current time को hand positions में convert करने के लिए trigonometry इस्तेमाल करता है: minute hand के लिए angle current minute value से आता है, और उस angle पर sin() और cos() apply करना (X, Y) endpoint compute करता है जहाँ हर hand draw होना चाहिए।
उदाहरण: Analog Clock
#include <graphics.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Include time functions
#include <time.h>
// Program execution starts in main()
int main() {
initgraph(0, 0, "");
time_t now = time(NULL);
struct tm *local = localtime(&now);
double angle = local->tm_min * 6 * (3.14159 / 180);
// Declare x and set it to 150 + (int)(50 * sin(angle))
int x = 150 + (int)(50 * sin(angle));
// Declare y and set it to 150 - (int)(50 * cos(angle))
int y = 150 - (int)(50 * cos(angle));
line(150, 150, x, y);
closegraph();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Simple Traffic Light Simulator
एक traffic-light simulator sequence में red, yellow, और green filled circles draw करने के through cycle करता है, हर state change के बीच delay() से pause करते हुए, जो एक छोटे program में timed animation को simple state-machine logic के साथ combine करने का एक practical तरीका है।
उदाहरण: Simple Traffic Light Simulator
#include <graphics.h>
int main() {
initgraph(0, 0, "");
setfillstyle(SOLID_FILL, RED);
fillellipse(100, 50, 20, 20);
delay(500);
setfillstyle(SOLID_FILL, GREEN);
fillellipse(100, 150, 20, 20);
closegraph();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Bar Chart
एक dynamic bar chart filled rectangles की एक series draw करता है जिनकी heights numeric values के एक set के proportional हैं, और उन heights को कई frames में zero से उनकी final value तक बढ़ते हुए animate करना एक static chart को visually engaging reveal में बदल देता है।
उदाहरण: Dynamic Bar Chart
#include <graphics.h>
// Program execution starts in main()
int main() {
initgraph(0, 0, "");
int heights[3] = {50, 80, 30};
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
bar(i * 50, 200 - heights[i], i * 50 + 30, 200);
}
closegraph();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- clear किए बिना दोबारा draw करना, ताकि पुराने frames screen पर जमा होते रहें।
delayभूल जाना ताकि animation देखने के लिए बहुत तेज़ चले।- program के अंत में graphics mode बंद करना भूल जाना।
- C graphics work screen पर draw करने के लिए
graphics.hlibrary और SDL2 इस्तेमाल करता है। - आप shapes, lines, curves, colors, और text draw करते हैं, और animation और mouse input add कर सकते हैं।
- Mini projects इन pieces को छोटे graphical programs में combine करते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: