[MOD] found a more eleganto solution using Union type
This commit is contained in:
parent
f635c452e6
commit
b993fec341
|
@ -8,10 +8,10 @@ end
|
||||||
|
|
||||||
|
|
||||||
type Rectangle <: Shape
|
type Rectangle <: Shape
|
||||||
t1::Tuple
|
x::Float64
|
||||||
t2::Tuple
|
y::Float64
|
||||||
#width::Float64 = abs(t2[1]-t1[1])
|
width::Float64
|
||||||
#height::Float64 = abs(t2[2]-t2[2])
|
height::Float64
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,6 +19,20 @@ type ShapeGroup <: Shape
|
||||||
members::Vector{Shape}
|
members::Vector{Shape}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
typealias Point Union{Tuple{Float64,Float64}, Tuple{Int64,Int64}}
|
||||||
|
|
||||||
|
|
||||||
|
"Create rectangle from to points `p` at the top left corner and `q` at the lower right corner"
|
||||||
|
function Rectangle(p::Point,q::Point)
|
||||||
|
px,py = p
|
||||||
|
qx,qy = q
|
||||||
|
w = qx - px
|
||||||
|
h = qy - py
|
||||||
|
Rectangle(px,py,w,h)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
"Group shapes into a group, which can be treated as a shape itself"
|
"Group shapes into a group, which can be treated as a shape itself"
|
||||||
function group(shapes::Shape...)
|
function group(shapes::Shape...)
|
||||||
ShapeGroup(collect(shapes))
|
ShapeGroup(collect(shapes))
|
||||||
|
@ -30,32 +44,35 @@ getposition(c::Circle) = (c.x,c.y)
|
||||||
|
|
||||||
|
|
||||||
"Get position of Rectangle"
|
"Get position of Rectangle"
|
||||||
getposition(r::Rectangle) = r.t2
|
getposition(r::Rectangle) = (r.x,r.y)
|
||||||
|
|
||||||
|
|
||||||
# Inform implementer of Shape subtypes of required functions
|
# Inform implementer of Shape subtypes of required functions
|
||||||
getposition(s::Shape) = error("You must implement for Shape")
|
getposition(s::Shape) = error("You must implement for Shape")
|
||||||
setposition(s::Shape, x, y) = error("You must implement this function for Shape")
|
setposition(s::Shape, x, y) = error("You must implement this function for Shape")
|
||||||
|
|
||||||
|
|
||||||
"Set absolute position of center of Circle"
|
"Set absolute position of center of Circle"
|
||||||
function setposition(c::Circle,x,y) # If you cast the type on the function
|
function setposition(c::Circle,x,y) # If you cast the type on the function
|
||||||
c.x = x # you can only use that input type
|
c.x = x # you can only use that input type
|
||||||
c.y = y # that is no type inference or conversion will be made
|
c.y = y # that is no type inference or conversion will be made
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
"Set absolute position upper left corner of Rectangle"
|
"Set absolute position upper left corner of Rectangle"
|
||||||
function setposition(r::Rectangle,x::Tuple)
|
function setposition(r::Rectangle,x,y)
|
||||||
r.t2 = x
|
r.x = x
|
||||||
r.t1[1] = r.t1[1] + x[1]
|
r.y = y
|
||||||
r.t1[2] = r.t1[2] + x[2]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
"Move Shape by `dx` units along the x-axis and `dy` units across the y-axis"
|
"Move Shape by `dx` units along the x-axis and `dy` units across the y-axis"
|
||||||
function move(s::Shape,dx,dy)
|
function move(s::Shape,dx,dy)
|
||||||
x,y = getposition(s)
|
x,y = getposition(s)
|
||||||
setposition(s,x+dx,y+dy)
|
setposition(s,x+dx,y+dy)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
"Check if point at `(x,y)` is inside circle"
|
"Check if point at `(x,y)` is inside circle"
|
||||||
function inside(c::Circle,x,y)
|
function inside(c::Circle,x,y)
|
||||||
dx = x - c.x
|
dx = x - c.x
|
||||||
|
@ -63,33 +80,28 @@ function inside(c::Circle,x,y)
|
||||||
hypot(dx,dy) <= c.radius
|
hypot(dx,dy) <= c.radius
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
"Check if point at `(x,y)` is inside rectangle"
|
"Check if point at `(x,y)` is inside rectangle"
|
||||||
function inside(r::Rectangle,x::Tuple)
|
function inside(r::Rectangle,x,y)
|
||||||
r.t1[1] <= x[1] <= r.t2[1] &&
|
r.x <= x <= r.x + r.width &&
|
||||||
r.t1[2] <= x[2] <= r.t2[2]
|
r.y <= y <= r.y + r.height
|
||||||
end
|
end
|
||||||
|
|
||||||
|
inside(s::Shape,p::Point) = inside(s,p...)
|
||||||
|
|
||||||
|
|
||||||
"Check if point at `(x,y)` is inside one of the shapes in the group"
|
"Check if point at `(x,y)` is inside one of the shapes in the group"
|
||||||
function inside(g::ShapeGroup,x,y)
|
function inside(g::ShapeGroup,x,y)
|
||||||
for m in g.members
|
for m in g.members
|
||||||
if string(typeof(m)) == "Rectangle"
|
if inside(m,x,y)
|
||||||
println(inside(m,(x,y)))
|
return true
|
||||||
elseif string(typeof(m)) == "Circle"
|
|
||||||
println(inside(m,x,y))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
"Check if point at `(x,y)` defined as a tuple is inside one of the shapes in the group"
|
# Inform implementer of Shape subtypes of required functions
|
||||||
function inside(g::ShapeGroup,x::Tuple)
|
inside(s::Shape,x,y) = error("You have to implement inside for Shape")
|
||||||
for m in g.members
|
|
||||||
if string(typeof(m)) == "Rectangle"
|
|
||||||
println(inside(m,x))
|
|
||||||
elseif string(typeof(m)) == "Circle"
|
|
||||||
println(inside(m,x...))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# TESTING CODE
|
# TESTING CODE
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue