Skip to content

Commit

Permalink
Add node class
Browse files Browse the repository at this point in the history
  • Loading branch information
l0stfake7 committed Apr 7, 2017
1 parent 183d519 commit eef4bc8
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 33 deletions.
3 changes: 3 additions & 0 deletions AntsAlgorithm/AntsAlgorithm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Ant.cs" />
<Compile Include="Classes\Node.cs" />
<Compile Include="Classes\Path.cs" />
<Compile Include="Classes\Utility.cs" />
<Compile Include="Classes\World.cs" />
<Compile Include="Enums\NodeType.cs" />
<Compile Include="Enums\SelectMode.cs" />
<Compile Include="Views\MainForm.cs">
<SubType>Form</SubType>
Expand Down
23 changes: 23 additions & 0 deletions AntsAlgorithm/Classes/Node.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AntsAlgorithm.Enums;

namespace AntsAlgorithm.Classes
{
class Node
{
public Point Point;
public NodeType NodeType;

public Node(Point point, NodeType nodeType)
{
this.Point = point;
this.NodeType = nodeType;
}

}
}
12 changes: 12 additions & 0 deletions AntsAlgorithm/Classes/Path.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntsAlgorithm.Classes
{
class Path
{
}
}
2 changes: 1 addition & 1 deletion AntsAlgorithm/Classes/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class World
public static bool Run = false;
public static short TimeInterval = 750;//in ms

public List<Point> NodeList;
public List<Node> NodeList;
public List<Ant> Ants;
public Dictionary<int, Tuple<Point, Point>> PathList;
}
Expand Down
9 changes: 9 additions & 0 deletions AntsAlgorithm/Enums/NodeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AntsAlgorithm.Enums
{
public enum NodeType
{
Default = 0,
Colony,
Food
}
}
64 changes: 32 additions & 32 deletions AntsAlgorithm/Views/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MainForm()

_world = new World
{
NodeList = new List<Point>(),
NodeList = new List<Node>(),
Ants = new List<Ant>(),
PathList = new Dictionary<int, Tuple<Point, Point>>()
};
Expand All @@ -43,7 +43,7 @@ public MainForm()

_brush = new SolidBrush(Color.Black);
_rOp = SelectMode.Select;
_lastSel = _world.NodeList[0];
_lastSel = _world.NodeList[0].Point;
_counter = 0;

}
Expand Down Expand Up @@ -76,24 +76,24 @@ private void pictureBox_Click(object sender, EventArgs ea)
{
case SelectMode.Select:
{
foreach (Point p in _world.NodeList)
foreach (Node node in _world.NodeList)
{
if (Utility.InsideCircle(p.X, p.Y, Radius, e.X, e.Y))
if (Utility.InsideCircle(node.Point.X, node.Point.Y, Radius, e.X, e.Y))
{
_lastSel = p; //current item in loop
DebugLabel2("Select " + p.X + " " + p.Y);
_lastSel = node.Point; //current item in loop
DebugLabel2("Select " + node.Point.X + " " + node.Point.Y);
break;
}
}
break;
}
case SelectMode.Node:
{
foreach (Point p in _world.NodeList)
foreach (Node node in _world.NodeList)
{
if (!Utility.InsideCircle(p.X, p.Y, Radius, e.X, e.Y))
if (!Utility.InsideCircle(node.Point.X, node.Point.Y, Radius, e.X, e.Y))
{
_world.NodeList.Add(new Point(e.X, e.Y));
_world.NodeList.Add(new Node(new Point(e.X, e.Y), NodeType.Default));
DrawNode(new Point(e.X, e.Y));
DebugLabel2("New node " + e.X + " " + e.Y);
//pictureBox.Refresh();
Expand All @@ -104,16 +104,16 @@ private void pictureBox_Click(object sender, EventArgs ea)
}
case SelectMode.Path:
{
foreach (Point p in _world.NodeList)
foreach (Node node in _world.NodeList)
{
if (Utility.InsideCircle(p.X, p.Y, Radius, e.X, e.Y))
if (Utility.InsideCircle(node.Point.X, node.Point.Y, Radius, e.X, e.Y))
{
if (_lastSel != Point.Empty)
{
var path = Tuple.Create(new Point(_lastSel.X, _lastSel.Y), new Point(p.X, p.Y));
var path = Tuple.Create(new Point(_lastSel.X, _lastSel.Y), new Point(node.Point.X, node.Point.Y));
_world.PathList.Add(_world.PathList.Count + 1, path);
DrawPath(_lastSel, new Point(p.X, p.Y));
DebugLabel2("New path " + _lastSel.X + " " + _lastSel.Y + " to " + p.X + " " + p.Y);
DrawPath(_lastSel, new Point(node.Point.X, node.Point.Y));
DebugLabel2("New path " + _lastSel.X + " " + _lastSel.Y + " to " + node.Point.X + " " + node.Point.Y);
}
break;
}
Expand All @@ -122,20 +122,20 @@ private void pictureBox_Click(object sender, EventArgs ea)
}
case SelectMode.Ant:
{
foreach (Point p in _world.NodeList)
foreach (Node node in _world.NodeList)
{
if (Utility.InsideCircle(p.X, p.Y, Radius, e.X, e.Y))
if (Utility.InsideCircle(node.Point.X, node.Point.Y, Radius, e.X, e.Y))
{
if (_lastSel != Point.Empty)
{
var path = Tuple.Create(new Point(_lastSel.X, _lastSel.Y), new Point(p.X, p.Y));
var path = Tuple.Create(new Point(_lastSel.X, _lastSel.Y), new Point(node.Point.X, node.Point.Y));
foreach (KeyValuePair<int, Tuple<Point, Point>> entry in _world.PathList)
{
if (entry.Value.Equals(path))
{//find path with start point p.x, p.y and end point e.x e.y, ///todo find from e.x e.y to p.x, p.y
IList<Tuple<Double, Double>> points = Utility.SplitLine(
new Tuple<Double, Double>(_lastSel.X, _lastSel.Y),
new Tuple<Double, Double>(p.X, p.Y), 25);
new Tuple<Double, Double>(node.Point.X, node.Point.Y), 25);
Ant ant = new Ant(points);
_world.Ants.Add(ant);
DebugLabel2("New Ant");
Expand Down Expand Up @@ -164,17 +164,17 @@ private void pictureBox_Paint(object sender, PaintEventArgs e)
g.DrawString(x.ToString(), drawFont, drawBrush, (float)_world.Ants[x].ActualPoint.Item1 + 25, (float)_world.Ants[x].ActualPoint.Item2 - 5);
}
_font = new Font(FontFamily.GenericMonospace, 7.5F, FontStyle.Regular);
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[0].X, _world.NodeList[0].Y, Radius + 5, Radius + 5);
g.DrawString("AntsColony " + "[" + _world.NodeList[0].X + "," + _world.NodeList[0].Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[0].X, _world.NodeList[0].Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[1].X, _world.NodeList[1].Y, Radius + 5, Radius + 5);
g.DrawString("Food :) " + "[" + _world.NodeList[1].X + "," + _world.NodeList[1].Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[1].X, _world.NodeList[1].Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[0].Point.X, _world.NodeList[0].Point.Y, Radius + 5, Radius + 5);
g.DrawString("AntsColony " + "[" + _world.NodeList[0].Point.X + "," + _world.NodeList[0].Point.Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[0].Point.X, _world.NodeList[0].Point.Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[1].Point.X, _world.NodeList[1].Point.Y, Radius + 5, Radius + 5);
g.DrawString("Food :) " + "[" + _world.NodeList[1].Point.X + "," + _world.NodeList[1].Point.Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[1].Point.X, _world.NodeList[1].Point.Y));

foreach (Point p in _world.NodeList)
foreach (Node node in _world.NodeList)
{
g.FillEllipse(_brush, p.X, p.Y, Radius, Radius);
g.FillEllipse(_brush, node.Point.X, node.Point.Y, Radius, Radius);
_font = new Font(FontFamily.GenericMonospace, 6.0F, FontStyle.Regular);
g.DrawString("Point " + _world.NodeList.Count + "[" + p.X + "," + p.Y + "]", _font,
new SolidBrush(Color.Brown), new Point(p.X, p.Y));
g.DrawString("Point " + _world.NodeList.Count + "[" + node.Point.X + "," + node.Point.Y + "]", _font,
new SolidBrush(Color.Brown), new Point(node.Point.X, node.Point.Y));
}

foreach (KeyValuePair<int, Tuple<Point, Point>> entry in _world.PathList)
Expand Down Expand Up @@ -275,13 +275,13 @@ private void DebugLabel2(String message)

private void DrawStart()
{
_world.NodeList.Add(new Point(100, 150));
_world.NodeList.Add(new Point(250, 175));
_world.NodeList.Add(new Node(new Point(100, 150), NodeType.Colony));
_world.NodeList.Add(new Node(new Point(250, 175), NodeType.Food));
_font = new Font(FontFamily.GenericMonospace, 7.5F, FontStyle.Regular);
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[0].X, _world.NodeList[0].Y, Radius + 5, Radius + 5);
g.DrawString("AntsColony " + "[" + _world.NodeList[0].X + "," + _world.NodeList[0].Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[0].X, _world.NodeList[0].Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[1].X, _world.NodeList[1].Y, Radius + 5, Radius + 5);
g.DrawString("Food :) " + "[" + _world.NodeList[1].X + "," + _world.NodeList[1].Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[1].X, _world.NodeList[1].Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[0].Point.X, _world.NodeList[0].Point.Y, Radius + 5, Radius + 5);
g.DrawString("AntsColony " + "[" + _world.NodeList[0].Point.X + "," + _world.NodeList[0].Point.Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[0].Point.X, _world.NodeList[0].Point.Y));
g.FillEllipse(new SolidBrush(Color.Red), _world.NodeList[1].Point.X, _world.NodeList[1].Point.Y, Radius + 5, Radius + 5);
g.DrawString("Food :) " + "[" + _world.NodeList[1].Point.X + "," + _world.NodeList[1].Point.Y + "]", _font, new SolidBrush(Color.Brown), new Point(_world.NodeList[1].Point.X, _world.NodeList[1].Point.Y));
pictureBox.Image = _bmp;
}

Expand Down

0 comments on commit eef4bc8

Please sign in to comment.