Skip to content

Commit fd5676b

Browse files
authored
Merge pull request #25 from JuliaGeometry/sd/additional_funcs
add some convenience functions for rects
2 parents 4221f84 + d2f8a71 commit fd5676b

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/rectangles.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,23 @@ function Base.:(*)(m::Mat{4,4,T}, h::Rect{3,T}) where T
268268
Rect{3,T}(_vmin, _vmax - _vmin)
269269
end
270270

271-
function Base.:(-)(h::Rect{N, T}, move::Vec{N}) where {N,T}
271+
Base.:(-)(h::Rect{N, T}, move::Number) where {N,T} = h - Vec{N, T}(move)
272+
Base.:(+)(h::Rect{N, T}, move::Number) where {N,T} = h + Vec{N, T}(move)
273+
274+
275+
function Base.:(-)(h::Rect{N, T}, move::StaticVector{N}) where {N,T}
272276
Rect{N, T}(minimum(h) .- move, widths(h))
273277
end
274-
function Base.:(+)(h::Rect{N, T}, move::Vec{N}) where {N,T}
278+
279+
function Base.:(+)(h::Rect{N, T}, move::StaticVector{N}) where {N,T}
275280
Rect{N, T}(minimum(h) .+ move, widths(h))
276281
end
277282

283+
284+
function Base.:(*)(rect::Rect, scaling::Union{Number, StaticVector})
285+
return Rect(minimum(rect) .* scaling, widths(rect) .* scaling)
286+
end
287+
278288
# Enables rectangular indexing into a matrix
279289
function Base.to_indices(A::AbstractMatrix{T}, I::Tuple{Rect2D{IT}}) where {T, IT<:Integer}
280290
rect = I[1]

test/geometrytypes.jl

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,36 @@ end
202202
points = decompose(Point2f0, circle, 20)
203203
@test length(points) == 20
204204

205-
# TODO circle mesh cant be 1:1 assembled from vertices + faces,
206-
# since middle point is missing...
207-
# mesh = triangle_mesh(circle, nvertices=32)
208-
# # end-1, since we add a middle point for the mesh!
209-
# @test decompose(Point2f0, mesh)[1:end-1] ≈ decompose(Point2f0, circle, 32)
205+
mesh = triangle_mesh(circle, nvertices=32)
206+
@test decompose(Point2f0, mesh)[1:end] decompose(Point2f0, circle, 32)
207+
end
208+
209+
210+
@testset "Rectangles" begin
211+
rect = FRect2D(0, 7, 20, 3)
212+
@test (rect + 4) == FRect2D(4, 11, 20, 3)
213+
@test (rect + Vec(2, -2)) == FRect2D(2, 5, 20, 3)
214+
215+
@test (rect - 4) == FRect2D(-4, 3, 20, 3)
216+
@test (rect - Vec(2, -2)) == FRect2D(-2, 9, 20, 3)
217+
218+
base = Vec3f0(1, 2, 3)
219+
wxyz = Vec3f0(-2, 4, 2)
220+
rect = FRect3D(base, wxyz)
221+
@test (rect + 4) == FRect3D(base .+ 4, wxyz)
222+
@test (rect + Vec(2, -2, 3)) == FRect3D(base .+ Vec(2, -2, 3), wxyz)
223+
224+
@test (rect - 4) == FRect3D(base .- 4, wxyz)
225+
@test (rect - Vec(2, -2, 7)) == FRect3D(base .- Vec(2, -2, 7), wxyz)
226+
227+
228+
rect = FRect2D(0, 7, 20, 3)
229+
@test (rect * 4) == FRect2D(0, 7*4, 20*4, 3*4)
230+
@test (rect * Vec(2, -2)) == FRect2D(0, -7*2, 20*2, -3*2)
210231

232+
base = Vec3f0(1, 2, 3)
233+
wxyz = Vec3f0(-2, 4, 2)
234+
rect = FRect3D(base, wxyz)
235+
@test (rect * 4) == FRect3D(base .* 4, wxyz .* 4)
236+
@test (rect * Vec(2, -2, 3)) == FRect3D(base .* Vec(2, -2, 3), wxyz .* Vec(2, -2, 3))
211237
end

0 commit comments

Comments
 (0)