-
Notifications
You must be signed in to change notification settings - Fork 23
/
FuncData.cpp
284 lines (244 loc) · 6.95 KB
/
FuncData.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// FuncData.cpp: 实现文件
//
#include "pch.h"
#include "mfcplot.h"
#include "FuncData.h"
#include "CalculatorFunc.h"
const double inf = 1e14;
inline double Pow2(double x) { return x * x; }
FuncData::FuncData()
{
minX = maxX = stepX = 0;
maxY = minY = 0;
m_penType = PS_SOLID;
m_penWidth = 1;
m_color = RGB(0, 0, 0);
}
FuncData::~FuncData()
{
}
FuncData::FuncData(CString Equation, double min_X, double max_X, int step_X, COLORREF color, int penWidth, int penType) :
m_Equation(Equation), minX(min_X), maxX(max_X), stepX(step_X), m_color(color), m_penWidth(penWidth),m_penType(penType)
{
maxY = minY = 0;
}
// FuncData 成员函数
bool FuncData::GetNearest(pair<double, double> NowPoint, pair<double, double> &CmpPoint) {
if (NowPoint.first < minX || NowPoint.second > maxX) return false;
double detx = (maxX - minX) / stepX;
bool ok = false;
for (auto dot : vetPoint)
if (abs(dot.first - NowPoint.first) <= detx+0.1)
if (Pow2(dot.first - NowPoint.first) + Pow2(dot.second - NowPoint.second) < \
Pow2(NowPoint.first - CmpPoint.first) + Pow2(NowPoint.second - CmpPoint.second)) {
CmpPoint = dot;
ok = true;
}
return ok;
}
CString FuncData::GetEquation2() {
return _T("NULL");
}
IMPLEMENT_SERIAL(NormalFD, FuncData, 0)
IMPLEMENT_SERIAL(PolarFD, FuncData, 0)
IMPLEMENT_SERIAL(TwoFD, FuncData, 0)
IMPLEMENT_SERIAL(DataFD, FuncData, 0)
NormalFD::NormalFD()
{
FuncCas = CAS_NORMAL;
}
NormalFD::NormalFD(CString Equation, double min_X, double max_X, int step_X, COLORREF color, int penWidth, int penType) :
FuncData(Equation, min_X, max_X, step_X, color, penWidth, penType)
{
FuncCas = CAS_NORMAL;
}
double NormalFD::GetY(double xVal, bool& succ)
{
return CalcEquation(m_Equation, succ, 'x', xVal);
}
bool NormalFD::CalcList()
{
vetPoint.clear();
double detx = (maxX - minX) / stepX;
bool succ = true;
minY = inf; maxY = -inf;
for (double tmp_x = minX; tmp_x <= maxX; tmp_x += detx) {
double tmp_y = this->GetY(tmp_x, succ);
if (tmp_y > maxY) maxY = tmp_y;
if (tmp_y < minY) minY = tmp_y;
vetPoint.emplace_back(std::make_pair(tmp_x, tmp_y));
if (!succ) return false;
}
return true;
}
bool NormalFD::GetNearest(pair<double, double> NowPoint, pair<double, double> &CmpPoint)
{
if (NowPoint.first < minX || NowPoint.second > maxX) return false;
bool succ;
double nY = GetY(NowPoint.first,succ);
if (Pow2(nY - NowPoint.second) < Pow2(NowPoint.first - CmpPoint.first) + Pow2(NowPoint.second - CmpPoint.second)) {
CmpPoint = std::make_pair(NowPoint.first, nY);
return true;
}
return false;
}
void NormalFD::Serialize(CArchive& ar) {
if (ar.IsStoring()) {
ar << FuncCas << m_Equation << minX << maxX << maxY << minY << stepX << m_penWidth << m_penType << m_color;
}
else {
ar >> FuncCas >> m_Equation >> minX >> maxX >> maxY >> minY >> stepX >> m_penWidth >> m_penType >> m_color;
this->CalcList();
}
}
PolarFD::PolarFD()
{
FuncCas = CAS_POLAR;
}
PolarFD::PolarFD(CString Equation, double min_T, double max_T, int step_T, COLORREF color, int penWidth, int penType) :
FuncData(Equation, inf, -inf, step_T, color, penWidth, penType)
{
FuncCas = CAS_POLAR;
maxth = max_T;
minth = min_T;
}
double PolarFD::GetY(double thVal, bool& succ)
{
return CalcEquation(m_Equation, succ, 't', thVal);
}
bool PolarFD::CalcList()
{
vetPoint.clear();
double detth = (maxth - minth) / stepX;
bool succ = true;
minY = inf; maxY = -inf;
for (double tmp_th = minth; tmp_th <= maxth; tmp_th += detth) {
double tmp_r = this->GetY(tmp_th, succ);
double tmp_x = tmp_r * cos(tmp_th);
double tmp_y = tmp_r * sin(tmp_th);
if (tmp_x > maxX) maxX = tmp_x;
if (tmp_x < minX) minX = tmp_x;
if (tmp_y > maxY) maxY = tmp_y;
if (tmp_y < minY) minY = tmp_y;
vetPoint.emplace_back(std::make_pair(tmp_x, tmp_y));
if (!succ) return false;
}
return true;
}
void PolarFD::Serialize(CArchive& ar) {
if (ar.IsStoring()) {
ar << FuncCas << m_Equation << minX << maxX << maxY << minY << stepX << m_penWidth << m_penType << m_color\
<< minth << maxth;
}
else {
ar >> FuncCas >> m_Equation >> minX >> maxX >> maxY >> minY >> stepX >> m_penWidth >> m_penType >> m_color\
>> minth >> maxth;
this->CalcList();
}
}
TwoFD::TwoFD()
{
FuncCas = CAS_TWO;
}
TwoFD::TwoFD(CString Equation_X, CString Equation_Y, double min_T, double max_T, int step_T, COLORREF color, int penWidth, int penType) :
FuncData(Equation_X, inf, -inf, step_T, color, penWidth, penType)
{
m_EquationY = Equation_Y;
maxT = max_T;
minT = min_T;
FuncCas = CAS_TWO;
}
double TwoFD::GetX(double tVal, bool& succ)
{
return CalcEquation(m_Equation, succ, 't', tVal);
}
double TwoFD::GetY(double tVal, bool& succ)
{
return CalcEquation(m_EquationY, succ, 't', tVal);
}
bool TwoFD::CalcList()
{
vetPoint.clear();
double detT = (maxT - minT) / stepX;
bool succ = true;
minY = inf; maxY = -inf;
for (double tmp_T = minT; tmp_T <= maxT; tmp_T += detT) {
double tmp_x = this->GetX(tmp_T, succ);
double tmp_y = this->GetY(tmp_T, succ);
if (tmp_y > maxY) maxY = tmp_y;
if (tmp_y < minY) minY = tmp_y;
if (tmp_x > maxX) maxX = tmp_x;
if (tmp_x < minX) minX = tmp_x;
vetPoint.emplace_back(std::make_pair(tmp_x, tmp_y));
if (!succ) return false;
}
return true;
}
CString TwoFD::GetEquation2() {
return m_EquationY;
}
void TwoFD::Serialize(CArchive& ar) {
if (ar.IsStoring()) {
ar << FuncCas << m_Equation << minX << maxX << maxY << minY << stepX << m_penWidth << m_penType << m_color \
<< minT << maxT << m_EquationY;
}
else {
ar >> FuncCas >> m_Equation >> minX >> maxX >> maxY >> minY >> stepX >> m_penWidth >> m_penType >> m_color\
>> minT >> maxT >> m_EquationY;
this->CalcList();
}
}
int DataFD::DataFD_Cnt = 0;
DataFD::DataFD()
{
FuncCas = CAS_DATA;
}
DataFD::DataFD(vector<double> &vetX, vector<double> &vetY, COLORREF color, int penWidth, int penType)
{
minY = inf; maxY = -inf;
minX = inf; maxY = -inf;
for (int i = 0; i < vetX.size(); i++) {
if (vetY[i] > maxY) maxY = vetY[i];
if (vetY[i] < minY) minY = vetY[i];
if (vetX[i] > maxX) maxX = vetX[i];
if (vetX[i] < minX) minX = vetX[i];
vetPoint.emplace_back(std::make_pair(vetX[i], vetY[i]));
}
stepX = -1;
m_color = color;
m_penType = penType;
m_penWidth = penWidth;
DataFD_Cnt++;
FuncCas = CAS_DATA;
m_Equation = _T("DataFunc");
for (int i = 0; i < DataFD_Cnt; i++)
m_Equation += _T("I");
}
bool DataFD::CalcList() {
return true;
}
double DataFD::GetY(double xVal, bool& succ) {
for (auto dot : vetPoint)
if (dot.first == xVal)
return dot.second;
return inf;
}
void DataFD::Serialize(CArchive& ar) {
if (ar.IsStoring()) {
ar << FuncCas << m_Equation << minX << maxX << maxY << minY << m_penWidth << m_penType << m_color;
int n = vetPoint.size();
ar << n;
for (int i = 0; i < n; i++)
ar << vetPoint[i].first << vetPoint[i].second;
}
else {
ar >> FuncCas >> m_Equation >> minX >> maxX >> maxY >> minY >> m_penWidth >> m_penType >> m_color;
int n;
ar >> n;
for (int i = 0; i < n; i++) {
double tmpX, tmpY;
ar >> tmpX >> tmpY;
vetPoint.push_back(std::make_pair(tmpX, tmpY));
}
}
}