This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.go
95 lines (77 loc) · 1.73 KB
/
draw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 Martin Planer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"image"
"image/color"
"image/png"
"os"
)
var (
black = color.RGBA{0, 0, 0, 255}
red = color.RGBA{255, 0, 0, 255}
green = color.RGBA{0, 255, 0, 255}
blue = color.RGBA{0, 0, 255, 255}
)
const imageSize = 500
const tileSize = imageSize / boardSize
var col color.Color
func exportBoard(b Board, filename string) {
img := drawBoard(b)
f, err := os.Create(filename)
if err != nil {
panic(err)
}
defer f.Close()
png.Encode(f, img)
}
func drawBoard(b Board) image.Image {
var img = image.NewRGBA(image.Rect(0, 0, imageSize, imageSize))
col = red
drawGrid(img)
for _, t := range b {
drawTile(img, t)
}
return img
}
func drawGrid(img *image.RGBA) {
for l := 0; l <= 10; l++ {
ll := max(0, min(imageSize-1, l*tileSize))
for i := 0; i < imageSize; i++ {
img.SetRGBA(ll, i, red)
img.SetRGBA(i, ll, red)
}
}
}
func drawTile(img *image.RGBA, t Tile) {
x1 := t.x * tileSize
x2 := x1 + t.w*tileSize
y1 := t.y * tileSize
y2 := y1 + t.h*tileSize
col = blue
drawRectFill(img, x1, y1, x2, y2)
col = black
drawRectOutline(img, x1, y1, x2, y2)
}
func drawRectOutline(img *image.RGBA, x1, y1, x2, y2 int) {
drawHLine(img, y1, x1, x2)
drawHLine(img, y2, x1, x2)
drawVLine(img, x1, y1, y2)
drawVLine(img, x2, y1, y2)
}
func drawRectFill(img *image.RGBA, x1, y1, x2, y2 int) {
for y := y1; y <= y2; y++ {
drawHLine(img, y, x1, x2)
}
}
func drawHLine(img *image.RGBA, y, x1, x2 int) {
for x := x1; x <= x2; x++ {
img.Set(x, y, col)
}
}
func drawVLine(img *image.RGBA, x, y1, y2 int) {
for y := y1; y <= y2; y++ {
img.Set(x, y, col)
}
}