-
Notifications
You must be signed in to change notification settings - Fork 17
/
core.jl
180 lines (144 loc) · 4.18 KB
/
core.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Type representing any object drawable on image
"""
abstract type Drawable end
"""
p = Point(x,y)
p = Point(c)
A `Drawable` point on the image
"""
struct Point <: Drawable
x::Int
y::Int
end
abstract type Line <: Drawable end
abstract type Circle <: Drawable end
"""
line = LineTwoPoints(p1, p2)
A `Drawable` infinite length line passing through the two points
`p1` and `p2`.
"""
struct LineTwoPoints <: Line
p1::Point
p2::Point
end
"""
line = LineNormal(ρ, θ)
A `Drawable` infinte length line having perpendicular length `ρ` from
origin and angle `θ` between the perpendicular and x-axis
"""
struct LineNormal{T<:Real, U<:Real} <: Line
ρ::T
θ::U
end
"""
circle = CircleThreePoints(p1, p2, p3)
A `Drawable` circle passing through points `p1`, `p2` and `p3`
"""
struct CircleThreePoints <: Circle
p1::Point
p2::Point
p3::Point
end
"""
circle = CirclePointRadius(center, ρ)
A `Drawable` circle having center `center` and radius `ρ`
"""
struct CirclePointRadius{T<:Real} <: Circle
center::Point
ρ::T
end
"""
ls = LineSegment(p1, p2)
A `Drawable` finite length line between `p1` and `p2`
"""
struct LineSegment <: Drawable
p1::Point
p2::Point
end
"""
path = Path([point])
A `Drawable` sequence of line segments connecting consecutive pairs
of points in `[point]`.
!!! note
This will create a non-closed path. For a closed path, see `Polygon`
"""
struct Path <: Drawable
vertices::Vector{Point}
end
"""
ellipse = Ellipse(center, ρx, ρy)
A `Drawable` ellipse with center `center` and parameters `ρx` and `ρy`
"""
struct Ellipse{T<:Real, U<:Real} <: Drawable
center::Point
ρx::T
ρy::U
end
"""
polygon = Polygon([vertex])
A `Drawable` polygon i.e. a closed path created by joining the
consecutive points in `[vertex]` along with the first and last point.
!!! note
This will create a closed path. For a non-closed path, see `Path`
"""
struct Polygon <: Drawable
vertices::Vector{Point}
end
"""
rp = RegularPolygon(center, side_count, side_length, θ)
A `Drawable` regular polygon.
#Arguments
* `center::Point` : the center of the polygon
* `side_count::Int` : number of sides of the polygon
* `side_length::Real` : length of each side
* `θ::Real` : orientation of the polygon w.r.t x-axis (in radians)
"""
struct RegularPolygon{T<:Real, U<:Real} <: Drawable
center::Point
side_count::Int
side_length::T
θ::U
end
"""
img = draw!(img, drawable, color)
img = draw!(img, drawable)
Draws `drawable` on `img` using color `color` which
defaults to `one(eltype(img))`
"""
draw!(img::AbstractArray{T,2}, object::Drawable) where {T<:Colorant} = draw!(img, object, one(T))
"""
img = draw!(img, [drawable], [color])
img = draw!(img, [drawable] ,color)
img = draw!(img, [drawable])
Draws all objects in `[drawable]` in the given order on `img` using
corresponding colors from `[color]` which defaults to `one(eltype(img))`
If only a single color `color` is specified then all objects will be
colored with that color.
"""
function draw!(img::AbstractArray{T,2}, objects::AbstractVector{U}, colors::AbstractVector{V}) where {T<:Colorant, U<:Drawable, V<:Colorant}
colors = copy(colors)
while length(colors) < length(objects)
push!(colors, one(T))
end
foreach((object, color) -> draw!(img, object, color), objects, colors)
img
end
draw!(img::AbstractArray{T,2}, objects::AbstractVector{U}, color::T = one(T)) where {T<:Colorant, U<:Drawable} =
draw!(img, objects, [color for i in 1:length(objects)])
"""
img_new = draw(img, drawable, color)
img_new = draw(img, [drawable], [color])
Draws the `drawable` object on a copy of image `img` using color
`color`. Can also draw multiple `Drawable` objects when passed
as a `AbstractVector{Drawable}` with corresponding colors in `[color]`
"""
draw(img::AbstractArray{T,2}, args...) where {T<:Colorant} = draw!(copy(img), args...)
Point(τ::Tuple{Int, Int}) = Point(τ...)
Point(p::CartesianIndex) = Point(p[2], p[1])
function draw!(img::AbstractArray{T,2}, point::Point, color::T) where T<:Colorant
if checkbounds(Bool, img, point.y, point.x)
img[point.y, point.x] = color
end
img
end