Friday, April 25, 2014

Find rectangular intersection

Two rectangles to find the intersection:

Rectangle 1:
left_bottom point(R1_x1,R1_y1) | right_top point(R1_x2,R1_y2)

Rectangle 2:
left_bottom point(R2_x1,R2_y1) | right_top point(R2_x2,R2_y2)

Intersection:
x_overlap = Math.max(0, Math.min(R1_x2,R2_x2) - Math.max(R1_x1,R2_x1))
y_overlap = Math.max(0, Math.min(R1_y2,R2_y2) - Math.max(R1_y1,R2_y1));

return x_overlap*y_overlap;

1 comment: