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

C में Lines और Curves बनाना

आप screen पर straight lines, arcs, और curves simple commands से draw कर सकते हैं, एक ruler और एक compass इस्तेमाल करने जैसा। आप बदल सकते हैं कि एक line कितनी thick या dotted है।
Syntax
c
line(x1, y1, x2, y2);                    // segment between two points

moveto(x, y);                            // move cursor without drawing
lineto(x, y);                            // draw from cursor to (x, y)

arc(x, y, start_angle, end_angle, radius);

Straight Lines Draw करना

line() आपके explicitly specify किए दो coordinates के बीच सीधे एक straight segment draw करता है — एक start point (X1, Y1) और एक end point (X2, Y2) — इसे किसी drawing state track किए बिना screen पर दो known points connect करने का सबसे simple तरीका बनाते हुए।

उदाहरण: Drawing Straight Lines

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	line(50, 50, 200, 200);
	getch();
	closegraph();
	return 0;
}

Current Position से Draw करना

BGI graphics एक implicit 'current position' cursor maintain करता है: moveto() बिना कुछ draw किए उस cursor को reposition करता है, जबकि lineto() cursor जहाँ currently बैठा है वहाँ से एक नए target point तक एक line draw करता है और फिर cursor को उस नए point पर छोड़ देता है, जो segments की एक connected chain draw करने के लिए convenient है।

उदाहरण: Drawing from Current Position

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	moveto(50, 50);
	lineto(150, 150);
	getch();
	closegraph();
	return 0;
}

Arcs Draw करना

arc() एक पूरे circle की बजाय इसकी circumference का एक curved segment draw करता है, center coordinates, एक starting angle, एक ending angle (दोनों degrees में), और radius लेते हुए — pie-chart wedges या rounded corners जैसी चीज़ों के लिए useful।

उदाहरण: Drawing Arcs

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	arc(150, 150, 0, 180, 50);
	getch();
	closegraph();
	return 0;
}

Line Styles और Thickness

setlinestyle() बदलता है कि बाद में draw की गई lines कैसी दिखती हैं, SOLID_LINE, DOTTED_LINE, या DASHED_LINE जैसे एक style constant को एक thickness value के साथ accept करते हुए, आपको same drawing में अलग तरह की lines (जैसे grid lines बनाम data lines) visually अलग करने देते हुए।

उदाहरण: Line Styles and Thickness

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	setlinestyle(DASHED_LINE, 0, 3);
	line(50, 50, 200, 50);
	getch();
	closegraph();
	return 0;
}

Custom Curves Draw करना

एक loop के अंदर एक mathematical formula से computed coordinates पर individual pixels plot करना — उदाहरण के लिए sin() और cos() इस्तेमाल करके एक wave के along points generate करना — आपको smooth curves draw करने देता है जो कोई भी built-in shape function सीधे produce नहीं कर सकता।

उदाहरण: Drawing Custom Curves

c
#include <graphics.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	initgraph(0, 0, "");
	// Loop: repeat while the condition holds
	for (int x = 0; x < 100; x++) {
		// Declare y and set it to 100 + (int)(50 * sin(x * 0.1))
		int y = 100 + (int)(50 * sin(x * 0.1));
		putpixel(x, y, WHITE);
	}
	getch();
	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. line(x1, y1, x2, y2) argument order confuse करना और गलत point पर draw करना।
  2. यह भूल जाना कि y नीचे की ओर बढ़ता है, इसलिए ऊपर जाने वाली एक line नीचे चली जाती है।
  3. arc को radians में angles pass करना, जब arc को degrees चाहिए।

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.