Skip to content

Commit 37133bc

Browse files
committed
Change getRouteFragment function location to Calculator
1 parent ec59c36 commit 37133bc

File tree

4 files changed

+98
-22
lines changed

4 files changed

+98
-22
lines changed

Calculator.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static String printPointsDictionary(Dictionary<Point, int> nodes, Diction
8080
{
8181
//Console.WriteLine(point.ToString());
8282
nodesCharge.TryGetValue(node.Key, out int charge);
83-
str += node.ToString() +" "+ charge + "%\n";
83+
str += node.ToString() + " " + charge + "%\n";
8484
//str += "(" + node.Key.X + ", " + node.Key.Y + ") [" + node.Value + "] " + "<" +""+ ">\n";
8585
}
8686
//Console.WriteLine();
@@ -351,7 +351,7 @@ public static void printIntArray(int[] array)
351351
public static void printIntList(List<int> list)
352352
{
353353
Console.WriteLine("Print int list");
354-
foreach (int num in list){
354+
foreach (int num in list) {
355355
Console.WriteLine(num.ToString());
356356
}
357357
}
@@ -361,5 +361,49 @@ public static int genRandInt(int startNum, int endNum) {
361361
int num = rand.Next(startNum, endNum);
362362
return num;
363363
}
364+
365+
public static List<Point> getRouteFragment(Point point, List<Point> routeList)
366+
{
367+
List<Point> fragment = new List<Point> { };
368+
369+
for (int i = 0; i < routeList.Count; i++)
370+
{
371+
//Console.WriteLine(i+". "+routeList[i]);
372+
if (routeList[i] == point)
373+
{
374+
fragment.Add(routeList[i - 1]);
375+
fragment.Add(routeList[i]);
376+
fragment.Add(routeList[i + 1]);
377+
378+
return fragment;
379+
}
380+
}
381+
return routeList;
382+
}
383+
384+
public static Point find_PointProjection_OnLine(Point point, Point a_point_inLine, Point b_point_inLine) {
385+
//Point a_line = new Point(1, 6);
386+
//Point b_point_inLine = new Point(4, 1);
387+
//Point point = new Point(6, 5);
388+
Point fulcrum = new Point(); // projection point on ab line
389+
double x4, y4;
390+
391+
double dx = b_point_inLine.X - a_point_inLine.X;
392+
double dy = b_point_inLine.Y - a_point_inLine.Y;
393+
double mag = Math.Sqrt(dx * dx + dy * dy);
394+
dx /= mag;
395+
dy /= mag;
396+
397+
// translate the point and get the dot product
398+
double lambda = (dx * (point.X - a_point_inLine.X)) + (dy * (point.Y - a_point_inLine.Y));
399+
x4 = (dx * lambda) + a_point_inLine.X;
400+
y4 = (dy * lambda) + a_point_inLine.Y;
401+
402+
fulcrum.X = (int)Math.Round(x4);
403+
fulcrum.Y = (int)Math.Round(y4);
404+
405+
//Console.WriteLine(fulcrum.ToString());
406+
return fulcrum;
407+
}
364408
}
365409
}

EnergyCalculator.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class EnergyCalculator // DEEC algorithm
1717

1818
//--------- Energy consumption parameters ------------
1919
double E_fs = 0.01;//nJ(10^-9) amplifier energy, free space model (short distance) | d<d0
20-
double E_mp = 1300000; //nJ // multipath fading model (large distance) | d >= d0
20+
//double E_mp = 1300000; //nJ // multipath fading model (large distance) | d >= d0
2121
int E_elec = 50; //nJ, energy for work signal transmission/recieve
2222
int node_E = 500000000; //nJ; = 0,5J // initial node energy
2323
double d0 = 87.7; // (m) distance threshold for swapping amplification models
@@ -48,14 +48,46 @@ public Dictionary<Point, int> GetNodesChargeDictionary()
4848
return chargeList;
4949
}
5050

51-
public void CalculteAllNodesEnergy(Dictionary<Point, int> nodesClustered, List<Point> clusterCenters, int stationHeight) {
51+
public void CalculteAllNodesEnergy(Dictionary<Point, int> nodesClustered, List<Point> routeList, int stationHeight) {
5252

53-
for (int i = 1; i < clusterCenters.Count; i++) {
53+
for (int i = 1; i < routeList.Count - 1; i++) {
5454
List<Point> cluster = Calculator.getClusterList(i, nodesClustered);
55-
Start_DT_Protocol(cluster, stationHeight);
55+
//Start_DT_Protocol(cluster, stationHeight);
56+
Start_DT_ToCloserPositionOnWay(cluster, stationHeight, routeList);
5657

5758

59+
}
60+
}
5861

62+
public void Start_DT_ToCloserPositionOnWay(List<Point> cluster, int stationHeight, List<Point> routeList) { // transmission while station moving on route
63+
Point center = Calculator.findCentroid(cluster);
64+
List<Point> routeFragment = Calculator.getRouteFragment(center, routeList);
65+
66+
for (int i = 0; i < cluster.Count; i++) {
67+
68+
Point closerFulcrum = center;
69+
double minDistance = 9999;
70+
71+
for (int j = 0; j < routeFragment.Count - 1; j++) {
72+
73+
Point fulcrum = Calculator.find_PointProjection_OnLine(cluster[i], routeFragment[j], routeFragment[j+1]);
74+
double distance = Calculator.calcDistance(fulcrum, cluster[i]);
75+
76+
if (distance < minDistance) {
77+
78+
// Point must be between a,b point (i, i+1 route). Then, check this by length between points:
79+
double ab_distance = Calculator.calcDistance(routeFragment[j], routeFragment[j+1]);
80+
double af_distance = Calculator.calcDistance(routeFragment[j], fulcrum);
81+
double bf_distance = Calculator.calcDistance(routeFragment[j+1], fulcrum);
82+
83+
if ((af_distance < ab_distance) && (bf_distance < ab_distance)) { // then fulcrum is between route points
84+
minDistance = distance;
85+
closerFulcrum = fulcrum;
86+
}
87+
}
88+
}
89+
90+
PPConnection(cluster[i], closerFulcrum, stationHeight);
5991
}
6092
}
6193

@@ -95,7 +127,7 @@ public int PPConnection(Point node, Point station, int stationHeight) { // trans
95127
}
96128

97129
stationUsedE += Convert.ToInt32(E_receive);
98-
130+
99131
return 0;
100132
}
101133

Form1.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private void btn1ClusterOn_Click(object sender, EventArgs e)
163163
PrintToTextBoxInfo(Calculator.getClusterDictionary(clusterNum, allPointsClustered));
164164

165165
Point center = Calculator.findCentroid(cluster); // find cluster center
166-
List<Point> routeFragment = routeBuilder.getRouteFragment(center); // find part route go throught cluster
166+
List<Point> routeFragment = Calculator.getRouteFragment(center, routeBuilder.RouteList); // find part route go throught cluster
167167

168168
//< Move all points to the start coordinate
169169
Point vector = new Point(center.X - radius, center.Y - radius);
@@ -222,7 +222,7 @@ private void DrawAllSavedObjects() {
222222

223223
private void btnCalcEnergy_Click(object sender, EventArgs e) {
224224
DrawAllSavedObjects();
225-
energyCalculator.CalculteAllNodesEnergy(allPointsClustered, clusterCenters, ReadStationHeighth());
225+
energyCalculator.CalculteAllNodesEnergy(allPointsClustered, routeBuilder.RouteList, ReadStationHeighth());
226226
PrintToTextBoxInfo(allPointsClustered);
227227
}
228228

RouteBuilder.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,22 @@ private void swap(int pos_1, int pos_2, List<Point> points)
222222
points[pos_2] = point;
223223
}
224224

225-
public List<Point> getRouteFragment(Point point) {
226-
List<Point> fragment = new List<Point> { };
225+
//public List<Point> getRouteFragment(Point point) {
226+
// List<Point> fragment = new List<Point> { };
227227

228-
for (int i = 0; i < routeList.Count; i++) {
229-
//Console.WriteLine(i+". "+routeList[i]);
230-
if (routeList[i] == point) {
228+
// for (int i = 0; i < routeList.Count; i++) {
229+
// //Console.WriteLine(i+". "+routeList[i]);
230+
// if (routeList[i] == point) {
231231

232-
fragment.Add(routeList[i - 1]);
233-
fragment.Add(routeList[i]);
234-
fragment.Add(routeList[i + 1]);
232+
// fragment.Add(routeList[i - 1]);
233+
// fragment.Add(routeList[i]);
234+
// fragment.Add(routeList[i + 1]);
235235

236-
return fragment;
237-
}
238-
}
239-
return routeList;
240-
}
236+
// return fragment;
237+
// }
238+
// }
239+
// return routeList;
240+
//}
241241

242242
//private void findIndexes()
243243
//{

0 commit comments

Comments
 (0)