/
Edge.cs
executable file
·100 lines (91 loc) · 2.44 KB
/
Edge.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
using System;
using Microsoft.Ink;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GraphAnimator
{
public class Edge
{
public static Color DEFAULT = Color.Black;
public static int DEFAULT_WEIGHT = 10;
private int weight;
private Stroke stroke;
private Node a, b;
private Color color;
public Edge(Node n1, Node n2, InkOverlay i, int weight)
{
//Keep track of the nodes its attached to
this.a = n1;
this.b = n2;
this.weight = weight;
color = DEFAULT;
//Make sure the nodes contain this in their edges
a.Edges.Add(this);
b.Edges.Add(this);
Point[] p = {a.CenterPoint,b.CenterPoint};
//Draw a stroke connecting both nodes
this.stroke = i.Ink.CreateStroke(p);
}
public Edge(Node n1, Node n2, InkOverlay i) : this(n1,n2,i,Edge.DEFAULT_WEIGHT) {}
#region Accessor and Mutator Methods
public Node NodeA
{
get{return a;}
}
public Node NodeB
{
get{return b;}
}
public Color Color
{
get{return color;}
set{color = value;}
}
public int Weight
{
get{return weight;}
set{weight = value;}
}
public Stroke Stroke
{
get{return stroke;}
set{stroke = value;}
}
#endregion
//Checks if the two nodes have an edge connecting them
public static bool hasEdge(Node a, Node b)
{
for(int i=0; i<a.Edges.Length(); i++)
{
if(a.Edges[i].NodeA.Equals(b) || a.Edges[i].NodeB.Equals(b))
{
return true;
}
}
return false;
}
public void Render(InkOverlay i, Graphics g)
{
int width = weight.ToString().Length*15;
int height = 18;
stroke.DrawingAttributes = i.DefaultDrawingAttributes.Clone();
stroke.DrawingAttributes.Color = color;
i.Renderer.Draw(g,stroke);
Rectangle rect = StrokeManager.InkSpaceToPixelRect(i,g,stroke.GetBoundingBox());
Point center = new Point(rect.X+rect.Width/2 - width/2, rect.Y+rect.Height/2 - height/2);
g.FillRectangle(new SolidBrush(Color.White),center.X,center.Y,width, height);
g.DrawString(weight.ToString(), new Font("Courier New",14, FontStyle.Bold),new SolidBrush(Color.Black), center);
}
public override bool Equals(object obj)
{
if(obj == null) return false;
Edge edge = obj as Edge;
return (this.Stroke.Id == edge.Stroke.Id);
}
//Checks if the given stroke is equal to this edge
public bool strokeEquals(Stroke s)
{
return this.Stroke.Id == s.Id;
}
}
}