← Back to C Course | Chapter 13: Graphics | Lesson 10 of 10

C के Graphics Mini Projects

ये graphics mini projects, जैसे एक clock या एक paint brush, आपने drawing के बारे में जो सीखा उसे एक साथ लाते हैं, हर part सीखने के बाद छोटे models बनाने जैसा। ये practice करने के मज़ेदार तरीके हैं।

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}

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

c
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. clear किए बिना दोबारा draw करना, ताकि पुराने frames screen पर जमा होते रहें।
  2. delay भूल जाना ताकि animation देखने के लिए बहुत तेज़ चले।
  3. program के अंत में graphics mode बंद करना भूल जाना।
चैप्टर सारांश
  • C graphics work screen पर draw करने के लिए graphics.h library और SDL2 इस्तेमाल करता है।
  • आप shapes, lines, curves, colors, और text draw करते हैं, और animation और mouse input add कर सकते हैं।
  • Mini projects इन pieces को छोटे graphical programs में combine करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.