liangbarsky incorrect

Issue #101 new
schneems
created an issue

When called with the following arguments:

liangbarsky(-0.25, 2.25, 1.25, 0.75, 0.5, 2.5, 0.5, 0.5)

I expect the line to intersect the box, however None, None, None, None is returned. I'm not totally sure where the bug is, for my needs all i wanted was a boolean yes or no to if the line intersected the box. I modified your code and made some changes based on outside resources and this is what I came up with:

dx = x2 - x1 * 1.0
dy = y2 - y1 * 1.0
t0 = 0.0
t1 = 1.0

checks = ((-dx, -(left - x1)),
          ( dx, right - x1),
          (-dy, -(bottom - y1)),
          ( dy, top - y1))

for p, q in checks:
    if p == 0 and q < 0:
        return False

    if p != 0:
        r = q / (p * 1.0)

        if p < 0:
            if r > t1:
                return False
            elif r > t0:
                t0 = r
        else:
            if r < t0:
                return False
            elif r < t1:
                t1 = r
return True

I reproduced the algorithm from the C code from http://www.skytopia.com/project/articles/compsci/clipping.html#alg

Comments (0)

  1. Log in to comment