-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossSection.cpp
178 lines (157 loc) · 4.23 KB
/
crossSection.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
#include "crossSection.h"
#include<cmath>
#include<QDebug>
#define PI 3.1415926
shape operator+(shape &shp1,shape &shp2)
{
shape shp;
double s=shp1.area+shp2.area;
double cx=(shp1.xc*shp1.area+shp2.xc*shp2.area)/s;
double cy=(shp1.yc*shp1.area+shp2.yc*shp2.area)/s;
double Ixx=shp1.Ix+shp2.Ix;
double Iyy=shp1.Iy+shp2.Iy;
double Ixxy=shp1.Ixy+shp2.Ixy;
shp=shape(cx,cy,s,Ixx,Iyy,Ixxy);
return shp;
}
shape operator-(shape &shp1,shape &shp2)
{
shape shp;
double s=shp1.area-shp2.area;
double cx=(s!=0)?(shp1.xc*shp1.area-shp2.xc*shp2.area)/s:0;
double cy=(s!=0)?(shp1.yc*shp1.area-shp2.yc*shp2.area)/s:0;
double Ixx=shp1.Ix-shp2.Ix;
double Iyy=shp1.Iy-shp2.Iy;
double Ixxy=shp1.Ixy-shp2.Ixy;
shp=shape(cx,cy,s,Ixx,Iyy,Ixxy);
return shp;
}
double shape::Ixc(){
return (Ix-area*yc*yc);
}
double shape::Iyc(){
return (Iy-area*xc*xc);
}
double shape::Ixyc(){
return (Ixy-area*xc*yc);
}
shape& shape::rotate(double theta)
{
double ix=Ix,iy=Iy,ixy=Ixy;
Iy=(ix+iy)*.5+.5*(ix-iy)*cos(2.0*theta)-ixy*sin(2.0*theta);
Iy=(ix+iy)*.5-.5*(ix-iy)*cos(2.0*theta)+ixy*sin(2.0*theta);
Ixy=(ix-iy)*sin(2.0*theta)+ixy*cos(2.0*theta);
ix=xc;iy=yc;
xc=ix*cos(theta)+iy*sin(theta);
yc=iy*cos(theta)-ix*sin(theta);
return *this;
}
shape& shape::move(double& x,double& y)
{
Ix=Ixc()+area*(yc+y)*(yc+y);
Iy=Iyc()+area*(xc+x)*(xc+x);
Ixy=Ixyc()+area*(yc+y)*(xc+x);
xc+=x;
yc+=y; //These should be written here or the Ixc, Iyc will return a wrong number
xSymmetry=(yc==0) && (Ixy==0);
ySymmetry=(xc==0) && (Ixy==0);
return *this;
}
shape::shape()
{
xc=0;
yc=0;
area=0;
Ix=0;
Iy=0;
Ixy=0;
xSymmetry=1;
ySymmetry=1;
}
shape::shape(double x,double y,double A,double ix,double iy,double ixy)
{
xc=x;
yc=y;
area=A;
Ix=ix;
Iy=iy;
Ixy=ixy;
xSymmetry=(y==0) && (ixy==0);
ySymmetry=(x==0) && (ixy==0);
}
rectangle::rectangle(double& x,double& y,double& wid,double& hei)
{
xc=x;
yc=y;
h=hei;
b=wid;
area=hei*wid;
Ix=b*h*h*h/12.0+area*y*y;
Iy=h*b*b*b/12.0+area*x*x;
Ixy=x*y*area;
xSymmetry=! y;
ySymmetry=! x;
}
circle::circle(double& x, double& y, double& d)
{
xc=x;
yc=y;
diameter=d;
area=PI*d*d/4.0;
Ix=PI*d*d*d*d/64+area*y*y;
Iy=PI*d*d*d*d/64+area*x*x;
Ixy=x*y*area;
xSymmetry=!y;
ySymmetry=!x;
}
ring::ring(double& x,double& y,double& d2,double d1)
{
xc=x;
yc=y;
innerDiameter=d1;
outerDiameter=d2;
area=PI*(d2*d2-d1*d1)/4.0;
Ix=PI*(d2*d2*d2*d2-d1*d1*d1*d1)/64+area*y*y;
Iy=PI*(d2*d2*d2*d2-d1*d1*d1*d1)/64+area*x*x;
Ixy=x*y*area;
xSymmetry=!y;
ySymmetry=!x;
}
bool Intersect(rectangle& rect, circle& cir)
{
//Algorithm from the Internet
//Milo Yip's answer in Zhihu
//https://www.zhihu.com/question/24251545
//It can work in n-dimension condition
double v[]={abs(rect.xc-cir.xc),abs(rect.yc-cir.yc)};
double u[]={0,0};
double h[]={rect.b/2.0,rect.h/2.0};
u[0]=((v[0]-h[0])>0)?(v[0]-h[0]):.0;
u[1]=((v[1]-h[1])>0)?(v[1]-h[1]):.0;
return (u[0]*u[0]+u[1]*u[1])<(cir.diameter*cir.diameter/4.0);
}
bool Intersect(circle& cir, rectangle& rect)
{
double v[]={abs(rect.xc-cir.xc),abs(rect.yc-cir.yc)};
double u[]={0,0};
double h[]={rect.b/2.0,rect.h/2.0};
u[0]=((v[0]-h[0])>0)?(v[0]-h[0]):.0;
u[1]=((v[1]-h[1])>0)?(v[1]-h[1]):.0;
return (u[0]*u[0]+u[1]*u[1])<(cir.diameter*cir.diameter/4.0);
}
bool Intersect(rectangle& rect1, rectangle& rect2)
{
double minx1=rect1.xc-rect1.b/2.0;
double miny1=rect1.yc-rect1.h/2.0;
double maxx1=rect1.xc+rect1.b/2.0;
double maxy1=rect1.yc+rect1.h/2.0;
double minx2=rect2.xc-rect2.b/2.0;
double miny2=rect2.yc-rect2.h/2.0;
double maxx2=rect2.xc+rect2.b/2.0;
double maxy2=rect2.yc+rect2.h/2.0;
double minx=std::min(minx1,minx2);
double miny=std::min(miny1,miny2);
double maxx=std::max(maxx1,maxx2);
double maxy=std::max(maxy1,maxy2);
return ((minx<maxx)||(miny<maxy));
}