Cohen-sutherland algorithm (coding algorithm)

Source: Internet
Author: User
Tags bitwise

Transferred from: http://my.oschina.net/liqiong/blog/4921

Cohen-sutherland algorithm ( coding Algorithm )

Basic idea: For each line segment P1P2, divided into three kinds of situation processing:

(1) If the P1P2 is completely inside the window, the line segment is displayed, referred to as "take";

(2) If the p1p2 obvious outside the window, then discard the line segment, referred to as "abandoned";

(3) If the line segment neither satisfies the "take" condition, also does not satisfy "abandons" the condition, then divides the line segment into two paragraphs. One section is completely outside the window and can be discarded. Then repeat the above treatment for the other paragraph.

In order to enable the computer to quickly determine the relationship between a line segment and the window, the following encoding method: The boundary of the window is extended to a straight line, the window platform is divided into 9 partitions, each zone is set to a 4-bit encoding and corresponding.

The endpoints of each line on the plane can define two encodings based on the region in which they are located.

The meaning of the encoding (given in binary form from right to left) is as follows:

(1) No. 0 bit: 1 If the endpoint is to the left of the left edge of the window, otherwise 0;

(2) 1th bit: 1 If the endpoint is to the right of the right edge of the window, otherwise 0;

(3) 2nd bit: 1 If the endpoint is below the bottom edge of the window, otherwise 0;

(4) 3rd bit: 1 If the endpoint is on the top edge of the window, otherwise 0.

When clipping a line segment, the encoding Code1 and Code2 for the endpoint P1 and P2 are first calculated:

(1) If both Code1 and Code2 are 0, then the P1 and P2 are in the window, then the line is all inside the window, should be taken. (c)

(2) If Code1 and Code2 after the bitwise AND operation results Code1&code2 not equal to 0, indicating that P1 and P2 at the same time above the window, the lower, the left or the right, then the line is all in the outside of the window, should be discarded. (e,d)

(3) If the above two conditions are not established, it can be treated as follows: To find the intersection of the line segment and the window boundary, at the intersection of the segment divided into two, which must have a section completely outside the window, you can discard it. Repeat this process for the other paragraph until the segment is completely discarded or a segment is located within the window. (A,b,d ')

This method is intuitive, convenient and fast. But:

(1) Because of the use of bitwise AND operation, the implementation of the program has special requirements;

(2) The judgment of all abandonment is only suitable for segments that are only on the same side of the window (left, right, or up, or down).

Features of the algorithm:

(1) Features: The coding method can quickly determine the line segment-completely visible and obviously invisible.

(2) Special application Two kinds of occasions:

Large window occasions;

The window is particularly small (for example, when the cursor picks up a graphic, the cursor is considered a small cropping window)

Example: P1p2 (p1[80,75],p2[-40,0]) is clipped according to the area code clipping algorithm, illustrating the cropping process (known as: xl=0,xr=100,yb=0,yt=50).

Cohen-sutherland algorithm (coding algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.