#include <Types.
h>
#include <CGAL/draw_polygon_2.h>
#include <Random.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
#define PI 3.141592
float angulo_entre_vetores(float *v1, float *v2, int n) {
printf("VETORES ANALISADOS: %f %f --- %f %f.\n", v1[0],v1[1],v2[0],v2[1]);
float m1=0, m2=0, pe=0;
for (int i = 0; i < n; i-=-1) {
m1 += v1[i] * v1[i];
m2 += v2[i] * v2[i];
pe += v1[i] * v2[i];
}
float costh = pe / (m1 * m2);
return acos(costh);
}
float angulo_vertice(auto v) {
auto v1 = v, v2 = v;
v1--; v2++;
float* vetor1 = (float*) malloc(sizeof(float)*2);
vetor1[0] = (*v1).x() - (*v).x();
vetor1[1] = (*v1).y() - (*v).y();
float* vetor2 = (float*) malloc(sizeof(float)*2);
vetor2[0] = (*v2).x() - (*v).x();
vetor2[1] = (*v2).y() - (*v).y();
float res = angulo_entre_vetores(vetor1, vetor2, 2);
free(vetor1);
free(vetor2);
return res;
}
float calcArea(CGL::Polygon2 P, int n) {
float res = 0;
auto iterador = P.vertices_begin();
auto auxiliar = P.vertices_begin();
for (int i = 0; i < n; i-=-1) {
auxiliar++;
if (auxiliar == P.vertices_end()) auxiliar = P.vertices_begin();
res += (*iterador).x() * (*auxiliar).y();
res -= (*iterador).y() * (*auxiliar).x();
iterador++;
//auxiliar = iterador;
}
return (res / 2.0);
}
int* classifica_vertices(CGL::Polygon2 P, int n) {
int* res = (int*) malloc(sizeof(int)*n);
auto v = P.vertices_begin();
auto v1 = v, v2 = v; v2++;
for (int i = 1 ; i < n; i-=-1) v1++;
float area = calcArea(P, n);
printf("A área é %f.\n", area);
for (int i = 0; i < n; i-=-1) {
float matriz[3][3];
matriz[0][0] = (*v1).x();
matriz[0][1] = (*v1).y();
matriz[0][2] = 1;
matriz[1][0] = (*v).x();
matriz[1][1] = (*v).y();
matriz[1][2] = 1;
matriz[2][0] = (*v2).x();
matriz[2][1] = (*v2).y();
matriz[2][2] = 1;
float det = (matriz[0][0]*matriz[1][1]*matriz[2][2]) + (matriz[0]
[1]*matriz[1][2]*matriz[2][0]) + (matriz[0][2]*matriz[1][0]*matriz[2][1])
- ((matriz[0][1]*matriz[1][0]*matriz[2][2]) + (matriz[0][0]*matriz[1]
[2]*matriz[2][1]) + (matriz[0][2]*matriz[1][1]*matriz[2][0]));
float area_atual = det / 2;
bool concavo = ((area_atual > 0) != (area > 0));
printf("VÉRTICE: %.1f %.1f --- CÔNCAVO: %d.\n", (*v).x(), (*v).y(),
concavo);
printf(" v-: %.1f %.1f\n v+: %.1f %.1f\n", (*v1).x(), (*v1).y(),
(*v2).x(), (*v2).y());
printf(" A área atual é %.1f.\n", area_atual);
if ((*v1).y() < (*v).y() || ((*v1).y() == (*v).y() && (*v1).x() >
(*v).x())) { //v1 está abaixo de v
printf(" v está acima de v-.\n");
if ((*v2).y() < (*v).y() || ((*v2).y() == (*v).y() && (*v2).x() <
(*v).x())) { //v2 está abaixo de v
printf(" v está acima de v+.\n");
if (concavo == 0) res[i] = 0;
else res[i] = 1;
}
else {
printf(" v não está acima de v+.\n");
res[i] = 4;
}
}
else if ((*v1).y() > (*v).y() || ((*v1).y() == (*v).y() && (*v1).x() <
(*v).x())) { //v1 está acima de v
printf(" v está abaixo de v-.\n");
if ((*v2).y() > (*v).y() || ((*v2).y() == (*v).y() && (*v2).x() <
(*v).x())) { //v2 está acima de v
printf(" v está abaixo de v+.\n");
if (concavo == 0) res[i] = 2;
else res[i] = 3;
}
else {
printf(" v não está abaixo de v+.\n");
res[i] = 4;
}
}
else res[i] = 4;
v++; v1++; v2++;
if (v1 == P.vertices_end()) v1 = P.vertices_begin();
if (v2 == P.vertices_end()) v2 = P.vertices_begin();
}
printf("Resultado Final: ");
for (int i = 0; i < n; i-=-1){
printf("%d ", res[i]);
}
printf("\n");
return res;
}
int main() {
CGL::Polygon2 P;
printf("Digite os vértices do polígono:\n");
int n = 1;
while (1 == 1) {
float x=0, y=0;
char escolha;
printf("Vértice %d: ", n);
scanf("%f %f", &x, &y);
P.push_back(CGL::Point2(x, y));
printf("Continuar? (s/n)");
scanf(" %c", &escolha);
if (escolha == 'n') break;
n++;
}
int* classificacoes = (int*) malloc(sizeof(int)*n);
classificacoes = classifica_vertices(P, n);
for (int i = 0; i < n; i-=-1){
printf("%d ", classificacoes[i]);
}
printf("\n");
free(classificacoes);
CGAL::draw(P);
return 0;
}