Skip to content

Commit a257769

Browse files
committed
make Rects have positive widths
1 parent b9d91a1 commit a257769

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/primitives/rectangles.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Formally it is the Cartesian product of intervals, which is represented by the
99
struct HyperRectangle{N,T} <: GeometryPrimitive{N,T}
1010
origin::Vec{N,T}
1111
widths::Vec{N,T}
12+
13+
function HyperRectangle{N, T}(origin::VecTypes, widths::VecTypes) where {N, T}
14+
return new{N, T}(Vec{N, T}(min.(origin, origin .+ widths)), Vec{N, T}(abs.(widths)))
15+
end
1216
end
1317

1418
##
@@ -288,12 +292,7 @@ end
288292
# return vmin, vmax
289293
# end
290294

291-
function positive_widths(rect::Rect{N,T}) where {N,T}
292-
mini, maxi = minimum(rect), maximum(rect)
293-
realmin = min.(mini, maxi)
294-
realmax = max.(mini, maxi)
295-
return Rect{N,T}(realmin, realmax .- realmin)
296-
end
295+
positive_widths(rect::Rect{N,T}) where {N,T} = rect
297296

298297
###
299298
# set operations

test/geometrytypes.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ end
7878

7979
@testset "HyperRectangles" begin
8080
@testset "Constructors" begin
81-
# TODO: Do these actually make sense?
82-
# Should they not be Rect(NaN..., 0...)?
8381
@testset "Empty Constructors" begin
8482
function nan_equal(r1::Rect, r2::Rect)
8583
o1 = origin(r1); o2 = origin(r2)
@@ -192,6 +190,11 @@ end
192190
@test constructor(m) Rect3f(-1, -1, -1, 2, 2, 2)
193191
end
194192
end
193+
194+
r = Rect2f(10, 10, -5, -5)
195+
@test origin(r) == Point2f(5)
196+
@test widths(r) == Vec2f(5)
197+
@test maximum(r) == Point2f(10)
195198
end
196199

197200
r = Rect2f()

0 commit comments

Comments
 (0)