← Back to C++ Course | Chapter 18: Graphics | Lesson 3 of 7

C++ में Shapes बनाना (SFML)

SFML के साथ आप screen पर circles, rectangles, और दूसरी shapes draw कर सकते हैं, shape stamps इस्तेमाल करने की तरह। आप इन्हें color, move, और rotate कर सकते हैं।
Syntax
cpp
sf::CircleShape circle(radius);
circle.setFillColor(sf::Color::Color_Name);
circle.setPosition(x, y);

sf::RectangleShape rect(sf::Vector2f(width, height));

window.draw(circle);

Circles Draw करना

sf::CircleShape सिर्फ इसका radius specify करके एक filled circle draw करता है; SFML internally outline को एक many-sided polygon की तरह compute करता है। आप इसका appearance setFillColor() से control करते हैं और इसे setPosition() से window में कहीं भी reposition कर सकते हैं।

उदाहरण: Drawing Circles

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::CircleShape circle(50.f);
	circle.setFillColor(sf::Color::Green);
	circle.setPosition(100, 100);
	return 0;
}

Rectangles Draw करना

sf::RectangleShape एक rectangle draw करता है जिसकी width और height आप setSize() को pass किए गए एक sf::Vector2f से define करते हैं -- यह two-component vector type SFML में हर जगह इस्तेमाल होता है जहाँ भी एक 2D size या position चाहिए हो।

उदाहरण: Drawing Rectangles

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::RectangleShape rect;
	rect.setSize(sf::Vector2f(120.f, 50.f));
	rect.setFillColor(sf::Color::Blue);
	return 0;
}

Convex और Custom Shapes

sf::ConvexShape आपको simple circles और rectangles से आगे arbitrary polygons draw करने देता है।

आपको पहले setPointCount() call करके declare करना होगा कि shape के कितने vertices हैं, फिर setPoint(index, position) से हर vertex की position individually set करनी होगी -- SFML outline बनाने के लिए इन्हें order में जोड़ता है।

उदाहरण: Convex and Custom Shapes

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::ConvexShape triangle;
	triangle.setPointCount(3);
	triangle.setPoint(0, sf::Vector2f(0, 0));
	triangle.setPoint(1, sf::Vector2f(100, 0));
	triangle.setPoint(2, sf::Vector2f(50, 100));
	return 0;
}

Shapes Transform करना

हर SFML shape setPosition(), setRotation(), और setScale() support करता है, जो shape की अपनी geometry से independently एक transform apply करते हैं -- इसलिए आप हर बार इसके underlying points redefine किए बिना एक shape को frame-to-frame move, spin, या resize कर सकते हैं (animation के लिए)।

उदाहरण: Transforming Shapes

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::CircleShape shape(30.f);
	shape.setPosition(50, 50);
	shape.setRotation(45.f);
	shape.setScale(1.5f, 1.5f);
	return 0;
}

Coloring और Outlines

एक solid fill color से आगे, setOutlineColor() और setOutlineThickness() एक shape के edge के चारों ओर एक visible border add करते हैं, जो overlapping shapes को visually distinct बनाने या user ने select की एक shape highlight करने के लिए useful है।

उदाहरण: Coloring and Outlines

cpp
#include <SFML/Graphics.hpp>

int main() {
	sf::CircleShape shape(40.f);
	shape.setFillColor(sf::Color::Yellow);
	shape.setOutlineColor(sf::Color::Black);
	shape.setOutlineThickness(3.f);
	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. window.clear() से पहले window.draw(shape) call करना, इसलिए नई shape दिखने से पहले wipe हो जाती है।
  2. एक position या fill color set करना भूल जाना, इसलिए shape default white color में top-left corner पर बैठती है।
  3. एक CircleShape radius set करना लेकिन position को इसका center समझना, जब position इसका top-left corner होता है जब तक आप origin न बदलें।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.