Skip to content

Commit 6a25e11

Browse files
committed
feat: generalized grid and line types
1 parent 01f7cd7 commit 6a25e11

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

scripts/plot_docs.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ function plot_line_types(grid)
2727
end
2828
end
2929
end
30-
plt = (canvas(; size = (200, 200)), canvas(; size = (200, 200)))
30+
plt = (
31+
canvas(; size = (200, 200), backgroud = :lightgray),
32+
canvas(; size = (200, 200), backgroud = :lightgray)
33+
)
3134
plot_lines!(plt[1], lines[1], colors[1]; linewidth = 3)
3235
plot_lines!(plt[2], lines[2], colors[2]; linewidth = 3)
3336
plot_lines!(plt[1], lines2[1], colors2[1]; linewidth = 3, linestyle = :dash)
@@ -41,9 +44,9 @@ grid = Grid(5)
4144
pattern = (shuffle(1:length(grid)), shuffle(1:length(grid)))
4245

4346
begin
44-
plt = canvas(; size = (200, 200))
47+
plt = canvas(; size = (200, 200), backgroud = :lightgray)
4548
plot_grid!(plt, grid; marker = :circle, color = :black, markersize = 5)
46-
savefig(plot_grid(grid), "docs/src/plots/grid.svg")
49+
savefig(plt, "docs/src/plots/grid.svg")
4750
end
4851

4952
begin
@@ -53,7 +56,9 @@ begin
5356
end
5457

5558
for i = 1:2
56-
plt = canvas(size = (200, 200))
59+
plt = canvas(size = (200, 200), backgroud = :lightgray)
5760
plot_lock_pattern!(plt, grid, pattern[i])
5861
savefig(plt, "docs/src/plots/lock_pattern_$i.svg")
5962
end
63+
64+
# TODO: symmetries

src/functions.jl

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## --- Grid ---
22

3-
# TODO: generalize for arbitrary grids
3+
# TODO: generalize for arbitrary rectangular grids
44

55
"""Two-dimensional grid."""
66
struct Grid
@@ -11,12 +11,14 @@ end
1111

1212
Base.length(grid::Grid) = prod(grid.n)
1313

14-
function Grid(n::Int)
15-
x = [i for i = 1:n for _ = 1:n]
16-
y = [j for _ = 1:n for j = 1:n]
17-
Grid(x, y, (n, n))
14+
function Grid(n::Tuple{Int,Int})
15+
x = [i for i = 1:n[1] for _ = 1:n[2]]
16+
y = [j for _ = 1:n[1] for j = 1:n[2]]
17+
Grid(x, y, n)
1818
end
1919

20+
Grid(n::Int) = Grid((n, n))
21+
2022

2123
## --- Line Types ---
2224

@@ -31,29 +33,23 @@ Base.length(t::LineTypes) = length(t.k)
3133
"""Generate all `x` and `y` differences and their multiple `k`
3234
that represent each line type in a grid."""
3335
function LineTypes(grid::Grid)
34-
(n, _) = grid.n
35-
xs = Int[0, 1, 1, 1]
36-
ys = Int[1, 0, 1, -1]
37-
ks = Int[n-1, n-1, n-1, n-1]
38-
for x = 1:n-2
39-
for y = (x+1):n-1
40-
if gcd(x, y) == 1
41-
k = div(n - 1, y)
42-
# 1
43-
push!(xs, x)
44-
push!(ys, y)
45-
push!(ks, k)
46-
# 2
47-
push!(xs, y)
48-
push!(ys, x)
49-
push!(ks, k)
50-
# 3
51-
push!(xs, x)
52-
push!(ys, -y)
36+
n = grid.n
37+
dx_max = n[1] -1
38+
dy_max = n[2] -1
39+
xs = Int[0, 1]
40+
ys = Int[1, 0]
41+
ks = Int[dx_max, dy_max]
42+
for dx = 1:dx_max
43+
for dy = 1:dy_max
44+
if gcd(dx, dy) == 1
45+
k = min(div(dx_max, dx), div(dy_max, dy))
46+
# Increasing y
47+
push!(xs, dx)
48+
push!(ys, dy)
5349
push!(ks, k)
54-
# 4
55-
push!(xs, y)
56-
push!(ys, -x)
50+
# Decreasing y
51+
push!(xs, dx)
52+
push!(ys, -dy)
5753
push!(ks, k)
5854
end
5955
end

src/plots.jl

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
using Plots
22

3-
function canvas(; size=(400, 400), backgroud=:lightgray)
4-
plot(;
5-
xticks = :none,
6-
yticks = :none,
7-
legend = false,
8-
axis = false,
9-
size = size,
10-
background = backgroud)
3+
"""Plot """
4+
function canvas(; kwargs...)
5+
plot(; xticks = :none, yticks = :none, legend = false, axis = false, kwargs...)
116
end
127

138
"""Create unique color for all line types in the grid."""
@@ -53,7 +48,7 @@ function create_plots(n::Int, path::AbstractString, directory::AbstractString; l
5348
if distance limit
5449
root = mkpath(joinpath(directory, "$(join(grid.n, "x"))", "$(distance)"))
5550
for pattern in patterns
56-
plt = canvas()
51+
plt = canvas(; size=(400, 400), backgroud=:lightgray)
5752
plot_lock_pattern!(plt, grid, pattern)
5853
savefig(plt, joinpath(root, "$(hash(pattern)).svg"))
5954
end

test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ using Test
1111
tmp = mktempdir()
1212
create_instance(3, tmp)
1313
extract_solutions(joinpath(@__DIR__, "output"))
14-
1514
@test true
1615
end

0 commit comments

Comments
 (0)