Pessoal, tenho um código que ao clicar na tela desenha um ponto e ao clicar pela segunda vez desenha outro ponto, ligando um ponto ao outro com uma linha, dizendo a distancia entre eles, o código está funcionando normal, só que eu gostaria de calcular o angulo entre os dois pontos e mostrar isso escrito na tela como é feita com a distancia, alguem poderia me ajudar com isso?
O código é o seguinte:
[code]#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include
#include
using namespace std;
class Circulo {//classe circulo
public:
int x;
int y;
int r;
};
float dist_eucl(int x1, int y1, int x2, int y2){//função para calculo de distancia euclidiana
return sqrt((((x2 - x1)(x2 - x1)) + ((y2 - y1)(y2 - y1))));
}
int main()//inicio da mais
{
sf::RenderWindow* app = new sf::RenderWindow(sf::VideoMode(800, 600, 32), “Exercicio 2”);
int id_ponto = 0;
Circulo* c1 = new Circulo;
Circulo* c2 = new Circulo;
sf::Shape p1;
sf::Shape p2;
int mouseX, mouseY;//posição do mouse na tela
float raio = 1;
//texto fixo
sf::String* txttitulo;
sf::String* txtpos1;
sf::String* txtpos2;
sf::String* txtdistancia;
int xtxttitulo;
int ytxttitulo;
int xtxtpos1;
int ytxtpos1;
int xtxtpos2;
int ytxtpos2;
int xtxtdistancia;
int ytxtdistancia;
// texto variavel
sf::String* txtpos11;
sf::String* txtpos22;
sf::String* txtdistancia1;
int xtxtpos11;
int ytxtpos11;
int xtxtpos22;
int ytxtpos22;
int xtxtdistancia1;
int ytxtdistancia1;
string str1;
string str2;
string str3;
bool abertura = true;
while (app->IsOpened()) {
xtxttitulo = 10;
ytxttitulo = 10;
xtxtpos1 = 10;
ytxtpos1 = 40;
xtxtpos2 = 10;
ytxtpos2 = 60;
xtxtdistancia = 10;
ytxtdistancia = 80;
xtxtpos11 = 120;
ytxtpos11 = 40;
xtxtpos22 = 120;
ytxtpos22 = 60;
xtxtdistancia1 = 120;
ytxtdistancia1 = 80;
sf::Font* fontTimes = new sf::Font();
if (!fontTimes->LoadFromFile("times.ttf")) {
std::cout << "ERRO FONTE" << std::endl;
}
txttitulo = new sf::String("Exercicio 2", *fontTimes, 20);
txttitulo->SetColor(sf::Color(0, 0, 0));
txttitulo->SetPosition(xtxttitulo, ytxttitulo);
txtpos1 = new sf::String("Coordenadas P1:", *fontTimes, 15);
txtpos1->SetColor(sf::Color(0, 0, 0));
txtpos1->SetPosition(xtxtpos1, ytxtpos1);
txtpos2 = new sf::String("Coordenadas P2:", *fontTimes, 15);
txtpos2->SetColor(sf::Color(0, 0, 0));
txtpos2->SetPosition(xtxtpos2, ytxtpos2);
txtdistancia = new sf::String("Distância pontos:", *fontTimes, 15);
txtdistancia->SetColor(sf::Color(0, 0, 0));
txtdistancia->SetPosition(xtxtdistancia, ytxtdistancia);
sf::Event* event = new sf::Event();
while (app->GetEvent(*event)) {
if (event->Type == sf::Event::Closed) {
app->Close();
}
}
if (app->GetInput().IsKeyDown(sf::Key::Escape)) {
return EXIT_SUCCESS;
}
if (app->GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
mouseX = app->GetInput().GetMouseX();
mouseY = app->GetInput().GetMouseY();
if (id_ponto == 0) {
id_ponto = 1;
c1 -> x = mouseX;
c1 -> y = mouseY;
c1 -> r = raio;
p1 = sf::Shape::Circle(c1->x, c1->y, c1->r, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0));
}else if(id_ponto == 1){
id_ponto = 2;
c2 -> x = mouseX;
c2 -> y = mouseY;
c2 -> r = raio;
p2 = sf::Shape::Circle(c2->x, c2->y, c2->r, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0));
}else if(id_ponto == 2){
c1 -> x = c2->x;
c1 -> y = c2->y;
c1 -> r = c2->r;
c2 -> x = mouseX;
c2 -> y = mouseY;
c2 -> r = raio;
p1 = sf::Shape::Circle(c1->x, c1->y, c1->r, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0));
p2 = sf::Shape::Circle(c2->x, c2->y, c2->r, sf::Color(0, 0, 0), 1, sf::Color(0, 0, 0));
}
}
//Posição dos pontos
stringstream x1;
stringstream y1;
stringstream x2;
stringstream y2;
if (id_ponto == 0){
x1 << 0;
y1 << 0;
x2 << 0;
y2 << 0;
}else if (id_ponto == 1){
x1 << c1->x;
y1 << c1->y;
x2 << 0;
y2 << 0;
}else{
x1 << c1->x;
y1 << c1->y;
x2 << c2->x;
y2 << c2->y;
}
str1 = "x: " + x1.str() + " - y: " + y1.str();
txtpos11 = new sf::String(str1, *fontTimes, 15);
txtpos11->SetColor(sf::Color(0, 0, 0));
txtpos11->SetPosition(xtxtpos11, ytxtpos11);
str2 = "x: " + x2.str() + " - y: " + y2.str();
txtpos22 = new sf::String(str2, *fontTimes, 15);
txtpos22->SetColor(sf::Color(0, 0, 0));
txtpos22->SetPosition(xtxtpos22, ytxtpos22);
stringstream distanciaFinal;
app->Clear(sf::Color(255, 255, 255));
app->Draw(*txttitulo);
app->Draw(*txtpos1);
app->Draw(*txtpos2);
app->Draw(*txtdistancia);
app->Draw(*txtpos11);
app->Draw(*txtpos22);
app->Draw(p1);
app->Draw(p2);
if (id_ponto > 1){
sf::Shape reta = sf::Shape::Line(c1->x, c1->y, c2->x, c2->y, 1, sf::Color(0,0,0), 1, sf::Color(0, 0, 0));
app->Draw(reta);
distanciaFinal << dist_eucl(c1->x, c1->y, c2->x, c2->y);
}else{
distanciaFinal << 0;
}
str3 = distanciaFinal.str();
txtdistancia1 = new sf::String(str3, *fontTimes, 15);
txtdistancia1->SetColor(sf::Color(0, 0, 0));
txtdistancia1->SetPosition(xtxtdistancia1, ytxtdistancia1);
app->Draw(*txtdistancia1);
sf::Sleep(0.1);
app->Display();
}
return EXIT_SUCCESS;
}
[/code]