Skip to content

Commit

Permalink
reuse Point in other shapes (#20)
Browse files Browse the repository at this point in the history
* reuse Point in other shapes

* update tests

* update README
  • Loading branch information
Sid-Bhatia-0 authored Oct 12, 2021
1 parent 25ef4de commit cedbc14
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 67 deletions.
48 changes: 20 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ This is a lightweight package that provides fast drawing methods for some simple
import SimpleDraw as SD

# create a canvas (could be any AbstractMatrix)
image = falses(17, 17)
image = falses(16, 16)

# create the shape
shape = SD.Line(5, 2, 13, 16)
shape = SD.Line(SD.Point(5, 2), SD.Point(12, 15))

# we will draw on the boolean image with the "color" true
color = true
Expand All @@ -45,7 +45,7 @@ SD.draw!(image, shape, color)
SD.visualize(image)
```

<img src="https://user-images.githubusercontent.com/32610387/123078332-7d471680-d438-11eb-9216-0f0b41efdbd6.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005406-6b9db65f-1a14-4008-85b8-92db93a02ad1.png" width = "400px">

## Notes

Expand Down Expand Up @@ -89,99 +89,91 @@ The `visualize` function helps visualize a binary image inside the terminal usin
end
```

<img src="https://user-images.githubusercontent.com/32610387/136922414-8af316f3-d4bb-4a46-a3bc-434d50748d27.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005163-a5ef6a22-3888-4005-bf98-fbe93c6342de.png" width = "400px">

1. ### `Background`

```julia
struct Background <: AbstractShape end
```

<img src="https://user-images.githubusercontent.com/32610387/136918547-21c71eba-fcb8-4f8c-8261-9be0fa2410b4.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005363-9434627a-e3fb-4377-a955-1acebf337d09.png" width = "400px">

1. ### `Line`

```julia
mutable struct Line{I <: Integer} <: AbstractShape
i1::I
j1::I
i2::I
j2::I
point1::Point{I}
point2::Point{I}
end
```

<img src="https://user-images.githubusercontent.com/32610387/123078332-7d471680-d438-11eb-9216-0f0b41efdbd6.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005406-6b9db65f-1a14-4008-85b8-92db93a02ad1.png" width = "400px">

1. ### `Circle`

```julia
mutable struct Circle{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/123078423-95b73100-d438-11eb-8329-546982bbb00c.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005414-1691f633-4ab5-441a-8308-c04b4791ff8a.png" width = "400px">

1. ### `FilledCircle`

```julia
mutable struct FilledCircle{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/123078474-a2d42000-d438-11eb-88cf-d0635380a21f.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005436-70d662f8-4182-4ba7-8c9b-d7eeec51ee3b.png" width = "400px">

1. ### `Rectangle`

```julia
mutable struct Rectangle{I <: Integer} <: AbstractShape
i_top_left::I
j_top_left::I
top_left::Point{I}
height::I
width::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/123078509-ac5d8800-d438-11eb-814f-b7fa32857878.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005464-c4efba73-9e30-4626-b05c-87fad58542cb.png" width = "400px">

1. ### `FilledRectangle`

```julia
mutable struct FilledRectangle{I <: Integer} <: AbstractShape
i_top_left::I
j_top_left::I
top_left::Point{I}
height::I
width::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/123078547-b67f8680-d438-11eb-94be-af77c473d0e9.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005489-481c84a8-bc92-49b8-9cbd-1f9266d4cbae.png" width = "400px">

1. ### `Cross`

```julia
mutable struct Cross{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/136918598-ba83ae87-a514-42cb-9d78-01b1aa7135aa.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005504-c7776d6d-d94c-4651-b2ac-e88d01f0ffea.png" width = "400px">

1. ### `HollowCross`

```julia
mutable struct HollowCross{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end
```

<img src="https://user-images.githubusercontent.com/32610387/136918626-e983bcb1-b722-4ba7-a449-fee5a198d40e.png" width = "400px">
<img src="https://user-images.githubusercontent.com/32610387/137005548-d5bdb2ab-1d7c-4a91-9ed4-a178dd9b6d10.png" width = "400px">
14 changes: 6 additions & 8 deletions src/circle.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
mutable struct Circle{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end

mutable struct FilledCircle{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end

Expand Down Expand Up @@ -47,8 +45,8 @@ end
Draw a circle. Ref: https://en.wikipedia.org/wiki/Midpoint_circle_algorithm variant with integer-based arithmetic
"""
function draw!(image::AbstractMatrix, shape::Circle{I}, color) where {I}
i_center = shape.i_center
j_center = shape.j_center
i_center = shape.center.i
j_center = shape.center.j
radius = shape.radius

zero_value = zero(I)
Expand Down Expand Up @@ -76,8 +74,8 @@ function draw!(image::AbstractMatrix, shape::Circle{I}, color) where {I}
end

function draw!(image::AbstractMatrix, shape::FilledCircle{I}, color) where {I}
i_center = shape.i_center
j_center = shape.j_center
i_center = shape.center.i
j_center = shape.center.j
radius = shape.radius

zero_value = zero(I)
Expand Down
14 changes: 6 additions & 8 deletions src/cross.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
mutable struct Cross{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end

mutable struct HollowCross{I <: Integer} <: AbstractShape
i_center::I
j_center::I
center::Point{I}
radius::I
end

function draw!(image::AbstractMatrix, shape::Cross, color)
height, width = size(image)

i_center = shape.i_center
j_center = shape.j_center
i_center = shape.center.i
j_center = shape.center.j
radius = shape.radius

for j in j_center - radius : j_center + radius
Expand All @@ -31,8 +29,8 @@ end
function draw!(image::AbstractMatrix, shape::HollowCross, color)
height, width = size(image)

i_center = shape.i_center
j_center = shape.j_center
i_center = shape.center.i
j_center = shape.center.j
radius = shape.radius

for j in j_center - radius : j_center + radius
Expand Down
14 changes: 6 additions & 8 deletions src/line.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
mutable struct Line{I <: Integer} <: AbstractShape
i1::I
j1::I
i2::I
j2::I
point1::Point{I}
point2::Point{I}
end

"""
Draw a line. Ref: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
"""
function draw!(image::AbstractMatrix, shape::Line, color)
i1 = shape.i1
j1 = shape.j1
i2 = shape.i2
j2 = shape.j2
i1 = shape.point1.i
j1 = shape.point1.j
i2 = shape.point2.i
j2 = shape.point2.j

di = abs(i2 - i1)
dj = -abs(j2 - j1)
Expand Down
14 changes: 6 additions & 8 deletions src/rectangle.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
mutable struct Rectangle{I <: Integer} <: AbstractShape
i_top_left::I
j_top_left::I
top_left::Point{I}
height::I
width::I
end

mutable struct FilledRectangle{I <: Integer} <: AbstractShape
i_top_left::I
j_top_left::I
top_left::Point{I}
height::I
width::I
end

function draw!(image::AbstractMatrix, shape::Rectangle, color)
i_top_left = shape.i_top_left
j_top_left = shape.j_top_left
i_top_left = shape.top_left.i
j_top_left = shape.top_left.j
i_bottom_right = i_top_left + shape.height - 1
j_bottom_right = j_top_left + shape.width - 1

Expand All @@ -38,8 +36,8 @@ function draw!(image::AbstractMatrix, shape::Rectangle, color)
end

function draw!(image::AbstractMatrix, shape::FilledRectangle, color)
i_top_left = shape.i_top_left
j_top_left = shape.j_top_left
i_top_left = shape.top_left.i
j_top_left = shape.top_left.j
i_bottom_right = i_top_left + shape.height - 1
j_bottom_right = j_top_left + shape.width - 1

Expand Down
14 changes: 7 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.Line(2, 2, height - 1, width - 1)
shape = SD.Line(SD.Point(2, 2), SD.Point(height - 1, width - 1))
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -88,7 +88,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.Circle(8, 8, 6)
shape = SD.Circle(SD.Point(8, 8), 6)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -115,7 +115,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.FilledCircle(8, 8, 6)
shape = SD.FilledCircle(SD.Point(8, 8), 6)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -142,7 +142,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.Rectangle(2, 2, height - 2, width - 2)
shape = SD.Rectangle(SD.Point(2, 2), height - 2, width - 2)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -169,7 +169,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.FilledRectangle(2, 2, height - 2, width - 2)
shape = SD.FilledRectangle(SD.Point(2, 2), height - 2, width - 2)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -196,7 +196,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.Cross(8, 8, 4)
shape = SD.Cross(SD.Point(8, 8), 4)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand All @@ -223,7 +223,7 @@ Test.@testset "SimpleDraw.jl" begin
height = 16
width = 16
image = falses(height, width)
shape = SD.HollowCross(8, 8, 4)
shape = SD.HollowCross(SD.Point(8, 8), 4)
color = true
SD.draw!(image, shape, color)
Test.@test image == BitArray([
Expand Down

0 comments on commit cedbc14

Please sign in to comment.