Graphics Reference
In-Depth Information
Listing 3.6: C# portion of the implementation of the Dürer algorithm.
public Window1()
{
1
2
3
4
5
6
7
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
InitializeComponent();
InitializeCommands();
// Now add some graphical items in the main Canvas,
whose name is "Paper"
gp = this .FindName( "Paper" ) as GraphPaper;
// Build a table of vertices:
int nPoints = 8;
int nEdges = 12;
double [,] vtable = new double [nPoints, 3]
{
{-0.5, -0.5, 2.5}, {-0.5, 0.5, 2.5},
{0.5, 0.5, 2.5}, ...};
// Build a table of edges
int [,] etable = new int [nEdges, 2]
{
{0, 1}, {1, 2}, ...};
double xmin = -0.5; double xmax = 0.5;
double ymin = -0.5; double ymax = 0.5;
Point [] pictureVertices = new Point[nPoints];
double scale = 100;
for ( int i = 0; i < nPoints; i++)
{
double x = vtable[i, 0];
double y = vtable[i, 1];
double z = vtable[i, 2];
double xprime = x / z;
double yprime = y / z;
pictureVertices[i].X = scale * (1 - (xprime - xmin) /
(xmax - xmin));
pictureVertices[i].Y = scale *
(yprime - ymin) /
(ymax - ymin);
gp.Children.Add( new Dot(pictureVertices[i].X,
pictureVertices[i].Y));
}
for ( int i = 0; i < nEdges; i++)
{
int n1 = etable[i, 0];
int n2 = etable[i, 1];
gp.Children.Add( new Segment(pictureVertices[n1],
pictureVertices[n2]));
}
...
}
We begin with a few comments. First, the code is not at all efficient (e.g.,
there was no need to declare the variables x , y , and z ), but it follows the algo-
rithm very closely. For making graphics programs that are debuggable, this is an
excellent place to start in general: Don't be clever until your code works and you
 
Search WWH ::




Custom Search