def clip_segment(start_point, end_point):
    """Return points for clipped segment.

    Parameters
    ==========
    start_point (iterable with two real numbers)
        represents the starting point of the line segment.
    end_point (iterable with two real numbers)
        represents the ending point of the line segment.

    Uses the Cohen-Sutherland algorithm to obtain the
    points for the clipped segment. If the segment doesn't
    touch the clipping area, a ValueError is raised.
    """
    ### reference individual coordinates of each point

    x1, y1 = start_point
    x2, y2 = end_point

    ### compute region codes for start_point, end_point

    code1 = get_region_code(x1, y1)
    code2 = get_region_code(x2, y2)

    ### calculate the new coordinates for the start and
    ### end points of the clipped segment

    while True:

        ### act based on one of the 03 possible scenarios:

        ## (1) if both points lie within the clipping area,
        ## that is, the bitwise OR of regions of two end
        ## points of the segment is 0, it means both points
        ## are inside the rectangle, so we can return them
        ## immediately

        if code1 == 0 and code2 == 0:

            start_point = x1, y1
            end_point = x2, y2

            return start_point, end_point

        ## (2) both points share at least one outside
        ## region, which implies that the segment does not
        ## cross the clipping area, so we raise a ValueError;
        ## (bitwise AND of endpoints != 0)

        elif (code1 & code2) != 0:
            raise ValueError("No collision detected")

        ## (3) otherwise, we must perform a clipping
        ## operation, that is, we must recalculate the
        ## coordinates of the point(s) of the segment
        ## so the final coordinates form the part of the
        ## segment which is inside the clipping area

        else:

            ## since the code for the points didn't pass
            ## the test for any of the previous scenarios
            ## (1 and 2), we know that at least one of the
            ## points is outside the rectangle, that is,
            ## its region code is different from 0, so if
            ## the code for the start point (code1) isn't
            ## 0 we pick it, otherwise we assume the code
            ## for the end point (code2) is the one
            ## different from 0;
            code_out = code1 if code1 != 0 else code2

            ## now find the intersection point using the
            ## following formulas, depending on the region
            ## of the point
            ##
            ##  slope = (y2 - y1) / (x2 - x1)
            ##
            ##  x = x1 + (1 / slope) * (ym - y1)
            ##  (where ym is y_min or y_max)
            ##
            ##  y = y1 + slope * (xm - x1)
            ##  (where xm is x_min or x_max)
            ##
            ## also, there's no need to worry about
            ## ZeroDivisionError because, in each case, the
            ## outcode bit being tested guarantees the
            ## denominator is non-zero

            # point is above the clipping area

            if code_out & TOP:

                x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1)
                y = y_max

            # point is below the clipping area

            elif code_out & BOTTOM:

                x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1)
                y = y_min

            # point is to the right of the clipping area

            elif code_out & RIGHT:

                x = x_max
                y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1)

            # point is to the left of the clipping area

            elif code_out & LEFT:

                x = x_min
                y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1)

            ## depending on which point you picked, update
            ## the coordinates of the corresponding point
            ## with the new coordinates calculated, and
            ## recalculate the region code of this new
            ## point

            if code_out == code1:

                x1, y1 = x, y
                code1 = get_region_code(x1, y1)

            else:

                x2, y2 = x, y
                code2 = get_region_code(x2, y2)
Return points for clipped segment.

    Parameters
    ==========
    start_point (iterable with two real numbers)
        represents the starting point of the line segment.
    end_point (iterable with two real numbers)
        represents the ending point of the line segment.

    Uses the Cohen-Sutherland algorithm to obtain the
    points for the clipped segment. If the segment doesn't
    touch the clipping area, a ValueError is raised.
    
nodezator/graphman/socketparenthood/utils.py