Skip to content

Commit 55dfdc6

Browse files
committed
updated 10print example
1 parent 328d2e7 commit 55dfdc6

File tree

10 files changed

+75
-70
lines changed

10 files changed

+75
-70
lines changed

examples/10print/main.go

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
6+
"github.com/aldernero/gaul"
7+
"github.com/aldernero/sketchy"
8+
"github.com/hajimehoshi/ebiten/v2"
9+
"github.com/hajimehoshi/ebiten/v2/inpututil"
510
"github.com/tdewolff/canvas"
611
"image/color"
712
"log"
813
"math"
9-
10-
"github.com/aldernero/sketchy"
11-
"github.com/hajimehoshi/ebiten/v2"
12-
"github.com/hajimehoshi/ebiten/v2/inpututil"
1314
)
1415

1516
// Define a grid of tiles and a random number generator
@@ -23,29 +24,42 @@ const (
2324
)
2425

2526
type Truchet struct {
26-
rows int
27-
cols int
28-
tiles []Tile
27+
rows int
28+
cols int
29+
originX float64
30+
originY float64
31+
cellSize float64
32+
tiles []Tile
2933
}
3034

31-
func (t *Truchet) init(r int, c int, s *sketchy.Sketch) {
32-
t.rows = r
33-
t.cols = c
34-
t.tiles = []Tile{}
35-
for i := 0; i < r*c; i++ {
35+
func (t *Truchet) init(cellSize float64, s *sketchy.Sketch) {
36+
t.rows = int(s.Height() / cellSize)
37+
t.cols = int(s.Width() / cellSize)
38+
t.cellSize = cellSize
39+
t.originX = 0.5 * (s.Width() - float64(t.cols)*cellSize)
40+
t.originY = 0.5 * (s.Height() - float64(t.rows)*cellSize)
41+
t.tiles = make([]Tile, t.rows*t.cols)
42+
for i := 0; i < t.rows*t.cols; i++ {
3643
// Use opensimplex noise to make it more interesting
37-
noise := s.Rand.Noise2D(float64(i%c), float64(i/r))
44+
noise := s.Rand.Noise2D(float64(i%t.rows), float64(i)/float64(t.cols))
3845
tile := BackSlash
3946
if noise > 0.5 {
4047
tile = ForwardSlash
4148
}
42-
//fmt.Println(i, noise, tile)
43-
t.tiles = append(t.tiles, tile)
49+
t.tiles[i] = tile
4450
}
51+
fmt.Println(t.rows, t.cols, t.originX, t.originY, t.cellSize)
4552
}
4653

47-
func (t *Truchet) flip(r int, c int) {
54+
func (t *Truchet) flip(x, y float64) {
55+
// Convert x, y to row, col
56+
r := int(math.Floor((y - t.originY) / t.cellSize))
57+
c := int(math.Floor((x - t.originX) / t.cellSize))
4858
i := r*t.cols + c
59+
if i < 0 || i >= len(t.tiles) {
60+
fmt.Println(x, y, r, c, i)
61+
fmt.Printf("Invalid tile index: %d Tile count: %d", i, len(t.tiles))
62+
}
4963
val := t.tiles[i]
5064
switch val {
5165
case EmptyTile:
@@ -57,6 +71,18 @@ func (t *Truchet) flip(r int, c int) {
5771
}
5872
}
5973

74+
func (t *Truchet) rectForTile(i int) gaul.Rect {
75+
cell := t.cellSize
76+
x := cell*float64(i%t.cols) + t.originX
77+
y := cell*float64(i/t.cols) + t.originY
78+
return gaul.Rect{
79+
X: x,
80+
Y: y,
81+
W: t.cellSize,
82+
H: t.cellSize,
83+
}
84+
}
85+
6086
func setup(s *sketchy.Sketch) {
6187
cellSize := s.Slider("cellSize")
6288
s.Rand.SetSeed(s.RandomSeed)
@@ -67,7 +93,7 @@ func setup(s *sketchy.Sketch) {
6793
s.Rand.SetNoiseScaleY(s.Slider("yscale"))
6894
s.Rand.SetNoiseOffsetX(s.Slider("xoffset"))
6995
s.Rand.SetNoiseOffsetY(s.Slider("yoffset"))
70-
board.init(int(s.SketchCanvas.W/cellSize), int(s.SketchCanvas.H/cellSize), s)
96+
board.init(cellSize, s)
7197
}
7298

7399
func update(s *sketchy.Sketch) {
@@ -77,33 +103,36 @@ func update(s *sketchy.Sketch) {
77103
setup(s)
78104
}
79105
// flip one tile
80-
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
106+
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
81107
x, y := ebiten.CursorPosition()
82-
if s.PointInSketchArea(float64(x), float64(y)) {
83-
p := s.CanvasCoords(float64(x), float64(y))
84-
c := int(math.Floor(float64(board.rows) * p.X / s.SketchCanvas.W))
85-
r := int(math.Floor(float64(board.cols) * p.Y / s.SketchCanvas.H))
86-
board.flip(r, c)
87-
}
108+
p := s.CanvasCoords(float64(x), float64(y))
109+
board.flip(p.X, p.Y)
88110
}
89111
}
90112

91113
func draw(s *sketchy.Sketch, c *canvas.Context) {
92114
// Drawing code goes here
93115
c.SetStrokeColor(color.White)
94-
c.SetStrokeWidth(0.7)
95-
dx := c.Width() / float64(board.cols)
96-
dy := c.Height() / float64(board.rows)
116+
c.SetStrokeWidth(0.5)
117+
// draw board rectangle
118+
c.MoveTo(board.originX, board.originY)
119+
c.LineTo(board.originX+float64(board.cols)*board.cellSize, board.originY)
120+
c.LineTo(board.originX+float64(board.cols)*board.cellSize, board.originY+float64(board.rows)*board.cellSize)
121+
c.LineTo(board.originX, board.originY+float64(board.rows)*board.cellSize)
122+
c.LineTo(board.originX, board.originY)
123+
// draw tiles
124+
c.SetStrokeWidth(0.5)
97125
for i, t := range board.tiles {
98-
x := dx * float64(i%board.cols)
99-
y := dy * float64(i/board.rows)
126+
rect := board.rectForTile(i)
100127
switch t {
101128
case BackSlash:
102-
c.MoveTo(x, y)
103-
c.LineTo(x+dx, y+dy)
129+
c.MoveTo(rect.X, rect.Y)
130+
c.LineTo(rect.X+board.cellSize, rect.Y+board.cellSize)
104131
case ForwardSlash:
105-
c.MoveTo(x, y+dy)
106-
c.LineTo(x+dx, y)
132+
c.MoveTo(rect.X, rect.Y+board.cellSize)
133+
c.LineTo(rect.X+board.cellSize, rect.Y)
134+
default:
135+
// do nothing
107136
}
108137
}
109138
c.Stroke()
@@ -131,6 +160,8 @@ func main() {
131160
s.Drawer = draw
132161
s.Init()
133162
setup(s)
163+
fmt.Println(s.SketchWidth, s.SketchHeight)
164+
fmt.Println(s.Width(), s.Height())
134165
ebiten.SetWindowSize(int(s.SketchWidth), int(s.SketchHeight))
135166
ebiten.SetWindowTitle("Sketchy - " + s.Title)
136167
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeDisabled)

examples/10print/sketch.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"Title": "10PRINT Interaction",
33
"Prefix": "sketch",
4-
"SketchWidth": 800,
5-
"SketchHeight": 800,
4+
"SketchWidth": 1080,
5+
"SketchHeight": 768,
66
"ControlBackgroundColor": "",
77
"ControlOutlineColor": "",
88
"SketchBackgroundColor": "#1e1e1e",
@@ -13,7 +13,7 @@
1313
"Name": "cellSize",
1414
"MinVal": 1,
1515
"MaxVal": 60,
16-
"Val": 5,
16+
"Val": 20,
1717
"Incr": 0.5
1818
},
1919
{

examples/10print/sketch_20220312_085532.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.
-64.2 KB
Binary file not shown.

examples/10print/sketch_20250301_175813.svg

Lines changed: 1 addition & 0 deletions
Loading
34.5 KB
Loading

examples/fractal/sketch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Title": "Fractal Sketch",
3-
"SketchWidth": 800,
4-
"SketchHeight": 800,
3+
"SketchWidth": 1080,
4+
"SketchHeight": 768,
55
"ControlWidth": 250,
66
"Sliders": [
77
{

examples/lissajous/sketch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Title": "Lissajous Curve Example",
3-
"SketchWidth": 800,
4-
"SketchHeight": 800,
3+
"SketchWidth": 1080,
4+
"SketchHeight": 768,
55
"Sliders": [
66
{
77
"Name": "nx",

examples/noise/sketch.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"Title": "OpenSimplex Noise Example",
3-
"SketchWidth": 800,
4-
"SketchHeight": 600,
5-
"ControlWidth": 200,
3+
"SketchWidth": 1080,
4+
"SketchHeight": 768,
65
"Sliders": [
76
{
87
"Name": "octaves",

sketch.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"image"
77
"image/color"
8-
"image/png"
98
"log"
109
"math/rand"
1110
"os"
@@ -56,12 +55,11 @@ type Sketch struct {
5655
DidTogglesChange bool `json:"-"`
5756
Rand gaul.Rng `json:"-"`
5857
sliderControlMap map[string]int
59-
toggleControlMap map[string]int `json:"-"`
58+
toggleControlMap map[string]int
6059
controlColorConfig gaul.ColorConfig
6160
sketchColorConfig gaul.ColorConfig
6261
isSavingPNG bool
6362
isSavingSVG bool
64-
isSavingScreen bool
6563
needToClear bool
6664
Tick int64 `json:"-"`
6765
SketchCanvas *canvas.Canvas `json:"-"`
@@ -146,9 +144,6 @@ func (s *Sketch) UpdateControls() {
146144
if inpututil.IsKeyJustReleased(ebiten.KeyS) {
147145
s.isSavingSVG = true
148146
}
149-
if inpututil.IsKeyJustReleased(ebiten.KeyQ) {
150-
s.isSavingScreen = true
151-
}
152147
if inpututil.IsKeyJustReleased(ebiten.KeyC) {
153148
s.saveConfig()
154149
}
@@ -260,26 +255,6 @@ func (s *Sketch) Draw(screen *ebiten.Image) {
260255
fmt.Println("Saved ", fname)
261256
s.isSavingSVG = false
262257
}
263-
if s.isSavingScreen {
264-
fname := s.Prefix + "_" + gaul.GetTimestampString() + ".png"
265-
sketchImage := screen.SubImage(s.getSketchImageRect())
266-
f, err := os.Create(fname)
267-
if err != nil {
268-
log.Fatal("error while trying to create screenshot file", err)
269-
}
270-
if err := png.Encode(f, sketchImage); err != nil {
271-
err := f.Close()
272-
if err != nil {
273-
panic(err)
274-
}
275-
log.Fatal("error while trying to encode screenshot image", err)
276-
}
277-
if err := f.Close(); err != nil {
278-
log.Fatal("error while trying to close screenshot file", err)
279-
}
280-
fmt.Println("Saved ", fname)
281-
s.isSavingScreen = false
282-
}
283258
img := rasterizer.Draw(s.SketchCanvas, canvas.DefaultResolution, canvas.DefaultColorSpace)
284259
op := &ebiten.DrawImageOptions{}
285260
screen.DrawImage(ebiten.NewImageFromImage(img), op)

0 commit comments

Comments
 (0)