Cohen-Sutherland line segmentation algorithm learning and detailed explanation

Cohen-Sutherland line segmentation algorithm learning and detailed explanation

【Two-dimensional line cutting】

The line cutting algorithm uses a series of tests and intersection calculations to determine whether the entire line segment or a portion of it can be saved. The intersection of the line segment and the window boundary calculates the time-consuming part of the line clipping function. Therefore, reducing the intersection calculation is the main goal of any line cropping algorithm.

To this end, we test before cutting to determine whether the line segment is completely inside or outside the cropping window. It is easy to determine that a line segment is complete inside the window, but it is more difficult to determine that a line segment is completely lower outside the window.

If you cannot determine whether a line segment is completely inside or outside the window, you must determine whether a part of the line segment falls inside the window by calculating the intersection point.

【Introduction to Cohen-Sutherland Algorithm】

Cohen-Sutherland line segmentation algorithm is a fast line segmentation algorithm that has been widely used. The algorithm reduces the calculation of intersection points through initial testing, thereby reducing the time used by the line segmentation algorithm.

The algorithm assigns a four-bit binary code called an area code to the endpoint of each line segment . Each bit is used to identify whether the endpoint is inside or outside of the corresponding crop window boundary.

The following is a schematic diagram of a four-bit binary code. In general, it is numbered 4321 from left to right, corresponding to the top, bottom, right, and left of the crop window (note that it is not up, down, left, and right).

Each boundary of the crop window divides the two-dimensional space into two spaces inside and outside the window. The four window boundaries (the line where the boundary lies) divide the plane into nine areas, as shown in the figure below.

Endpoints in nine different areas will be given different area codes. For example, the end point of the area in the lower left corner of the crop window is assigned an area code of 0101, while the area code of the end point in the crop window is 0000.

The bit value of the area code is determined by comparing the coordinates of the endpoint (x, y) with the boundary of the crop window. If x <xmin, then position 1 is 1, as in the area with area codes 1001, 0001, and 0101 in the figure above. Others are similar.

In addition to using the inequality test, we can also use the bit processing operation and the following two-step operation to determine the value of the area code more efficiently:

(1) Calculate the difference between the coordinates of the end point and the cutting edge.

(2) Use the sign bit calculated by each difference to set the corresponding value in the area code.

In the order of the above binary code diagram, bit 1 is set to the sign bit of x-xw min ; bit 2 is set to the sign bit of xw min -x; bit 3 is set to the sign bit of bit y-yw min ; bit 4 is set to yw min -y sign bit.

[Cohen-Sutherland algorithm description]

Once the area codes have been established for all line segment endpoints, you can quickly determine which line segment is completely inside the crop window and which line segment is completely outside the window.

① The line segment is completely within the window: the area codes of both endpoints of the line segment are 0000, and such a line segment is reserved.

② The line segment is completely outside the window: a pair of the same position in the area codes of the two endpoints of the line segment is 1, and such line segments are discarded.

For example: the area code of one end point of the line segment is 1001, and the area code of the other end point is 0101, then this line segment is discarded, because both end points of this line segment are on the left side of the crop rectangle, the first bit of the end point area code Are all 1.

The judgment of whether the line segment is inside or outside the window in the program is to perform a logical OR operation on the area codes of the two endpoints . If the operation result is 0000, the line segment is completely within the crop window, keep this line segment and test the next line segment. If the result of the logical AND operation of the area codes of the two endpoints is true (not 0000), it means that the line segment is completely outside the window, and this line segment is discarded.

③ For the line segment that cannot be judged to be completely outside or inside the window, the intersection point with the window boundary should be tested.

These line segments may or may not pass through the inside of the window. Therefore, it may take multiple intersection operations to complete the cutting of a line segment. The number of intersections depends on the order of selecting the cutting boundary. After processing a cropped window boundary each time, part of it is cut off, and the rest is checked against the other boundary of the window. This process continues until the line segment is completely cut off or the remaining line segment is completely within the cropping window.

Below we discuss the two line segments in the figure below that cannot be judged immediately inside / outside the window. Assuming that the window boundary processing order is left, right, bottom, and top , to check whether a line segment intersects with a cropping boundary, you can check the corresponding bits of the area code at both ends of the line. If one of them is 1 and the other is 0, the line segment and the boundary intersect.

As shown above, P1P2 and P3P4 are two line segments that cannot be immediately determined whether they are inside or outside the window. First deal with P1P2:

1. Left border processing:

The area codes of the line segment P1 to P2 are 0100 and 1001. Therefore, P1 is inside the left boundary and P2 is outside the left boundary.

Next, calculate the intersection point P2 ', and crop the part from point P2 to P2'.

2. Right border processing:

The remaining line segment is located inside the right boundary and does not need to be processed.

3. Lower boundary processing:

P1 is below the lower boundary; and P2 'is above the lower boundary, so find the intersection point P1' on this boundary, and clear the part of P1 to P1 '.

4. Upper boundary processing:

Determine the position of the upper boundary intersection point P2 '', crop the part above the upper boundary and save the inner segment from P1 'to P2' '.

Then process P3P4:

For the line segment P3P4, P3 is outside the left boundary, and P4 is inside the left boundary. Therefore, calculate the intersection point P3 'and clear the line segment from P3 to P3'.

By testing the area codes of the endpoints P3 'and P4, the remaining line segments are all below the crop window, so they are cleared.


References:
1. "Computer Graphics: Third Edition" Cai Shijie, etc. Translated Electronic Industry Press 2010.7

                                </div>
Published 16 original articles · Like1 · Visits 180

Guess you like

Origin blog.csdn.net/weixin_44931542/article/details/105562658
Recommended