Numericals CG Unit II

You might also like

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

COMPUTER GRAPHICS

Numericals based on Unit II with solution

1. Let ABCD be the rectangular window with A(20, 20), B(90, 20), C(90, 70) and D(20,
70). Find region codes for endpoints and use the Cohen-Sutherland algorithm to clip the
lines : (i) P 1 P 2 with P 1 (10, 30), P 2 (80, 90).
Line P1 P2
Given that, end points of line are P 1 (10, 30) and P 2 (80, 90)
 Outcode (P 1 ) = 0001 = X
 Outcode (P 2 ) = 1000 = Y
 X AND Y = 0001 AND 1000 = 0000
Logical AND of outcodes of both endpoint is 0000. So the line is not completely outside
 Let us decide the visible portion of the line.
Outcode of endpoint, P1 is 0001, that means line intersects left edge while moving from P
1 to P 2 . Let us call the intersection of point P1 with the left edge is P. P is on line x =
20. So its x-coordinate is 20, we have to compute the only y-coordinate of P.
Slope of line = m = 0.86
From, y = mx + c, c = y – mx
By putting one of the points of line P1 P2 in this equation, we can find intercept of line
with y-axis.
Let’s put P1 (10, 30) in above equation,
c = 30 – (0.86) *(10) = 30 – 8.6 = 21.4
From, y = mx + c = 0.86 *(20) + 21.4 = 17.2 + 21.4 = 38.6
Outcode of end point P2 is 1000, that means line intersects top edge while moving from
P1 to P2 . Let us call the intersection of point P2 with top edge is P. P is on line y = 70.
So it’s y-coordinate is 70, we have to compute only x-coordinate of P.
From, y = mx + c,
x = (y-c)/m
By putting the value of y of top edge in this equation, we can find the intersection x-
coordinate of P.
x = 56.51

So line segment with endpoints P (20, 38.6) and P(56.51, 70) is the visible segment.
2. Clip the line PQ having coordinates P(4,1) and Q(6,4) against the clip window having
vertices A(3,2), B(7,2),

The line is to be clipped against rectangle A (3, 2), B (7, 2), C (7, 6) and D (3, 6).
Outcode (P) = 0100 = P1
Outcode (Q) = 0000 = Q1
P1 AND Q1 = 0100 AND 0000 = 0000
Logical AND of outcodes of both endpoints is 0000, so the line is partially visible.
y2 – y1 4 – 1 3
Slope of line = m= x – x = 6 – 4 = 2 = 1.5
2 1

From,
y=mx + c,
c = y – mx
By putting one of the points of line PQ in this equation, we can find intercept of line with
y axis. Let’s put
P (4, 1) in above equation,
 c=1 – (1.5)  (4) = 1 – 6 = – 5
From,
y=mx + c
y–c
x= m
By putting the value of y of a bottom edge in this equation, we can find the intersection
x-coordinate of P.
2 – (– 5) 7
 x= 1.5 = 1.5 = 4.67
 Coordinate of P would be (4.67, 2).
Outcode of P’ will be 0000
Outcode (Q)=0000
So line segment PQ is the visible segment.
Original line Segments Visible line segments
P(4, 1) and Q(6, 4) P(4.67, 2) and Q(6, 4)

3. Let ABCD be the rectangular window with A(20, 20), B(90, 20), C(90, 70) and D(20,
70). Find region codes for endpoints and use the Cohen-Sutherland algorithm to clip the
lines : Q1 Q2 with Q1(10, 10), Q2(70, 60)
Line Q1Q2
Given that, endpoints of the line are Q1 (10, 10) and Q2 (70, 60)
 Outcode (Q1) = 0101 = X
 Outcode (Q2) = 0000 = Y
 X AND Y = 0101 AND 0000 = 0000
 Logical AND of outcodes of both endpoint is 0000. So the line is partially visible
Let us decide the visible portion of the line.
Outcode of endpoint Q1 is 0101, that means line intersects bottom and left edges while
moving from Q1 to Q2. Let us call the intersection of point Q1 with the bottom edge is
Q1. Q1 is on line y = 20. So its y-coordinate is 20, we have to compute only x-coordinate
of Q1.
y2 – y1 60 – 10 50
Slope of line = m= x – x = 70 – 10 = 60 = 0.83
2 1
From, y=mx + c, c = y – mx
By putting one of the points of line P1P2 in this equation, we can find intercept of line
with y-axis. Let’s put
Q1 (10, 10) in above equation,
 c= 10 – (0.83)  (10) = 10 – 8.3 = 1.7
From,
y=mx + c,
y–c
x= m
20 – 1.7 18.3
 x= 0.83 = 0.83 = 22.04
Outcode (Q1 ) as well as outcode (Q2) both are 0000
So line segment with endpoints Q1 (22.04, 20) and P2 (70, 60) is the visible segment.
Original line Segments Visible line segments

Q1(10, 10) and Q2(70, 60) Q1 (22.04, 20) and Q2(70,
60)

4. Clip the line PQ having coordinates P(4,1) and Q(6,4) against the clip window having
vertices A (3,2), B (7,2), Use Cohen-Sutherland algorithm

The line is to be clipped against rectangle A (3, 2), B (7, 2), C (7, 6) and D (3, 6).
Outcode (P) = 0100 = P1
Outcode (Q) = 0000 = Q1
P1 AND Q1 = 0100 AND 0000 = 0000
Logical AND of outcodes of both endpoints is 0000, so the line is partially visible.
y2 – y1 4 – 1 3
Slope of line = m= x – x = 6 – 4 = 2 = 1.5
2 1

From,
y=mx + c, c = y – mx
By putting one of the points of line PQ in this equation, we can find intercept of line with
y axis. Let’s put
P (4, 1) in above equation,
 c=1 – (1.5)  (4) = 1 – 6 = – 5
From,
y=mx + c
y–c
x= m
By putting the value of y of a bottom edge in this equation, we can find the intersection
x-coordinate of P.
2 – (– 5) 7
 x= 1.5 = 1.5 = 4.67

 Coordinate of P would be (4.67, 2).


outcode (P) as well as outcode (Q) is 0000
So line segment PQ is the visible segment.
Original line Segments Visible line segments
P(4, 1) and Q(6, 4) P(4.67, 2) and Q(6, 4)

5. Use Cohen-Sutherland algorithm to clip two lines P1 (40,15) – P2 (75, 45) and P3 (70,
20) – P4 (100,10) against a window A(50,10), B (80,10), C (80,40) and D (50,40)

Line P1P2
Given that, end points of line are P1(40, 15) and P2(75, 45)
Outcode (P1) = 0001 = A1
Outcode (P2) = 1000 = B1
A1 AND B1 = 0001 AND 1000 = 0000
Logical AND of outcodes of both endpoints is 0000, so the line is partially visible.
Let us decide the visible portion of the line.
Outcode of endpoint A is 0001, that means line intersects left edge while moving from P1
to P2. Let us call the intersection of point P1’. P1’ is on line x = xmin = 50. So its x-
coordinate is 50, we have to compute only
y-coordinate of P1’.
y2 – y1 45 – 15 30
Slope of line = m= x – x = 75 – 40 = 35 = 0.857
2 1

From,
y=mx + c, c = y – mx
By putting one of the points of line AB in this equation, we can find intercept of line with
Y-axis. Let’s put
P1 (40, 15) in above equation,
c=15 – (0.857)  (40) = 15 – 34.28 = – 19.28
Put, x = xmin = 50 in explicit line equation to compute corresponding Y-coordinate,
y= mx + c = 0.857  (50) – 19.28 = 42.85 – 19.28 = 23.57
Thus,
P1’=(50, 23.57)
Outcode of endpoint P2 is 1000, that means line intersects top edge. Let us call the
intersection of point P2’.
P2’ is on line y = 40. So its y-coordinate is 40, we have to compute only x-coordinate of
B.
From,
y=mx + c,
y–c
x= m

By putting the value of y of top edge in this equation, we can find the intersection x-
coordinate of P2.
40 + 19.28 59.28
x= 0.857 = 0.857 = 69.17
Thus,
P2’=(69.17, 40)
So, line segment with endpoints P1(50, 23.57) and P2’(69.17, 40) is the visible segment.
Line P3P4
Given that, endpoints of the line are P3(70, 20) and P4(100, 10)
Outcode (P3) = 0000 = C1
Outcode (P4) = 0010 = D1
C1 AND D1 = 0000 AND 0010 = 0000
Logical AND of outcodes of both endpoint is 0000. So the line is partially visible
Let us decide the
visible portion of the line.
y2 – y1 10 – 20 – 10
Slope of line = m= = = = – 0.33
x2 – x1 100 – 70 30
From, y = mx + c, c = y – mx
By putting one of the points of line P3P4 in this equation, we can find intercept of line
with Y-axis. Let’s put
P3(70, 20) in above equation,
c= 20 – ( – 0.33)  (70) = 20 + 23.1 = 43.1
Outcode of endpoint P4 is 0010, that means line intersects right edge. Let us call the
intersection of point
P3’.
From, y=mx + c,
By putting the value of x of a right edge in this equation, we can find the intersection y-
coordinate of P3.
y = (– 0.33  80) + 43.1 = – 26.4 + 43.1 = 16.7
So, line segment with endpoints P3(70, 20) and P3(80, 16.7) is the visible segment.

Original line Segments Visible line segments


P1(40, 15) and P2(75, 45) P1’(50, 23.57) and P2’ (69.17, 40)
P3(70, 20) and P4(100, 10) P3’ (70, 20) and P3’ (80, 16.7)

You might also like