-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathForel.cs
166 lines (133 loc) · 4.83 KB
/
Forel.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Clusterization_algorithms
{
class Forel
{
/* Dictionary contain all pairs (point, number_of_cluster),
* if (cluster = 0) => point not belong to cluster*/
private Dictionary<Point, int> points = new Dictionary<Point, int>();
public int clusterNum = 0;
private int radius; // search radius
private Graphic graphic;
public List<Point> listOfClastersCenter = new List<Point>();
public int Radius { get => radius; set => radius = value; }
public Forel()
{
}
public Forel(Graphic graphic)
{
this.graphic = graphic;
}
public void setPoints(Dictionary<Point, int> pointDictionary)
{
points.Clear();
listOfClastersCenter.Clear();
foreach (KeyValuePair<Point, int> point in pointDictionary) {
points.Add(point.Key, point.Value);
}
//graphic.DrawPointDictionary(pointDictionary);
}
public Dictionary<Point, int> getPoints() {
return points;
}
//write select point to cluster
private void addToCluster(Point key)
{
//Console.WriteLine("#AddToCluster");
points.Remove(key);
points.Add(key, clusterNum);
//Console.WriteLine("Add point " + key + " to cluster " + clusterNum);
//Calculator.printPointsDictionary(points);
}
// write points in list to cluster
private void addPointsToCluster(List<Point> pointsList)
{
//Console.WriteLine("#AddPointsToCluster");
foreach (Point point in pointsList)
{
addToCluster(point);
}
}
// get cluster from center coordinates
private List<Point> getCluster(Point center)
{
//Console.WriteLine("#GetCluster");
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);
}
}
//Console.WriteLine();
//Console.WriteLine("<< GetCluster return cluster list");
return cluster;
}
// find new centroid (mass center)
private Point findNewCenter(Point center)
{
List<Point> cluster = getCluster(center);
if (cluster.Count == 0){ // if cluster no have elements, new_center = center
graphic.DrawCircle(center, radius, Color.Black);
listOfClastersCenter.Add(center);
return center;
}
//Point newCenter = findBarycenter(cluster);
Point newCenter = Calculator.findCentroid(cluster);
graphic.DrawPoint(newCenter, Brushes.Pink, 6);
graphic.DrawCircle(newCenter, radius, Color.LightGray);
if (center == newCenter){
graphic.DrawCircle(center, radius, Color.Black);
graphic.DrawPoint(newCenter, Brushes.Red, 6);
listOfClastersCenter.Add(center);
}
return newCenter;
}
//start clusters searching process
private Point clusterisation(Point center)
{
//Console.WriteLine("#Clusterisation");
Point newCenter = findNewCenter(center);
//Console.WriteLine(newCenter);
if (newCenter != center)
clusterisation(newCenter);
else
addPointsToCluster(getCluster(newCenter));
//Console.WriteLine("<< Clusterization return " + center);
return newCenter;
}
// THIS METHOD RUN FOREL ALGORITHM
public void startForel()
{
clusterNum = 0;
//Console.WriteLine("#StartForel");
for (int i = 0; i < points.Count; i++)
{
if (points.ElementAt(i).Value == 0)
{
clusterNum++;
clusterisation(points.ElementAt(i).Key);
}
}
}
// print formatted to TextBox
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++;
}
}
}
}