-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCalculator.cs
More file actions
366 lines (295 loc) · 11.7 KB
/
Calculator.cs
File metadata and controls
366 lines (295 loc) · 11.7 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Clusterization_algorithms
{
/// <summary>
/// Class calculate math operations and print data. Call without creating object.
/// </summary>
public class Calculator
{
/// <summary>
/// Find points center | centroid | barycenter | mass center
/// </summary>
public static Point FindCentroid(List<Point> pointGroup)
{
int x = 0;
int y = 0;
for (int i = 0; i < pointGroup.Count; i++)
{
x += pointGroup.ElementAt(i).X;
y += pointGroup.ElementAt(i).Y;
}
x = x / pointGroup.Count;
y = y / pointGroup.Count;
Point point = new Point(x, y);
return point;
}
/// <summary>
/// Calculates distance between A, B points
/// </summary>
public static double CalcDistance(Point a, Point b)
{
double delX = Math.Pow(b.X - a.X, 2);
double delY = Math.Pow(b.Y - a.Y, 2);
return Math.Sqrt(delX + delY);
}
/// <summary>
/// Generate random points and add them to the dictionary
/// </summary>
public static Dictionary<Point, int> GeneratePoints(int count, int rangeX, int rangeY)
{
Random rand = new Random();
Dictionary<Point, int> points = new Dictionary<Point, int>();
for (int i = 0; i < count; i++)
{
Point point;
do
{
point = new Point(rand.Next(rangeX), rand.Next(rangeY));
}
while (points.ContainsKey(point));
points.Add(point, 0);
}
return points;
}
public static string PrintNodesAndCharge(Dictionary<Point, int> nodes, Dictionary<Point, int> nodesCharge)
{
nodes = SortDictionaryByValue(nodes);
string str = "";
foreach (var node in nodes)
{
nodesCharge.TryGetValue(node.Key, out int charge);
str += node.ToString() + " " + charge + "%\n";
}
return str;
}
public static double Find3PointsLength(Point a, Point b, Point c)
{
double l1 = CalcDistance(a, b);
double l2 = CalcDistance(b, c);
return l1 + l1;
}
/// <summary>
/// Calculates external corner cos between the lines.
/// </summary>
public static double GetCOS(Point a, Point b, Point c)
{
Point v1 = new Point(b.X - a.X, b.Y - a.Y); // calculate ab vector
Point v2 = new Point(c.X - b.X, c.Y - b.Y); // calculate bc vector
double modV1 = Math.Sqrt(v1.X * v1.X + v1.Y * v1.Y);
double modV2 = Math.Sqrt(v2.X * v2.X + v2.Y * v2.Y);
double skal = v1.X * v2.X + v1.Y * v2.Y;
return skal / (modV1 * modV2);
}
public static double GetSIN(Point a, Point b, Point c)
{
double cos = GetCOS(a, b, c);
return Math.Sqrt(1 - cos * cos);
}
public static List<Point> DictionaryToList(Dictionary<Point, int> dictionary)
{
List<Point> points = new List<Point>() { };
for (int i = 0; i < dictionary.Count; i++)
points.Add(dictionary.ElementAt(i).Key);
return points;
}
public static List<Point> DictionaryToList(Dictionary<Point, double> dictionary)
{
List<Point> points = new List<Point>() { };
for (int i = 0; i < dictionary.Count; i++)
points.Add(dictionary.ElementAt(i).Key);
return points;
}
public static Dictionary<Point, int> ListToDictionary(List<Point> pointList)
{
Dictionary<Point, int> dictionary = new Dictionary<Point, int> { };
for (int i = 0; i < pointList.Count; i++)
dictionary.Add(pointList[i], 0);
return dictionary;
}
public static double CalcRouteLength(List<Point> points)
{
if(points.Count == 0)
return 0;
double dist = CalcDistance(points[0], points[points.Count - 1]);
for (int i = 0; i < points.Count - 1; i++)
dist += CalcDistance(points[i], points[i + 1]);
return dist;
}
public static Dictionary<Point, int> SortDictionaryByValue(Dictionary<Point, int> dictionary)
{
return dictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
public static Dictionary<Point, double> SortDictionaryByValue(Dictionary<Point, double> dictionary)
{
return dictionary.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
}
public static List<Point> GetClusterList(int clusterNum, Dictionary<Point, int> dictionary)
{
List<Point> cluster = new List<Point> { };
for (int i = 0; i < dictionary.Count; i++)
{
if (dictionary.ElementAt(i).Value == clusterNum)
{
cluster.Add(dictionary.ElementAt(i).Key);
}
}
return cluster;
}
public static Dictionary<Point, int> GetClusterDictionary(int clusterNum, Dictionary<Point, int> dictionary)
{
Dictionary<Point, int> cluster = new Dictionary<Point, int> { };
for (int i = 0; i < dictionary.Count; i++)
{
if (dictionary.ElementAt(i).Value == clusterNum)
{
cluster.Add(dictionary.ElementAt(i).Key, dictionary.ElementAt(i).Value);
}
}
return cluster;
}
public static void MovePointList(List<Point> cluster, Point vector)
{
for (int i = 0; i < cluster.Count; i++)
{
cluster[i] = new Point(cluster[i].X - vector.X, cluster[i].Y - vector.Y);
}
}
public static void ZoomPointList(List<Point> list, double zoom)
{
for (int i = 0; i < list.Count; i++)
{
list[i] = new Point((int)Math.Round(list[i].X * zoom), (int)Math.Round(list[i].Y * zoom));
}
}
public static Dictionary<Point, int> CopyDictionary(Dictionary<Point, int> dictionary)
{
Dictionary<Point, int> newDictionary = new Dictionary<Point, int> { };
foreach (var pair in dictionary)
newDictionary.Add(pair.Key, pair.Value);
return newDictionary;
}
public static void PrintIntArray(int[] array)
{
Console.WriteLine("Print int array");
foreach (int num in array)
{
Console.Write(num + " ");
}
}
public static void PrintIntList(List<int> list)
{
Console.WriteLine("Print int list");
foreach (int num in list)
{
Console.WriteLine(num.ToString());
}
}
public static int GenRandInt(int startNum, int endNum)
{
Random rand = new Random();
return rand.Next(startNum, endNum);
}
public static List<Point> GetRouteFragment(Point point, List<Point> routeList)
{
List<Point> fragment = new List<Point> { };
for (int i = 0; i < routeList.Count; i++)
{
if (routeList[i] == point)
{
fragment.Add(routeList[i - 1]);
fragment.Add(routeList[i]);
fragment.Add(routeList[i + 1]);
return fragment;
}
}
return routeList;
}
public static Point GetPointProjectionOnLine(Point point, Point a_point_inLine, Point b_point_inLine)
{
Point fulcrum = new Point(); // point projection on ab line
double x4, y4;
double dx = b_point_inLine.X - a_point_inLine.X;
double dy = b_point_inLine.Y - a_point_inLine.Y;
double mag = Math.Sqrt(dx * dx + dy * dy);
dx /= mag;
dy /= mag;
// translate the point and get the dot product
double lambda = (dx * (point.X - a_point_inLine.X)) + (dy * (point.Y - a_point_inLine.Y));
x4 = (dx * lambda) + a_point_inLine.X;
y4 = (dy * lambda) + a_point_inLine.Y;
fulcrum.X = (int)Math.Round(x4);
fulcrum.Y = (int)Math.Round(y4);
return fulcrum;
}
public static Point GetPointProjectionOnMultiLine(Point point, List<Point> intersectionList)
{
Point closerFulcrum;
double minDistance = double.MaxValue;
if (intersectionList.Contains(point))
{
closerFulcrum = point;
}
else
{
closerFulcrum = Calculator.FindClosestPoint(point, intersectionList); // if no projection return closer intersection
}
for (int j = 0; j < intersectionList.Count - 1; j++)
{
Point fulcrum = Calculator.GetPointProjectionOnLine(point, intersectionList[j], intersectionList[j + 1]);
double dist_point_fulcrum = Calculator.CalcDistance(fulcrum, point);
if (dist_point_fulcrum < minDistance)
{
// Point must be between a,b point (i, i+1 route). Then, check this by _length between points:
double ab_distance = Calculator.CalcDistance(intersectionList[j], intersectionList[j + 1]);
double af_distance = Calculator.CalcDistance(intersectionList[j], fulcrum);
double bf_distance = Calculator.CalcDistance(intersectionList[j + 1], fulcrum);
if ((af_distance < ab_distance) && (bf_distance < ab_distance))
{ // then fulcrum is between route points
minDistance = dist_point_fulcrum;
closerFulcrum = fulcrum;
}
}
}
return closerFulcrum;
}
public static List<Point> SortPointListByX(List<Point> points)
{
return points.OrderBy(point => point.X).ToList();
}
public static Point FindClosestPoint(Point point, List<Point> list)
{
Point minPoint = list[0];
double minDist = Calculator.CalcDistance(point, minPoint);
foreach (Point p in list) {
double dist = Calculator.CalcDistance(p, point);
if (dist > 0 && dist < minDist) {
minDist = dist;
minPoint = p;
}
}
return minPoint;
}
public static void Console_PrintDictionary(Dictionary<Point, int> dictionary)
{
Console.WriteLine("*** Dictionary ***");
foreach (var keyValuePair in dictionary)
Console.WriteLine(keyValuePair.Key + "/" + keyValuePair.Value);
Console.WriteLine("******************");
}
public static String Console_PrintPointList(List<Point> points)
{
String str = "";
Console.WriteLine("*** Points list: ***");
foreach (var point in points)
{
Console.WriteLine(point.ToString());
str += point.ToString() + "\n";
}
Console.WriteLine("********************");
return str;
}
}
}