0% found this document useful (0 votes)
31 views41 pages

Line Clipping Algorithms: Cohen Sutherland

The document describes the Cohen-Sutherland line clipping algorithm. It works as follows: 1. It computes 4-bit codes for both endpoints (A and B) of the line, where each bit indicates if the point is inside the corresponding clipping region. 2. It checks the codes to determine if both points are inside, both outside, or one inside and one outside the clipping region. 3. If one point is inside and one outside, it finds the intersection point with the clipping boundary indicated by the outside point's code and replaces the outside point. 4. It repeats step 3 until both points are inside the clipping region or no intersection is found, returning the clipped line segment or empty

Uploaded by

radekxka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views41 pages

Line Clipping Algorithms: Cohen Sutherland

The document describes the Cohen-Sutherland line clipping algorithm. It works as follows: 1. It computes 4-bit codes for both endpoints (A and B) of the line, where each bit indicates if the point is inside the corresponding clipping region. 2. It checks the codes to determine if both points are inside, both outside, or one inside and one outside the clipping region. 3. If one point is inside and one outside, it finds the intersection point with the clipping boundary indicated by the outside point's code and replaces the outside point. 4. It repeats step 3 until both points are inside the clipping region or no intersection is found, returning the clipped line segment or empty

Uploaded by

radekxka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 41

Line Clipping Algorithms

Cohen Sutherland

Alexej Kolcun, KIP PrF OU


Why the line must be clipped?
Why the line must be clipped?

What is the value of i and j after 2nd step?


1. char i=127, j=-128;
2. i++; j--;
3. …
Why the line must be clipped?

What is the value of i and j after 2nd step?


1. char i=127, j=-128;
2. i++; j--;
3. …

i=-128, j=127
Why the line must be clipped?

Similar situation appears when the line is drawn.


Why the line must be clipped?
Why the line must be clipped?
Why the line must be clipped?
Why the line must be clipped?
Basic Algorithm
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

Lep=A, P2, P1, P4, P3, Rep=B

Lep=Left End Point


Rep=Right End Point
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

Lep=A, P2, P1, P4, P3, Rep=B


Lp=P1, Rp=P4

Lep=Left End Point


Rep=Right End Point
Rn=Right Neighbor of A
Ln=Left Neighbor of B
Lp=Left of P1…P4 in Ω
Rp=Right of P1…P4 in Ω
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
Next example then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

Lep=Left End Point


Rep=Right End Point
Rn=Right Neighbor of A
Ln=Left Neighbor of B
Lp=Left of P1…P4 in Ω
Rp=Right of P1…P4 in Ω
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

Lep=Left End Point


Rep=Right End Point
Rn=Right Neighbor of A
Ln=Left Neighbor of B
Lp=Left of P1…P4 in Ω
Rp=Right of P1…P4 in Ω
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

P2, P1, Lep=A, P4, P3, Rep=B

Lep=Left End Point


Rep=Right End Point
Rn=Right Neighbor of A
Ln=Left Neighbor of B
Lp=Left of P1…P4 in Ω
Rp=Right of P1…P4 in Ω
1. if A,B in Ω then clip= AB. End.
2. Find P1, P2, P3, P4.
3. Reorder A, B, P1, P2, P3, P4
Basic Algorithm 4.
according to x-coord.
if non(A,B,P1,P2,P3,P4) in Ω
then clip=empty. End.
5. if A in Ω then clip=(A,Rn). End.
6. if B in Ω then clip=(Ln,B). End.
7. if A,B not in Ω then clip=(Lp,Rp).
8. End.

P2, P1, Lep=A, P4, P3, Rep=B


Rn=P4

Lep=Left End Point


Rep=Right End Point
Rn=Right Neighbor of A
Ln=Left Neighbor of B
Lp=Left of P1…P4 in Ω
Rp=Right of P1…P4 in Ω
Resume

Basic algorithm:
• Computing all four projections P1, P2, P3, P4.
• Reordering.
• Clipping.
Resume

Basic algorithm:
• Computing all four projections P1, P2, P3, P4 ← TIME DEMANDING !
• Reordering.
• Clipping.
Resume

Basic algorithm:
• Computing all four projections P1, P2, P3, P4 ← TIME DEMANDING !
• Reordering.
• Clipping.

Improvement:
• Analysis of position of A,B.
• Loop on i:
• { Find Pi.
• Put Pi into the right position.
• Clip.
• }
Resume

Basic algorithm:
• Computing all four projections P1, P2, P3, P4 ← TIME DEMANDING !
• Reordering.
• Clipping.

Improvement:
• Analysis of position of A,B.
• Loop on i: ← Not necessary 4x - exit depends on A,B,Pi.
• { Find Pi.
• Put Pi into the right position.
• Clip.
• }
Improvement:
• Analysis of position of A,B.

Cohen -Sutherland • Loop on i:


• { Find Pi.
• Put Pi into the right position.
• Clip.
• }

4-bit code of point A – C(A)


• half-plane πi is defined by boundary line pi and inner normal (arrow)
• i-th bit of code =1  A is not in πi
Improvement:
• Analysis of position of A,B.

Cohen -Sutherland • Loop on i:


• { Find Pi.
• Put Pi into the right position.
• Clip.
• }

4-bit code of point A – C(A)


• half-plane πi is defined by boundary line pi and inner normal (arrow)
• i-th bit of code =1  A is not in πi

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Improvement:
• Analysis of position of A,B.

Cohen -Sutherland • Loop on i:


• { Find Pi.
• Put Pi into the right position.
• Clip.
• }

4-bit code of point A – C(A)


• half-plane πi is defined by boundary line pi and inner normal (arrow)
• i-th bit of code =1  A is not in πi
• C(A)*C(B) -- bitwise multiplication (e.g. 1100*0100=0100; 0100*0011=0000)

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Improvement:
• Analysis of position of A,B.

Cohen -Sutherland • Loop on i:


• { Find Pi.
• Put Pi into the right position.
• Clip.
• }

4-bit code of point A – C(A)


• half-plane πi is defined by boundary line pi and inner normal (arrow)
• i-th bit of code =1  A is not in πi
• C(A)*C(B) -- bitwise multiplication (e.g. 1100*0100=0100; 0100*0011=0000)

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland
Example of non-empty clip

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland
Non-empty clip

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.

Resume: computing only 2 projections (P2, P4).


In general case 0 - 4 projections.
Cohen -Sutherland
Example of empty clip

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.
Cohen -Sutherland
Empty clip

1. Find C(B).
2. Find C(A).
3. if (C(A)=0000 and C(B)=0000) then clip=(A,B).
End.
4. if C(A)*C(B) ≠ 0000 then clip=empty. End.
5. if C(A)=0000 then swap(A,B).
6. Find intersection P with pi, where C(A)i =1.
7. A=P. GO TO 2.
8. End.

Resume: computing at most 2 projections.

You might also like