| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build example |
| 6 | // +build example |
| 7 | |
| 8 | // |
| 9 | // This build tag means that "go install golang.org/x/exp/shiny/..." doesn't |
| 10 | // install this example program. Use "go run main.go board.go xy.go" to run it |
| 11 | // or "go install -tags=example" to install it. |
| 12 | |
| 13 | package main |
| 14 | |
| 15 | import ( |
| 16 | "fmt" |
| 17 | "image" |
| 18 | ) |
| 19 | |
| 20 | // Note that a "square" is not a drawn square on the board, but |
| 21 | // the rectangle (sic) centered on the game point. |
| 22 | type Dims struct { |
| 23 | dim int // Size of board, always square. Full size is 19. |
| 24 | percent int // Scale. |
| 25 | xInset int // From left edge to left edge of first square. |
| 26 | yInset int // From top edge to top edge of first square. |
| 27 | stoneDiam int // Diameter of stone in pixels. |
| 28 | stoneRad2 int // Stone radius squared. |
| 29 | squareWidth int // Width of square in pixels. |
| 30 | squareHeight int // Height of square in pixels. |
| 31 | lineWidth int // Width of lines. |
| 32 | } |
| 33 | |
| 34 | // Initial values, in pixels. These numbers match the images that are loaded; they are resized. |
| 35 | const ( |
| 36 | // These numbers are tuned for the images in asset/. |
| 37 | xInset0 = 100 // Distance from the edge of the board image to the left side of the first stone. |
| 38 | yInset0 = 115 // Distance from the top of the board image to the top side of the first stone. |
| 39 | stoneSize0 = 256 // Size of a stone on the board. |
| 40 | stoneDiam0 = 225 // Diameter of the circular part of the stone within the square. |
| 41 | // The square is a little smaller than the stone, for crowding. |
| 42 | squareWidth0 = 215 // Width of a square on the board. |
| 43 | squareHeight0 = 218 // Height of a square on the board. |
| 44 | stoneRad2 = stoneDiam0 * stoneDiam0 / 4 |
| 45 | ) |
| 46 | |
| 47 | func (d *Dims) Init(dim, percent int) { |
| 48 | d.dim = dim |
| 49 | d.Resize(percent) |
| 50 | } |
| 51 | |
| 52 | func (d *Dims) Resize(percent int) { |
| 53 | d.percent = percent |
| 54 | d.xInset = xInset0 * percent / 100 |
| 55 | d.yInset = yInset0 * percent / 100 |
| 56 | d.stoneDiam = stoneDiam0 * percent / 100 |
| 57 | d.squareWidth = squareWidth0 * percent / 100 |
| 58 | d.squareHeight = squareHeight0 * percent / 100 |
| 59 | d.stoneRad2 = d.stoneDiam * d.stoneDiam / 4 |
| 60 | d.lineWidth = 4 * percent / 100 |
| 61 | if d.lineWidth < 2 { |
| 62 | d.lineWidth = 2 |
| 63 | } |
| 64 | if d.lineWidth > 2 { |
| 65 | d.lineWidth = 4 |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // An XY represents a pixel in the board image. Y is downwards. |
| 70 | type XY struct { |
| 71 | x, y int |
| 72 | } |
| 73 | |
| 74 | // An IJ represents a position on the board. It is 1-indexed and J is upwards. |
| 75 | type IJ struct { |
| 76 | i, j int |
| 77 | } |
| 78 | |
| 79 | func (ij IJ) String() string { |
| 80 | return fmt.Sprintf("%c%d", 'A'+int(ij.i-1), ij.j) |
| 81 | } |
| 82 | |
| 83 | // A go board is played on centers, not on squares, but we think of it internally |
| 84 | // as IJ squares (1, 1) through (19, 19), and draw the lines through the middle |
| 85 | // of those squares. That is how the arithmetic is done converting between |
| 86 | // IJ and XY. |
| 87 | |
| 88 | // IJ converts a position in the board image to a Go position (IJ) on the board. |
| 89 | // The boolean is false if the point does not represent a Go position. |
| 90 | func (xy XY) IJ(d *Dims) (IJ, bool) { |
| 91 | x, y := xy.x, xy.y |
| 92 | x -= d.xInset |
| 93 | y -= d.yInset |
| 94 | i := x / d.squareWidth |
| 95 | j := y / d.squareHeight |
| 96 | // Now zero indexed. Make j goes up and switch to 1-indexed. |
| 97 | j = d.dim - 1 - j |
| 98 | i++ |
| 99 | j++ |
| 100 | if 1 <= i && i <= d.dim && 1 <= j && j <= d.dim { |
| 101 | return IJ{i, j}, true |
| 102 | } |
| 103 | return IJ{}, false |
| 104 | } |
| 105 | |
| 106 | // XY converts a Go position to the upper left corner of the square for that position |
| 107 | // on the board image. |
| 108 | func (ij IJ) XY(d *Dims) XY { |
| 109 | // Change to 0-indexed. |
| 110 | ij.i-- |
| 111 | ij.j-- |
| 112 | // j goes down. |
| 113 | ij.j = (d.dim - 1) - ij.j |
| 114 | return XY{d.xInset + ij.i*d.squareWidth, d.yInset + ij.j*d.squareHeight} |
| 115 | } |
| 116 | |
| 117 | // IJtoXYCenter converts a Go position to the center of the square for that position |
| 118 | // on the board image. |
| 119 | func (ij IJ) XYCenter(d *Dims) XY { |
| 120 | xy := ij.XY(d) |
| 121 | xy.x += d.squareWidth / 2 |
| 122 | xy.y += d.squareHeight / 2 |
| 123 | return xy |
| 124 | } |
| 125 | |
| 126 | // IJtoXYStone converts a Go position to the square holding the stone for that position |
| 127 | // on the board image. |
| 128 | func (ij IJ) XYStone(d *Dims) image.Rectangle { |
| 129 | center := ij.XYCenter(d) |
| 130 | min := image.Point{center.x - d.stoneDiam/2, center.y - d.stoneDiam/2} |
| 131 | max := image.Point{center.x + d.stoneDiam/2, center.y + d.stoneDiam/2} |
| 132 | return image.Rectangle{Min: min, Max: max} |
| 133 | } |