-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistogram.cpp
More file actions
324 lines (299 loc) · 10.1 KB
/
Histogram.cpp
File metadata and controls
324 lines (299 loc) · 10.1 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "pch.h"
#include "Histogram.h"
using namespace AlgorithmVisualization;
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Shapes;
using namespace Microsoft::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::System::Threading;
using namespace Windows::UI::Core;
/// <summary>
/// 获取向量中最大的一项
/// </summary>
/// <param name="vec">要查找的向量</param>
/// <returns></returns>
int findMax(IVector<int>^ vec)
{
int _max = INT32_MIN;
for (int i : vec)
{
if (i > _max) _max = i;
}
return _max;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="_width"></param>
/// <param name="_height"></param>
AlgorithmVisualization::Histogram::Histogram(float _width, float _height)
{
width = _width;
height = _height;
this->container = ref new StackPanel(); //创建堆栈面板
this->container->Orientation = Windows::UI::Xaml::Controls::Orientation::Horizontal; //设置堆栈面板为水平方向
pillars = ref new Vector<Pillar^>(); //清空柱体向量
}
/// <summary>
/// 加载柱状图的每一条柱体
/// </summary>
/// <param name="numbers">要展示的数字向量</param>
void AlgorithmVisualization::Histogram::load(IVector<int>^ numbers)
{
load(numbers, ref new Vector<PillarState>(numbers->Size, PillarState::Default));
}
/// <summary>
/// 载入
/// </summary>
/// <param name="numbers"></param>
/// <param name="states"></param>
void AlgorithmVisualization::Histogram::load(IVector<int>^ numbers, IVector<PillarState>^ states)
{
load(numbers, states, ref new Vector<int>{});
}
/// <summary>
/// 载入
/// </summary>
/// <param name="numbers"></param>
/// <param name="states"></param>
/// <param name="isTemp"></param>
void AlgorithmVisualization::Histogram::load(IVector<int>^ numbers, IVector<PillarState>^ states, IVector<int>^ isTemp)
{
container->Children->Clear(); //先清空原有的容器
pillars->Clear();
float64 margin = 4.0; //边界大小
int _maxNumber = max(1, findMax(numbers)); //找到最高的,以它为基准作图
maxNumber = _maxNumber;
int pillarWidth = (int)(width / numbers->Size); //计算宽度
for (unsigned int i = 0; i < numbers->Size; ++i)
{
int number = numbers->GetAt(i);
auto rect = ref new Rectangle(); //创建矩形
int pillarHeight = (int)(height * number / maxNumber); //计算高度
unsigned int n = -1;
bool temp = isTemp->IndexOf(i, &n);
auto pillar = ref new Pillar{ number, pillarWidth, pillarHeight,
stateToColor(states->GetAt(i)), temp }; //实例化柱体,设置合理的高度和宽度
container->Children->Append(pillar->getView()); //追加进容器
pillars->Append(pillar); //追加到向量末尾
}
}
/// <summary>
/// 交换两个柱体
/// </summary>
/// <param name="left">左边要交换的</param>
/// <param name="right">右边要交换的</param>
void AlgorithmVisualization::Histogram::swap(int left, int right)
{
if (right < left) swap(left, right); //如果左右颠倒则保证左小右大
if (left == right) throw ref new InvalidArgumentException(L"传入的参数必须是两个不同的位置"); //如果二者标号一样则爆出异常
auto children = container->Children;
auto leftRect = (StackPanel^)children->GetAt(left); //获取左侧元素
auto rightRect = (StackPanel^)children->GetAt(right); //获取右侧元素
children->RemoveAt(right); //删除右侧元素
children->RemoveAt(left); //删除左侧元素
children->InsertAt(left, rightRect); //在左侧的位置插入右侧元素
children->InsertAt(right, leftRect); //在右侧的位置插入左侧元素
setState(left, PillarState::Swapping);
setState(right, PillarState::Swapping);
}
/// <summary>
/// 在UI线程交换
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
void AlgorithmVisualization::Histogram::swapOnUI(int left, int right)
{
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler([this, left, right]()
{
swap(left, right);
}
));
}
/// <summary>
/// 在UI线程显示比较
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
void AlgorithmVisualization::Histogram::compareOnUI(int left, int right)
{
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler([this, left, right]()
{
setState(left, PillarState::Compared);
setState(right, PillarState::Compared);
}
));
}
/// <summary>
/// 设置柱体的颜色
/// </summary>
/// <param name="index">要设置的索引</param>
/// <param name="color">要设置的颜色</param>
void AlgorithmVisualization::Histogram::setColor(int index, Windows::UI::Color color)
{
auto rect = (StackPanel^)container->Children->GetAt(index); //获取对应编号的元素
((Rectangle^)(rect->Children->GetAt(0)))->Fill = ref new SolidColorBrush(color); //对该位置的元素设置颜色
}
/// <summary>
/// 设置柱体的状态
/// </summary>
/// <param name="index">索引</param>
/// <param name="state">状态</param>
void AlgorithmVisualization::Histogram::setState(int index, PillarState state)
{
Color color = stateToColor(state);
setColor(index, color);
}
/// <summary>
/// 在UI线程设置状态
/// </summary>
/// <param name="index"></param>
/// <param name="state"></param>
void AlgorithmVisualization::Histogram::setStateOnUI(int index, PillarState state)
{
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler([this, index, state]()
{
setState(index, state);
}
));
}
/// <summary>
/// 在UI线程设置多个状态
/// </summary>
/// <param name="indexs"></param>
/// <param name="states"></param>
void AlgorithmVisualization::Histogram::setStatesOnUI(IVector<int>^ indexs, IVector<PillarState>^ states)
{
CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, ref new DispatchedHandler([this, indexs, states]()
{
for (unsigned int i = 0; i < indexs->Size; i++)
{
setState(indexs->GetAt(i), states->GetAt(i));
}
}
));
}
/// <summary>
/// 尺寸改变回调
/// </summary>
/// <param name="_width"></param>
/// <param name="_height"></param>
void AlgorithmVisualization::Histogram::onSizeChanged(float _width, float _height)
{
width = _width;
//height = _height;
float64 margin = 4.0; //边界大小
int pillarWidth = (int)(width / pillars->Size); //计算宽度
for (Pillar^ i : pillars)
{
int pillarHeight = (int)(height * i->number / maxNumber); //计算高度
i->resize(pillarWidth, pillarHeight);
}
}
/// <summary>
/// 将状态转换为颜色
/// </summary>
/// <param name="state"></param>
/// <returns></returns>
Color AlgorithmVisualization::Histogram::stateToColor(PillarState state)
{
Color color;
switch (state)
{
case PillarState::Default:
color = DefaultColor;
break;
case PillarState::Compared:
color = ComparedColor;
break;
case PillarState::Swapping:
color = SwappingColor;
break;
case PillarState::Completed:
color = CompletedColor;
break;
case PillarState::Selected:
color = SelectedColor;
break;
case PillarState::SetValue:
color = SetValueColor;
break;
default:
break;
}
return color;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="_number"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="color"></param>
AlgorithmVisualization::Pillar::Pillar(int _number, int width, int height, Color color)
{
Pillar(_number, width, height, color, false);
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="color">颜色</param>
/// <param name="text">文字</param>
AlgorithmVisualization::Pillar::Pillar(int _number, int width, int height, Color color, bool temp)
{
number = _number;
outBox = ref new StackPanel(); //创建外侧盒子
outBox->Orientation = Orientation::Vertical;
outBox->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Bottom; //设置对齐模式
rect = ref new Rectangle(); //创建矩形
rect->Width = width - margin * 2; //设置宽度
rect->Height = height; //设置高度
rect->Margin = Thickness(margin); //设置边界
rect->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Bottom; //设置对齐方向为向底部对齐
rect->Fill = ref new SolidColorBrush(color); //上色
topTextBlock = ref new TextBlock(); //创建文本
topTextBlock->Width = width - margin * 2; //设置宽度
if (temp) topTextBlock->Text = L"临时"; //写入文本
else topTextBlock->Text = L""; //写入文字
topTextBlock->TextWrapping = TextWrapping::Wrap; //环绕方式
topTextBlock->HorizontalAlignment = HorizontalAlignment::Center; //设置对齐
topTextBlock->TextAlignment = TextAlignment::Center; //设置文本对齐
bottomTextBlock = ref new TextBlock(); //创建文本
bottomTextBlock->Width = width - margin * 2; //设置宽度
bottomTextBlock->Text = number >= 100 ? L" + " : number >= 10 ? number.ToString() : L"0" + number.ToString(); //写入文本
bottomTextBlock->TextWrapping = TextWrapping::Wrap;
bottomTextBlock->HorizontalAlignment = HorizontalAlignment::Center; //设置对齐
bottomTextBlock->TextAlignment = TextAlignment::Center; //设置文本对齐
outBox->Children->Append(topTextBlock); //向堆栈面板中追加矩形
outBox->Children->Append(rect); //向堆栈面板中追加矩形
outBox->Children->Append(bottomTextBlock); //向堆栈面板中追加文字
}
/// <summary>
/// 获取视图
/// </summary>
/// <returns></returns>
Windows::UI::Xaml::Controls::StackPanel^ AlgorithmVisualization::Pillar::getView()
{
return outBox;
}
/// <summary>
/// 修改尺寸
/// </summary>
/// <param name="_width"></param>
/// <param name="_height"></param>
void AlgorithmVisualization::Pillar::resize(int _width, int _height)
{
int newWidth = max(_width - margin * 2, 2);
int newHeight= max(_height, 2);
rect->Width = newWidth; //设置宽度
rect->Height = newHeight; //设置高度
topTextBlock->Width = newWidth; //设置宽度
bottomTextBlock->Width = newWidth; //设置宽度
}