521493S Computer Graphics Exercise 1 (Chapters 1-3) : Solutions

You might also like

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

521493S Computer Graphics Exercise 1 (Chapters 1-3)

1. Consider the clipping of a line segment defined by the latter’s two endpoints (𝒙𝟏 , 𝒚𝟏 ) and (𝒙𝟐 , 𝒚𝟐 )
in two dimensions against a rectangular clipping window (𝒙𝒎𝒊𝒏 , 𝒚𝒎𝒊𝒏 ; 𝒙𝒎𝒂𝒙 , 𝒚𝒎𝒂𝒙 ). Show that only
the endpoints are required to determine whether the line segment is not clipped, is partially visible,
or is clipped out completely.

Solutions:

Suppose that the line segment is between the points 𝐏1 = (𝑥1 , 𝑦1 ) and 𝐏2 = (𝑥2 , 𝑦2 ). We can use
the endpoints of the line segment to determine where it resides in the window
(𝑥𝑚𝑖𝑛 , 𝑦𝑚𝑖𝑛 ; 𝑥𝑚𝑎𝑥 , 𝑦𝑚𝑎𝑥 ):

If we create a line using parametric form:

𝐏(𝑡) = (1 − 𝑡)𝐏1 + 𝑡𝐏2 , 𝑡 ∈ [0, 1]

For x-axis we can solve 𝑡𝑥 :

𝑃1𝑥 − 𝑥
𝑡𝑥 =
𝑃1𝑥 − 𝑃2𝑥

We need to deal with horizontal and vertical line segments as special cases.

We can check if we are in the line segment at the borders by evaluating 𝑡𝑥 (𝑥𝑚𝑖𝑛 ) and 𝑡𝑥 (𝑥𝑚𝑎𝑥 ). Y-
axis can be checked similarly.

By checking the available unclipped range of 𝑡 using information from all the dimensions, we can
determine if the line is fully clipped, partially clipped or completely unclipped.

1.1 – Alternative solution

We can solve 𝑦(𝑥) from the known end points:

1
∆𝑦 𝑦2 − 𝑦1
𝑦 = 𝑎𝑥 + 𝑏 = 𝑥+𝑏 = 𝑥+𝑏
∆𝑥 𝑥2 − 𝑥1

The unknown offset 𝑏 can be solved by using any of the known points on the line. We use (𝑥1 , 𝑦1 )
here:
𝑦2 − 𝑦1
𝑦1 = 𝑥 +𝑏
𝑥2 − 𝑥1 1

Solving 𝑏 from the above equation:


𝑦2 − 𝑦1
𝑏 = 𝑦1 − 𝑥
𝑥2 − 𝑥1 1

Assigning 𝑏 back to the original equation:


𝑦2 − 𝑦1 𝑦2 − 𝑦1
𝑦= 𝑥 + 𝑦1 − 𝑥
𝑥2 − 𝑥1 𝑥2 − 𝑥1 1
Similarly 𝑥(𝑦) can be solved:
𝑥2 − 𝑥1 𝑥2 − 𝑥1
𝑥= 𝑦 + 𝑥1 − 𝑦
𝑦2 − 𝑦1 𝑦2 − 𝑦1 1

We need to deal with horizontal and vertical line segments as special cases.

We can find the intersections with the sides of the window by substituting 𝑦 = 𝑦𝑚𝑎𝑥 , 𝑦 =
𝑦𝑚𝑖𝑛 , 𝑥 = 𝑥𝑚𝑎𝑥 and 𝑥 = 𝑥𝑚𝑖𝑛 (the equations for the sides of the window) into the above
equations. We can check the locations of the points of intersection to determine if they are on the
line segment or only on the line of which it is part.

2. To avoid flicker, the memory in a frame buffer must be fast enough to allow the display to be
refreshed at a rate sufficiently high. A typical workstation display can have a resolution of 1280 x
1024 pixels. If it is refreshed 72 times per second, how fast must the memory be? That is, how
much time can we take to read one pixel from memory? What is this number for a 480x640 display
that operates at 60 Hz but is interlaced?

Solutions:

We have to process 1280 x 1024 x 72 pixels/sec. If we process each successively, there is only about
10 nanoseconds to process each. For a 480 x 640 interlaced display operating at 60 Hz we must
process only 480 x 640 x 30 pixels/sec which gives us about 109 nanoseconds to process each pixel.

3. A fundamental operation in graphics system is to map a point (𝒙, 𝒚), which lies within a clipping
rectangle, to a point (𝒙𝒔 , 𝒚𝒔 ), which lies in the viewport of a window on the screen. Assume that
the two rectangles are defined by OpenGL function calls.

glViewport(u, v, w, h);
gluOrtho2D(x_min, x_max, y_min, y_max);

Find the mathematical equations that map (𝒙, 𝒚) into (𝒙𝒔 , 𝒚𝒔 ).

2
Solutions:

We can solve this problem separately in the 𝑥 and 𝑦 directions. The transformation is linear, that is
𝑥𝑠 = 𝑎𝑥 + 𝑏, 𝑦𝑠 = 𝑐𝑦 + 𝑑. We must maintain proportions, so that 𝑥𝑠 in the same relative
position in the viewport as 𝑥 is in the window, hence
𝑥 − 𝑥𝑚𝑖𝑛 𝑥𝑠 − 𝑢
= ,
𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛 𝑤
𝑥 − 𝑥𝑚𝑖𝑛
𝑥𝑠 = 𝑢 + 𝑤 ,
𝑥𝑚𝑎𝑥 − 𝑥𝑚𝑖𝑛

Likewise
𝑦 − 𝑦𝑚𝑖𝑛
𝑦𝑠 = 𝑣 + ℎ .
𝑦𝑚𝑎𝑥 − 𝑦𝑚𝑖𝑛

4. Devise a test for convexity of two-dimensional polygon.

Solutions:

Consider the lines defined by the sides of the polygon. We can assign a direction for each of these
lines by traversing the vertices in a counter-clockwise order.

One very simple test is obtained by noting that any point inside the object is on the left of each of
these lines. Thus, if we substitute the point into the equation for each of the lines (𝑎𝑥 + 𝑏𝑦 + 𝑐),
we should always get the same sign.

5. The mapping from a point in three-dimensional world coordinates to one in screen coordinates is
well defined. What problems exist for inverting the mapping? i.e. Trying to map screen coordinates
to world coordinates.

Solutions:

3
The most significant problem is that going from three dimensions to two dimensions we lose one
dimension. Therefore each point in the screen map to some kind of line in the world coordinates
instead of a single coordinate.

Additional problems are that multiple three-dimensional lines may map to the same screen
coordinate even when the coordinates are not integers.

There may also be points in the screen that do not map to any valid positions in the world coordinates.
For example if the whole world is mapped to a rectangle at the center of the screen, inverse mapping
will not exist for pixels outside the rectangle at all.

6. Suppose a user is running a program using a GLUT window to draw OpenGL graphics. The window
size is 400 x 300 pixels. An orthogonal projection of the scene is setup as follows:

glViewport(0, 0, 400, 300);


gluOrtho2D(0, 400, 0, 300);

There are four circles in the scene that user can select with a mouse. As each circle can be defined
as a triplet (𝒙, 𝒚, 𝒓) where 𝒙 and 𝒚 are the coordinates and 𝒓 is the circle radius, the four circles are
expressed by 𝒄𝟏 = (𝟏𝟎𝟎, 𝟏𝟎𝟎, 𝟓𝟎) 𝒄𝟐 = (𝟑𝟎𝟎, 𝟏𝟎𝟎, 𝟓𝟎) , 𝒄𝟑 = (𝟏𝟎𝟎, 𝟐𝟎𝟎, 𝟓𝟎) , 𝒄𝟒 =
(𝟑𝟎𝟎, 𝟐𝟎𝟎, 𝟓𝟎) .
When user selects his favorite circle, the GLUT callback function for mouse pointer position reports
that the mouse is at coordinates (𝟏𝟏𝟎, 𝟏𝟏𝟎). What circle (if any) does the user select? Please also
explain your answer.

Solutions:

glViewport and gluOrtho2D create an orthographic view of the scene with visible size of 400
x 300. Also the actual window is of the same size so no scaling or stretching happens. Therefore a
circle 𝑐1 centered at (100,100) with radius of (50) is drawn centered as the same OpenGL screen
coordinates.

However, there is a difference between GLUT coordinates and OpenGL coordinates in the screen.
GLUT passes down coordinates that come from the underlying windowing system as relative
coordinates to the window position where the origin is at top left, x-axis grows to the right and y-
axis down. In OpenGL, using presented setup and no additional transformations, the origin is at
bottom left with y-axis pointing up.

This means that when reported mouse pointer is at (110, 110), the position in the scene is
(110, (300 − 1) − 110) that equals (110, 189). This point is in the area of circle 𝑐3 =
(100, 200, 50)

4
7. Double buffering is used to reduce flickering of the screen caused by simultaneously drawing
objects while the screen is displayed. What is the best time to flip the buffers to minimize any
visual problems caused by the screen update? And why?

Solutions:

During vertical retraces.

Suppose that we have a fast computer that can render 300 frames per second. Screen of this
computer is physically updated at 60 Hz. This means that we can render a completely different
scene and show it 5 times during one screen update. This can cause a visual tearing effect as a set
of lines come from a different image than ones above it. Quickly horizontally moving objects have
parts of them shown at different positions during the same screen update.

By limiting updates to one per screen refresh and refreshing the screen during vertical retrace
period, no visible artifacts can be seen.

You might also like