[OpenGL] computer graphics: Cohen-Sutherland algorithm and Liang-Barsky algorithm in the linear cropping Algorithm

Source: Internet
Author: User

Today, we are going to systematically learn two linear cropping algorithms.

First, we will introduce the basic principle of linear cropping:

As shown in the following figure, there may be several relationships between the straight line and the window boundary.

You can determine how to crop the line by checking whether the two endpoints of the line are in the window.

Before cropping:

After reduction:

In, if the two endpoints of the persistent line are within the window boundary (a straight line between P5 and P6 in 1), the line should be retained.

If one endpoint of a straight line is outside the window (such as P9) and another point is in the window (such as p10), the line segment outside the boundary should be cropped from the intersection of the line and the boundary (P9.

If the two endpoints of a straight line are all outside the boundary, there are two possible cases: one is that the line is all outside the window;

In another case, a straight line passes through two window boundaries. The straight lines from P3 to P4 in the figure belong to the previous situation and should all be cropped out;

The straight line from P7 to P8 belongs to the latter case. The lines from P7 to P8 should be retained, and the remaining parts should be cropped out.


The linear cropping algorithm should first determine which straight lines are retained or all cropped, and the rest is the partially cropped straight lines.

For Partially cropped straight lines, the intersection of these straight lines and the window boundary is first required, and the portion starting from the intersection is cropped out of the boundary.

A complex image may contain thousands of straight lines. To improve the algorithm efficiency and speed up the cropping, a small amount of calculations should be used to calculate the intersection of the straight line and window boundary.


The following describes the Cohen-Sutherland linear cropping algorithm.

The general idea of the Cohen-Sutherland algorithm is that for each line segment P1P2, there are three solutions.

① If the two endpoints of the straight line P1P2 are completely in the window, the P1P2 line is displayed.

② If the two endpoints of the straight line P1P2 are obviously outside the window, the line segment is discarded.

③ If a line segment does not meet the condition of "Fetch" or "discard", the line segment is divided into two sections. A section is completely out of the window and can be discarded. Repeat the preceding processing for another segment.


1. Zone code and its establishment

The core of the Cohen-Sutherland linear cropping algorithm is to allocate a four-bit binary code to the endpoints of all straight lines to indicate their relative positions. This code is called a partition code.

The area code is encoded according to the relative position between the endpoint and the window boundary. That is, the four digits of the area code indicate that the endpoint is located at the top, bottom, left, and right of the window respectively.

The coordinates represented by the area code from right to left are as follows:

Bits 4 3 2 1

Upper, bottom, right, left

If one of the above values is 1, The point is in this coordinate zone.

Shows the area codes of the coordinates around the window.


It can be seen that the dot in the window should have a zone code of 0000,

In the lower-left corner of the window, the zone code should be 0101, and so on.

The values of the area codes can be obtained by comparing the endpoints coordinates (x, y) with the window boundary.

If x <xwmin, the first region code is 1, and the rest are similar.

Currently, all computer languages can perform bit operations. Therefore, you can follow these steps to create a region code:

① Calculate the difference between the endpoint coordinate and the window boundary.

② According to the calculated symbols of each difference, the corresponding location of the zone code is 0 or 1, that is

The first position of the region code is the symbol bit of (x-xwmin;

The second position of the region code is the symbol bit of (xwmin-x;

The third position of the region code is the symbol bit of (Y-ywmin;

The fourth position of the region code is the symbol bit of (ywmin-Y.

2. area code pruning algorithm

After a zone code is set for the endpoints of all straight lines, you can determine whether the straight line is within or outside the window by zone code. This can be divided into the following situations:

① If the two endpoints of a straight line have a zone code of 0000, the line should be kept within the window boundary.

② If the same bit of the two endpoints of a straight line is 1 at the same time, all the lines are located outside the window boundary and must be cropped.

For example, if the region code of one endpoint is 1001 and that of the other is 0101, the first region code of the two ends is 1, the two vertices are on the left of the window boundary,

Therefore, the straight line must be cropped out of the window boundary. You can perform the "and" (&) operation on the Zone codes of the two endpoints of a line to determine whether the line is outside the window,

If the result of the "and" operation is 0000, the domain code of the two ends is not 1 at the same time, and this line is not necessarily cropped.

③ In the above two cases, a straight line may pass through the window or not through the window.

This type of line can be processed as follows: compare an endpoint outside the window with the window boundary to determine which part of the line can be ruled out,

Then compare the remaining parts of the line with other boundaries until the line is completely excluded or the part of the line is determined within the window.

You can create an algorithm to check the relationship between the line endpoint and the window boundary in the order of "Left, right, bottom, and top.



The following describes the Liang-Barsky linear cropping algorithm.

The Liang (Liang youdong)-Barsky algorithm is also called the parameter equation method. First, write the parameter equation of the endpoint and the link between them as follows:


 


The value of the U parameter can be 0 to 1. The coordinate indicates a point in a straight line defined by the U value in this range.

When u = 0, (x, y) = (x1-y1), the other end of the line U = 1, (x, y) = (x2-y2 ).

We found that if a point in a straight line is in the window defined by (x, y), namely (xmin, ymin) and (xmax, Ymax), the following conditions are met:

These four inequalities can be expressed:

The parameter is defined:



According to the relative position of the straight line and the window boundary, it can be divided into the following Qian Kuang.

① Any straight line parallel to the window boundary, whose PK = 0. Here, the K value indicates which boundary (k =, 3 and 4, corresponding to the left, right, bottom, and upper boundary respectively ).

If the qk value is less than 0 for a certain K value, the straight line is completely out of the Boundary. If the qk is greater than or equal to 0, the straight line parallel to the boundary is within the boundary.

② If qk is less than 0, the situation is more complicated. In this case, you can take the line from (x1, Y1) to (X2, Y2) as the forward direction, and extend the line infinitely while extending the border of each window.


The following two cases are discussed:

When qk is less than 0, the infinite extension line from a straight line outside the window enters the interior of the infinite extension line at the corresponding window boundary.

B. When qk is greater than 0, the opposite is true. That is, the extension line of a straight line is from the interior of the window border extension line to the exterior.

Take the line L1 in Figure 3 as an example to describe the above two cases. Table 1.1 shows the direction and qk value of the L1 extension line relative to the four border extension lines.


When qk is less than 0, a parameter U can be calculated using the following formula. The U value must be at the intersection of the line extension line and the window border K:

For each straight line, a pair of u values (U1 and U2) can be calculated. The two parameters can be used to determine how a straight line is cropped.

The value of U1 can be determined by the window boundary involved in the straight line (Pk <0) that is sent out of the window boundary.

For these window boundaries, the respective r values can be calculated:


The maximum value in each rk is U1. U2 can be determined by the window boundary involved in the straight line (PK> 0) that is sent out from the window boundary to the window boundary,

You can also calculate the rk values of these window boundaries. The minimum value of each rk is u2.

If U1> u2, the straight line is excluded from the window. Otherwise, the straight line is located in the window, and the endpoint of the specific cropping line can be calculated by U1 and U2.

Figure 3 shows the intersection of the L1 and L2 lines corresponding to U1 and U2 and the boundary.

If U2> U1 in L1 is used, L1 is cropped and the cropping point can be calculated by U1 and U2;

L2 U1> u2, then this line is all exclusive.

To sum up the Liang-Barsky algorithm:

1. Basic Idea of Algorithms

Based on the linear parameter equation, the corresponding parameter values are obtained for different cropping conditions.

2. algorithm derivation process

Case 1 PK = 0

(1) P1 = P2 = 0

If Q1 <0 or Q2 <0, you can delete the line segment

If Q1> = 0 and Q2> = 0, further judge

U = qk/PK (k = 3, 4)

Set U1 = max (0, u | PK <0)

U2 = min (1, u | PK> 0)

If U1> u2, you can delete the line segment.

If U1 <= U2, place U1 and U2 into the linear equation to obtain two visible endpoints of the linear segment.

(2) P3 = P4 = 0

If Q3 <0 or Q4 <0, you can delete the line segment

If Q3> = 0 and Q4> = 0, further judge

U = qk/PK (k = 1, 2)

Set U1 = max (0, u | PK <0)

U2 = min (1, u | PK> 0)

If U1> u2, you can delete the line segment.

If U1 <= U2, place U1 and U2 into the linear equation to obtain two visible endpoints of the linear segment.

Case 2 PK is not 0

U = qk/PK (k = 1, 2, 4)

U1 = max (0, u | PK <0, u | PK <0)

U2 = min (1, u | PK> 0, u | PK> 0)

If U1> u2, you can delete the line segment.

If U1 <= U2, place U1 and U2 into the linear equation to obtain two visible endpoints of the linear segment.

3. algorithm steps

(1) coordinates of the two ends of the input line segment and the four boundary coordinates of the window.

(2) If △x = 0, P1 = P2 = 0. Determine whether Q1 <0 or Q2 <0 is satisfied. If yes, the line segment is not in the window and is switched to (7 ). Otherwise, if Q1> 0 and Q2> 0 are met, u1 and U2 are further calculated. Switch (5 ).

(3) If △y = 0, P3 = P4 = 0. Determine whether Q3 <0 or Q4 <0 is satisfied. If yes, the line segment is not in the window and is switched to (7 ). Otherwise, if Q1> 0 and Q2> 0 are met, u1 and U2 are further calculated. Switch (5 ).

(4) If neither of the above two conditions is met, then PK =0 (k = 1, 2, 4 ). Calculate U1 and U2.

(5) After obtaining U1 and U2, make a judgment: If U1> u2, the straight line segment is outside the window and is switched to (7 ). If U1 <U2, use the linear parameter equation to obtain the coordinate of the two ends of the linear segment in the window.

(6) use the scanning and conversion algorithm of a straight line to draw a straight line segment in a window.

(7) The algorithm ends.


Related Article

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.