-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShearing algorithm.cpp
More file actions
76 lines (61 loc) · 2.26 KB
/
Shearing algorithm.cpp
File metadata and controls
76 lines (61 loc) · 2.26 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
int main()
{
// Window size
const int WIDTH = 800;
const int HEIGHT = 600;
// Create the window
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Shearing Example");
// Create a rectangle shape
sf::RectangleShape rectangle(sf::Vector2f(200.0f, 100.0f));
rectangle.setPosition(WIDTH / 2.0f, HEIGHT / 2.0f);
rectangle.setOrigin(100.0f, 50.0f);
rectangle.setFillColor(sf::Color::Red);
// Shearing factor
float shearFactor = 0.0f;
bool increasing = true;
// Animation speed
float animationSpeed = 0.01f;
// Game loop
while (window.isOpen())
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Clear the window
window.clear(sf::Color::Black);
// Update the shearing factor
if (increasing)
{
shearFactor += animationSpeed;
if (shearFactor >= 0.5f)
increasing = false;
}
else
{
shearFactor -= animationSpeed;
if (shearFactor <= -0.5f)
increasing = true;
}
// Create a sheared rectangle shape
sf::ConvexShape shearedRect;
shearedRect.setPointCount(4);
shearedRect.setPoint(0, sf::Vector2f(rectangle.getPosition().x, rectangle.getPosition().y));
shearedRect.setPoint(1, sf::Vector2f(rectangle.getPosition().x + rectangle.getSize().x, rectangle.getPosition().y));
shearedRect.setPoint(2, sf::Vector2f(rectangle.getPosition().x + rectangle.getSize().x + rectangle.getSize().y * shearFactor, rectangle.getPosition().y + rectangle.getSize().y));
shearedRect.setPoint(3, sf::Vector2f(rectangle.getPosition().x + rectangle.getSize().y * shearFactor, rectangle.getPosition().y + rectangle.getSize().y));
shearedRect.setFillColor(rectangle.getFillColor());
// Draw the original and sheared rectangle
window.draw(shearedRect);
// Update the window
window.display();
// Delay between frames
sf::sleep(sf::milliseconds(16)); // 60 FPS (1000 ms / 60)
}
return 0;
}