• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 21st, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <SFML/Graphics.hpp>
2. #include <SFML/Window.hpp>
3. #include <SFML/System.hpp>
4. #include <list>
5. #include <math.h>
6. #include <iostream>
7.
8. // Pi
9. #define PI 3.1415926535f
10.
11. using namespace std;
12.
13. // Structures
14. struct sPoint
15. {
16.     float x = 0;
17.     float y = 0;
18. };
19. struct sSpline
20. {
21.     std::vector<sPoint> points;
22.     std::vector<sf::Sprite*> sprites;
23.
24.     sPoint getSplinePoint(float t)
25.     {
26.         sPoint p;
27.
28.         int p0, p1, p2, p3;
29.         p1 = (int)t + 1;
30.         p2 = p1 + 1;
31.         p3 = p2 + 1;
32.         p0 = p1 - 1;
33.
34.         float tt = t * t;
35.         float ttt = tt * t;
36.
37.         float q1 = -ttt + 2.0f * tt - t;
38.         float q2 = 3.0f * ttt - 5.0f * tt + 2.0f;
39.         float q3 = -3.0f * ttt + 4.0f * tt + t;
40.         float q4 = ttt - tt;
41.
42.         p.x = 0.5f * (points[p0].x * q1 + points[p1].x * q2 + points[p2].x * q3 + points[p3].x * q4);
43.         p.y = 0.5f * (points[p0].y * q1 + points[p1].y * q2 + points[p2].y * q3 + points[p3].y * q4);
44.
45.         return p;
46.     }
47. };
48.
49. // Prototypes
50. void UserInput(std::vector<sPoint> &ControlPoints, std::vector<sf::Sprite*> &ControlPointsSprites, int &ControlPointHandler);
51. void MoveControlPoint(std::vector<sPoint> &ControlPoints, std::vector<sf::Sprite*> &ControlPointsSprites, int &ControlPointHandler);
52. void drawSpline(sSpline &spline, std::vector<sPoint> &ControlPoints, sf::RenderWindow &window);
53.
54. int main()
55. {
56.
57.     // Fenster öffnen
58.     sf::RenderWindow window(sf::VideoMode(1920, 1080), "Bezier Curve", sf::Style::Fullscreen);
59.
60.     // Hintergrundbild
61.     sf::Sprite s;
62.     sf::IntRect r;
63.     sf::Texture t;
64.
66.     r.top = 0.0f;
67.     r.left = 0.0f;
68.     r.width = 1920.0f;
69.     r.height = 1080.0f;
70.
71.     s.setTexture(t);
72.     s.setTextureRect(r);
73.     window.draw(s);
74.
75.     // Punkte
76.     sPoint p1;
77.     p1.x = 100.0f;
78.     p1.y = 500.0f;
79.     sPoint p2;
80.     p2.x = 300.0f;
81.     p2.y = 500.0f;
82.     sPoint p3;
83.     p3.x = 500.0f;
84.     p3.y = 500.0f;
85.     sPoint p4;
86.     p4.x = 700.0f;
87.     p4.y = 500.0f;
89.     std::vector<sPoint> ControlPoints;
90.     ControlPoints.push_back(p1);
91.     ControlPoints.push_back(p2);
92.     ControlPoints.push_back(p3);
93.     ControlPoints.push_back(p4);
94.     // Vector for sprites
95.     std::vector<sf::Sprite*> ControlPointsSprites;
96.
97.     // Draw ControlPoints
98.     for (int i = 0; i < ControlPoints.size(); i++)
99.     {
100.         // Init
101.         sf::Sprite *sprite = new sf::Sprite;
102.         sf::Texture *texture = new sf::Texture;
103.         sf::IntRect *rect = new sf::IntRect;
104.
105.         // Def
107.         rect->left = 0;
108.         rect->top = 0;
109.         rect->width = 16.0f;
110.         rect->height = 16.0f;
111.         sprite->setTexture(*texture);
112.         sprite->setTextureRect(*rect);
113.         sprite->setOrigin(8.0f, 8.0f);
114.         sprite->setPosition(ControlPoints.at(i).x, ControlPoints.at(i).y);
115.
116.         ControlPointsSprites.push_back(sprite);
117.     }
118.
119.     // Variables for main loop
120.     bool beenden = false;
121.     int ControlPointsHandler = 100;
122.     sSpline spline;
123.     spline.points = ControlPoints;
124.     drawSpline(spline, ControlPoints, window);
125.
126.     while (!beenden)
127.     {
128.         // Clear
129.         window.clear();
130.
131.         // Events
132.         sf::Event e;
133.         if (window.pollEvent(e))
134.         {
135.             if (e.type = sf::Event::KeyPressed)
136.                 if(e.key.code == sf::Keyboard::LAlt)
137.                     beenden = true;
138.         }
139.
140.         // Handle UserInput
141.         UserInput(ControlPoints, ControlPointsSprites, ControlPointsHandler);
142.         MoveControlPoint(ControlPoints, ControlPointsSprites, ControlPointsHandler);
143.
144.         // Draw
145.             // BackgroundImage
146.             window.draw(s);
147.             // ControlPoints
148.             for (int i = 0; i < ControlPointsSprites.size(); i++)
149.                 window.draw(*ControlPointsSprites.at(i));
150.             // Spline points
151.             for (int i = 0; i < spline.sprites.size(); i++)
152.                 window.draw(*spline.sprites.at(i));
153.
154.         // Display
155.         window.display();
156.     }
157.
158.     return 0;
159. }
160.
161. void UserInput(std::vector<sPoint> &ControlPoints, std::vector<sf::Sprite*> &ControlPointsSprites, int &ControlPointHandler)
162. {
163.     if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && ControlPointHandler == 100)
164.     {
165.         for (int i = 0; i < ControlPoints.size(); i++)
166.         {
167.             if (ControlPointsSprites.at(i)->getGlobalBounds().contains(sf::Mouse::getPosition().x,
168.                                                                        sf::Mouse::getPosition().y))
169.             {
170.                 ControlPointHandler = i;
171.                 ControlPointsSprites.at(ControlPointHandler)->setColor(sf::Color::Red);
172.             }
173.         }
174.     }
175.     if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
176.     {
177.         if(ControlPointHandler != 100)
178.             ControlPointsSprites.at(ControlPointHandler)->setColor(sf::Color::Black);
179.         ControlPointHandler = 100;
180.     }
181. }
182. void MoveControlPoint(std::vector<sPoint> &ControlPoints, std::vector<sf::Sprite*> &ControlPointsSprites, int &ControlPointHandler)
183. {
184.     if (ControlPointHandler != 100)
185.     {
186.         ControlPoints.at(ControlPointHandler).x = sf::Mouse::getPosition().x;
187.         ControlPoints.at(ControlPointHandler).y = sf::Mouse::getPosition().y;
188.         ControlPointsSprites.at(ControlPointHandler)->setPosition(ControlPoints.at(ControlPointHandler).x, ControlPoints.at(ControlPointHandler).y);
189.     }
190. }
191. void drawSpline(sSpline &spline, std::vector<sPoint> &ControlPoints, sf::RenderWindow &window)
192. {
193.     for (float t = 0.0f; t < 1.0f; t += 0.01f)
194.     {
195.         // Punkt
196.         sPoint pos = spline.getSplinePoint(t);
197.
198.         // Draw
199.         sf::Sprite *sprite = new sf::Sprite;
200.         sf::IntRect *rect = new sf::IntRect;
201.         sf::Texture *texture = new sf::Texture;
202.
203.         // Winkel zwischen Punkten
204.         float gegenkathete = spline.points.at(t).y - ControlPoints.at(t - 0.01f).y;
205.         float ankathete = spline.points.at(t).x - ControlPoints.at(t - 0.01f).x;
206.         float hypothenuse = std::pow(gegenkathete, 2) + std::pow(ankathete, 2);
207.         float winkel = atan(gegenkathete / ankathete) * 180 / PI;
208.
210.         rect->top = 0.0f;
211.         rect->left = 0.0f;
212.         rect->width = 16.0f;
213.         rect->height = 16.0f;
214.
215.         sprite->setTexture(*texture);
216.         sprite->setTextureRect(*rect);
217.         sprite->setRotation(winkel);
218.         sprite->setScale(std::pow(hypothenuse, 0.5f) / 2.0f, 1);
219.         sprite->setOrigin(rect->width / 2, rect->height / 2);
220.         sprite->setPosition(spline.points.at(t).x, spline.points.at(t).y);
221.
222.         spline.sprites.push_back(sprite);
223.
224.         window.draw(*sprite);
225.     }
226. }
227.
228.
229. /*void drawBezier(Point p1, Point p2, Point p3, sf::RenderWindow &window)
230. {
231.     std::list<Point> p;
232.
233.     for (float t = 0.0f; t < 1; t += 0.01f)
234.     {
235.
236.         Point P;
237.         P.x = std::pow(1 - t, 2) * p1.x + (1 - t) * 2 * t * p2.x + t * t * p3.x;
238.
239.         P.y = std::pow(1 - t, 2) * p1.y + (1 - t) * 2 * t * p2.y + t * t * p3.y;
240.         p.push_back(P);
241.     }
242.
243.     // Iterator
244.     std::list<Point>::iterator it = p.begin();
245.     Point lastPoint = *it;
246.     it++;
247.
248.     while(it != p.end())
249.     {
250.         // SFML Graphic
251.         sf::Sprite *S = new sf::Sprite;
252.         sf::IntRect *R = new sf::IntRect;
253.         sf::Texture *T = new sf::Texture;
254.
255.         // Winkel zwischen Punkten
256.         float gegenkathete = it->y - lastPoint.y;
257.         float ankathete = it->x - lastPoint.x;
258.         float hypothenuse = std::pow(gegenkathete, 2) + std::pow(ankathete, 2);
259.         float winkel = atan(gegenkathete / ankathete) * 180 / PI;
260.
262.         R->top = 0.0f;
263.         R->left = 0.0f;
264.         R->width = 2.0f;
265.         R->height = 2.0f;
266.
267.         S->setTexture(*T);
268.         S->setTextureRect(*R);
269.         S->setRotation(winkel);
270.         S->setScale(std::pow(hypothenuse, 0.5f) / 2.0f, 1);
271.         //S->setOrigin(R->width / 2, R->height / 2);
272.
273.         S->setPosition(it->x, it->y);
274.         lastPoint = *it;
275.         it++;
276.
277.         window.draw(*S);
278.     }
279. }
280. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top