-
Notifications
You must be signed in to change notification settings - Fork 0
/
Support2d.cpp
223 lines (208 loc) · 6.23 KB
/
Support2d.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "Support2d.h"
#include "Transform2d.h"
//-----------------------------------------------------------------------------------------------------
// Point2d
//-----------------------------------------------------------------------------------------------------
Point2d::Point2d(){
x=y=0;
}
Point2d::Point2d(float x, float y){
this->x=x;
this->y=y;
}
Point2d::~Point2d(){}
//set
void Point2d::set(float x, float y){
this->x=x;
this->y=y;
}
//translate
void Point2d::translate(float x, float y){
Transform2d::translate(this,x,y);
}
//rotate relative to origin of coord system
void Point2d::rotateRelativeToOrigin(float angleRad){
Transform2d::rotate(this,angleRad);
}
//rotate relative to another point in the same coord system
void Point2d::rotateRelativeToPoint(Point2d p, float angleRad){
Transform2d::rotateRelativeToAnotherPoint(this,&p,angleRad);
}
void Point2d::scale(float sx, float sy){
Transform2d::scale(this,sx,sy);
}
void Point2d::scaleRelativeToPoint(Point2d p, float sx, float sy){
Transform2d::scaleRelativeToAnotherPoint(this, &p, sx, sy);
}
//-----------------------------------------------------------------------------------------------------
// Object2d
//-----------------------------------------------------------------------------------------------------
//constructor & destructor
Object2d::Object2d(std::vector<Point2d> points, std::vector<int> topology){
this->points = points;
this->topology = topology;
//color
colorx=colory=colorz=1;
//drawaxis
drawaxis=true;
//set axis
axiscenter.set(0,0);
axisup.set(0,1);
axisright.set(1,0);
computeAxis();
}
Object2d::~Object2d(){
}
//get center
void Object2d::getCenter(float *x, float *y){
float centerx=0;
float centery=0;
if(topology.size()>0 && points.size()>0){
for(unsigned int i=0;i<topology.size();i++){
centerx+=points[topology[i]].x;
centery+=points[topology[i]].y;
}
centerx/=(float)topology.size();
centery/=(float)topology.size();
}else{
centerx=0;
centery=0;
}
(*x)=centerx;
(*y)=centery;
}
//recompute axis
void Object2d::computeAxis(){
float centerx, centery;
getCenter(¢erx, ¢ery);
float oldux=axisup.x-axiscenter.x;
float olduy=axisup.y-axiscenter.y;
float oldrx=axisright.x-axiscenter.x;
float oldry=axisright.y-axiscenter.y;
axiscenter.set(centerx,centery);
axisup.set(centerx+oldux,centery+olduy);
axisright.set(centerx+oldrx,centery+oldry);
}
//rotate relative to center
void Object2d::rotateSelf(float angleRad){
//rotate each point
for(unsigned int i=0;i<points.size();i++){
points[i].rotateRelativeToPoint(axiscenter,angleRad);
}
//adjust axis
axisup.rotateRelativeToPoint(axiscenter,angleRad);
axisright.rotateRelativeToPoint(axiscenter, angleRad);
}
//translate
void Object2d::translate(float x, float y){
//translate each point
for(unsigned int i=0;i<points.size();i++){
points[i].translate(x,y);
}
//adjust axis
axiscenter.translate(x,y);
axisup.translate(x,y);
axisright.translate(x,y);
}
//rotate relative to another point
void Object2d::rotateRelativeToPoint(Point2d pct, float angleRad){
//rotate each pt
for(unsigned int i=0;i<points.size();i++){
points[i].rotateRelativeToPoint(pct,angleRad);
}
//adjust axis
axiscenter.rotateRelativeToPoint(pct,angleRad);
axisup.rotateRelativeToPoint(pct,angleRad);
axisright.rotateRelativeToPoint(pct, angleRad);
}
void Object2d::scale(float sx, float sy){
//scale each pt
for(unsigned int i=0;i<points.size();i++){
points[i].scale(sx,sy);
}
computeAxis();
}
void Object2d::scaleRelativeToPoint(Point2d p, float sx, float sy){
//scale each pt
for(unsigned int i=0;i<points.size();i++){
points[i].scaleRelativeToPoint(p,sx,sy);
}
computeAxis();
}
//color
void Object2d::setcolor(float colorx, float colory, float colorz){
this->colorx= colorx;
this->colory= colory;
this->colorz= colorz;
}
//-----------------------------------------------------------------------------------------------------
// CoordinateSystem2d
//-----------------------------------------------------------------------------------------------------
//constructor & destructor
CoordinateSystem2d::CoordinateSystem2d(){
axiscenter.set(0,0);
axisright.set(20,0);
axisup.set(0,20);
draw_axis=false;
}
CoordinateSystem2d::~CoordinateSystem2d(){
}
//add
void CoordinateSystem2d::objectAdd(Object2d *obj){
objects.push_back(obj);
}
void CoordinateSystem2d::objectRemove(Object2d *obj){
if(!obj) return;
for(unsigned int i=0;i<objects.size();i++){
if(objects[i]==obj) objects.erase(objects.begin()+i);
//multiple ptr friendly
}
}
void CoordinateSystem2d::objectTranslate(Object2d *obj, float tx, float ty){
float vux,vuy,vrx,vry; //relative axis
vux=axisup.x-axiscenter.x;
vuy=axisup.y-axiscenter.y;
float dimu=sqrt(vux*vux+vuy*vuy);
vrx=axisright.x-axiscenter.x;
vry=axisright.y-axiscenter.y;
float dimr=sqrt(vrx*vrx+vry*vry);
vrx/=dimr;
vry/=dimr;
vux/=dimu;
vuy/=dimu;
float newtx=tx*vrx+ty*vux;
float newty=ty*vuy+tx*vry;
obj->translate(newtx,newty);
}
void CoordinateSystem2d::objectRotateSelf(Object2d *obj, float angleRad){
obj->rotateSelf(angleRad);
}
void CoordinateSystem2d::objectRotateRelativeToPoint(Object2d *obj, Point2d *pct,float angleRad){
obj->rotateRelativeToPoint((*pct),angleRad);
}
void CoordinateSystem2d::objectScale(Object2d *obj,float sx,float sy){
obj->scale(sx,sy);
}
void CoordinateSystem2d::objectScaleRelativeToPoint(Object2d* obj, Point2d *ref, float sx, float sy){
obj->scaleRelativeToPoint((*ref),sx,sy);
}
//translate
void CoordinateSystem2d::translate(float x, float y){
for(unsigned int i=0;i<objects.size();i++) objects[i]->translate(x,y);
axiscenter.translate(x,y);
axisup.translate(x,y);
axisright.translate(x,y);
}
//rotate
void CoordinateSystem2d::rotateSelf(float angleRad){
for(unsigned int i=0;i<objects.size();i++) objects[i]->rotateRelativeToPoint(axiscenter,angleRad);
axisup.rotateRelativeToPoint(axiscenter, angleRad);
axisright.rotateRelativeToPoint(axiscenter, angleRad);
}
//rotate
void CoordinateSystem2d::rotateRelativeToCoordinateSystem(CoordinateSystem2d cd, float angleRad){
for(unsigned int i=0;i<objects.size();i++) objects[i]->rotateRelativeToPoint(cd.axiscenter,angleRad);
axiscenter.rotateRelativeToPoint(cd.axiscenter,angleRad);
axisup.rotateRelativeToPoint(cd.axiscenter, angleRad);
axisright.rotateRelativeToPoint(cd.axiscenter, angleRad);
}