Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
generative_art_cartoon
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Finn Bear
generative_art_cartoon
Commits
d8decbde
Commit
d8decbde
authored
2 years ago
by
Finn Bear
Browse files
Options
Downloads
Patches
Plain Diff
Improve automata slide.
parent
fd35df4c
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/slide/s4_automata.rs
+53
-10
53 additions, 10 deletions
src/slide/s4_automata.rs
with
53 additions
and
10 deletions
src/slide/s4_automata.rs
+
53
−
10
View file @
d8decbde
...
...
@@ -38,7 +38,12 @@ enum AutomataState {
rules
:
Option
<
f64
>
,
},
/// Simulation.
Life
,
Life
{
/// Whether we started expanding the grid yet.
expanding
:
bool
,
/// Conway iteration counter.
iterations
:
usize
,
},
}
impl
Default
for
Automata
{
...
...
@@ -88,10 +93,20 @@ impl Slide for Automata {
}
else
if
rules
.is_none
()
{
*
rules
=
Some
(
ctx
.input
()
.time
)
}
else
{
self
.state
=
AutomataState
::
Life
;
self
.state
=
AutomataState
::
Life
{
expanding
:
false
,
iterations
:
0
,
};
}
}
AutomataState
::
Life
{
expanding
,
..
}
=>
{
if
*
expanding
{
// Done with the slide.
return
true
;
}
else
{
*
expanding
=
true
;
}
}
AutomataState
::
Life
=>
return
true
,
}
false
}
...
...
@@ -126,19 +141,41 @@ impl Slide for Automata {
// How much grid goes left and rules go right.
const
HORIZONTAL_OFFSET
:
f32
=
250.0
;
match
&
self
.state
{
match
&
mut
self
.state
{
AutomataState
::
Empty
=>
{}
AutomataState
::
Static
{
rules
,
..
}
=>
{
if
let
&
Some
(
rules
)
=
rules
{
if
let
Some
(
rules
)
=
*
rules
{
let
elapsed
=
ui
.ctx
()
.input
()
.time
-
rules
;
self
.life.offset_x
=
(
elapsed
as
f32
*
-
400.0
)
.max
(
-
HORIZONTAL_OFFSET
);
rules_fade
=
Some
(
rules
);
}
}
AutomataState
::
Life
=>
{
AutomataState
::
Life
{
expanding
,
iterations
,
}
=>
{
// Iterate grid ~5 times a second.
if
self
.governor
.ready
(
ui
.ctx
()
.input
()
.time
,
0.2
)
{
self
.life
=
conways_game_of_life
(
&
self
.life
);
if
*
iterations
==
10
{
// Automatically begin expansion.
*
expanding
=
true
;
}
else
if
*
iterations
==
20
{
// Draw a line to create a cool pattern.
for
y
in
4
..
self
.life.size_cells
-
4
{
self
.life
.set_fill
(
2
,
y
,
Color32
::
BLACK
);
}
}
else
if
*
iterations
==
30
{
// Draw another line to create a cool pattern.
for
x
in
4
..
self
.life.size_cells
-
4
{
self
.life
.set_fill
(
x
,
self
.life.size_cells
-
2
,
Color32
::
BLACK
);
}
}
// Boolean to int cast yields 1 if true, 0 otherwise.
let
expansion
=
(
*
expanding
&&
self
.life.size_cells
<
64
)
as
usize
;
self
.life
=
conways_game_of_life
(
&
self
.life
,
expansion
);
*
iterations
+=
1
;
}
// Kludge to render rules with full opacity.
...
...
@@ -198,10 +235,15 @@ impl Slide for Automata {
}
/// Run one iteration of Conway's Game of Life on the grid, producing a new grid.
fn
conways_game_of_life
(
grid
:
&
Grid
)
->
Grid
{
///
/// A non-zero expansion parameter expands the grid on each side by that many squares.
fn
conways_game_of_life
(
grid
:
&
Grid
,
expansion
:
usize
)
->
Grid
{
let
mut
new
=
grid
.clone
();
new
.clear_fill
();
// Allocate more room on left, right, top and bottom.
new
.size_cells
+=
expansion
*
2
;
for
cy
in
0
..
grid
.size_cells
{
for
cx
in
0
..
grid
.size_cells
{
let
mut
neighbors
=
0
;
...
...
@@ -230,9 +272,10 @@ fn conways_game_of_life(grid: &Grid) -> Grid {
(
true
,
_
)
=>
false
,
};
// Write back to the middle of the expanded area to avoid shift.
new
.set_fill
(
cx
,
cy
,
cx
+
expansion
,
cy
+
expansion
,
if
alive
{
Color32
::
BLACK
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment