Consertado spawn do mapa 2
[Projeto-PCG.git] / geometry.cpp
blob1a3e3e8c7b858bc9e84656bb6188099ab0e58bbc
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 void Linha::translate(Ponto v) {
13 vertices[0] = vertices[0] + v;
14 vertices[1] = vertices[1] + v;
17 Rect::Rect(double x1,double y1,double x2, double y2) {
18 vertices[0].x = x1;
19 vertices[0].y = y1;
20 vertices[1].x = x2;
21 vertices[1].y = y2;
22 normaliza();
25 void Rect::normaliza() {
26 double xmin,xmax,ymin,ymax;
27 xmin = std::min(vertices[0].x,vertices[1].x);
28 xmax = std::max(vertices[0].x,vertices[1].x);
29 ymin = std::min(vertices[0].y,vertices[1].y);
30 ymax = std::max(vertices[0].y,vertices[1].y);
31 vertices[0].x = xmin;
32 vertices[1].x = xmax;
33 vertices[0].y = ymin;
34 vertices[1].y = ymax;
37 void Linha::desenha() {
38 glColor3f(color[0],color[1],color[2]);
39 glBegin(GL_LINES);
40 glVertex3f(vertices[0].x, vertices[0].y, -1.5f); // origin of the line
41 glVertex3f(vertices[1].x, vertices[1].y, -1.5f); // origin of the line
42 glEnd( );
43 glColor3f(0,0,0);
46 void Polygon::desenha() {
47 int n = linhas.size();
48 for (int i = 0; i < n; i++) {
49 linhas[i].desenha();
53 void Polygon::translate(Ponto v) {
54 for (int i = 0; i < linhas.size(); i++)
55 linhas[i].translate(v);
58 double operator*(const Ponto &a, const Ponto &b) {
59 return a.x * b.y - b.x * a.y;
62 Ponto operator-(const Ponto &a, const Ponto &b) {
63 Ponto ret;
64 ret.x = a.x - b.x;
65 ret.y = a.y - b.y;
66 return ret;
69 Ponto operator+(const Ponto &a, const Ponto &b) {
70 Ponto ret;
71 ret.x = a.x + b.x;
72 ret.y = a.y + b.y;
73 return ret;
76 double distance(const Ponto &a,const Ponto &b) {
77 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
80 void drawCircle(double radius, int lines)
82 double i2rad = PI/(lines/2.0);
83 glColor3f(1.0,1.0,1.0);
84 glBegin(GL_POLYGON);
85 for (int i=0; i < lines; i++) {
86 double degInRad = i*i2rad;
87 glVertex3f(cos(degInRad)*radius,sin(degInRad)*radius,0);
89 glEnd();
90 glColor3f(0.0,0.0,0.0);
91 glBegin(GL_LINE_LOOP);
92 for (int i=0; i < lines; i++) {
93 double degInRad = i*i2rad;
94 glVertex3f(cos(degInRad)*radius,sin(degInRad)*radius,0);
97 glEnd();
100 //calcula se a direcao dos segmentos p0p1 p1p2 é horaria, antihoraria ou se sao colineares
101 double direction(const Ponto &p0,const Ponto &p1,const Ponto &p2) {
102 return (p2 - p0) * (p1 - p0);
105 bool onsegment(const Ponto &pi,const Ponto &pj,const Ponto &pk) {
106 if ((std::min(pi.x, pj.x) <= pk.x && pk.x <= std::max(pi.x, pj.x)) &&
107 (std::min(pi.y, pj.y) <= pk.y && pk.y <= std::max(pi.y, pj.y)))
108 return true;
109 else
110 return false;
113 bool linesIntersect(const Linha a,const Linha b) {
114 Ponto p1 = a.vertices[0];
115 Ponto p2 = a.vertices[1];
116 Ponto p3 = b.vertices[0];
117 Ponto p4 = b.vertices[1];
118 double d1 = direction(p3, p4, p1);
119 double d2 = direction(p3, p4, p2);
120 double d3 = direction(p1, p2, p3);
121 double d4 = direction(p1, p2, p4);
122 if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) &&
123 ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
124 return true;
125 else if (d1 == 0.0 && onsegment(p3, p4, p1))
126 return true;
127 else if (d2 == 0.0 && onsegment(p3, p4, p2))
128 return true;
129 else if (d3 == 0.0 && onsegment(p1, p2, p3))
130 return true;
131 else if (d4 == 0.0 && onsegment(p1, p2, p4))
132 return true;
133 else
134 return false;
136 #include <iostream>
137 double timeToIntersection(const Linha a,const Linha b) {
138 double x1 = a.vertices[0].x, y1 = a.vertices[0].y,
139 x2 = a.vertices[1].x, y2 = a.vertices[1].y,
140 x3 = b.vertices[0].x, y3 = b.vertices[0].y,
141 x4 = b.vertices[1].x, y4 = b.vertices[1].y;
143 double denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3)*(y2 - y1));
144 double numeratorA = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3));
145 double numeratorB = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3));
146 double ua,ub;
147 if (denominator == 0) {
148 ua = 9999;
149 ub = 9999;
150 return ua;
152 ua = numeratorA / denominator;
153 ub = numeratorB / denominator;
154 return ua;
157 double abs(double x) {
158 if (x < 0.0)
159 return -x;
160 return x;
163 double sign(double n) {
164 if (n == 0.0)
165 return 0.0;
166 if (n < 0.0)
167 return -1.0;
168 if (n > 0.0)
169 return 1.0;
172 double closerToZero(double a, double b) {
173 if (abs(a) < abs(b))
174 return a;
175 return b;