Graphics Reference
In-Depth Information
for{int i = 0; i < number of edges in o ; i++){
int i0 = edges[i][0];
int i1 = edges[i][1];
Draw a line segment from pictureVertices[i0]
13
14
15
16
17
18
to
pictureVertices[i1];
}
These zero-to-one coordinates are often called normalized device coordi-
nates: They can be thought of as describing a fraction of the left-to-right or
top-to-bottom range of a display device; if the display device isn't square, then
a coordinate of 1. 0 represents the smaller of the two dimensions. A typical desk-
top monitor might have vertical coordinates that range from 0 to 1, but horizontal
coordinates that range from 0 to 1. 33.
The normalization process—converting from the range [ x min , x max ] to the
range [ 0, 1 ] —occurs often; it's worth memorizing the form of Equation 3.3.
Inline Exercise 3.5: Verify, in Listing 3.5, that a vertex that happens to be
located at the lower-left corner of the view square, that is, ( x min , y min ,1 ) ,
does indeed transform to the lower-left corner of the final picture, that is, the
corresponding pictureVertex is ( 0, 0 ) ; similarly, verify that ( x max , y max ,1 )
transforms to ( 1, 1 ) .
3.4 The Program
We'll use a simple WPF program, based on a standard test bed described exten-
sively in the next chapter, to implement this algorithm. The resultant program is
downloadable from the topic's website for you to run and to experiment with.
For this application, the critical elements of the test bed are the ability to create
and display dots (small disks that indicate points) and segments (line segments
between dots) on a Canvas that we call the GraphPaper . Positions on our graph
paper are measured in units of millimeters, which are more readily comprehended
than WPF default units, which are about
1
96 of an inch. To make the drawing a
reasonable size, we'll multiply our algorithmic results (coordinates ranging from
zero to one) by 100. The important parts of the program are shown in Listing 3.6,
with elisions indicated by [...] .
Before you examine the code, though, we'll warn you that this use of WPF is
very different from what you saw in Chapter 2. In that chapter, we demonstrated
the declarative aspects of WPF that are easy to expose through XAML. The test
bed uses these declarative aspects to build a window, some menus, and controls,
and to lay out the GraphPaper on which we'll do our drawing. But the production
of actual pictures within the GraphPaper is all done in C#. That's because for most
of the programs we'll want to write, expressing things in XAML is either cum-
bersome or impossible (not every feature of WPF is exposed through XAML).
In general, it makes sense to use the declarative specification whenever possible,
especially for layout and for data dependencies, and to use C# whenever substan-
tial algebraic manipulations may be needed.
 
 
 
Search WWH ::




Custom Search