+ All Categories
Home > Engineering > Lect 5 2d clipping

Lect 5 2d clipping

Date post: 15-Jul-2015
Category:
Upload: majicyoung
View: 38 times
Download: 4 times
Share this document with a friend
38
UNIT-3 Computer Graphics : Viewing In 2D
Transcript

UNIT-3

Computer Graphics :

Viewing In 2D

Contents

Windowing Concepts

Clipping

Introduction

Brute Force

Cohen-Sutherland Clipping Algorithm

Area Clipping

Sutherland-Hodgman Area Clipping Algorithm

Windowing I A scene is made up of a collection of objects

specified in world coordinates

World Coordinates

Windowing II When we display a scene only those objects within a

particular window are displayed

wymax

wymin

wxmin wxmax

Window

World Coordinates

Windowing III Because drawing things to a display takes time we

clip everything outside the window

wymax

wymin

wxmin wxmax

World Coordinates

Window

Clipping For the image below consider which lines and points

should be kept and which ones should be clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P3

P6

P5P7

P10

P9

P4

P8

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

wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax

otherwise it is clipped

wymax

wymin

wxmin wxmax

Window

P1

P2

P5

P7

P10

P9

P4

P8

Clipped

Points Within the Window

are Not Clipped

Clipped

Clipped

Clipped

Line Clipping

Harder - examine the end-points of each line to see if

they are in the window or not

Situation Solution Example

Both end-points inside

the windowDon’t clip

One end-point inside

the window, one

outside

Must clip

Both end-points

outside the windowDon’t know!

9

Cohen-Sutherland Algorithm

Idea: eliminate as many cases as possible

without computing intersections

Start with four lines that determine the sides of

the clipping window

x = xmaxx = xmin

y = ymax

y = ymin

10

The Cases Case 1: both endpoints of line segment inside

all four lines Draw (accept) line segment as is

Case 2: both endpoints outside all lines and on same side of a line Discard (reject) the line segment

x = xmaxx = xmin

y = ymax

y = ymin

11

The Cases

Case 3: One endpoint inside, one outside

Must do at least one intersection

Case 4: Both outside

May have part inside

Must do at least one intersection

x = xmaxx = xmin

y = ymax

12

Defining Outcodes For each endpoint, define an outcode

Outcodes divide space into 9 regions

Computation of outcode requires at most 4 subtractions

b0b1b2b3

b0 = 1 if y > ymax, 0 otherwise

b1 = 1 if y < ymin, 0 otherwise

b2 = 1 if x > xmax, 0 otherwise

b3 = 1 if x < xmin, 0 otherwise

13

Using Outcodes

Consider the 5 cases below

AB: outcode(A) = outcode(B) = 0

Accept line segment

14

Using Outcodes

CD: outcode (C) = 0, outcode(D) 0

Compute intersection

Location of 1 in outcode(D) determines which edge

to intersect with

Note if there were a segment from A to a point in a

region with 2 ones in outcode, we might have to do

two interesections

15

Using Outcodes

EF: outcode(E) logically ANDed with outcode(F)

(bitwise) 0

Both outcodes have a 1 bit in the same place

Line segment is outside of corresponding side of

clipping window

reject

16

Using Outcodes

GH and IJ: same outcodes, neither zero but

logical AND yields zero

Shorten line segment by intersecting with one of

sides of window

Compute outcode of intersection (new endpoint of

shortened line segment)

Reexecute algorithm

17

Efficiency

In many applications, the clipping window is small

relative to the size of the entire data base

Most line segments are outside one or more side of

the window and can be eliminated based on their

outcodes

Inefficiency when code has to be reexecuted for

line segments that must be shortened in more

than one step

Liang – Barsky clipping

In computer graphics, the Liang–Barsky

algorithm (named after You-Dong

Liang and Brian A. Barsky) is a line

clipping algorithm.

The Liang–Barsky algorithm uses the parametric

equation of a line and inequalities describing the

range of the clipping window to determine the

intersections between the line and the clipping

window. With these intersections it knows which

portion of the line should be drawn.

This algorithm is significantly more efficient

than Cohen–Sutherland.

The idea of the Liang-Barsky clipping algorithm is

to do as much testing as possible before

computing line intersections. Consider first the

usual parametric form of a straight line:

A point is in the clip window, if

To compute the final line segment:

A line parallel to a clipping window edge has for that boundary.

If for that the line is completely outside and can be eliminated.

When the line proceeds outside to inside the clip window and when , the line proceeds inside to outside.

For nonzero , gives the intersection point.

Here

Liang-Barsky

EnterEnter

Leave

Leave

Enter

Leave

Enter

Leave

Liang-Barsky - Algorithm

Compute entering u values, which are qk/pk for each pk<0

Compute leaving u values, which are qk/pk for each pk>0

Parameter value for small u end of line is:usmall= max(0, entering u’s)

parameter value for large t end of line is: ularge=min(1, leaving u’s)

If usmall<ularge, there is a line segment - compute endpoints by substituting t values

Improvement (and actual Liang-Barsky): compute u’s for each edge in turn (some rejects occur earlier

like this.)

Area Clipping Similarly to lines, areas must

be clipped to a window

boundary

Consideration must be taken

as to which portions of the

area must be clipped

Sutherland-Hodgman Area Clipping

Algorithm

A technique for clipping areas

developed by Sutherland &

Hodgman

Put simply the polygon is clipped

by comparing it against each

boundary in turn

Original Area Clip Left Clip Right Clip Top Clip Bottom

Sutherland

turns up

again. This

time with

Gary Hodgman with

whom he worked at

the first ever

graphics company Evans & Sutherland

The Sutherland-Hodgeman Polygon-Clipping

Algorithms clips a given polygon successively against

the edges of the given clip-rectangle.

These clip edges are denoted with e1, e2, e3, and

e4, here.

The closed polygon is represented by a list of its

vertices (v1 to vn; Since we got 15 vertices in the

example shown above, vn = v15.).

Clipping is computed successively for each edge.

The output list of the previous clipping run is used as

the input list for the next clipping run.

Sutherland-Hodgman Area Clipping

Algorithm (cont…) To clip an area against an individual boundary:

Consider each vertex in turn against the boundary

Vertices inside the boundary are saved for clipping against

the next boundary

Vertices outside the boundary are clipped

If we proceed from a point inside the boundary to one

outside, the intersection of the line with the boundary is

saved

If we cross from the outside to the inside intersection point

and the vertex are saved

Sutherland-Hodgman Example Each example shows

the point being

processed (P) and the

previous point (S)

Saved points define

area clipped to the

boundary in question

S

P

Save Point P

S

P

Save Point I

I

P

S

No Points Saved

S

P

Save Points I & P

I

Other Area Clipping Concerns

Clipping concave areas can be a little more tricky as often superfluous lines must be removed

Clipping curves requires more work For circles we must find the two intersection points on

the window boundary

Window WindowWindow Window


Recommended