Sunteți pe pagina 1din 80

Computer Graphics

TWO DIMENSIONAL
VIEWING

Two-Dimensional Viewing
Co-ordinate Systems.

Cartesian offsets along the x and y axis from (0.0)

Polar rotation around the angle .

Graphic libraries mostly using Cartesian co-ordinates

Any polar co-ordinates must be converted to Cartesian coordinates

Four Cartesian co-ordinates systems in computer


Graphics.
1. Modeling co-ordinates
2. World co-ordinates

3. Normalized device co-ordinates


4. Device co-ordinates

Modeling Coordinates

Also known as local coordinate.

Ex: where individual object in a scene within separate


coordinate reference frames.

Each object has an origin (0,0)

So the part of the objects are placed with reference to the


objects origin.

In term of scale it is user defined, so, coordinate values


can be any size.

World Co-ordinates.

The world coordinate system describes the relative


positions and orientations of every generated objects.

The scene has an origin (0,0).

The object in the scene are placed with reference to the


scenes origin.

World co-ordinate scale may be the same as the modeling


co-ordinate scale or it may be different.

However, the coordinates values can be any size (similar


to MC)

Normalized Device Co-ordinates

Output devices have their own co-ordinates.

Co-ordinates values:

The x and y axis range from 0 to 1

All the x and y co-ordinates are floating point numbers in


the range of 0 to 1

This makes the system independent of the various devices


coordinates.

This is handled internally by graphic system without user


awareness.

Device Co-ordinates

Specific co-ordinates used by a device.

Pixels on a monitor
Points on a laser printer.
mm on a plotter.

The transformation based on the individual device is


handled by computer system without user concern.

Two-Dimensional Viewing

Example:
Graphic program which draw an entire building
by an architect but we only interested on the
ground floor
Map of sales for entire region but we only like to
know from certain region of the country.

Two-Dimensional Viewing

When we interested to display certain portion of the


drawing, enlarge the portion, windowing technique is used

Technique for not showing the part of the drawing which


one is not interested is called clipping

An area on the device (ex. Screen) onto which the window


will be mapped is called viewport.

Window defines what to be displayed.

A viewport defines where it is to be displayed.

Most of the time, windows and viewports are usually


rectangles in standard position(i.e. aligned with the x and y
axes). In some application, others such as general polygon
shape and circles are also available

However, other than rectangle will take longer time to


process.

Viewing Transformation

Viewing transformation is the mapping of a part of a


world-coordinate scene to device coordinates.

In 2D (two dimensional) viewing transformation is simply


referred as the window-to-viewport transformation or the
windowing transformation.

Mapping a window onto a viewport involves converting


from one coordinate system to another.

If the window and viewport are in standard position, this


just

involves translation and scaling.

if the window and/or viewport are not in standard,


then extra transformation which is rotation is
required.

Viewing Transformation

y-world

y-view
window

x-view
x-world
world

window

Normalised device

Window-To-Viewport Coordinate Transformation

Window-to-Viewport transformation

Window-To-Viewport Coordinate Transformation

YWmax

YVmax
.

xw,yw

YWmin
XWmin

YVmin
XWmax

xv,yv

XVmin

XVmax

Window-To-Viewport Coordinate Transformation

xv - xvmin

xvmax - xvmin
yv yvmin

yvmax yvmin

xw - xwmin

xwmax - xwmin
=

yw - ywmin

ywmax - ywmin

From these two equations we derived


xv = xvmin + (xw xwmin)sx
yv = yvmin + (yw ywmin)sy
where the scaling factors are
sx = xvmax xvmin
xwmax xwmin

sy = yvmax - yvmin
ywmax - ywmin

Window-To-Viewport Coordinate Transformation

The sequence of transformations are:


1. Perform a scaling transformation using a fixedpoint position of (xwmin,ywmin) that scales the
window area to the size of the viewport.
2. Translate the scaled window area to the position
of the viewport.

Window-To-Viewport Coordinate Transformation

Relative proportions of objects are maintained if the scaling factors


are the same (sx = sy). Otherwise, world objects will be stretched or
contracted in either x or y direction when displayed on output
device.

How about character strings when map to viewport?

maintains a constant character size (apply when standard


character fonts cannot be changed).

If character size can be changed, then windowed will be


applied like other primitives.

For characters formed with line segments, the mapping to


viewport is carried through sequence of line transformations
.

Viewport-to-Normalized Device Coordinate Transformation

From normalized coordinates, object descriptions can be


mapped to the various display devices

When mapping window-to-viewport transformation is


done to different devices from one normalized space, it is
called workstation transformation.

The Viewing Pipeline

Clipping
Any procedure that identifies those portions of a

picture that are either inside or outside of a specified


region of space is referred to as a clipping
algorithm, or simp.ly clipping
Because drawing things to a display takes time we
clip everything outside the window

Window
A scene is made up of a collection of objects specified

in world coordinates

World Coordinates

Window

contd

When we display a scene only those objects within a

particular window are displayed


Window
wymax

wymin

wxmax

wxmin
World Coordinates

Window contd..
Because drawing things to a display takes time we

clip everything outside the window


Window
wymax

wymin

wxmax

wxmin
World Coordinates

Clipping Operation
For the image below if we want to know which lines and

points should be kept and which ones should be clipped


Clip Window: The region against which an object is

to be clipped.

Point Clipping

P4

Line Clipping

Area Clipping (polygons)


wy

Window

wymax

P6

P3
P7

P2
P1

P5
P9

P8

min

Text Clipping

P10

wxmin

wxmax

Point Clipping
Solution is easy - a point (x,y) is not clipped if:

wxmin x wxmax AND wymin y wymax


otherwise it is clipped

P4 Clipped

Window

wymax
Clipped

P7

wymin

Clipped

P5

P2

P1

Points Within the


Window are Not
P9 Clipped

P10

wxmin

Clipped

P8

Clipped

wxmax

Line Clipping

before clipping

after clipping

Line Clipping
Examine the end-points of each line to see if they are

in the window or not and take action


Situation

Solution

Both end-points inside


Dont clip
the window
One end-point inside
the window, one
outside

Must clip

Both end-points
outside the window

Dont know!

Example

Brute force line clipping


Brute force line clipping can

be performed as follows:

Dont clip lines with both


end-points within the
window
For lines with one endpoint inside the window
and one end-point
outside, calculate the
intersection point (using the
equation of the line) and clip
from this point out

Brute force line clipping


What to do with when both end points are outside the window?

For lines with both end-

points outside the window


test
the
line
for
intersection with all of the
window boundaries, and
clip appropriately
However, calculating line intersections is

computationally expensive
Because a scene can contain so many lines, the
brute force approach to clipping is much too slow

Brute force line clipping: calculating


intersection point
A line segment with endpoints (x1, y1) and (x2, y2) and one

or both endpoints outside the clipping rectangle, the


parametric representation
x = x1 + u ( x2 x1)
y = y1 + u ( y2 y1),

0u1

If the value of u for an intersection with a rectangle


boundary edge is outside the range 0 to 1, the line does
not intersect with that boundary.

If the value of u is within the range from 0 to 1, the line


segment intersect with that boundary.

Example
Solve u by substitute x = xwmax in
x = x1 + u (x2 x1)
5 = 4 + u (7 4)

u = 1/3
The value of u is within the range from 0 to 1, the line

segment intersects with the boundary xwmax


Solve y by substitute u in y = y1 + u ( y2 y1)
y = 2 + 1/3 (3 2) = 2.33
Intersection point at xwmax is (5, 2.33)

Cohen-Sutherland Clipping Algorithm


Cohen-Sutherland is an efficient line clipping algorithm.
The key advantage of the algorithm is that it vastly reduces

the number of line intersections that must be calculated.


Every lone end point in a picture is assigned a four-digit
binary code, called a region code
It identifies the location of the point relative to the
boundaries of the clipping rectangle.
Each bit position in the region code is used to indicate one
of the four relative coordinate positions of the point with
respect to the clip window: to the left, right, top, or bottom

Cohen-Sutherland Clipping Algorithm


By numbering the bit positions (1

through 4) from left to right in the


region code the coordinate regions
can be correlated with the bit
positions as

above below right

0
left

Region Code Legend

1001 1000 1010


A value of 1 in any bit position

indicates that the point is in that


0000
0001
0010
relative position; otherwise, the bit
Window
position is set to 0.

0101 0100 0110

Region Code Computation


Top bit

Bottom bit

Right bit

Left bit

xwmax x

x xwmin
1

ywmax y
0

y ywmin
0

Cohen-Sutherland Clipping Algorithm


Bit 1 is set to 1 if x < wxmin. Bit 2 is set to 1 if x>wxmax

Bit 3 is set to 1 if y < wymin. Bit 4 is set to 1 if y>wymax


We can do that with following two steps:
(1) Calculate differences between endpoint coordinates
and clipping boundaries.
(2) Use the resultant sign bit of each difference
calculation to set the corresponding value in the region code.
bit 1 is the sign bit of x - wxmin
bit 2 is the sign bit of wxmax x
bit 3 is the sign bit of y - wymin and
bit 4 is the sign bit of wymax - y.

Cohen-Sutherland Clipping Algorithm


Every end-point is labelled with the appropriate

region code
P11 [1010]

P4 [1000]

Window

wymax

P6 [0000]

P3 [0001]

P12 [0010]

P5 [0000]
P7 [0001]
P9 [0000]

wymin
P13 [0101]

wxmin

P8 [0010]

P10 [0100]

wxmax

P14 [0110]

Cohen-Sutherland Clipping Algorithm


Lines completely contained within the window

boundaries have region code [0000] for both endpoints so are not clipped
P11 [1010]

P4 [1000]

Window

wymax

P6 [0000]

P3 [0001]

P12 [0010]

P5 [0000]

P7 [0001]
P9 [0000]

wymin
P13 [0101]

wxmin

P8 [0010]

P10 [0100]

wxmax

P14 [0110]

Cohen-Sutherland Clipping Algorithm


Any lines with a common set bit (i.e. having bit 1 in the same

position) in the region codes of both end-points can be clipped or


discarded as it is completely outside of the any one boundary

The AND operation can efficiently check this


P11 [1010]

P4 [1000]

p11 : 1010
p12 : 0010

Window

wymax

P6 [0000]

P3 [0001]

p11 AND p12 =0010(outside)

P12 [0010]

P5 [0000]
P7 [0001]
P9 [0000]

wymin
P13 [0101]

wxmin

P8 [0010]

P10 [0100]

wxmax

P14 [0110]

Cohen-Sutherland Clipping Algorithm


Lines that cannot be identified as completely inside

or outside the window may or may not cross the


window interior
These lines are processed as follows:
Compare an end-point outside the window to a
boundary (choose any order in which to consider
boundaries e.g. left, right, bottom, top) and
determine how much can be discarded
If the remainder of the line is entirely inside or
outside the window, retain it or clip it respectively

Cohen-Sutherland Clipping Algorithm

Otherwise, compare the remainder of the line against the other


window boundaries
Continue until the line is either discarded or a segment inside
the window is found

We can use the region codes to determine which

window boundaries should be considered for


intersection

To check if a line crosses a particular boundary we compare


the appropriate bits in the region codes of its end-points
If one of these is a 1 and the other is a 0 then the line crosses
the boundary

Cohen-Sutherland Clipping Algorithm


Consider the line P9 to P10 below

wymax

Window

Start at P10
From the region codes
of the two end-points we
P [0000]
wymin
know the line doesnt
P [0000]
cross the left or right
P [0100]
boundary
wxmin
wxmax
Calculate the intersection of
the line with the bottom boundary to generate point P10
The line P9 to P10 is completely inside the window so is
retained

10

10

Cohen-Sutherland Clipping Algorithm


Consider another the line P3 to P4 below
P [1001]
P [1000]
Start at P4
Window
wy
From the region codes of the
max
P [0001]
two end-points we know the line
crosses the left boundary so
calculate the intersection point wy
min
to generate P4
The line P3 to P4 is completely
wxmin
outside the window so is clipped

wxmax

Cohen-Sutherland Clipping Algorithm


Consider the line P7 to P8 below

Start at P7
From the two region
codes of the two
end-points we know
the line crosses the
left boundary so
calculate the
intersection point to
generate P7

Window

wymax

P7 [0000]
P8 [0010]
P7 [0001]
P8 [0000]

wymin

wxmin

wxmax

Consider the line P7 to P8


Start at P8
Calculate the intersection with the right boundary to generate
P8
P7 to P8 is inside the window so is retained

Another Example
p2
1

p2

p2

p3
p1

p3
0

p4

p1

Calculating Line Intersections for Cohen-Sutherland


Clipping Algorithm
Intersection points with the window boundaries are

calculated using the line-equation parameters

Consider a line with the end-points (x1, y1) and (x2, y2)
The y-coordinate of an intersection with a vertical window boundary
can be calculated using:

y = y1 + m (xboundary - x1)

where xboundary can be set to either wxmin or wxmax


The x-coordinate of an intersection with a horizontal window
boundary can be calculated using:

x = x1 + (yboundary - y1) / m

where yboundary can be set to either wymin or wymax


m is the slope of the line in question and can be calculated as
m = (y2 - y1) / (x2 - x1)

Liang-Barsky Line Clipping Algorithm


Treat undecided lines in Cohen-Sutherland more efficiently.
Define clipping window by intersections of four half-planes.
xwmin

xwmax
ywmax

ywmin

Liang-Barsky Line Clipping Algorithm


For a line segment with endpoints (x1, y1) and (x2,
y2), we can describe the line with parametric form:
x = x1 + u x

y = y1 + u y,

0 u 1

where x= x2 x1 and y= y2 y1

The point-clipping condition in the parametric form:


xwmin x1 + u x xwmax

ywmin y1 + u y ywmax

Liang-Barsky Line Clipping Algorithm


xwmin x1 + u x xwmax
ywmin y1 + u y ywmax
Each of these four inequalities can be expressed as:

upk qk, k=1, 2, 3, 4


where the parameters p and q are defined as:
p1 = x,

q1 = x1 xwmin

p2 = x,

q2 = xwmax x1

p3 = y,

q3 = y1 ywmin

p4 = y,

q4 = ywmax y1

Liang-Barsky Line Clipping Algorithm


Any lines that is parallel to one of the clipping
boundaries has pk=0 for the value of k
corresponding to that boundary where k = 1, 2, 3,
4 correspond to left, right, bottom and top
boundaries respectively.
If, for that value of k, we also find qk < 0, then the
line is completely outside the boundary and can
be eliminated from further consideration.
If qk 0, then the line is inside the parallel clipping
boundary.

Liang-Barsky Line Clipping Algorithm


If pk < 0, the infinite extension of the line
proceeds from the outside to the inside of the
infinite extension of this clipping boundary.
If pk > 0, the line proceeds from the inside to
the outside.
For a nonzero value of pk (pk 0), we can
calculate the value of u that corresponds to the
point where the infinitely extended line intersects
the extension of boundary k as:

u = q k / pk

Liang-Barsky Line Clipping Algorithm


For each line, we can calculate values for parameters u1 and
u2 that define that part of the line that lies within the clip
rectangle.
The value of u1 is determined by looking at the rectangle
edges for which the line proceeds from outside to the
inside (pk < 0).
For these edges, we calculate rk = qk / pk.
The value of u1 is taken as the largest of the set consisting
of 0 and the various values of r.

Liang-Barsky Line Clipping Algorithm


The value of u2 is determined by examining the boundaries
for which the line proceeds from inside to the outside
(pk > 0).
A value of rk is calculated for each of these boundaries, and
the value of u2 is the minimum of the set consisting of 1
and the calculated r values.
If u1 > u2, the line is completely outside the clip

window and it can be discarded.

Otherwise, the endpoints of the clipped line are calculated

from the two values of parameter u.

Algorithm: Liang-Barsky Line Clipping


The

Liang-Barsky Line
summarized as follows:

Clipping

Algorithm

1. For each line, calculate the parameters p, q and r:

p1 = x, q1 = x1 xwmin, r1 = q1/ p1
p2 = x, q2 = xwmax x1 , r2 = q2/ p2
p3 = y, q3 = y1 ywmin, r3 = q3/ p3
p4 = y, q4 = ywmax y1, r1 = q4/ p4

can

be

Algorithm: Liang-Barsky Line Clipping


2. Determine the value of u1 is by looking at the rectangle

edges for which the line proceeds from outside to the


inside (pk < 0). The value of u1 is taken as the largest of
the set consisting of 0 and the various values of r.

3. Determine the value of u2 by examining the boundaries for

which the line proceeds from inside to the outside (pk >
0). A value of rk is calculated for each of these boundaries,
and the value of u2 is the minimum of the set consisting
of 1 and the calculated r values.

Algorithm: Liang-Barsky Line Clipping


4. If u1 > u2, the line is completely outside the clip

window and it can be discarded.

5. Otherwise,

the endpoints of the clipped line are


calculated from the two values of parameter u.

If u2 <1

Otherwise

If u1 >0

Otherwise

x= x1+u2 x,
x = x2 ,
x= x1+u1 x,
x = x1 ,

y = y1 +u2 y
y = y2
y = y1 +u1 dy
y = y1

Example : Liang-Barsky Line Clipping

Nicholl-Lee-Nicholl Line Clipping Algorithm


Creates more regions around clipping window to

avoid multiple line intersection calculations.


Performs fewer comparisons and divisions than

Cohen-Sutherland and Liang-Barsky, but cannot

be extended to 3D, while they can.


For complete inclusion in clipping window or

certain exclusion well use Cohen-Sutherland.

N-L-N Line Clipping Algorithm


P0
P0

P0

Examine first where the starting point P0 is located.


Only three regionsare considered. Location in any
of the other six regions can be handled by symmety
transformation.

N-L-N Line Clipping Algorithm


If P0 is inside the clip window and

P0
B

Pend is outside,
we set up the four regions shown in
Fig. The
intersection with the
appropriate window boundary is

then carried out, depending on which one of the four


regions (L, T, R, or B) contains Pend. Of course, if
both P0 and Pend are inside the clipping rectangle,
we simply save the entire line.

N-L-N Line Clipping Algorithm


LT

If P0 is in the region to the left


L
of the window, we set up the
L
LR
P0
four regions, L, LT, LR, and LB,
L
shown in Fig.
LB
These four regions determine a unique boundary for
the line segment. For instance, if Pend is in region L,
we clip the line at the left boundary and save the line
segment from this intersection point to Pend. But if
Pend is in region LT, we save the line segment from the
left window boundary to the top boundary. If Pend is
not in any of the four regions, L, LT, LR, or LB, the entire
line is clipped.

N-L-N Line Clipping Algorithm


The slope of P0 , Pend is

Pend
LT

compared to P0 , Pcorner

L
L

P0

LR

L
LB

for each corner to find the


region of Pend .

Pend

Once one of LT, LR or LB regions is found,


intersection point with appropriate border is calculated.

P0 , Pend is entierely clipped if Pend is positioned


outside the regions.

N-L-N Line Clipping Algorithm


P0
P0

TR

L
L

T
T

LB

TR

L
LB

LR

TB

There are two cases, depending on whether P0 is


closer to left or top borders.

N-L-N Line Clipping Algorithm


P0
P0

TR

L
L

T
T

TR

LB

LR
LB

TB

Notice that simple inclusion test of Pend in


clipping rectangle is not enough since there
are both T and L labels for the regions inside.
Testing of the angular regions is a must.

Area Clipping (polygons)


A polygon is a connected area of different line or edges. Can

we apply line clipping algorithm directly to clip polygon?

No to clip a polygon, we cannot directly apply a line-clipping


method to the individual polygon edges because this approach
would produce a series of unconnected line segments as
shown in figure .

Area Clipping (polygons)


The clipped polygons must be
a bounded area after clipping
as shown in figure.

For polygon clipping, we require an algorithm that will

generate one or more closed areas that are then scan


converted for the appreciate area fill.

The output of a polygon clipper should be a sequence of

vertices that defines the clipped polygon boundaries.

Sutherland-Hodgman Polygon Clipping


Clip a polygon by processing the polygon boundary as a

whole against each window edge.

Processing all polygon vertices against each clip rectangle


boundary in turn.

Beginning with the initial set of polygon vertices, we could


first clip the polygon against the left rectangle boundary to
produce a new sequence of vertices.

The new set of vertices could be successively passed to a

right boundary clipper, a bottom boundary clipper, and a


top boundary clipper.

Sutherland-Hodgman Polygon Clipping


Lift Clipper

Right
Clipper

Bottom
Clipper

At each step, a new sequence of output


vertices is generated and passed to the next
window boundary clipper.
Top Clipper

Sutherland-Hodgman Polygon Clipping


There are four possible cases when processing vertices in

sequence around the perimeter of a polygon.


As each pair of adjacent polygon vertices is passed to a

next window boundary clipper, we make the following tests:

Sutherland-Hodgman Polygon Clipping


1. If the first vertex is outside the window boundary and

the second vertex is inside


Then , both the intersection point of the polygon edge

with the window boundary and the second vertex are


added to the output vertex list.

Sutherland-Hodgman Polygon Clipping


2.If

both input vertices are inside the window


boundary.

Then,

only the second vertex is added to the


output vertex list.

Sutherland-Hodgman Polygon Clipping


3. If the first vertex is inside the window boundary and the

second vertex is outside.


Then,

only the edge intersection with the window


boundary is added to the output vertex list.

Sutherland-Hodgman Polygon Clipping


4. If both input vertices are outside the window boundary.
Then, nothing is added to the output vertex list.

Sutherland-Hodgman Polygon Clipping (Example)


We illustrate this algorithm by processing the area in

figure against the left window boundary.


Vertices 1 and 2 are outside of the boundary.
Ignore both point.
Vertex 2 outside and Vertex 3, is inside, so
intersection point 1' and vertex 3 are saved.
Vertex 3 and 4 are inside, and so second
Vertex 4 is saved
Vertex 4 and 5 are inside, and so second
Vertex 5 is saved
Vertex 5 is inside and Vertex 6 is outside, so only intersection point 5' is saved.
Using the five saved points (1 , 3, 4, 5, 5) , we would repeat the process for the
next window boundary

Sutherland-Hodgman Polygon Clipping (Example)


The Sutherland-Hodgman algorithm correctly clips convex

polygons, but concave polygons may be displayed with


extraneous lines as demonstrated in figure.
Since there is only one output vertex list, the last vertex in

the list is always joined to the first vertex.

Weiler-Atherton Polygon Clipping


This algorithm was developed for identifying visible

surfaces, and can be used to clip a fill area that is either a


convex polygon or a concave polygon.
The basic idea of this algorithm is that instead of

proceeding around the polygon edges as vertices are


processed, we will follow the window boundaries.
The path we follow depends on:

polygon-processing
counterclockwise)

direction

(clockwise

The pair of polygon vertices


outside-to-inside or an inside-to-outside.

or

Weiler-Atherton Polygon Clipping


For clockwise processing of polygon vertices, we use the

following rules:

For an outside-to-inside pair of vertices, follow polygon


boundaries.

For an inside-to-outside pair of vertices, follow window


boundaries in a clockwise direction.

Weiler-Atherton Polygon Clipping

Text Clipping
There are several techniques that can be used to provide

text clipping in a graphics packages.

The

choice of clipping method depends on how


characters are generated and what requirements we
have for displaying character strings.

Text Clipping
All-or-none string-clipping
If all of the string is inside a clip window, we keep it.
Otherwise the string is discarded.

Text Clipping
All-or-none character-clipping
Here we discard only those characters that are not

completely inside the window

Text Clipping
Clip the components of individual characters
We treat characters in much the same way that we treated

lines.

If an individual character overlaps a clip window boundary,

we clip off the parts of the character that are outside the
window

S-ar putea să vă placă și