Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Assignment 1
Framework for World View Read XML Render Points in World Window Save to an image
Details:
April 12th , 2pm Makefile required Submit via email Attach zip/tar.gz
Due
Assignment 1: XML
<?xml version="1.0"?> <world> <window XLowerBound=700" YLowerBound=100" XUpperBound=800" YUpperBound=300" /> <shape type="point" color="000000"> <params x="25" y="25" /></shape> </world>
XML
version="1.0"?> <world> Tags are nodes <window Even white space is a node! XLowerBound=700" YLowerBound=100" Nodes have children XUpperBound=800" YUpperBound=300" /> <shape type="dot"> <params x="50" y="50" /> </shape> </world>
<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>
Getsallofthenodeswithaspecificname!
<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>
Nodescanbeanything,anelementisanodecorrespondingtoaTAG Elementsmayhaveattributes(flagsinsidethetag)
<?xml version="1.0"?> <company> <employee type="actor"> <firstname>Tom</firstname> <lastname>Cruise</lastname> </employee> <employee type="terminated"> <firstname>George</firstname>
Nodesalsocanhavechildnodes,hereweaccessthefirstnamenode.
Assignment Objectives
Read an xml file containing the world-view corrdinates, a set of colored points, and a list of transformation matricies. Create a global transformation matrix from the list of transformation matricies. Apply this transformation matrix to the points. Clip the points against the world window. Draw the points to the world window buffer. Save the results to an image
Assignment 1
Skeleton Java Code on Assignment Page use it! Start EARLY to avoid problems! Dont get caught up in XML, contact me if you have issues!
Line Drawing
Scan-Conversion Algorithms
Scan-Conversion:
Computing pixel coordinates for ideal line on 2D raster grid Pixels best visualized as circles/dots
Why?
Monitor hardware
12
Drawing a Line
y = mx + B m = y / x Start at leftmost x and increment by 1
x = 1
more multiplication!
y Compute corresponding yk y (x) = m (1/m) xk+ = xk + x 1 yk+ = yk + y 1 Round (x,y) for pixel location
yk+1
xk xk+1
15
Generalizing DDA
point should be right of starting point point should be above starting point
x = 0
16
Bresenhams Algorithm
1965
Consider
17
Bresenhams Algorithm
Given: implicit line equation: Let: where r and q are points on the line and dx ,dy are positive
Then: Observe that all of these are integers and: for points above the line for points below the line
18
Bresenhams Algorithm
Suppose
we just finished
(assume
Which
E
pixel next?
or NE
19
Pics/Math courtesy of Dave Mount @ UMD-CP
Bresenhams Algorithm
Assume: Q = exact y value at y midway between E and NE: Observe: If Q < M , then pick E Else pick NE If Q=M , it doesnt matter
20
Pics/Math courtesy of Dave Mount @ UMD-CP
Bresenhams Algorithm
Create modified implicit function (2x) Create a decision variable D to select, where D is the value of f at the midpoint:
M
21
Pics/Math courtesy of Dave Mount @ UMD-CP
Bresenhams Algorithm
If
NE
If
E
22
23
Pics/Math courtesy of Dave Mount @ UMD-CP
24
Pics/Math courtesy of Dave Mount @ UMD-CP
25
Pics/Math courtesy of Dave Mount @ UMD-CP
The Algorithm
void bresenham( point q, point r ){ int dx,dy,D,x,y, incrE, incrNE; if( r.x<q.x ) swap(q,r); y=q.y;dx=r.x-q.x; dy=r.y-q.y; D=2*dy-dx; incrE=dy*2; incrNE=(dy-dx)*2; for( x=q.x;x<r.x;x++) { writePixel(x,y); if( D<=0 ) D+=incrE; else { D+=incrNE; y++; } }
Assumptions:
0 slope 1
26
Pre-computed:
Pics/Math courtesy of Dave Mount @ UMD-CP
Problem!
Only works for In class exercise: extend so it works for
void bresenham( point q, point r ){
int dx,dy,D,x,y, incrE, incrNE; if( r.x<q.x ) swap(q,r); y=q.y;dx=r.x-q.x; dy=r.y-q.y; D=2*dy-dx; incrE=dy*2; incrNE=(dy-dx)*2; for( x=q.x;x<r.x;x++) { writePixel(x,y); if( D<=0 ) D+=incrE; else { D+=incrNE; y++; } }
Problem!
Generalize Algorithm
If slope > 1, always increment y, conditionally increment x If -1 < slope < 0, always increment x, conditionally decrement y If slope > 1, always increment y, conditionally increment x (Rework D increments) If slope < -1, always decrement y, conditionally increment x (Rework D increments)
29
lines look darker, more pixels per unit length Endpoint order Line from P1 to P2 should match P2 to P1 Always choose E when hitting M,
30
to handle the line when it hits the clip window? Vertical intersections
Could
intersections
changes in the boundary conditions Cant just intersect the line w/ the box
31
Line clipping
Scissoring Clipping Performed during scan conversion of the line (circle, polygon)
Compute the next point (x,y) If xmin x xmax and ymin y ymax Then output the point. Else do nothing
Issues
with scissoring:
Too
to clip lines to window, than draw lines that are outside of window
33
Pics/MathcourtesyofDaveMount@UMDCP
to tell if whole line falls w/in window harder to tell what part falls inside
34
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Basic Idea: First, do easy test
completely
If
no, we need a more complex test Note: we will also need to figure out how line
35
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Assign each end point a location code Perform bitwise logical operations on a lines location codes Accept or reject based on result Frequently provides no information
Then
36
Cohen-Sutherland
The
37
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Line is completely visible iff both code values of endpoints are 0, i.e.
38
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Otherwise, we clip the lines: We know that there is a bit flip, w.o.l.g. assume its (x0 ,x1 )
Which
suppose
its bit 4 Then x0 < WL and we know that x1 WL We need to find the point: (xc ,yc )
39
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Clearly: Using Solving
similar triangles
for yc gives
40
Pics/MathcourtesyofDaveMount@UMDCP
Cohen-Sutherland
Replace
(x0 ,y0 ) with (xc ,yc ) Re-compute codes Continue until all bit flips (clip lines) are processed, i.e. all points are inside the clip window
41
Pics/MathcourtesyofDaveMount@UMDCP
Cohen Sutherland
0101 1010
42
Cohen Sutherland
0101 0010
43
Cohen Sutherland
0001 0010
44
Cohen Sutherland
0001 0000
45
Cohen Sutherland
0000 0000
46
by Cyrus and Beck in 1978 Used to clip 2D/3D lines against convex polygon/polyhedron Liang and Barsky (1984) algorithm efficient in clipping upright 2D/3D clipping regions Cyrus-Beck may be reduced to more efficient Liang-Barsky case Based on parametric form of a line
Line:
P(t) = P0 +t(P1-P0)
47
P0
Line: P(t) = P0 +t(P1-P0)
P1
t value defines a point on the line going through P0 and P1 0 <= t <= 1 defines line segment between P0 and P1 P(0) = P0 P(1) = P1
48
49
N i [P(t)-PEi ] = 0 [P0 + t(P -P0 )-PEi ] = 0 1 [P0 -PEi ] + N i t[P -P0 ] = 0 1 D = (P -P0 ) 1
N i N i
Let
N i [P0 PEi ] t= Ni D
50
1994Foley/VanDam/Finer/Huges/PhillipsICG
Calculating Ni
E = (V1 - V0) / |V1 - V0| Be sure to process edges in CCW order Nx = E y Ny = -Ex
51
Calculate intersection points between line and every window line Classify points as potentially entering (PE) or leaving (PL) PE if crosses edge into inside half plane => angle P0 P1 and Ni greater 90 => Ni D < 0 PL otherwise. Find Te = max(te) Find Tl = min(tl) Discard if Te > Tl If Te < 0 Te = 0 If Tl > 1 Tl = 1 Use Te, Tl to compute intersection coordinates (xe,ye), (xl,yl)
52
1994Foley/VanDam/Finer/Huges/PhillipsICG