-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.cs
155 lines (136 loc) · 4.67 KB
/
Vertex.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.ComponentModel;
namespace PolygonEditor
{
public struct VertexNeighbours
{
public PolygonEdge edge;
public Vertex next;
public Vertex prev;
}
public class Vertex: INotifyPropertyChanged
{
int _x;
public int X
{
get { return _x; }
set
{
if(value < 0 || value > (Application.Current.MainWindow as MainWindow).canvas.ActualWidth)
{
return;
}
SetField(ref _x, value, "X");
if (window.canvas.Children.Contains(control))
{
Canvas.SetLeft(control , value - control.Width/2);
}
if(this.ownerPolygon != null && this.ownerPolygon.isDone)
{
this.neighbours.edge.ActualiseVisualPosition();
this.neighbours.prev.neighbours.edge.ActualiseVisualPosition();
}
}
}
int _y;
public int Y
{
get { return _y; }
set
{
if (value < 0 || value > (Application.Current.MainWindow as MainWindow).canvas.ActualHeight)
{
return;
}
SetField(ref _y, value, "Y");
if (window.canvas.Children.Contains(control))
{
Canvas.SetTop(control, value - control.Height/2);
}
if (this.ownerPolygon != null && this.ownerPolygon.isDone)
{
this.neighbours.edge.ActualiseVisualPosition();
this.neighbours.prev.neighbours.edge.ActualiseVisualPosition();
}
}
}
public Polygon ownerPolygon;
public VertexNeighbours neighbours;
public Point Position => new Point(X, Y);
public VertexControl control;
MainWindow window;
public Vertex(double x, double y) : this((int)x, (int)y) { }
private int _vN;
public int VertexNumber
{
get { return _vN; }
set {
SetField(ref _vN, value, "VertexNumber");
OnPropertyChanged("VertexText");
}
}
public string VertexText
{
get { return LetterConverter.Convert(VertexNumber); }
}
public Vertex(int x, int y)
{
window = (MainWindow)Application.Current.MainWindow;
control = new VertexControl(this);
neighbours = new VertexNeighbours();
window.canvas.Children.Add(control);
X = x;
Y = y;
}
public Vertex(Polygon polygon, int x, int y) : this(x, y)
{
ownerPolygon = polygon;
VertexNumber = polygon.vertices.Count + 1;
control.SetNumber(VertexNumber);
}
public void SimpleMoveVertex(Point to)
{
X = (int)to.X;
Y = (int)to.Y;
ownerPolygon.CalculateBalancePoint();
}
public void MoveVertex(Point to)
{
SimpleMoveVertex(to);
List<Vertex> moved = new List<Vertex>();
moved.Add(this);
Vertex next = this.neighbours.next;
PolygonEdge edge = this.neighbours.edge;
while(moved.Count < ownerPolygon.vertices.Count)
{
edge.CorrectVertexOnEdge(next);
moved.Add(next);
next = moved.Count % 2 == 0 ? moved[moved.Count - 2].neighbours.prev :
moved[moved.Count - 2].neighbours.next;
edge = moved.Count % 2 == 0 ? next.neighbours.edge : next.neighbours.prev.neighbours.edge;
}
}
public Vertex(Point pt) : this((int)pt.X, (int)pt.Y) { }
public void AddOwner(Polygon polygon)
{
ownerPolygon = polygon;
}
#region Property Changed
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
#endregion
}
}