Co3150- Unit II
Co3150- Unit II
M. Sarosh Umar
Department of Computer Engineering,
Aligarh Muslim University, Aligarh
Email: [email protected]
1
Computer Graphics
• Point Plotting Techniques
2
Scan Converting Lines
Line Drawing
• Draw a line on a raster screen between two points
• What’s wrong with statement of problem?
- doesn’t say anything about which points are allowed as endpoints
- doesn’t give a clear meaning of “draw”
- doesn’t say what constitutes a “line” in raster world
- doesn’t say how to measure success of proposed algorithms
Problem Statement
• Given two points P and Q in XY plane, both with integer coordinates,
determine which pixels on raster screen should be on in order to make
picture of a unit-width line segment starting at P and ending at Q
Finding next pixel:
Special case:
• Horizontal Line:
Draw pixel P and increment x coordinate value by 1 to get next pixel.
• Vertical Line:
Draw pixel P and increment y coordinate value by 1 to get next pixel.
• Diagonal Line:
Draw pixel P and increment both x and y coordinate by 1 to get next
pixel.
(x2, y2)
• By similar triangles we can see that true distances to line (in blue) are
directly proportional to vertical distances to line (in black) for each point
• Therefore, point with smaller vertical distance to line is closest to line
Strategy 1 - Incremental
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 = mxi + B
where m = slope, B = y intercept
• Intensify pixel at (xi, Round(yi)) where
Round (yi) = Floor (0.5 + yi)
Incremental Algorithm:
• Each iteration requires a floating-point multiplication
- therefore, modify algorithm.
• yi+1 = mxi+1 + B = m(xi + Dx) + B = yi + m Dx
• If Dx = 1, then yi+1 = yi + m
• At each step, we make incremental calculations based on preceding
step to find next y value
Strategy 1 - Incremental
( xi , yi ) ( xi + 1, Round ( yi + m))
( xi , Round ( yi )) ( xi + 1, yi + m)
Objectives
• Use for 1 ³ m ³ 0
• For m > 1, swap role of x and y
- For each y, plot closest x
DDA Algo (General)
#include “device. h“
#define ROUND (a) ((int)(a+0.5))
void lineDDA (int xa, int ya, int xb, int yb)
{
int dx = xb - xa, dy = yb - ya, steps, k;
float xIncrement, yIncrement, x = xa, y = ya;
if (abs (dx) > abs (dy)) steps = abs (dx) ;
else steps = abs (dy);
xIncrement = dx / steps;
yIncrement = dy / steps;
y = mx + b
d2
y = m(x+1) + b
y d1
x x+1
19
Bresenham’s Algo (|m|<1)
d1=y-yk= m(xk+1)+b-yk
p0 = 2∆y - ∆x = 16-10 =6
Wide Lines & Line styles
struct linesettingstype
{
int linestyle;
unsigned upattern;
int thickness;
};
Wide Lines & Line styles
Wide Lines & Line styles
Pattern:
• 16-bit pattern that applies only if linestyle is
USERBIT_LINE (4)
• whenever a bit in the upattern word is 1, the
corresponding pixel in the line is drawn in the
current drawing color
• a solid line corresponds to a pattern of 0xFFFF
(all pixels drawn), and a dashed line can
correspond to a upattern of 0x3333 or 0x0F0F
Wide Lines & Line styles
Pattern:
• How to extract a bit from the pattern to decide
that the pixel is to be plotted?
P=10011011
M=00000001
--------------------
R= P&M
Wide Lines & Line styles
32
Circle generating
algorithms
• Direct
• Polar coordinate based
• Bresenham’s
Direct circle algorithm
• Cartesian coordinates
• Circle equation:
( x - xc )2 + ( y - yc )2 = r2
• Step along x axis from xc - r to xc + r and
calculate
y = yc ± Ö r2 - ( x - xc )2
• Considerable computations!!
• Spacing is not uniform – > interchange x & y
whenever |slope| > 1
Polar coordinates
36
Optimisation and speed-up
n 8 segments of octants for a circle:
• Symmetry of a (-x,y) (x,y)
circle can be
used
(-y,x) (y,x)
• Calculations of
(-y,-x) (y,-x)
point
coordinates only
for a first one- (-x,-y) (x,-y)
eighth of a circle
37
Midpoint Circle Drawing
Algorithm
n 8 segments of octants for a circle:
38
Midpoint Circle Drawing
Algorithm
n Circle function: fcircle (x,y) = x2 + y2 –r2
{
> 0, (x,y) outside the circle
39
Midpoint Circle Drawing
Algorithm
yk yk
midpoint midpoint
yk-1 yk-1
pk = fcircle(xk+1, yk- ½)
pk < 0 pk >= 0
yk+1 = yk yk+1 = yk - 1
Next pixel = (xk+1, yk) Next pixel = (xk+1, yk-1)
40
Midpoint Circle Drawing
Algorithm
We know xk+1 = xk+1,
pk = fcircle(xk+1, yk- ½)
pk = (xk +1)2 + (yk - ½)2 - r2 -------- (1)
pk+1 = fcircle(xk+1+1, yk+1- ½)
pk+1 = (xk +2)2 + (yk+1 - ½)2 - r2 -------- (2)
(2) – (1)
pk+1 = pk + 2(xk+1) + (y2k+1 – y2k) - (yk+1 – yk) + 1
42
Midpoint Circle Drawing
Algorithm
1. Input radius r and circle centre (xc, yc), and obtain the first point
on the circumference of a circle centred on the origin as (0,r)
2. Calculate the initial value of the decision parameter as
p0 = 5/4 – r
x=x+xc and
y=y+yc
44
Midpoint Circle Drawing
Algorithm
Example:
Given a circle radius = 10, determine the circle octant in
the first quadrant from x=0 to x=y.
Solution:
p0 = 5 – r
4
= 5 – 10
4
= -8.75
≈ –9
45
Midpoint Circle Drawing
Algorithm
Initial (x0, y0) = (1,10)
Decision parameters are: 2x0 = 2, 2y0 = 20
k pk x y 2xk+1 2yk+1
0 -9 1 10 2 20
1 -9+2+1=-6 2 10 4 20
2 -6+4+1=-1 3 10 6 20
3 -1+6+1=6 4 9 8 18
4 6+8+1-18=-3 5 9 10 18
5 -3+10+1=8 6 8 12 16
6 8+12+1-16=5 7 7 14 14
46
Midpoint Circle Drawing
Algorithm
void circleMidpoint (int xCenter, int yCenter, int radius)
{
int x = 0;
Int y = radius;
int f = 1 – radius;
circlePlotPoints(xCenter, yCenter, x, y);
while (x < y) {
x++;
if (p < 0)
p += 2*x+1;
else {
y--;
p += 2*(x-y)+1; }
}
circlePlotPoints(xCenter, yCenter, x, y);
47
}
Midpoint Circle Drawing
Algorithm
void circlePlotPoints (int xCenter, int yCenter, int x,
int y)
{
setPixel (xCenter + x, yCenter + y);
setPixel (xCenter – x, yCenter + y);
setPixel (xCenter + x, yCenter – y);
setPixel (xCenter – x, yCenter – y);
setPixel (xCenter + y, yCenter + x);
setPixel (xCenter – y, yCenter + x);
setPixel (xCenter + y, yCenter – x);
setPixel (xCenter – y, yCenter – x);
}
48
Curves
Curves for Airplane wings, car hoods and human faces are
not simple circles or ellipses!
Curves
Curves
Parametric representation:
Represent a line
P(t) = p0 + (p1-p0)t
( x, y ) = (1 - t ) p0 + tp1
x = x0 (1 - t ) + x1t
y = y0 (1 - t ) + y1t
z = z0 (1 - t ) + z1t
Curves
Circle:
x = xc + r cosj
y = yc + r sinj
P (u ) = (cos(u ), sin(u ))
Surfaces with two
parameters u,v
P (u ) = ( x(u ), y (u ),z (u ))
P (u , v) = ( x(u , v), y (u , v),z (u , v))
Curves
n
x (u ) = å x B (u )
i i
i =1
n
y (u ) = å y B (u ) i i
i =1
n
z (u ) = å z B (u )
i i
i =1
n-2 n 1 0
Curves
At u=-1 it is
(-1)(-2)(-3)…….[1-n] ….(2)
• Similarly
u(u - 1)(u - 2)
B1(u ) =
(-1)(-2)(-3)
Curves
(u + 1)(u - 1)(u - 2)
B 2(u ) =
(1)(-1)(-2)
(u + 1)u(u - 2)
B3(u ) =
(2)(1)(-1)
(u + 1)u(u - 1)
B 4(u ) =
(3)(2)(1)
Curves
C0 continuity C1 continuity 70
Important Properties for
Designing Curves
Bezier Method
n
P (u ) = å p i Bi ,n (u ), 0 £ u £1
i =0
n -i n!
Bi ,n (u ) = C (n, i )u (1 - u ) ,
i
C (n, i) =
i!(n - i )!
n
x(u ) = å x B (u )
i i, n
i =0
n
y (u ) = å y B (u ) i i, n
i =0
n
z (u ) = å z B (u )
i i, n
i =0
Bezier Method – Blending
functions for n=3
1.2
0.8 B0
B1
0.6
B2
0.4 B3
0.2
0
Bezier Method – Blending
functions for n=3
Bezier Method – Blending
functions for n=3
The Blending function curves represent the
influence that each control point exerts on
the curve for various values of u.
B0,3(u) is most influential when u=0
p1 and p2 are most influential for u=1/3 and 2/3
respectively
Important Properties for
Bezier Curves
1. Control Points: The curve passes
through p0 and pn
The curve is tangent at the end points
P0 to (P1-P0) and at Pn to (Pn-Pn-1)
Important Properties for
Bezier Curves
Control Points:
Important Properties for
Bezier Curves
2. Multiple Values: The parametric form
allows multiple values. If the 1st and last
CPs coincide, the curve is closed.
3. Axis Independence: Bezier curves is
independent of coordinate system used
to measure the location of CPs
Important Properties for
Bezier Curves
4. Global or Local Control: These curves
do not provide localized control. The
location of each CP influences the
curve location almost everywhere.
Important Properties for
Bezier Curves
5. Variation Diminishing Property: Bezier
curves are variation diminishing. The
curve always lies within the convex hull
of the CPs. Bezier curves never oscillate
wildly away from its defining CPs.
Important Properties for
Bezier Curves
• If we take all of the control points for a Bezier curve and
construct a convex polygon around them, we have the
convex hull of the curve
• An important property of Bezier curves is that every point on
the curve itself will be somewhere within the convex hull of
the control points
p1 p3
p0
p2
Important Properties for
Bezier Curves
6. Versatility: CPs govern the versatility of
Bezier curves.
C0 continuity C1 continuity 84
Bezier Continuity
Bezier Continuity
P0,1 P0,2
P0,0
P1,3
J
P1,2
P1,1
PowerPoint curves are not Bezier curves, they are interpolating piecewise quadratic curves! This
diagram is an approximation.
Bézier Surfaces
• Bezier surfaces are generated by Cartesian product of two
curves. Two similar Bézier blending functions are used, one
for each parameter. CPs: (n+1)x(m+1)
n m
P(u, v) = åå pi , j Bi ,n (u)B j ,m (v) 0 £ u, v £ 1
i =0 j =0
Bézier Surfaces
• Two sets of curves are drawn: One set holds the parameter
u constant and allows v to vary from 0 to 1; the other set
holds v constant and varies u.
n m
P(u, v) = åå pi , j Bi ,n (u)B j ,m (v) 0 £ u, v £ 1
i =0 j =0
Bézier Patches
• A common form of approximating larger surfaces by tiling
with cubic Bézier patches. m=n=3
• 4 by 4 = 16 control points.
Neat Bezier Spline
Trick
• A Bezier curve with 4 control points:
- P0 P1 P2 P3
• Can be split into 2 new Bezier curves:
- P0 P’1 P’2 P’3
- P’3 P’4 P’5 P3 A Bézier curve
is bounded by
the convex hull
of its control
points.
92
De Casteljau's Method
93
Alternate Method:
Bézier Curve
• de Casteljau's algorithm for constructing Bézier
curves
t t t
t
t
94
De Casteljau Algorithm
95
De Casteljau
r0 = (1 - u) p0 + u p1
r1 = (1 - u) p1 + u p2
r2 = (1 - u) p2 + u p3
Notation differs
from figure
s0 = (1 - u)r0 + ur1
s1 = (1 - u)r1 + ur2
t 0 = (1 - u)s0 + u s1
Expand one level
r0 = (1- u)p0 + u p1
r1 = (1- u)p1 + u p2
Similarly
t 0 = (1 - u ) p0 + 3u (1 - u ) p1 + 3u (1 - u ) p2 + u p3
3 2 2 3
Recall!!
n
P (u ) = å p i Bi ,n (u ), 0 £ u £1
i =0
n -i n!
Bi ,n (u ) = C (n, i )u (1 - u ) ,
i
C(n,i) =
i!(n - i )!
Splines
100
Splines
ì1 if ti £ u < ti +1
N i ,1 (0) = í
î0 otherwise
(u - ti ) (ti + k - u )
N i ,k (u ) = N i ,k -1 (u ) + N i +1,k -1 (u )
ti + k -1 - ti ti + k - ti +1
112
B-Spline Curves
b) Periodic B-Spline ti =i
114
B-Spline Curves
115
Important Properties for B-
Spline Curves
1. Control Points: The location of the curve
depends only on few control points.
Several consecutive CPs at the same
location introduces corners.
117
Important Properties for B-
Spline Curves
2. Multiple Values: The parametric form
allows multiple values. If the 1st and last
CPs coincide, the curve is closed.
3. Axis Independence: B-Spline curve is
independent of coordinate system used
to measure the location of CPs
Important Properties for B-
Spline Curves
4. Local Control: These curves provide
localized control. The shape of the curve
changes only in the vicinity of a changed
CP.
Important Properties for B-
Spline Curves
5. Variation Diminishing Property: B-Spine
curves are variation diminishing. The
curve always lies within the convex hull
of the CPs.
Important Properties for B-
Spline Curves
Graphical Interactive Input
Methods
• Graphics system used for (examples):
• Architectural Design
- construct and display views of buildings by
positioning walls, doors, windows, and other
building components.
• Facility layout system
- objects could be defined as a set of furniture
items (tables, chairs, etc.), and the available
operations would include those for positioning
and removing different pieces of furniture within
the facility layout 123
Graphical Interactive Input
Methods
• Graphics system used for (contd):
• Circuit design application
- might use electrical or logic elements
- adding or deleting elements
User Dialogue
124
Graphical Interactive Input
Methods
• User Dialogue considerations
- Windows and Icons:
• opening and closing windows, repositioning windows,
resizing windows, and display routines that provide interior
and exterior clipping and other graphics functions
• application icons - The icons representing objects
• control icons, or command icons – The icons representing
actions, such as rotate, magnify, scale, clip, and paste
- Consistency
• Icon shapes, combination of keys, colour codes etc should
have a single meaning
• Menus should be placed on same relative position
125
Graphical Interactive Input
Methods
• User Dialogue considerations
- Minimize memorization
- Backup and Error Handling
• Undo
• Good diagnostics and Error messages: Not allowing to
paste if nothing is on clipboard
- Feedback
• As each input is received, the system provides some type
of response e.g. An object is highlighted or blinks, an icon
appears, colour changes or a message is displayed
• Without feedback, uncertainty is there regarding what the
system is doing and whether the input should be given
again
126
Graphical Interactive Input
Methods
Logical Classification of Input Devices
• LOCATOR- a device for specifying a coordinate
position ( x , y)
• STROKE- a device for specifying a series of
coordinate positions
• STRING- a device for specifying text input
• VALUATOR- a device for specifying scalar value
• CHOICE- a device for selecting menu options
• PICK-a device for selecting picture components
127
Graphical Interactive Input
Methods
Interactive Picture Construction Techniques
Positioning: Moving the cursor to the desired spot
on the screen + clicking a button or key
(A very commonly used operation!!)
E.g. A single positioning operation can be used to
insert a symbol
Two in succession can define end points of a line.
128
Graphical Interactive Input
Methods
Interactive Picture Construction Techniques
Problem:
Very difficult to align input information with other
information already on the screen. Accurate
positioning cannot be achieved without assistance
from the computer.
Solution:
Geometric Constraints
129
Graphical Interactive Input
Methods
Interactive Picture Construction Techniques
Constraints are the rules that we wish certain
information such as input coordinates to obey.
• Modular
• Directional
• Gravity Field Effect
• Scales & Guidelines
130
Graphical Interactive Input
Methods
Interactive Picture Construction Techniques
Positioning Feedback:
131
Graphical Interactive Input
Methods
Interactive Picture Construction Techniques
Pointing & Selection
132