Skip to content

Commit bc2247d

Browse files
committed
fix logic for case where box specified by corners
1 parent 0802a52 commit bc2247d

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

spatialmath/base/graphics.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ def plot_box(
280280
:type wh: scalar, array_like(2), optional
281281
:param centre: centre of box, defaults to None
282282
:type centre: array_like(2), optional
283-
:param l: left side of box, minimum x, defaults to None
284-
:type l: float, optional
285-
:param r: right side of box, minimum x, defaults to None
286-
:type r: float, optional
287-
:param b: bottom side of box, minimum y, defaults to None
288-
:type b: float, optional
289-
:param t: top side of box, maximum y, defaults to None
290-
:type t: float, optional
291283
:param w: width of box, defaults to None
292284
:type w: float, optional
293285
:param h: height of box, defaults to None
@@ -309,7 +301,7 @@ def plot_box(
309301
310302
The box can be specified in many ways:
311303
312-
- bounding box which is a 2x2 matrix [xmin, xmax; ymin, ymax]
304+
- bounding box which is a 2x2 matrix [xmin, xmax, ymin, ymax]
313305
- bounding box [xmin, xmax, ymin, ymax]
314306
- alternative box [xmin, ymin, xmax, ymax]
315307
- centre and width+height
@@ -337,10 +329,7 @@ def plot_box(
337329
else:
338330
w, h = wh
339331

340-
# l - left side, minimum x
341-
# r - right side, maximuim x
342-
# b - bottom side, minimum y, top in an image
343-
# t - top side, maximum y, bottom in an image
332+
# test for various 4-coordinate versions
344333
if bbox is not None:
345334
lb = bbox[:2]
346335
w, h = bbox[2:]
@@ -364,17 +353,34 @@ def plot_box(
364353
rt = (ltrb[2], ltrb[1])
365354
w, h = rt[0] - lb[0], rt[1] - lb[1]
366355

367-
elif centre is not None:
368-
lb = (centre[0] - w/2, centre[1] - h/2)
356+
elif w is not None and h is not None:
357+
# we have width & height, one corner is enough
369358

370-
elif lt is not None:
371-
lb = (lt[0], lt[1] - h)
359+
if centre is not None:
360+
lb = (centre[0] - w/2, centre[1] - h/2)
372361

373-
elif rt is not None:
374-
lb = (rt[0] - w, rt[1] - h)
362+
elif lt is not None:
363+
lb = (lt[0], lt[1] - h)
375364

376-
elif rb is not None:
377-
lb = (rb[0] - w, rb[1])
365+
elif rt is not None:
366+
lb = (rt[0] - w, rt[1] - h)
367+
368+
elif rb is not None:
369+
lb = (rb[0] - w, rb[1])
370+
371+
else:
372+
# we need two opposite corners
373+
if lb is not None and rt is not None:
374+
w = rt[0] - lb[0]
375+
h = rt[1] - lb[1]
376+
377+
elif lt is not None and rb is not None:
378+
lb = (lt[0], rb[1])
379+
w = rb[0] - lt[0]
380+
h = lt[1] - rb[1]
381+
382+
else:
383+
raise ValueError('cant compute box')
378384

379385
if w < 0:
380386
raise ValueError("width must be positive")

0 commit comments

Comments
 (0)