Graphics Reference
In-Depth Information
Subdivision amounts to determining, for each vertex, the previous and next ver-
tices and then combining coordinates in a two-thirds-to-one-third fashion to find
the location of the corner-cutting points. (This combination is closely analogous
to the idea of averaging the coordinates of two points to find the coordinates of
the midpoint of the segment between them, which you'll recall from elementary
geometry.)
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
// Subdivide button
public void b1Click(object sender, RoutedEventArgs e)
{
Debug.Print( "Subdivide button clicked! \ n" );
if (isSubdivided)
{
myPolygon.Points = mySubdivPolygon.Points;
mySubdivPolygon.Points = new PointCollection();
}
int n = myPolygon.Points.Count;
if (n > 0)
{
isSubdivided = true;
}
for (int i = 0; i < n; i++)
{
int nexti = (i + 1) % n; // index of next point.
int lasti = (i + (n - 1))%n; // previous point
double x = (1.0f/3.0f) * myPolygon.Points[lasti].X
+(2.0f/3.0f) * myPolygon.Points[i].X;
double y = (1.0f/3.0f) * myPolygon.Points[lasti].Y
+(2.0f/3.0f) * myPolygon.Points[i].Y;
mySubdivPolygon.Points.Add(new Point(x, y));
x = (1.0f/3.0f) * myPolygon.Points[nexti].X
+(2.0f/3.0f) * myPolygon.Points[i].X;
y = (1.0f/3.0f) * myPolygon.Points[nexti].Y
+(2.0f/3.0f) * myPolygon.Points[i].Y;
mySubdivPolygon.Points.Add(new Point(x, y));
}
e.Handled = true; // don't propagate click further
}
Finally, we must handle mouse clicks. Anytime the user presses the mouse
button, we want to add a new vertex to the polygon unless it's already subdivided .
We therefore check the isSubdivided flag, and if it's false , we add the point to
our Polygon .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void MouseButtonDownA(object sender,
RoutedEventArgs e)
{
if (sender != this) return;
System.Windows.Input.MouseButtonEventArgs ee =
(System.Windows.Input.MouseButtonEventArgs)e;
if (!isSubdivided)
{
myPolygon.Points.Add(ee.GetPosition(gp));
}
e.Handled = true;
}
}
}
 
Search WWH ::




Custom Search