/
Node.cs
executable file
·141 lines (130 loc) · 3.26 KB
/
Node.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
using System;
using Microsoft.Ink;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GraphAnimator
{
public class Node
{
public static Color DEFAULT = Color.AntiqueWhite;
public static Color TEXT_DEFAULT = Color.Black;
private int id;
private Stroke stroke;
private Point centerPoint;
private string text;
private int distance;
private bool isRect;
private Color fillColor, textColor;
private Edges edges;
public Node(Stroke stroke)
{
this.stroke = stroke;
this.id = stroke.Id;
Rectangle r = stroke.GetBoundingBox();
//Determine whether this node is a rectangle or not
isRect = r.Width*Math.PI < StrokeManager.StrokeLength(stroke);
centerPoint = new Point(r.X + r.Width/2, r.Y + r.Height/2);
fillColor = DEFAULT;
edges = new Edges();
textColor = TEXT_DEFAULT;
text = "";
//Set initial distance to infinity (max int)
distance = Int32.MaxValue;
}
#region Accessor and Mutators
public int Id
{
get{return id;}
}
public Stroke Stroke
{
get{return stroke;}
}
public Color Color
{
get{return fillColor;}
set{fillColor = value;}
}
public Edges Edges
{
get{return edges;}
}
public Point CenterPoint
{
get{return centerPoint;}
set
{
//When setting a new center point, must make new edges.
centerPoint = value;
for(int i=0; i<edges.Length(); i++)
{
Point[] p = new Point[2];
p[0] = centerPoint;
p[1] = Node.GetOther(this,edges[i]).CenterPoint;
edges[i].Stroke.Ink.DeleteStroke(edges[i].Stroke);
edges[i].Stroke = stroke.Ink.CreateStroke(p);
}
}
}
public string Text
{
get{return text;}
set{text = value;}
}
public Color TextColor
{
get{return textColor;}
set{textColor = value;}
}
public int Distance
{
get{return distance;}
set{distance = value;}
}
#endregion
public override bool Equals(object obj)
{
if(obj == null) return false;
Node n = obj as Node;
return this.Id == n.Id;
}
public void Render(InkOverlay i, Graphics g)
{
if(stroke.Deleted == true) return;
Rectangle rect = StrokeManager.InkSpaceToPixelRect(i, g, stroke.GetBoundingBox());
if(isRect)
g.FillRectangle(new SolidBrush(fillColor), rect);
else
g.FillEllipse(new SolidBrush(fillColor), rect);
i.Renderer.Draw(g, stroke);
Point[] p = {new Point(centerPoint.X, centerPoint.Y)};
i.Renderer.InkSpaceToPixel(g, ref p);
p[0].X -= 15;
p[0].Y -= 10;
g.DrawString(text, new Font("Arial",10), new SolidBrush(textColor), p[0]);
}
//Get the node attached to Edge e that isn't Node n
public static Node GetOther(Node n, Edge e)
{
if(n == null || !n.Edges.Contains(e)) return null;
return (n.Equals(e.NodeB)) ? e.NodeA : e.NodeB;
}
//Used for Priority Queue
#region IComparer Methods and Classes
public static IComparer sortAscending()
{
return (IComparer) new sortAscendingDistance();
}
private class sortAscendingDistance : IComparer
{
public int Compare(object x, object y)
{
Node nx = x as Node;
Node ny = y as Node;
return nx.Distance - ny.Distance;
}
}
#endregion
}
}