Computer Graphics Drawing Line. Lines and Polylines Convex: For every pair of points in the polygon,...
-
Upload
lesley-chambers -
Category
Documents
-
view
221 -
download
1
Transcript of Computer Graphics Drawing Line. Lines and Polylines Convex: For every pair of points in the polygon,...
Computer Graphics
Drawing Line
Lines and Polylines
Convex: For every pair of points in the polygon, the line between them is fully contained in the polygon.
Convex vs. Concave Polygons
Concave: Not convex: some two points in the polygon are joined by a line not fully contained in the polygon.
• Polylines: lines drawn between ordered points
• Same first and last point make closed polyline or polygon• If it does not intersect itself, called simple polygon
2D Object
Circles• Consis t of all points equidis tant from one predetermined point (the center)• (radius ) r = c, where c is a cons tant
• On a Cartesian grid with center of circ le at origin equation is r2 = x 2 + y 2
triangle square
rectangle
P0
P1r0
yx
r
Special polygons
2D Object
Circle as polygon• A circle can be approximated by a polygon with many sides (>15)
0 1
1
2
2
3 4 5 6 7 8 9 10
3 4 5
6
0 1
1
2
2
3 4 5 6 7 8 9 10
3
4 5
6
(Aligned) Ellipses
A circle scaled along the x or y axis
Example: height, on y-axis, remains 3, while length, on x-axis, changes from 3 to 6
2D Object
Vertices in motion (“Generative object description”)
• Line is drawn by tracing path of a point as it moves (one dimensional entity)
• Square drawn by tracing vertices of a line as it moves perpendicularly to itself (two dimensional entity)
• Cube drawn by tracing paths of vertices of a square as it moves perpendicularly to itself (three-dimensional entity)
• Circle drawn by swinging a point at a fixed length around a center point
2D to 3D Object
• Triangles and tri-meshes
• Parametric polynomials, like the aforementioned splines used to define surface patches.
Building 3D Primitives
Points
• A point is a list of n numbers referring to a location in n-D
• The individual components of a point are often referred to as coordinates– i.e. (2, 3, 4) is a point in 3-D space
• This point’s x-coordinate is 2, it’s y-coordinate is 3, and it’s z-coordinate is 4
Vectors
• A vector is a list of n numbers referring to a direction (and magnitude) in n-D
Rays
• A ray is just a vector with a starting point– Ray = (Point, Vector)
Points and Vectors
• Can define a vector by 2 points– Point - Point = Vector
• Can define a new point by a point and a vector– Point + Vector = Point
Planes
• How can we define a plane?– 3 non-linear points
• Use linear interpolation
– A perpendicular vector and an incident point• n • (x-x0) = 0
– ax + by + cz + d = 0
• Hessian normal form: Normalize n first– n • x = - p
Raster Graphics
• Image produced as an array (the raster) of picture elements (pixels) in the frame buffer
Rasterization
• In the rasterization step, geometry in device coordinates is converted into fragments in screen coordinates
• After this step, there are no longer any “polygons”
Line Drawing
• A classic part of the computer graphics curriculum
• Input:– Line segment definition
• (x1, y1), (x2, y2)
• Output:– List of pixels
(x1, y1)
(x2, y2)
How Do They Look?
• So now we know how to draw lines
• But they don’t look very good:
16
Towards the Ideal Line• We can only do a discrete approximation
• Illuminate pixels as close to the true path as possible, consider bi-level display only– Pixels are either lit or not lit
Simple Line
Based on slope-intercept algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic required
Line drawing algorithm
• Need algorithm to figure out which intermediate pixels are on line path
• Pixel (x,y) values constrained to integer values
• Rounding may be required. E.g. computed point
(10.48, 20.51) rounded to (10, 21)
Line Drawing Algorithm
0 1 2 3 4 5 6 7 8 9 10 11 12
87654321
Line: (3,2) -> (9,6)
? Which intermediate pixels to turn on?
Line Drawing Algorithm
• Slope-intercept line equation– y = mx + b – Given two end points (x0,y0), (x1, y1), how
to compute m and b?
(x0,y0)
(x1,y1)
dx
dy
01
01
xx
yy
dx
dym
0*0 xmyb
Does it Work?It seems to work okay for lines with a slope of 1 or less,
but doesn’t work well for lines with slope greater than 1 – lines become more discontinuous in appearance and we must add more than 1 pixel per column to make it work.
Solution? - use symmetry.
increment along x-axis if dy<dx else increment along y-axis
Line Drawing Algorithm
• Numerical example of finding slope m:
• (Ax, Ay) = (23, 41), (Bx, By) = (125, 96)
5392.0102
55
23125
4196
AxBx
AyBym
Basic Algorithm• Find equation of line that connects two points P and Q• Starting with leftmost point P, increment xi by 1 to calculate yi = m*xi
+ Bwhere m = slope, B = y intercept
• Draw pixel at (xi, Round(yi)) where
Round (yi) = Floor (0.5 + yi)
Incremental Algorithm:• Each iteration requires a floating-point multiplication
– Modify algorithm to use deltas– (yi+1 – yi) = m*(xi+1 - xi) + B – B– yi+1 = yi + m*(xi+1 – xi)– If x = 1, then yi+1 = yi + m
• At each step, we make incremental calculations based on preceding step to find next y value
Incremental Algorithm
Incremental Algorithm
• Start at t = 0
• At each step, increment t by dt
• Choose appropriate value for dt
• Ensure no pixels are missed:– Implies: and
• Set dt to maximum of dx and dy
)()(
)()(
121
121
yytyty
xxtxtx
dt
dyyy
dt
dxxx
oldnew
oldnew
1
dt
dx1
dt
dy
Example Code// Incremental Line Algorithm
// Assume x0 < x1
void Line(int x0, int y0,
int x1, int y1) {
int x, y;
float dy = y1 – y0;
float dx = x1 – x0;
float m = dy / dx;
y = y0;
for (x = x0; x < x1; x++) {
WritePixel(x, Round(y));
y = y + m;
}
}
Line Drawing Algorithm Pseudocode
compute m;
if m < 1:
{
float y = y0; // initial value
for(int x = x0;x <= x1; x++, y += m)
setPixel(x, round(y));
}
else // m > 1
{
float x = x0; // initial value
for(int y = y0;y <= y1; y++, x += 1/m)
setPixel(round(x), y);
}• Note: setPixel(x, y) writes current color into pixel in column x and row y in
frame buffer
Observation on lines.
while( n-- ) {draw(x,y);move right;if( below line )move up;}
Bresenham Mid-Point algorithm
Testing for the side of a line.
• Need a test to determine which side of a line a pixel lies.
• Write the line in implicit form:
0),( cbyaxyxF
• Easy to prove
F<0 for points above the line,
F>0 for points below.
Testing for the side of a line.
• Need to find coefficients a,b,c.• Recall explicit, slope-intercept form :
• So:
0),( cbyaxyxF
bxdx
dyybmxy so and
0..),( cydxxdyyxF
Decision variable.
PreviousPixel
(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
Evaluate F at point M
Referred to as decision variable
)2
1,1( pp yxFd
M
NE
E
Mid-Point Algorithm
Evaluate d for next pixel, Depends on whether E or NE Is chosen :
If E chosen :
cybxayxFd ppppnew )2
1()2()
2
1,2(
But recall :
cybxa
yxFd
pp
ppold
)2
1()1(
)2
1,1(
So :
dyd
add
old
oldnew
M
E
NE
PreviousPixel
(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
Decision variable.
If NE was chosen :
cybxayxFd ppppnew )2
3()2()
2
3,2(
So :
dxdyd
badd
old
oldnew
M
E
NE
PreviousPixel
(xp,yp)
Choices forCurrent pixel
Choices forNext pixel
Summary of mid-point algorithm
• Choose between 2 pixels at each step based upon the sign of a decision variable.
• Update the decision variable based upon which pixel is chosen.
• Start point is simply first endpoint (x1,y1).
• Need to calculate the initial value for d
Initial value of d.
2
)2
1()1()
2
1,1(
11
1111
bacbyax
cybxayxFdstart
But (x1,y1) is a point on the line, so F(x1,y1) =0
2/dxdydstart
Conventional to multiply by 2 to remove fraction doesn’t effect sign.
2),( 11
bayxF
Start point is (x1,y1)
algorithm
void MidpointLine(int x1,y1,x2,y2)
{
int dx=x2-x1;
int dy=y2-y1;
int d=2*dy-dx;
int increE=2*dy;
int incrNE=2*(dy-dx);
x=x1;
y=y1;
WritePixel(x,y);
while (x < x2) {if (d<= 0) {
d+=incrE;x++
} else {d+=incrNE;x++;y++;
}WritePixel(x,y);
}}
It was (not) the end!
2-step algorithm by Xiaolin Wu:
Treat line drawing as an automaton , or finite state machine, ie. looking at next two pixels of a line, easy to see that only a finite set of possibilities exist.
The 2-step algorithm exploits symmetry by simultaneously drawing from both ends towards the midpoint.
Two-step Algorithm
Possible positions of next two pixels dependent on slope – current pixel in blue:
Slope between 0 and ½
Slope between ½ and 1
Slope between 1 and 2
Slope greater than 2
Bresenham Exercise
• Go through the steps of the Bresenham line drawing algorithm for a line going from (21,12) to (29,16)
Bresenham Exercise
17
16
15
14
13
12
11
10
18
292726252423222120 28 30
k pk (xk+1,yk+1)
0
1
2
3
4
5
6
7
8