clip_line - arnthor89/pygame GitHub Wiki

Link to funciton

Description

The purpose of clip_line is to detect if a line segment cuts a given bounding box. The function calculates the coordinates of the part of the line segment within the bounding box. The line and the box(defined by left, top, right, bottom) are passed in as arguments to the function. The function returns true if the line segment cuts the bounding box (false otherwise) and fills in an list with the end points of the line that clips the box.

Complexity

The high CC is not really connected to how complex the method is. The main reason for the high CC is that the method is sprinkled with if-statements. They are used to check how the coordinates of the line and borders of the box relate. As the function also calculates what part of the line cuts the box, it increases the complexity.

Refactoring strategy

One way to simplify the method would be to moce out the part of the function that calculates the line segments that clips the box. The lines 170-183 could be moved out to a separate function. This would decrease the CC by ~6.