-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRenderer.cs
More file actions
217 lines (189 loc) · 7.56 KB
/
GraphRenderer.cs
File metadata and controls
217 lines (189 loc) · 7.56 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace WinFormsApp1
{
public static class GraphRenderer
{
public static void DrawGraph(Graphics g, WeightedGraph graph,
WeightedGraph.Node selectedNode = null,
WeightedGraph.Node sourceNode = null,
WeightedGraph.Node sinkNode = null,
bool showWeights = true,
bool showFlow = false)
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// Рисуем рёбра
foreach (var edge in graph.Edges)
{
DrawEdge(g, edge, graph.IsDirected, showWeights, showFlow);
}
// Рисуем узлы
foreach (var node in graph.Nodes)
{
bool isSource = node == sourceNode;
bool isSink = node == sinkNode;
bool isSelected = node == selectedNode;
DrawNode(g, node, isSelected, isSource, isSink);
}
}
private static void DrawEdge(Graphics g, WeightedGraph.Edge edge,
bool isDirected, bool showWeights, bool showFlow)
{
Point p1 = edge.From.Position;
Point p2 = edge.To.Position;
Color edgeColor = edge.Color;
float width = edge.IsInResult ? 4 : (edge.IsHighlighted ? 3 : 2);
if (edge.IsInResult)
edgeColor = Color.Green;
else if (edge.IsHighlighted)
edgeColor = Color.Orange;
using (Pen pen = new Pen(edgeColor, width))
{
if (isDirected)
{
// Рисуем стрелку
pen.CustomEndCap = new AdjustableArrowCap(5, 5);
// Укорачиваем линию чтобы стрелка была на границе узла
double angle = Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
int radius = 25;
Point adjustedP2 = new Point(
(int)(p2.X - radius * Math.Cos(angle)),
(int)(p2.Y - radius * Math.Sin(angle)));
Point adjustedP1 = new Point(
(int)(p1.X + radius * Math.Cos(angle)),
(int)(p1.Y + radius * Math.Sin(angle)));
g.DrawLine(pen, adjustedP1, adjustedP2);
}
else
{
g.DrawLine(pen, p1, p2);
}
}
// Текст на ребре
Point mid = new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
string text = "";
if (showFlow)
{
text = $"{edge.Flow}/{edge.Capacity}";
}
else if (showWeights)
{
text = edge.Weight.ToString();
}
if (!string.IsNullOrEmpty(text))
{
using (Font font = new Font("Arial", 9, FontStyle.Bold))
using (Brush bgBrush = new SolidBrush(Color.FromArgb(220, Color.White)))
{
SizeF textSize = g.MeasureString(text, font);
RectangleF rect = new RectangleF(
mid.X - textSize.Width / 2 - 2,
mid.Y - textSize.Height / 2 - 2,
textSize.Width + 4,
textSize.Height + 4);
g.FillRectangle(bgBrush, rect);
g.DrawString(text, font, Brushes.Black,
mid.X - textSize.Width / 2,
mid.Y - textSize.Height / 2);
}
}
}
private static void DrawNode(Graphics g, WeightedGraph.Node node,
bool isSelected, bool isSource, bool isSink)
{
int radius = 25;
Rectangle rect = new Rectangle(
node.Position.X - radius,
node.Position.Y - radius,
radius * 2,
radius * 2);
// Тень
Rectangle shadowRect = new Rectangle(rect.X + 3, rect.Y + 3, rect.Width, rect.Height);
using (Brush shadowBrush = new SolidBrush(Color.FromArgb(40, Color.Black)))
{
g.FillEllipse(shadowBrush, shadowRect);
}
// Определяем цвет
Color nodeColor = node.Color;
if (isSource)
nodeColor = Color.Lime;
else if (isSink)
nodeColor = Color.Coral;
// Градиентная заливка
using (var brush = new LinearGradientBrush(
rect,
nodeColor,
ControlPaint.Light(nodeColor, 0.5f),
LinearGradientMode.ForwardDiagonal))
{
g.FillEllipse(brush, rect);
}
// Обводка
Color borderColor = isSelected ? Color.Red : Color.Black;
float borderWidth = isSelected ? 3 : 2;
using (Pen pen = new Pen(borderColor, borderWidth))
{
g.DrawEllipse(pen, rect);
}
// ID узла
string text = node.Id.ToString();
using (Font font = new Font("Arial", 12, FontStyle.Bold))
{
SizeF textSize = g.MeasureString(text, font);
g.DrawString(text, font, Brushes.Black,
node.Position.X - textSize.Width / 2,
node.Position.Y - textSize.Height / 2);
}
// Метки S/T для source/sink
if (isSource || isSink)
{
string label = isSource ? "S" : "T";
using (Font font = new Font("Arial", 8, FontStyle.Bold))
{
g.DrawString(label, font, Brushes.Black,
node.Position.X + radius - 5,
node.Position.Y - radius - 5);
}
}
}
public static void DrawLegend(Graphics g, Dictionary<Color, string> items,
int x = 10, int y = 10)
{
int legendWidth = 160;
int legendHeight = items.Count * 25 + 10;
using (Brush bgBrush = new SolidBrush(Color.FromArgb(240, Color.White)))
{
g.FillRectangle(bgBrush, x, y, legendWidth, legendHeight);
}
using (Pen borderPen = new Pen(Color.Gray, 1))
{
g.DrawRectangle(borderPen, x, y, legendWidth, legendHeight);
}
int itemY = y + 5;
foreach (var item in items)
{
Rectangle rect = new Rectangle(x + 5, itemY, 20, 20);
using (var brush = new LinearGradientBrush(
rect,
item.Key,
ControlPaint.Light(item.Key, 0.5f),
LinearGradientMode.ForwardDiagonal))
{
g.FillEllipse(brush, rect);
}
using (Pen pen = new Pen(Color.Black, 1))
{
g.DrawEllipse(pen, rect);
}
using (Font font = new Font("Arial", 9))
{
g.DrawString(item.Value, font, Brushes.Black, x + 30, itemY + 2);
}
itemY += 25;
}
}
}
}