-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathForel.cs
More file actions
168 lines (138 loc) · 4.57 KB
/
Forel.cs
File metadata and controls
168 lines (138 loc) · 4.57 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Clusterization_algorithms
{
public class Forel
{
public int clusterNum = 0;
public List<Point> ListOfClastersCenter = new List<Point>();
/* Dictionary contain all pairs (point, number_of_cluster),
* if (cluster = 0) => point/node not belong to cluster*/
private Dictionary<Point, int> _points = new Dictionary<Point, int>();
private int _radius; // nodes search radius
private GraphicsController _graphics;
public int Radius { get => _radius; set => _radius = value; }
public Forel()
{
}
public Forel(GraphicsController graphics)
{
_graphics = graphics;
}
public void SetPoints(Dictionary<Point, int> pointDictionary)
{
_points.Clear();
ListOfClastersCenter.Clear();
foreach (KeyValuePair<Point, int> point in pointDictionary)
{
_points.Add(point.Key, point.Value);
}
}
public Dictionary<Point, int> GetPoints()
{
return _points;
}
/// <summary>
/// Add selected point to cluster
/// </summary>
private void AddToCluster(Point key)
{
_points.Remove(key);
_points.Add(key, clusterNum);
}
/// <summary>
/// Add points to the cluster
/// </summary>
private void AddPointsToCluster(List<Point> pointsList)
{
foreach (Point point in pointsList)
{
AddToCluster(point);
}
}
/// <summary>
/// Get cluster from the cluster center coordinates
/// </summary>
private List<Point> GetCluster(Point center)
{
List<Point> cluster = new List<Point> { };
for (int i = 0; i < _points.Count; i++)
{
if (_points.ElementAt(i).Value == 0)
{
double distance = Calculator.CalcDistance(center, _points.ElementAt(i).Key);
if (distance <= _radius)
{
cluster.Add(_points.ElementAt(i).Key);
}
}
}
return cluster;
}
/// <summary>
/// Calculate new centroid (mass center)
/// </summary>
private Point FindNewCenter(Point center)
{
List<Point> cluster = GetCluster(center);
if (cluster.Count == 0){ // if cluster no have elements, new_center = center
_graphics.DrawCircle(center, _radius, Color.Black);
ListOfClastersCenter.Add(center);
return center;
}
Point newCenter = Calculator.FindCentroid(cluster);
_graphics.DrawPoint(newCenter, Brushes.Pink, 6);
_graphics.DrawCircle(newCenter, _radius, Color.LightGray);
if (center == newCenter){
_graphics.DrawCircle(center, _radius, Color.Black);
_graphics.DrawPoint(newCenter, Brushes.Red, 6);
ListOfClastersCenter.Add(center);
}
return newCenter;
}
/// <summary>
/// Start calculating clusters
/// </summary>
private Point StartClusterisation(Point center)
{
Point newCenter = FindNewCenter(center);
if (newCenter != center)
StartClusterisation(newCenter);
else
AddPointsToCluster(GetCluster(newCenter));
return newCenter;
}
/// <summary>
/// START FOREL CLUSTERISATION
/// </summary>
public void StartForel()
{
clusterNum = 0;
for (int i = 0; i < _points.Count; i++)
{
if (_points.ElementAt(i).Value == 0)
{
clusterNum++;
StartClusterisation(_points.ElementAt(i).Key);
}
}
}
/// <summary>
/// Print formatted to TextBox
/// </summary>
private void PrintList(List<Point> list)
{
Console.WriteLine("Print point list:");
int counter = 1;
foreach (Point p in list)
{
Console.Write(p + " ");
if (counter % 10 == 0)
Console.WriteLine();
counter++;
}
}
}
}