Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

MATERI 04.

VIEWING & CLIPPING


2020/2021 - 1 Komputer Grafik
Viewing & Clipping - Outline
1

 Viewing in 2D
 Clipping in 2D
 Cohen-Sutherland Algorithm
 Cyrus & Beck Algorithm
 Clipping Polygons
 Sutherland-Hodgman Algorithm
Viewing in 2 Dimensions
2

 In 2D, a ‘world’ consists of an infinite plane, defined in


‘world’ coordinates, i.e metres, angstroms or other units
appropriate to the model
 We normally pick an area of the 2D plane to view,
referred to as the viewing ‘window’.
 On our display device, need to allocate an area for
display, referred to as the ‘viewport’ in device specific
coordinates.
 We “Clip” objects outside of window.
 We “Translate” coordinates to fit the viewport.
 We “Scale” to the device coordinates.
Viewing in 2D - Viewport
3

250°

45°

Window in world coordinates.

250 x 250
Viewport in Pixels.
Device coords
Clipping in 2D
4

 Need to clip primitives (eg lines) against the sides of the


viewing window
 e.g lines or polygons
 We only see what is inside the window
Trivial Clipping Acceptance
5

If all a line’s vertices lie inside box we “accept” it


Trivial Vertex Rejection
6

All line vertices lie outside and on same side → reject.


Cohen-Sutherland Clipping Algorithm
7

 This is an efficient method of accepting or rejecting


lines that do not intersect the window edges.
 Assign a binary 4 bit code to each vertex :
 Firstbit : above top of window, y > ymax
 Second bit : below bottom, y < ymin

 Third bit : to right of right edge, x > xmax

 Fourth bit : to left of left edge, x < xmin

 4-bit code called: Outcode


Cohen-Sutherland 2D Outcodes
8

1001 1000 1010

0001 0000 0010

0101 0100 0110


Cohen-Sutherland Algorithm
9

1001 1000 1010

0001 0000 0010

0101 0100 0110

Both endpoint codes 0000, trivial acceptance, else:


Do logical AND of Outcodes (reject if non-zero)
Cohen-Sutherland Algorithm
10

1001 1000 1010


1000

0001

0001 0000 0010


0000
0000

0101 0100 0110

Logical AND between codes for 2 endpoints,


Reject line if non-zero – trivial rejection.
Cohen-Sutherland Algorithm
11

 Given a line segment with endpoint P1 (x1, y1) and P2(x2,y2).


 Compute the 4-bit codes for each end point. If both codes are 0000,(bitwise OR
of the codes yields 0000 ) line lies completely inside the window: pass the
endpoints to the draw routine.
 If both codes have a 1 in the same bit position (bitwise AND of the codes
is not 0000), the line lies outside the window. It can be trivially rejected.
 If a line cannot be trivially accepted or rejected, at least one of the two
endpoints must lie outside the window and the line segment crosses a window
edge. This line must be clipped at the window edge before being passed to the
drawing routine.
 Examine one of the endpoints, say P1(x1,y1) .
 Read P1 's 4-bit code in order: Left-to-Right, Bottom-to-Top.
 When a set bit (1) is found, compute the intersection (see line intersections) I of
the corresponding window edge with the line from P1 to P2.
Replace P1 with I and repeat the algorithm.
Line intersections
12
13
14
Midpoint Subdivision Algorithm
15
Midpoint Subdivision Algorithm
16
17
Line Intersection.
18

 Now need to intersect line segments with edges of clip


rectangle.
 Select any clip edge, trivial line-splitting, feed two new
lines back into algorithm - known as re-entrant
 Alternatively express line in parametric form to handle
vertical lines. ( ie x=x(t), y=y(t), parameter t )
 Substitute for x or y.
 Solve for t.
 Need to perform 4 intersection checks for each line.
Parametric line
19
20
Result
21
22

Akhir Materi 04.

You might also like