This is the fifth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

1 the introduction

Cohen-sutherland algorithm is one of the most famous line clipping algorithms, which is a clipping method based on code

Based on the linear clipping algorithm, this paper analyzes, applies and generalizes the linear clipping in different situations in the two-dimensional coordinate system

2 ideas

The overall idea flow chart is as follows:

  • Reduction and reduction are two relatively simple cases, and the pruning of complex cases has several problems to pay attention to:

    • For the case of two intersection points, the line of two points after the calculation of intersection points can be obtained after cutting the straight line
    • In the case of one intersection point, one end of the “new” line must be in the clipping box, and the other end must determine the x (or y) coordinate of the rectangle boundary and the x (or Y) coordinate of the external end of the original line
  • About the calculation of intersection, the idea is as follows:

    1. First, construct a zero matrix with four rows and three columns
    2. According to the coding values of the four directions, the x and y coordinates are obtained.
    3. The third column is used to record whether the intersection is a real intersection, which is 1 in real and 0 in virtual
    4. Finally, the matrix is used as the return value

3 process

3.1 Main function Cohen.m

First draw a straight line, crop the box, and calculate the two endpoint codes

The simplification, simplification and complexity are judged respectively

% Jane abandon
if code1(1)+code2(1) >1||code1(2)+code2(2) >1||code1(3)+code2(3) >1||code1(4)+code2(4) >1
    figure('Name'.'Jane leave')
    quyu(x1,x2,y1,y2);
% Jane take
elseif code1(1) = =0&&code1(2) = =0&&code1(3) = =0&&code1(4) = =0&&code2(1) = =0&&code2(2) = =0&&code2(3) = =0&&code2(4) = =0
    figure('Name'.'Jane take')
    hold on;
    plot(xx,yy);
    quyu(x1,x2,y1,y2);
    hold off;
Copy the code

Finally, draw the clipped line in a new window for easy comparison with the original figure

3.2 To find the function of intersection jiaodian.m

The intersections are first obtained and recorded

jd=zeros(4.3);
% o intersection
k=(dy2-dy1)/(dx2-dx1); % Line slope
yzuo=k*(x1-dx1)+dy1;
jd(1.1)=x1; jd(1.2)=yzuo;
Same for other directions
Copy the code

Find the larger and smaller coordinates of the end point of a line

% find the larger and smaller coordinates of the end point of a linexx=[dx1 dx2]; yy=[dy1 dy2]; xmax=max(xx); ymax=max(yy);
xmin=min(xx); ymin=min(yy);
Copy the code

Then the left, right, down and up four cases to judge whether the intersection is real

3.3 Find the encoding function code.m

Code=zeros(1.4);
if dx<x1
    Code(1.4) =1;
elseif dy<y1
    Code(1.2) =1;
% and so on
end
Copy the code

3.4 Get the clipping box function quyu.m

By default, x1,y1 is smaller than x2,y2, otherwise an error will be thrown: x1 is the left-hand line and cannot be greater than x2

3.5 Rectangle drawing function juxing.m

I wrote a function to draw a rectangle. A rectangle can be seen as a combination of four straight lines

4 the result

Due to a large number of clipping cases, this section only shows the case where the line and window have 1 and 2 intersection points. For more examples and complete codes, please refer to Cohen(gitee.com)

4.1 One Intersection (left intersection)

4.2 two points of intersection (upper right intersection)