GrMidSpring2012_ModelAnswer
GrMidSpring2012_ModelAnswer
Question 1
[b] It is required to draw the broken line defined by the following equation:
Write a suitable DDA based algorithm to draw the line for . Your algorithm should
exploit the symmetry. The algorithm should have three parameters: m, c, a as shown below:
Ans.:
y
c x
Clearly, there is symmetry about the vertical line: x=c. So, every point (c+x, y) has a similar point (c-
x,y). The idea is to generate the points of the line y=mx and call the function Draw2Points with
every generated point. The Draw2Points draws shifted versions of the point and its mirror as
shown below
Algorithm BrokenLine(m, c, a)
x=0
y=0
Draw2Points(c,x,y)
If abs(m)<1 then
while x<>a
x=x+1
y=y+m
Draw2Points(c,x,y)
End while
Else
minv=1/m
yend=m*a;
while y<>yend
y=y+1
x=x+minv
Draw2Points(c,x,y)
End while
End Algorithm
Utility Draw2Points(c, x, y)
DrawPoint(c+x,y)
DrawPoint(c-x,y)
End Utility
Question 2
[a] Write the logic of the Sutherland-Hodgman algorithm to clip a polygon P with n vertices against
the left edge (located at x=xleft) of a rectangular window.
Algorithm ClipLeft(P, n, xleft)
V1=P[n-1]
Outlist=[]
For i=0 to n-1 step 1
V2=P[i]
If V1.x<xleft and V2.x>=xleft then
V=Intersect(V1,V2,xleft)
Outlist.Append(V)
Outlist.Append(V2)
Elseif V1.x>=xleft and V2.x>=xleft then
Outlist.Append(V2)
Elseif V2.x>=xleft then
V=Intersect(V1,V2,xleft)
Outlist.Append(V)
Endif
V1=V2
End for
End Algorithm
Utility Intersect(V1,V2,xleft)
Let V be Point
V.x=xleft
V.y=V1.y+(xleft-V1.x)*(V2.y-V1.y)/(V2.x-V1.x)
Return V
End utility
Question 3
[b] Derive the parametric quadratic equations of a curve with end points and
where the lines from the control point to the end points are tangential to the curve as
shown in the figure below. Write an algorithm to draw the curve given the three control points
.........................................(1)
........................................(2)
.........................................(3)
.........................................(4)
So, we have four equations in three unknowns!! Fortunately, the solution obtained using equations
1, 2 and 3 is consistent with equation 4. The solution is:
To verify that equation 4 is consistent, substitute the solution in the left hand side of the equation: