#include <iostream>
#include <vector>
#include <cmath>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RectangleShape.hpp>
namespace vec
{
float length(const sf::Vector2f& v)
{
return std::sqrtf(v.x * v.x + v.y * v.y);
}
sf::Vector2f unit(const sf::Vector2f& v)
{
constexpr float eps = std::numeric_limits<float>::epsilon();
float len = length(v);
return std::abs(len) > eps ? v / len : sf::Vector2f{0, 0};
}
}
constexpr float PI = 3.141592653f;
int main(int argc, char** argv)
{
constexpr unsigned int windowWidth = 800;
constexpr unsigned int windowHeight = 600;
constexpr float shapeSize = 50;
sf::RenderWindow window;
window.create({windowWidth, windowHeight}, "sfml", sf::Style::Close | sf::Style::Titlebar);
window.setVerticalSyncEnabled(true);
window.setKeyRepeatEnabled(false);
// entities
sf::RectangleShape shape{{shapeSize, shapeSize}};
shape.setFillColor(sf::Color::Green);
shape.setOrigin(shapeSize / 2.f, shapeSize / 2.f);
shape.setPosition(shapeSize, shapeSize);
std::vector<sf::VertexArray> arrows;
arrows.emplace_back(sf::Lines, 2);
arrows.back()[0].position = {shapeSize, shapeSize};
arrows.back()[0].color = sf::Color::Cyan;
arrows.back()[1].position = {shapeSize, shapeSize * 2};
arrows.back()[1].color = sf::Color::Cyan;
arrows.emplace_back(sf::Lines, 2);
arrows.back()[0].position = {shapeSize, shapeSize};
arrows.back()[0].color = sf::Color::Magenta;
arrows.back()[1].position = {shapeSize * 2, shapeSize * 2};
arrows.back()[1].color = sf::Color::Magenta;
auto vec0Dir = vec::unit(arrows.front()[1].position - arrows.front()[0].position);
auto vec1Dir = vec::unit(arrows.back()[1].position - arrows.back()[0].position);
auto angle0 = std::atan2f(vec0Dir.y, vec0Dir.x) * 180.f / PI;
auto angle1 = std::atan2f(vec1Dir.y, vec1Dir.x) * 180.f / PI;
std::cout << "angle 0: " << angle0 << '\n'
<< "angle 1: " << angle1 << '\n';
sf::Clock clock;
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
if(clock.getElapsedTime() > sf::seconds(4))
{
shape.setRotation(angle0);
clock.restart();
}
else if(clock.getElapsedTime() > sf::seconds(2))
{
shape.setRotation(angle1);
}
window.clear();
window.draw(shape);
for(auto& d : arrows)
window.draw(d);
window.display();
}
return 0;
}