Algorithm Description:
The basic starting point of the Liang_barsky algorithm is the parametric equation of the straight line. Give any straight line segment, the two ends of the points are and, so, the linear equation is:
If any point in the line is inside the window, the following relationships must be met:
The above inequalities can be expressed as:
where p and q are defined as:
If any line is parallel to a clipping boundary, then the subscript k corresponds to the window boundary parallel to the line segment (and represents the left, right, bottom, and top bounds of the cropping window, respectively). If for a certain k value, satisfies, then the straight line is completely outside the window, can discard. If the line is inside its parallel window boundary, further calculations are required to determine whether the line is inside, outside, or intersecting the window. At that time, the line was extended from the outside of the clipping boundary to the interior, and if so, the line was extended from the inside of the clipping boundary to the outside. For pk≠0, the parameter U of the intersection of the line and the boundary K can be calculated using the formula.
For each line, you can calculate the parameter value of the line in the inner segment of the cropping window.
Values are determined by the window boundaries that make the line extend from outside to inside.
For these edges, calculate. Value and the maximum value in the set of 0.
Values are determined by the bounds of the window that makes the line extend from inside to outside.
Calculates the minimum value in the set consisting of 1. If the line is completely outside the window, you can simply discard it, otherwise the end of the cropped segment is calculated based on the two values of the parameter U.
Detailed code: Computer Graphics-code_1
Build Result:
Computer Graphics-Segment clipping-Liang Barsky algorithm (Liang Youdong algorithm)