bounding_boxes_intersect_2d#

regridding.geometry.bounding_boxes_intersect_2d(x_p1, y_p1, x_p2, y_p2, x_q1, y_q1, x_q2, y_q2)[source]#

Test if two bounding boxes, \(p\) and \(q\), intersect.

If the edges of the two boxes are touching, it’s counted as an intersection.

Parameters:
  • x_p1 (float) – \(x\)-coordinate of the first point of box \(p\)

  • y_p1 (float) – \(y\)-coordinate of the first point of box \(p\)

  • x_p2 (float) – \(x\)-coordinate of the second point of box \(p\)

  • y_p2 (float) – \(y\)-coordinate of the second point of box \(p\)

  • x_q1 (float) – \(x\)-coordinate of the first point of box \(q\)

  • y_q1 (float) – \(y\)-coordinate of the first point of box \(q\)

  • x_q2 (float) – \(x\)-coordinate of the second point of box \(q\)

  • y_q2 (float) – \(y\)-coordinate of the second point of box \(q\)

Return type:

bool

Examples

Create 4 boxes \(P\), \(Q\), \(R\), and \(S\). Check if boxes \(Q\), \(R\), and \(S\) intersect with box \(P\), and plot the boxes as filled if they intersect, and unfilled if they don’t intersect.

import matplotlib as mpl
import matplotlib.pyplot as plt
import regridding

# box P
x_p1 = 0
y_p1 = 0
x_p2 = 1
y_p2 = 1

# box Q
x_q1 = 1.1
y_q1 = 1.1
x_q2 = 2.1
y_q2 = 2.1

# box R
x_r1 = 1
y_r1 = 1
x_r2 = 2
y_r2 = 2

# box S
x_s1 = 0.9
y_s1 = 0.9
x_s2 = 1.9
y_s2 = 1.9

p_and_q_intersect = regridding.geometry.bounding_boxes_intersect_2d(
    x_p1=x_p1, y_p1=y_p1,
    x_p2=x_p2, y_p2=y_p2,
    x_q1=x_q1, y_q1=y_q1,
    x_q2=x_q2, y_q2=y_q2,
)

p_and_r_intersect = regridding.geometry.bounding_boxes_intersect_2d(
    x_p1=x_p1, y_p1=y_p1,
    x_p2=x_p2, y_p2=y_p2,
    x_q1=x_r1, y_q1=y_r1,
    x_q2=x_r2, y_q2=y_r2,
)

p_and_s_intersect = regridding.geometry.bounding_boxes_intersect_2d(
    x_p1=x_p1, y_p1=y_p1,
    x_p2=x_p2, y_p2=y_p2,
    x_q1=x_s1, y_q1=y_s1,
    x_q2=x_s2, y_q2=y_s2,
)

fig, axs = plt.subplots(ncols=3, sharex=True, sharey=True, figsize=(9, 3), constrained_layout=True)
axs[0].add_patch(
    mpl.patches.Rectangle(xy=(x_p1, y_p1), width=x_p2 - x_p1, height=y_p2 - y_p1, fill=p_and_q_intersect)
)
axs[0].add_patch(
    mpl.patches.Rectangle(xy=(x_q1, y_q1), width=x_q2 - x_q1, height=y_q2 - y_q1, fill=p_and_q_intersect)
)
axs[1].add_patch(
    mpl.patches.Rectangle(xy=(x_p1, y_p1), width=x_p2 - x_p1, height=y_p2 - y_p1, fill=p_and_r_intersect)
)
axs[1].add_patch(
    mpl.patches.Rectangle(xy=(x_r1, y_r1), width=x_r2 - x_r1, height=y_r2 - y_r1, fill=p_and_r_intersect)
)
axs[2].add_patch(
    mpl.patches.Rectangle(xy=(x_p1, y_p1), width=x_p2 - x_p1, height=y_p2 - y_p1, fill=p_and_s_intersect)
)
axs[2].add_patch(
    mpl.patches.Rectangle(xy=(x_s1, y_s1), width=x_s2 - x_s1, height=y_s2 - y_s1, fill=p_and_s_intersect)
)
axs[0].autoscale_view();
../_images/regridding.geometry.bounding_boxes_intersect_2d_0_0.png