C++ में Shapes बनाना (SFML)
In this page:
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
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape circle(50.f);
circle.setFillColor(sf::Color::Green);
circle.setPosition(100, 100);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Rectangles Draw करना
sf::RectangleShape एक rectangle draw करता है जिसकी width और height आप setSize() को pass किए गए एक sf::Vector2f से define करते हैं -- यह two-component vector type SFML में हर जगह इस्तेमाल होता है जहाँ भी एक 2D size या position चाहिए हो।
उदाहरण: Drawing Rectangles
#include <SFML/Graphics.hpp>
int main() {
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(120.f, 50.f));
rect.setFillColor(sf::Color::Blue);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
Coloring और Outlines
एक solid fill color से आगे, setOutlineColor() और setOutlineThickness() एक shape के edge के चारों ओर एक visible border add करते हैं, जो overlapping shapes को visually distinct बनाने या user ने select की एक shape highlight करने के लिए useful है।
उदाहरण: Coloring and Outlines
#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;
}
Login to try C/C++/Java/PHP code in the editor
window.clear()से पहलेwindow.draw(shape)call करना, इसलिए नई shape दिखने से पहले wipe हो जाती है।- एक position या fill color set करना भूल जाना, इसलिए shape default white color में top-left corner पर बैठती है।
- एक
CircleShaperadius set करना लेकिन position को इसका center समझना, जब position इसका top-left corner होता है जब तक आप origin न बदलें।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: