performance - Performant intersection algorithm for Bounding Box with a Line Segment emanating from its center point -
there many close, helpful answers similar questions listed on stackoverflow, have not found yet match particular case.
i need performance efficient algorithm calculate intersection of bounding box line segment emanating center point. each bounding box may have multiple line segment emanations.
by definition of problem, each line segment intersect 1 (and except 4 points one) of bounding box edge segments.
here illustration.

i want , computationally "cheaply" compute:
which bounding box edge line segment intersects?
what point @ edge , line segment intersects?
thanks.
let c center of bounding box , p other endpoint of segment. let v = (vx, vy) = p - c vector pointing c p.
let bounding box have height = 2 * h , width = 2 * w. w , h half of normal height , width, make computations below simpler.
here idea: consider case v in first quadrant, vx > 0 , vy > 0. segment intersects top side or right side. can tell 1 comparing slope of v , slope of segment center of box top right corner. if slope larger, intersects top , otherwise intersects side.
so, if v in first quadrant, , if vy / vx > h / w segment intersects top. if vy / vx < h / w, intersects side. if equal, intersects corner.
once know side intersects, can use similar triangles figure out point of intersection. if intersects top @ point = (ix, h), vx / vy = ix / h or ix = h * vx / vy. if intersects side @ point = (w, iy) vy / vx = iy / w or iy = w * vy / vx.
the same methods work in other quadrants; need keep track of signs of vx , vy. can make life bit easier asserting return value satisfies sign(ix) == sign(vx) , sign(iy) == sign(vy).
note that, if careful signs, can avoid division: vy / vx > h / w equivalent vy * w > vx * h, assuming vx > 0.
Comments
Post a Comment