Lanca-chamas agora atira gotas, nao bolhas...
[Projeto-PCG.git] / geometry.cpp
blobb7c372501586ad975eba41eae089302130531880
1 #include "geometry.h"
2 #include "math.h"
3 #include <vector>
5 Linha::Linha(double x1,double y1,double x2, double y2) {
6 vertices[0].x = x1;
7 vertices[0].y = y1;
8 vertices[1].x = x2;
9 vertices[1].y = y2;
12 Rect::Rect(double x1,double y1,double x2, double y2) {
13 vertices[0].x = x1;
14 vertices[0].y = y1;
15 vertices[1].x = x2;
16 vertices[1].y = y2;
17 normaliza();
20 void Rect::normaliza() {
21 double xmin,xmax,ymin,ymax;
22 xmin = std::min(vertices[0].x,vertices[1].x);
23 xmax = std::max(vertices[0].x,vertices[1].x);
24 ymin = std::min(vertices[0].y,vertices[1].y);
25 ymax = std::max(vertices[0].y,vertices[1].y);
26 vertices[0].x = xmin;
27 vertices[1].x = xmax;
28 vertices[0].y = ymin;
29 vertices[1].y = ymax;
32 void Linha::desenha() {
33 glColor3f(0,0,0);
34 glBegin(GL_LINES);
35 glVertex3f(vertices[0].x, vertices[0].y, 0.0f); // origin of the line
36 glVertex3f(vertices[1].x, vertices[1].y, 0.0f); // origin of the line
37 glEnd( );
40 void Polygon::desenha() {
41 int n = linhas.size();
42 for (int i = 0; i < n; i++) {
43 linhas[i].desenha();
47 double operator*(const Ponto &a, const Ponto &b) {
48 return a.x * b.y - b.x * a.y;
51 Ponto operator-(const Ponto &a, const Ponto &b) {
52 Ponto ret;
53 ret.x = a.x - b.x;
54 ret.y = a.y - b.y;
55 return ret;
58 Ponto operator+(const Ponto &a, const Ponto &b) {
59 Ponto ret;
60 ret.x = a.x + b.x;
61 ret.y = a.y + b.y;
62 return ret;
65 double distance(const Ponto &a,const Ponto &b) {
66 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
69 void drawCircle(double radius, int lines)
71 double i2rad = PI/(lines/2.0);
72 glBegin(GL_LINE_LOOP);
73 for (int i=0; i < lines; i++) {
74 double degInRad = i*i2rad;
75 glVertex2f(cos(degInRad)*radius,sin(degInRad)*radius);
78 glEnd();
81 //calcula se a direcao dos segmentos p0p1 p1p2 é horaria, antihoraria ou se sao colineares
82 double direction(const Ponto &p0,const Ponto &p1,const Ponto &p2) {
83 return (p2 - p0) * (p1 - p0);
86 bool onsegment(const Ponto &pi,const Ponto &pj,const Ponto &pk) {
87 if ((std::min(pi.x, pj.x) <= pk.x && pk.x <= std::max(pi.x, pj.x)) &&
88 (std::min(pi.y, pj.y) <= pk.y && pk.y <= std::max(pi.y, pj.y)))
89 return true;
90 else
91 return false;
94 bool linesIntersect(const Linha a,const Linha b) {
95 Ponto p1 = a.vertices[0];
96 Ponto p2 = a.vertices[1];
97 Ponto p3 = b.vertices[0];
98 Ponto p4 = b.vertices[1];
99 double d1 = direction(p3, p4, p1);
100 double d2 = direction(p3, p4, p2);
101 double d3 = direction(p1, p2, p3);
102 double d4 = direction(p1, p2, p4);
103 if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) &&
104 ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
105 return true;
106 else if (d1 == 0.0 && onsegment(p3, p4, p1))
107 return true;
108 else if (d2 == 0.0 && onsegment(p3, p4, p2))
109 return true;
110 else if (d3 == 0.0 && onsegment(p1, p2, p3))
111 return true;
112 else if (d4 == 0.0 && onsegment(p1, p2, p4))
113 return true;
114 else
115 return false;