Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support julia 0.7 #11

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
language: julia
os:
- linux
- osx
julia:
- 0.4
- 0.5
- 0.7
- 1.0
- nightly
matrix:
allow_failures:
- julia: nightly
notifications:
email: false
sudo: false
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.4
julia 0.7
Compat 0.17
79 changes: 34 additions & 45 deletions src/Hexagons.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
VERSION >= v"0.4.0-dev+6641" && __precompile__()

module Hexagons

using Compat

import Base: convert, start, next, done, length, collect
import Base: convert, iterate, length, collect

export HexagonAxial, HexagonCubic, HexagonOffsetOddR, HexagonOffsetEvenR,
hexagon, center, hexpoints, cube_round, cube_linedraw, neighbor,
Expand All @@ -19,25 +17,25 @@ export HexagonAxial, HexagonCubic, HexagonOffsetOddR, HexagonOffsetEvenR,
# Various ways to index hexagons in a grid
# ----------------------------------------

@compat abstract type Hexagon end
abstract type Hexagon end

immutable HexagonAxial <: Hexagon
struct HexagonAxial <: Hexagon
q::Int
r::Int
end

immutable HexagonCubic <: Hexagon
struct HexagonCubic <: Hexagon
x::Int
y::Int
z::Int
end

immutable HexagonOffsetOddR <: Hexagon
struct HexagonOffsetOddR <: Hexagon
q::Int
r::Int
end

immutable HexagonOffsetEvenR <: Hexagon
struct HexagonOffsetEvenR <: Hexagon
q::Int
r::Int
end
Expand Down Expand Up @@ -107,7 +105,7 @@ end
# Neighbor hexagon iterator
# -------------------------

immutable HexagonNeighborIterator
struct HexagonNeighborIterator
hex::HexagonCubic
end

Expand All @@ -123,10 +121,8 @@ const CUBIC_HEX_NEIGHBOR_OFFSETS = [
neighbors(hex::Hexagon) = HexagonNeighborIterator(convert(HexagonCubic, hex))

length(::HexagonNeighborIterator) = 6
start(::HexagonNeighborIterator) = 1
done(::HexagonNeighborIterator, state::Int) = state > 6

function next(it::HexagonNeighborIterator, state::Int)
function iterate(it::HexagonNeighborIterator, state::Int=1)
state>6 && return nothing
dx = CUBIC_HEX_NEIGHBOR_OFFSETS[state, 1]
dy = CUBIC_HEX_NEIGHBOR_OFFSETS[state, 2]
dz = CUBIC_HEX_NEIGHBOR_OFFSETS[state, 3]
Expand All @@ -138,7 +134,7 @@ end
# Diagonal hexagon iterator
# -------------------------

immutable HexagonDiagonalIterator
struct HexagonDiagonalIterator
hex::HexagonCubic
end

Expand All @@ -154,10 +150,8 @@ const CUBIC_HEX_DIAGONAL_OFFSETS = [
diagonals(hex::Hexagon) = HexagonDiagonalIterator(convert(HexagonCubic, hex))

length(::HexagonDiagonalIterator) = 6
start(::HexagonDiagonalIterator) = 1
done(::HexagonDiagonalIterator, state::Int) = state > 6

function next(it::HexagonDiagonalIterator, state::Int)
function iterate(it::HexagonDiagonalIterator, state::Int=1)
state>6 && return nothing
dx = CUBIC_HEX_DIAGONAL_OFFSETS[state, 1]
dy = CUBIC_HEX_DIAGONAL_OFFSETS[state, 2]
dz = CUBIC_HEX_DIAGONAL_OFFSETS[state, 3]
Expand All @@ -169,7 +163,7 @@ end
# Iterator over the vertices of a hexagon
# ---------------------------------------

immutable HexagonVertexIterator
struct HexagonVertexIterator
x_center::Float64
y_center::Float64
xsize::Float64
Expand All @@ -195,23 +189,21 @@ end

# TODO: remove this function?
function hexpoints(x, y, xsize=1.0, ysize=1.0)
collect((@compat Tuple{Float64, Float64}),
HexagonVertexIterator((@compat Float64(x)), (@compat Float64(y)),
(@compat Float64(xsize)), (@compat Float64(ysize))))
collect(Tuple{Float64, Float64},
HexagonVertexIterator(Float64(x), Float64(y),
Float64(xsize), Float64(ysize)))
end

length(::HexagonVertexIterator) = 6
start(::HexagonVertexIterator) = 1
done(::HexagonVertexIterator, state::Int) = state > 6

function next(it::HexagonVertexIterator, state)
function iterate(it::HexagonVertexIterator, state::Int=1)
state>6 && return nothing
theta = 2*pi/6 * (state-1+0.5)
x_i = it.x_center + it.xsize * cos(theta)
y_i = it.y_center + it.ysize * sin(theta)
return ((x_i, y_i), state+1)
end

immutable HexagonDistanceIterator
struct HexagonDistanceIterator
hex::HexagonCubic
n::Int
end
Expand All @@ -223,10 +215,8 @@ end
hexagons_within(hex::Hexagon, n::Int) = hexagons_within(n, hex)

length(it::HexagonDistanceIterator) = it.n * (it.n + 1) * 3 + 1
start(it::HexagonDistanceIterator) = (-it.n, 0)
done(it::HexagonDistanceIterator, state::(@compat Tuple{Int, Int})) = (state[1] > it.n)

function next(it::HexagonDistanceIterator, state::(@compat Tuple{Int,Int}))
function iterate(it::HexagonDistanceIterator, state::Tuple{Int,Int}=(-it.n, 0))
state[1]>it.n && return nothing
x, y = state
z = -x-y
hex = HexagonCubic(x, y, z)
Expand All @@ -243,7 +233,7 @@ collect(it::HexagonDistanceIterator) = collect(HexagonCubic, it)
# Iterator over a ring of hexagons
# ---------------------------------------

immutable HexagonRingIterator
struct HexagonRingIterator
hex::HexagonCubic
n::Int
end
Expand All @@ -256,10 +246,8 @@ end
ring(hex::Hexagon, n::Int) = ring(n, hex)

length(it::HexagonRingIterator) = it.n * 6
start(it::HexagonRingIterator) = (1, neighbor(it.hex, 5, it.n))
done(it::HexagonRingIterator, state::(@compat Tuple{Int, HexagonCubic})) = state[1] > length(it)

function next(it::HexagonRingIterator, state::(@compat Tuple{Int, HexagonCubic}))
function iterate(it::HexagonRingIterator, state::(Tuple{Int, HexagonCubic})=(1, neighbor(it.hex, 5, it.n)))
state[1]>length(it) && return nothing
hex_i, cur_hex = state
# println("HexagonRingIterator: at position $hex_i ($cur_hex)")
ring_part = div(hex_i - 1, it.n) + 1
Expand All @@ -272,12 +260,12 @@ collect(it::HexagonRingIterator) = collect(HexagonCubic, it)
# Iterator over all hexes within a certain distance
# -------------------------------------------------

immutable HexagonSpiralIterator
struct HexagonSpiralIterator
hex::HexagonCubic
n::Int
end

immutable HexagonSpiralIteratorState
struct HexagonSpiralIteratorState
hexring_i::Int
hexring_it::HexagonRingIterator
hexring_it_i::Int
Expand All @@ -291,17 +279,18 @@ end
spiral(hex::Hexagon, n::Int) = spiral(n, hex)

length(it::HexagonSpiralIterator) = it.n * (it.n + 1) * 3
function start(it::HexagonSpiralIterator)
first_ring = ring(it.hex, 1)
HexagonSpiralIteratorState(1, first_ring, start(first_ring)...)
end
done(it::HexagonSpiralIterator, state::HexagonSpiralIteratorState) = state.hexring_i > it.n

# The state of a HexagonSpiralIterator consists of
# 1. an Int, the index of the current ring
# 2. a HexagonRingIterator and its state to keep track of the current position
# in the ring.
function next(it::HexagonSpiralIterator, state::HexagonSpiralIteratorState)
function iterate(it::HexagonSpiralIterator)
first_ring = ring(it.hex, 1)
iterate(it, HexagonSpiralIteratorState(1, first_ring, start(first_ring)...))
end

function iterate(it::HexagonSpiralIterator, state::HexagonSpiralIteratorState)
state.hexring_i>it.n && return nothing
# Get current state
hexring_i, hexring_it, hexring_it_i, hexring_it_hex =
state.hexring_i, state.hexring_it, state.hexring_it_i, state.hexring_it_hex
Expand Down Expand Up @@ -357,7 +346,7 @@ end

# Find the nearest hexagon in cubic coordinates.
function nearest_cubic_hexagon(x::Real, y::Real, z::Real)
rx, ry, rz = @compat round(Integer, x), round(Integer, y), round(Integer, z)
rx, ry, rz = round(Integer, x), round(Integer, y), round(Integer, z)
x_diff, y_diff, z_diff = abs(rx - x), abs(ry - y), abs(rz - z)

if x_diff > y_diff && x_diff > z_diff
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Hexagons
using Base.Test
using Test

convert(HexagonOffsetOddR, HexagonAxial(2, 4))

Expand Down