Hallo alle zusammen,
Ich versuche gerade eine Klasse für SFML zu implementieren, die Linien mit abgerundeten Ecken zeichnet. Meine Klasse erbt von der sf:
![:S](wcf/images/smilies/unsure.png.pagespeed.ce.aMqJfMEsmU.png)
hape Klasse. Ich dachte mir ich fange an mit ner Klasse, die ganz normale Linien macht. Das funktioniert auch alles sehr gut soweit. Aber ich bin mir nicht sicher, wie ich die abgerundeten ecken machen soll. Ich dachte mir, dass ich einfach zwei Halbkreise berechne und diese einfach an die enden anfüge, aber ich weis nicht genau, wie ich die "Rotation" der Halbkreise machen soll. Könnte mir jemand helfen ?
Hier ist mein Code:
Header File:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#ifndef ROUNDENDEDLINE_H
#define ROUNDENDEDLINE_H
#include <SFML/Graphics/Shape.hpp>
#include <cmath>
#include <iostream>
class CRoundendedLine : public sf::Shape
{
public:
CRoundendedLine(const sf::Vector2f& endPoint = sf::Vector2f(0, 0), const float width = 1.0);
void setEndPoint(const sf::Vector2f& endPoint);
void setWidth(const float width);
virtual unsigned int getPointCount() const;
virtual sf::Vector2f getPoint(unsigned int index) const;
private :
sf::Vector2f m_endPoint;
float m_Width;
};
#endif //ROUNDENDEDLINE_H
|
Implementierung:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include "RoundendedLine.h"
CRoundendedLine::CRoundendedLine(const sf::Vector2f& endPoint, const float width) : m_endPoint (endPoint), m_Width (width)
{
update();
}
void CRoundendedLine::setEndPoint(const sf::Vector2f& endPoint)
{
m_endPoint = endPoint;
update();
}
void CRoundendedLine::setWidth(const float width)
{
m_Width = width;
update();
}
unsigned int CRoundendedLine::getPointCount() const
{
return 4;
}
// Compute the normal of a segment
sf::Vector2f computeNormal(const sf::Vector2f& p1, const sf::Vector2f& p2)
{
sf::Vector2f normal(p1.y - p2.y, p2.x - p1.x);
float length = std::sqrt(normal.x * normal.x + normal.y * normal.y);
if (length != 0.f)
normal /= length;
return normal;
}
sf::Vector2f CRoundendedLine::getPoint(unsigned int index) const
{
sf::Vector2f P1(1.0, 0.0);
sf::Vector2f P2(m_endPoint + sf::Vector2f(1.0, 0.0) - getPosition());
// Compute the extrusion direction
sf::Vector2f Normal = computeNormal(P1, P2);
Normal *= m_Width / 2;
switch (index)
{
default:
case 0: return sf::Vector2f(P1 - Normal);
case 1: return sf::Vector2f(P2 - Normal);
case 2: return sf::Vector2f(P2 + Normal);
case 3: return sf::Vector2f(P1 + Normal);
}
}
|
Verwendung:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <SFML/Graphics.hpp>
#include "RoundendedLine.h"
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
CRoundendedLine RoundendedLine;
RoundendedLine.setPosition(200,200);
RoundendedLine.setEndPoint(sf::Vector2f(270, 300));
RoundendedLine.setWidth(11);
sf::Vertex* startVertex = new sf::Vertex(sf::Vector2f(200, 200), sf::Color::Red);
sf::Vertex* endVertex = new sf::Vertex(sf::Vector2f(270, 300), sf::Color::Blue);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Close Event
if (event.type == sf::Event::Closed)
window.close();
// Escape key pressed
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
window.close();
}
window.clear();
window.draw(RoundendedLine);
window.draw(startVertex, 1, sf::Points);
window.draw(endVertex, 1, sf::Points);
window.display();
}
return 0;
}
|