Point inside 2D axis aligned rectangle, no branches
问题 I'm searching for the most optimized method to detect whether a point is inside an axis aligned rectangle. The easiest solution needs 4 branches (if) which is bad for performance. 回答1: Given a segment [x0, x1] , a point x is inside the segment when (x0 - x) * (x1 - x) <= 0 . In two dimensions case, you need to do it twice, so it requires two conditionals. 回答2: Consider BITWISE-ANDing the values of XMin-X, X-XMax, YMin-Y, Y-YMax and use the resulting sign bit. Will work with both ints and