1 Computer Graphics Chapter 4 2D Viewing Algorithms.

22
1 Computer Graphics Chapter 4 2D Viewing Algorithms
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    233
  • download

    2

Transcript of 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

Page 1: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

1

Computer Graphics

Chapter 42D Viewing Algorithms

Page 2: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-2RM

World Coordinates:• User-Defined Limits• Floating point values

Device Coordinates:• Device dependent Limits• Positive Integer values

Coordinate Systems

x

y

0

v

0 u

(x, y)(u,v)

Page 3: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-3RM

Sine Function

-1.5

-1

-0.5

0

0.5

1

1.5

-180

-150

-120 -90

-60

-30 0 30 60 90 120

150

180

theta

sin

(th

eta

)

(0,300)

(360,0)(0,0)

Window: A rectangular region in World Coordinate System.

Viewport: A rectangular region in Device Coordinate System.

Page 4: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-4RM

Window to Viewport Transformation

We denote the boundaries of the world window by four real values xmin, ymin, xmax, ymax denoting its left, bottom, right, top margins respectively. Similary the boundaries of the viewport on the screen are defined by four integer values umin, vmin, umax, vmax. When a graphics display is generated using arbitrary coordinates on the world window, the important problem encountered in viewing this display on the actual viewport on the screen is to have a function which maps every point (x,y) on the window to a corresponding point (u,v) on the viewport. The following window to viewport transformation achieves this relationship.

Page 5: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-5RM

(xmin,ymin)

(xmax, ymax)

(umin, vmin)

(umax, vmax)(x, y)

(u, v)

vminvmax

vminv

yminymax

yminy

uminumax

uminu

xminxmax

xminx

;

(x, y) (u, v)

Window to Viewport Transformation

Page 6: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-6RM

xmincuminc

xminxmax

uminumaxc

12

1

Window to Viewport Transformation

ymindvmind

yminymax

vminvmaxd

12

1

u = c1 x + c2

v = d1 y + d2

Page 7: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-7RM

Window to Viewport Transformation

-0.05 +0.050.1

0.2

100

400

250 550

u = 3000 x + 400v = 3000 y 200

Page 8: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-8RM

Aspect Ratio = Width/Height.

• Aspect ratio of a world window = (xmax-xmin) / (ymax-ymin).• Aspect ratio of the viewport = (umax-umin) / (vmax-vmin).

The transformation from the window to viewport is said to preserve the aspect ratio, if both the above quantities are same. In such a case, we have c1 = d1. When this condition is satisfied, the mapping from the window to the viewport is distortion-free.

Aspect Ratio

Page 9: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-9RM

Window to Viewport Transformation

-0.05 +0.050.1

0.2

100

400

250 350

u = 1000 x + 300v = 3000 y 200

For distortion-free mapping from the window to the viewport, we must havec1 = d1 (in magnitude)

Page 10: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-10RM

W-V Transform (OpenGL)

Specifying a World Window:gluOrtho2D(xmin, xmax, ymin, ymax);

Specifying a Viewport:glViewport(umin, vmin, wid, hgt);

where,wid=umax-uminhgt=vmax-vmin

Page 11: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-11RM

A line is required to be clipped against a rectangular clipping window such that the portion of the line that falls outside the window is not displayed.

There are many possible arrangements of a segment with respect to the window.

We therefore need an organized approach to identify the correct situation and to compute the new end points of each clipped segment.

Efficiency is important because a typical picture contains thousands of line segments, each of which must be clipped against a window.

Line Clipping

Page 12: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-12RM

Window Clipping Window

Line Clipping

Page 13: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-13RM

Cohen-Sutherland Algorithm

Clipping window

A rapid divide-and-conquer approach to the line clipping

Trivial Accept: When both end points are inside the window, and therefore the line is completely visible.

Trivial Reject: When both end points are outside the window and on the same side of the window. Then the line is completely outside, and hence can be rejected.

Page 14: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-14RM

A point is assigned a unique region code depending on the locationof the point with respect to the window.

1001 1000 1010

0001 0000 0010

0101 0100 0110

Clipping window

B R L

Region Codes

A

Page 15: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-15RM

Trivial Accept

0000

0000

r1 == 0000 and r2 == 0000

(r1 OR r2) == 0000 (OR: Bitwise)

Region Codes

Page 16: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-16RM

Trivial Reject

0010

1010

r1 and r2 have a common bit set to 1

(r1 AND r2) 0000 (AND: Bitwise)

Region Codes

Page 17: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-17RM

“Other Cases”

0000

1010

Region Codes

1001

0100

Page 18: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-18RM

Region Codes

i. The point that lies outside the window is considered. (This is the point whose region code is non-zero).

ii. For the above point, an edge outside which the point lies is identified. (If a particular bit is non-zero, the corresponding edge of the window is considered).

iii. The intersection of the line with the edge is computed. The initial point in (i) is now replaced by the new intersection point. Its region code is computed.

iv. The conditions for the “trivial accept” or “trivial reject” or “other case” is again checked for the new line segment.

“Other Cases”

Page 19: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-19RM

Region Codes

0000

1010

0000Trivial Accept

Page 20: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-20RM

Computing Intersection Point (Eg.)

The equation of the line PQ is

The intersection point A lies on the left edge and therefore its x-coordinate is x = xmin.

To get the y-coordinate of A, we substitute for x in the above equation of PQ:

y = )(

)()min(

12

1211 xx

yyxxy

12

1

12

1

xx

xx

yy

yy

P

QA

x=xmin

Page 21: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-21RM

Computing Intersection Point (Eg.)

Now P is replaced by the point A (effectively discarding the segment PA), and the whole process of checking is repeated for the segment AQ. Now both A, Q will have region codes 0000, and hence will satisfy the condition “trivial accept”.

P

QA

x=xmin

Page 22: 1 Computer Graphics Chapter 4 2D Viewing Algorithms.

[4]-22RM

Line Clipping (OpenGL)

Specifying a Clipping Window:

gluOrtho2D(xmin, xmax, ymin, ymax);