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
92abd91a
Commit
92abd91a
authored
2 years ago
by
Finn Bear
Browse files
Options
Downloads
Patches
Plain Diff
Improve automata slide.
parent
94898eba
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/component/grid.rs
+1
-1
1 addition, 1 deletion
src/component/grid.rs
src/slide/s4_automata.rs
+30
-20
30 additions, 20 deletions
src/slide/s4_automata.rs
with
31 additions
and
21 deletions
src/component/grid.rs
+
1
−
1
View file @
92abd91a
...
...
@@ -145,7 +145,7 @@ impl Grid {
let
cell_width
=
1.0
/
self
.size_cells
as
f32
;
// Make sure neighboring cells overlap without gaps.
let
tolerance
=
0.00
1
;
let
tolerance
=
0.00
08
;
for
y
in
0
..
self
.size_cells
{
let
y_coord
=
y
as
f32
/
self
.size_cells
as
f32
;
for
x
in
0
..
self
.size_cells
{
...
...
This diff is collapsed.
Click to expand it.
src/slide/s4_automata.rs
+
30
−
20
View file @
92abd91a
...
...
@@ -40,8 +40,8 @@ enum AutomataState {
},
/// Simulation.
Life
{
/// Whether we
sta
rted
expanding the grid
yet.
expanding
:
bool
,
/// Whether we
inve
rted
the color and turned on fading
yet.
rule_changed
:
bool
,
/// Conway iteration counter.
iterations
:
usize
,
},
...
...
@@ -95,17 +95,17 @@ impl Slide for Automata {
*
rules
=
Some
(
ctx
.input
()
.time
)
}
else
{
self
.state
=
AutomataState
::
Life
{
expanding
:
false
,
rule_changed
:
false
,
iterations
:
0
,
};
}
}
AutomataState
::
Life
{
expanding
,
..
}
=>
{
if
*
expanding
{
AutomataState
::
Life
{
rule_changed
,
..
}
=>
{
if
!*
rule_changed
{
*
rule_changed
=
true
;
}
else
{
// Done with the slide.
return
true
;
}
else
{
*
expanding
=
true
;
}
}
}
...
...
@@ -146,17 +146,14 @@ impl Slide for Automata {
}
}
AutomataState
::
Life
{
expanding
,
rule_changed
,
iterations
,
}
=>
{
// Iterate grid ~5 times a second.
if
self
.governor
.ready
(
ui
.ctx
()
.input
()
.time
,
0.2
)
{
// Schedule some special events at some iteration counts.
let
expanding
=
*
iterations
>=
10
;
match
*
iterations
{
10
=>
{
// Automatically begin expansion.
*
expanding
=
true
;
}
20
=>
{
for
y
in
4
..
self
.life.size_cells
-
4
{
// Line on the left.
...
...
@@ -185,8 +182,17 @@ impl Slide for Automata {
}
// 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
);
let
expansion
=
(
expanding
&&
self
.life.size_cells
<
64
)
as
usize
;
// Implement rule changes.
let
(
background_color
,
alive_color
,
stroke_color
,
fade
)
=
if
*
rule_changed
{
(
Color32
::
BLACK
,
Color32
::
WHITE
,
Color32
::
TRANSPARENT
,
true
)
}
else
{
(
Color32
::
TRANSPARENT
,
Color32
::
BLACK
,
Color32
::
GRAY
,
false
)
};
self
.life.background
=
background_color
;
self
.life.stroke
=
stroke_color
;
self
.life
=
conways_game_of_life
(
&
self
.life
,
expansion
,
alive_color
,
fade
);
*
iterations
+=
1
;
}
...
...
@@ -251,7 +257,7 @@ impl Slide for Automata {
/// Run one iteration of Conway's Game of Life on the grid, producing a new 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
{
fn
conways_game_of_life
(
grid
:
&
Grid
,
expansion
:
usize
,
alive_color
:
Color32
,
fade
:
bool
)
->
Grid
{
let
mut
new
=
grid
.clone
();
new
.clear_fill
();
...
...
@@ -271,15 +277,15 @@ fn conways_game_of_life(grid: &Grid, expansion: usize) -> Grid {
// Don't consider self.
continue
;
}
if
grid
.fill
(
x
,
y
)
.
a
()
>
0
{
if
grid
.fill
(
x
,
y
)
.
is_opaque
()
{
neighbors
+=
1
;
}
}
}
let
mut
alive
=
grid
.fill
(
cx
,
cy
)
.
a
()
>
0
;
let
was_
alive
=
grid
.fill
(
cx
,
cy
)
.
is_opaque
()
;
alive
=
match
(
alive
,
neighbors
)
{
let
alive
=
match
(
was_
alive
,
neighbors
)
{
(
false
,
3
)
=>
true
,
(
false
,
_
)
=>
false
,
(
true
,
2
)
|
(
true
,
3
)
=>
true
,
...
...
@@ -291,9 +297,13 @@ fn conways_game_of_life(grid: &Grid, expansion: usize) -> Grid {
cx
+
expansion
,
cy
+
expansion
,
if
alive
{
Color32
::
BLACK
}
else
{
alive_color
}
else
if
!
fade
{
Color32
::
TRANSPARENT
}
else
{
// Fade out gradually.
let
previous_alpha
=
grid
.fill
(
cx
,
cy
)
.a
();
set_alpha
(
alive_color
,
previous_alpha
as
f32
/
(
255.0
*
2.0
))
},
);
}
...
...
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