Title: Line Drawing
1Line Drawing
2In the time before time
- Many algorithms in computer graphics date back to
early plotting devices. - Cartography, CAD, x-ray crystallography were
early applications.
- The pen in a plotter is moved by stepper motors,
so they were capable of moving to only discretely
addressable positions.
3How do we draw a point?
- A point on the screen with position (x, y) plot
a pixel with corresponding position - Sample code
- SetPixel ( x, y )
4Drawing an ideal line
- Recall that a vector-scan display was capable of
a near perfect line - Move to the start point
- Turn on the electron beam
- Move to the end point
- Turn off the electron beam
- Raster-scan displays are not as easy to draw
lines on - Electron beam motion is predetermined
- Have to draw (rasterize, scan-convert) into the
framebuffer to control image intensity at
discrete points
5Less than ideal lines
- Raster-scan devices address discrete pixels
- The endpoints and intermediate points must be set
individually - The points are calculated from the
slope-intercept equation - Ideal Line
- Rasterized line
6Less than ideal lines
- As close to ideal line as possible
- Even thickness, brightness
- No discontinuities (gaps in the line)
- Fast line drawing is a fundamental operation on
which many other routines are built
7Equation of a line
- y m x c . Eq. 1
- dx x1 - x0
- dy y1 - y0
- m dy / dx
- c y0 - m x0
8Digital Differential Analyzer (DDA)
- Based on Eq.1 and positions of 2 endpoints (x1,
y1), (x2, y2) - Therefore,
- and
9DDA Example
Sample at unit x
Corresponding y pos.
10DDA Example
Sample at unit x
Corresponding y pos.
11DDA Example
Sample at unit x
Corresponding y pos.
12DDA Example
Sample at unit x
Corresponding y pos.
13DDA Example
Sample at unit x
Corresponding y pos.
14DDA Example
Sample at unit x
Corresponding y pos.
15DDA Example
Sample at unit x
Corresponding y pos.
Consider endpoints P1(0,0), P2(7,4)
16DDA Exercise
1. Consider endpoints P1(0,0), P2(6, 4)
Calculate the points that made up the line P1P2
2. Now, consider endpoints P3(0,0), P4(4, 6)
Calculate the points that made up the line P3P4
17What went wrong?
- Our algorithm steps along the x-axis setting
pixels, so it leaves obvious gaps
(discontinuities) when the magnitude of the slope
is greater than 1 - Easy fix?
- Check the magnitude of the slope, if its greater
than 1 step along the y-axis (sampling at y
direction)
Sample code can be found at section 3-2 in
Hearn Bakers book see the limitation and
make enhancement
18Bresenhams Line Algorithm
19Bresenhams Line Algorithm
- Based on integer calculation.
16
15
14
Actual path
13
12
11
10
9
8
8 9 10 11 12 13 14 15 16
Next pixel ? (11, 10) or (11, 12)?? Bresenhams
algorithm selects the nearest point (to the
actual path). At each sampling, increment is
either 0 or 1.
20X Y increments
- Consider the first condition
- m lt 1, m has a positive value
- Bresenhams increments x by 1 and y by 0 or 1
- This makes sense for our lines, we want them to
be continuous - If the magnitude of the slope were more than 1,
wed swap x y
21Pixel selection
- If current pixel is at (xk, yk)
Next pixel to be plotted (xk1, yk1)(xk1, yk),
or (xk1, yk1)? Determine by the value of
decision parameter, pk
Please refer to Hearn Bakers, section 3-2.
22Pixel selection
- If current pixel is at (xk, yk)
yk1
d2
y
d1
yk
xk1
Next pixel to be plotted (xk1, yk1)(xk1, yk),
or (xk1, yk1)? Determine by the value of
decision parameter, pk
Please refer to Hearn Bakers, section 3-2.
23Decision parameter
pk negative d1 ? d2 pixel at scanline yk is nearer to the actual path plot (xk1, yk)
pk positive d1 ? d2 pixel at scanline yk1 is nearer to the actual path plot (xk1, yk1)
24Algorithm
- Bresenhams line algorithm for m lt 1
- Input 2 endpoints, store left endpoints as (x0,
y0). - Load into frame buffer (plot the first point).
- Calculate constants ?x, ?y, 2?y, 2?y 2?x and
initial value of decision parameter - At each xk along the line, start at k0, test
- if pk lt 0, plot (xk1, yk) and
-
- else plot (xk1, yk1) and
- Repeat step 4 ?x times.
25Exercise
- Calculate pixel positions that made up the line
connecting endpoints (12, 10) and (17, 14).
1. (x0, y0) ?
2. ?x ?, ?y ?, 2?y ?, 2?y 2?x ?
3. p0 2?y ?x ?
k pk (xk1, yk1)
26Exercise
- Calculate pixel positions that made up the line
connecting endpoints (12, 10) and (17, 14).
1. (x0, y0) (12, 10)
2. ?x 5, ?y 4, 2?y 8, 2?y 2?x -2
3. p0 2?y ?x 3
k pk (xk1, yk1)
0 3
1
2
27Exercise
- Calculate pixel positions that made up the line
connecting endpoints (12, 10) and (17, 14).
1. (x0, y0) (12, 10)
2. ?x 5, ?y 4, 2?y 8, 2?y 2?x -2
3. p0 2?y ?x 3
k pk (xk1, yk1)
0 3 (13, 11)
1 1 (14, 12)
2 -1 (15, 12)
3 7 (16, 13)
4 5 (17, 14)
28Circle Drawing
29A very fine place to start
- Lets begin by looking at the equation of a
circle - Solving for y
- And this is one of the sample codes
- void circleSimple(int xCenter, int yCenter, int
radius, Color c) - int x, y, r2
- r2 radius radius
- for (x -radius x lt radius x)
- y (int)(sqrt(r2 - xx) 0.5)
- setPixel(xCenter x, yCenter y, c)
- setPixel(xCenter x, yCenter y, c)
-
30The result
- We came across this problem with lines before
- Sometimes the slope of the line tangent to a
point the circle is greater than 1. Stepping in x
wont work there. - So we could look for this case and step in y
31Is it a good solution?
- maybe not!
- But on both cases, we were taking advantage of
the circles symmetry when we draw both positive
and negative values for y (or x). - This is called 2-way symmetry.
- Are there more symmetries to exploit?
32Symmetry abounds
- Sure, lets try 4-way symmetry.
- With just a quick hack, we get
- void circle4Way(int xCenter, int yCenter, int
radius, Color c) - int x, y, r2
- setPixel(xCenter, yCenter radius, c)
- setPixel(xCenter, yCenter radius, c)
-
- r2 radius radius
- for (x 1 x lt radius x)
- y (int)(sqrt(r2 - xx) 0.5)
- setPixel(xCenter x, yCenter y, c)
- setPixel(xCenter x, yCenter y, c)
- setPixel(xCenter - x, yCenter y, c)
- setPixel(xCenter - x, yCenter y, c)
-
-
33The same result but in half the time
- That didnt fix a thing.
- Oh sure, its faster just half as many
evaluations but its no more correct than our
first try. - Why didnt this work?
34Symmetry to the rescue this time for sure
- What about 8-way symmetry?
- Now were looking for symmetries across the
diagonal lines, i.e. where xy. - Now when we step along x, we can permute the
coordinates (swap x and y) and simultaneously be
stepping along y in another part of the circle. - Thats back to how we fixed lines!
- Bonus, its 4 times faster than the original!
358-way symmetry
- void circle8Way(int xCenter, int yCenter, int
radius, Color c) - int x, y, r2
- setPixel(xCenter, yCenter radius, c)
- setPixel(xCenter, yCenter radius, c)
- setPixel(xCenter radius, yCenter, c)
- setPixel(xCenter - radius, yCenter, c)
- r2 radius radius
- x 1
- y (int)(sqrt(r2 1) 0.5)
- while (x lt y)
- setPixel(xCenter x, yCenter y, c)
- setPixel(xCenter x, yCenter y, c)
- setPixel(xCenter - x, yCenter y, c)
- setPixel(xCenter - x, yCenter y, c)
- setPixel(xCenter y, yCenter x, c)
- setPixel(xCenter y, yCenter x, c)
- setPixel(xCenter - y, yCenter x, c)
36Are we there yet?
- We now have a working circle rasterizer. Were
happy. Were content. - We have a square root in our loop.
- Can we make that go away?
- Lets look at the original equation again
- Can we do anything with this? Lets start by
centering it at the origin.
37Equation of a circle revisited
- We now have
- x2 y2 r2
- What if we had a function of x and y?
- f(x,y) x2 y2 - r2
- This is called a discriminator function and has a
few nice properties - f(x,y) lt 0 for a point inside the circle
- f(x,y) gt 0 for a point outside the circle
- f(x,y) 0 for a point on the circle
38Midpoint Circle Algorithm
- Consider current point is at (xk, yk)
- Next point (xk1, yk), or (xk1, yk-1)?
- Take the midpoint (xk1, yk-0.5)
- Use the discriminator function to decide
39Using the circle discriminator
- Based on the value return
- lt 0 (x, y) is inside the circle
- f(x, y) 0 (x, y) is on the circle path
- gt0 (x, y) is outside the circle
- By using the midpoint between 2 pixel candidates,
we can introduce a decision parameter, pk , to
decide which to plot next - -ve midpoint is inside the circle plot
(xk1, yk) - pk
- ve midpoint is outside the circle plot
- (xk1, yk-1)
40If the current point is inside the circle
- pk lt 0!
- Lets drop the subscript for a while
- We want to know f(x1, y) so we can update p
- f(x1, y) (x 1)2 y2 r2
- f(x1, y) (x2 2x 1) y2 r2
- f(x1, y) f(x, y) 2x 1
- So we increment
- p 2x 1
Pk1
Pk
41If the current point is outside the circle
- pk lt 0!
- Lets drop the subscript for a while
- We want to know f(x1, y-1) so we can update p
- f(x1, y1) (x 1)2 (y 1)2 r2
- f(x1, y1) (x2 2x 1) (y2 2y 2) r2
- f(x1, y1) f(x, y) 2x 2y 2
- And we increment
- p 2x 2y 2
Pk1
Pk
42Where to begin?
- We can determine where to go next, how do we
start? - We have a variety of choices for our first point
on the circle, but weve designed the increments
to work from (0, r). - Calculate initial value of p0 by evaluating
- p0 f(1, r0.5) 12 (r 0.5)2 r2
- p0 f(1, r0.5) 1 (r2 r 0.25) r2
- p0 1.25 r
We want to use integer calculation you can
round p0
43Midpoint circle algorithm
- Input radius, r, and circle center (xc, yc).
First point to be plotted will be(0, r) this is
based on the circle centered at the origin! - Calculate initial value of decision paramater
- At each xk, starting at k 0, test the value of
pk - If pk lt 0, next point will be (xk1, yk) and
- Otherwise, next point will be (xk1, yk-1) and
-
- Determine symmetry points in the other 7 octants.
- Get the actual point for circle centered at (xc,
yc) that is (x xc, y yc). - Repeat step 3 to 5 until x ? y.
44Midpoint circle algorithm
- void circleMidpoint(int xCenter, int yCenter, int
radius, Color c) - int x 0
- int y radius
- int p (5 - radius4)/4
- circlePoints(xCenter, yCenter, x, y, c)
- while (x lt y)
- x
- if (p lt 0)
- p 2x1
- else
- p 2(x-y1)
- y--
-
- circlePoints(xCenter, yCenter, x, y, c)
-
-
45circlePoints()
- void circlePoints(int cx, int cy, int x, int y,
Color c) - if (x 0)
- setPixel(cx, cy y, c)
- setPixel(cx, cy y, c)
- setPixel(cx y, cy, c)
- setPixel(cx - y, cy, c)
- else if (x y)
- setPixel(cx x, cy y, c)
- setPixel(cx - x, cy y, c)
- setPixel(cx x, cy y, c)
- setPixel(cx - x, cy y, c)
- else if (x lt y)
- setPixel(cx x, cy y, c)
- setPixel(cx - x, cy y, c)
- setPixel(cx x, cy y, c)
- setPixel(cx - x, cy y, c)
- setPixel(cx y, cy x, c)
- setPixel(cx - y, cy x, c)
- setPixel(cx y, cy x, c)
46Ellipse Drawing
47Equation
- General equation of an ellipse
- d1 d2 constant
- Or,
- However, we will only consider standard
ellipse
48Symmetry
- An ellipse only has a 2-way symmetry.
49Equation of an ellipse revisited
- Consider an ellipse centered at the origin
- What is the discriminator function?
- and its properties
- fe(x,y) lt 0 for a point inside the ellipse
- fe(x,y) gt 0 for a point outside the ellipse
- fe(x,y) 0 for a point on the ellipse
50Midpoint Ellipse Algorithm
- Ellipse is different from circle.
- Similar approach with circle, different is
sampling direction. - Region 1
- Sampling is at x direction
- Choose between (xk1, yk), or (xk1, yk-1)
- Midpoint (xk1, yk-0.5)
- Region 2
- Sampling is at y direction
- Choose between (xk, yk-1), or (xk1, yk-1)
- Midpoint (xk0.5, yk-1)
51Decision parameters
- Region 1
-
- p1k ve
- midpoint is inside
- choose pixel (xk1, yk)
-
- p1k ve
- midpoint is outside
- choose pixel (xk1, yk-1)
- Region 2
-
- p2k ve
- midpoint is inside
- choose pixel (xk1, yk-1)
-
- p2k ve
- midpoint is outside
- choose pixel (xk, yk-1)
52Midpoint Ellipse Algorithm
- Input rx, ry and ellipse center (xc, yc). First
point on the similar ellipse centered at the
origin is (0, ry). - Initial value for decision parameter at region 1
- At each xk in region 1, starting from k 0, test
p1k - If p1k lt 0, next point (xk1, yk) and
- else, next point (xk1, yk-1) and
-
- Determine symmetry points in the other 3 octants.
- Get the actual point for ellipse centered at (xc,
yc) that is (x xc, y yc). - Repeat step 3 - 6 until 2ry2x ? 2rx2yi.
-
53Midpoint Ellipse Algorithm
- Initial value for decision parameter in region 2
- At each yk in region 2, starting from k 0, test
p2k - If p2k gt 0, next point is (xk, yk-1) and
- else, next point is (xk1, yk-1) and
-
- Determine symmetry points in the other 3 octants.
- Get the actual point for ellipse centered at (xc,
yc) that is (x xc, y yc). - Repeat step 8 - 10 until y lt 0.
-