Skip to content

Commit

Permalink
use height, width for Rectangle & FilledRectangle (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-Bhatia-0 authored Jun 17, 2021
1 parent 4d05a3b commit 90e8d17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
40 changes: 23 additions & 17 deletions src/rectangle.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
struct Rectangle{I <: Integer} <: AbstractShape
i1::I
j1::I
i2::I
j2::I
i_top_left::I
j_top_left::I
height::I
width::I
end

struct FilledRectangle{I <: Integer} <: AbstractShape
i1::I
j1::I
i2::I
j2::I
i_top_left::I
j_top_left::I
height::I
width::I
end

function draw!(image::AbstractMatrix, shape::Rectangle, color)
i1 = shape.i1
j1 = shape.j1
i2 = shape.i2
j2 = shape.j2
i_top_left = shape.i_top_left
j_top_left = shape.j_top_left
i_bottom_right = i_top_left + shape.height - 1
j_bottom_right = j_top_left + shape.width - 1

image[i1:i2, j1] .= color
image[i1:i2, j2] .= color
image[i1, j1:j2] .= color
image[i2, j1:j2] .= color
image[i_top_left:i_bottom_right, j_top_left] .= color
image[i_top_left:i_bottom_right, j_bottom_right] .= color
image[i_top_left, j_top_left:j_bottom_right] .= color
image[i_bottom_right, j_top_left:j_bottom_right] .= color

return nothing
end

function draw!(image::AbstractMatrix, shape::FilledRectangle, color)
image[shape.i1:shape.i2, shape.j1:shape.j2] .= color
i_top_left = shape.i_top_left
j_top_left = shape.j_top_left
i_bottom_right = i_top_left + shape.height - 1
j_bottom_right = j_top_left + shape.width - 1

image[i_top_left:i_bottom_right, j_top_left:j_bottom_right] .= color

return nothing
end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.Rectangle(2, 2, height - 1, width - 1)
shape = SD.Rectangle(2, 2, height - 2, width - 2)
SD.draw!(image, shape, true)
Test.@test image == BitArray([
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Expand All @@ -111,7 +111,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.FilledRectangle(2, 2, height - 1, width - 1)
shape = SD.FilledRectangle(2, 2, height - 2, width - 2)
SD.draw!(image, shape, true)
Test.@test image == BitArray([
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Expand Down

0 comments on commit 90e8d17

Please sign in to comment.