-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.cpp
More file actions
129 lines (100 loc) · 3.17 KB
/
geometry.cpp
File metadata and controls
129 lines (100 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "geometri.h"
#include <cmath>
// POINTARRAY 4.2
PointArray :: PointArray (){
size = 0;
points = new Point [0]; //Permite borrar más tarde
}
PointArray :: PointArray(const Point ptsToCopy[], const int toCopySize) {
{
size = toCopySize;
points = new Point[toCopySize];
for(int i = 0; i < toCopySize; ++i){
points[i] = ptsToCopy[i];
}
}
PointArray :: PointArray(const PointArray &other){
size = other.size;
points = new Point[size];
for(int i = 0; i < size; i++){
points[i]=other.points[i];
}
}
void PointArray :: resize(int newSize){
Point *pts = new Point[newSize];
int minSize = (newSize > size ? size : newSize);
for(int i = 0; i < minSize; i++)
pts[i] = points[i];
delete[] points;
size = newSize;
points = pts;
}
void PointArray ::clear(){
resize(0);
}
void PointArray :: push_back(const Point &p){
resize(size+1);
points[size-1] = p;
}
void PointArray :: insert(const int pos, const Point &p){
resize(size + 1);
for(int i = size - 1; i > pos; i--){
points[i] = points[i-1];
}
points[pos] = p;
}
void PointArray :: remove(const int pos){
if(pos >= 0 && pos < size){ // desplazar a la izquierda
for(int i = pos; i < size - 2; i++){
points[i] = points[i+1];
}
resize(size - 1);
}
}
Point *PointArray ::get(const int pos){
return pos >= 0 && pos < size ? points + pos : NULL;
}
const Point *PointArray ::get(const int pos)cons {
return pos >= 0 && pos < size ? points + pos : NULL;
}
// POLYGON
int Polygon ::n = 0;
Polygon :: Polygon(const PointArray &pa) : points(pa){
++numPolygons;
}
Polygon :: Polygon(const Point pointArr[], const int numPoints) : points(pointArr ,numPoints){
++numPolygons;
}
// RECTANGLE
Point constructorPoints [4];
Point *updateConstructorPoints (const Point &p1 , const Point &p2,
const Point &p3 , const Point &p4 = Point (0,0) ) {
constructorPoints [0] = p1 ;
constructorPoints [1] = p2 ;
constructorPoints [2] = p3 ;
constructorPoints [3] = p4 ;
return constructorPoints ;
}
Rectangle :: Rectangle ( const Point &11 , const Point &ur) : Polygon(updateConstructorPoints(ll, Point(ll.getX(), ur.getY()),ur, Point(ur.getX(), ll.getY ())), 4) {}
Rectangle :: Rectangle ( const int llx , const int lly , const int urx ,const int ury ) Point ( urx , ury ) , Point ( urx ,lly ) ) , 4) {}
double Rectangle :: area () const {
int length = points . get (1) -> getY () - points . get (0) -> getY () ;
int width = points . get (2) -> getX () - points . get (1) -> getX () ;
return std :: abs (( double ) length * width ) ;
}
//TRIANGLE
Triangle :: Triangle(const Point &a, const Point &b, const Point &c)
:Polygon(updateConstructorPoints(a,b,c),3){}
double Triangle ::area()const{
int dx01=points.get(0)->getX()- points.get(1)->getX(),
dx12=points.get(1)->getX()- points.get(2)->getX(),
dx20=points.get(2)->getX()- points.get(0)->getX();
int dy01=points.get(0)->getY()- points.get(1)->getY(),
dy12=points.get(1)->getY()- points.get(2)->getY(),
dy20=points.get(2)->getY()- points.get(0)->getY();
double a = std::sqrt(dx01*dx01 + dy01*dy01),
b = std::sqrt(dx12*dx12 + dy12*dy12),
c = std::sqrt(dx20*dx20 + dy20*dy20);
double s = (a + b + c)/ 2;
return std::sqrt(s*(s-a)*(s-b)*(s-c));
}