Mid Exam-Model Answer Cairo University
Spring, 2012 Faculty of Computers & Information
Time allowed: 1 Hour IT Department
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)
[Link](V)
[Link](V2)
Elseif V1.x>=xleft and V2.x>=xleft then
[Link](V2)
Elseif V2.x>=xleft then
V=Intersect(V1,V2,xleft)
[Link](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
The t-coordinates are assigned to the given points as follows:
is at t=0
is at t=0.5
is at t=1
.........................................(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:
The parametric equation of x is thus as follows:
For , same procedure is followed:
The algorithm is thus as follows:
Algorithm DrawSpline(x1, y1, x2, y2, x3, y3)
alpha1= x3-2*x2+x1
beta1=2*(x2-x1)
gamma1= x1
alpha2= y3-2*y2+y1
beta2=2*(y2-y1)
gamma2= y1
for t=0 to 1 step 0.001
t2=t*t
x=alpha1*t2+beta1*t+gamma1
y=alpha2*t2+beta2*t+gamma2
DrawPoint(x,y)
end for
End