| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"bufio" |
| 21 |
"fmt" |
| 22 |
"image" |
| 23 |
"image/color" |
| 24 |
"image/draw" |
| 25 |
"image/png" |
| 26 |
"log" |
| 27 |
"math" |
| 28 |
"os" |
| 29 |
|
| 30 |
"github.com/golang/freetype/raster" |
| 31 |
"golang.org/x/image/math/fixed" |
| 32 |
) |
| 33 |
|
| 34 |
. |
| 35 |
func pDot(p, q fixed.Point26_6) fixed.Int52_12 { |
| 36 |
px, py := int64(p.X), int64(p.Y) |
| 37 |
qx, qy := int64(q.X), int64(q.Y) |
| 38 |
return fixed.Int52_12(px*qx + py*qy) |
| 39 |
} |
| 40 |
|
| 41 |
func main() { |
| 42 |
const ( |
| 43 |
n = 17 |
| 44 |
r = 64 * 80 |
| 45 |
) |
| 46 |
s := fixed.Int26_6(r * math.Sqrt(2) / 2) |
| 47 |
t := fixed.Int26_6(r * math.Tan(math.Pi/8)) |
| 48 |
|
| 49 |
m := image.NewRGBA(image.Rect(0, 0, 800, 600)) |
| 50 |
draw.Draw(m, m.Bounds(), image.NewUniform(color.RGBA{63, 63, 63, 255}), image.ZP, draw.Src) |
| 51 |
mp := raster.NewRGBAPainter(m) |
| 52 |
mp.SetColor(image.Black) |
| 53 |
z := raster.NewRasterizer(800, 600) |
| 54 |
|
| 55 |
for i := 0; i < n; i++ { |
| 56 |
cx := fixed.Int26_6(6400 + 12800*(i%4)) |
| 57 |
cy := fixed.Int26_6(640 + 8000*(i/4)) |
| 58 |
c := fixed.Point26_6{X: cx, Y: cy} |
| 59 |
theta := math.Pi * (0.5 + 0.5*float64(i)/(n-1)) |
| 60 |
dx := fixed.Int26_6(r * math.Cos(theta)) |
| 61 |
dy := fixed.Int26_6(r * math.Sin(theta)) |
| 62 |
d := fixed.Point26_6{X: dx, Y: dy} |
| 63 |
|
| 64 |
|
| 65 |
z.Start(c) |
| 66 |
z.Add1(c.Add(fixed.Point26_6{X: r, Y: 0})) |
| 67 |
z.Add2(c.Add(fixed.Point26_6{X: r, Y: t}), c.Add(fixed.Point26_6{X: s, Y: s})) |
| 68 |
z.Add2(c.Add(fixed.Point26_6{X: t, Y: r}), c.Add(fixed.Point26_6{X: 0, Y: r})) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
dot := 256 * pDot(d, fixed.Point26_6{X: 0, Y: r}) / (r * r) |
| 73 |
multiple := fixed.Int26_6(150-(150-128)*(dot-181)/(256-181)) >> 2 |
| 74 |
z.Add2(c.Add(fixed.Point26_6{X: dx, Y: r + dy}.Mul(multiple)), c.Add(d)) |
| 75 |
|
| 76 |
z.Add1(c) |
| 77 |
} |
| 78 |
z.Rasterize(mp) |
| 79 |
|
| 80 |
for i := 0; i < n; i++ { |
| 81 |
cx := fixed.Int26_6(6400 + 12800*(i%4)) |
| 82 |
cy := fixed.Int26_6(640 + 8000*(i/4)) |
| 83 |
for j := 0; j < n; j++ { |
| 84 |
theta := math.Pi * float64(j) / (n - 1) |
| 85 |
dx := fixed.Int26_6(r * math.Cos(theta)) |
| 86 |
dy := fixed.Int26_6(r * math.Sin(theta)) |
| 87 |
m.Set(int((cx+dx)/64), int((cy+dy)/64), color.RGBA{255, 255, 0, 255}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
outFile, err := os.Create("out.png") |
| 93 |
if err != nil { |
| 94 |
log.Println(err) |
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
defer outFile.Close() |
| 98 |
b := bufio.NewWriter(outFile) |
| 99 |
err = png.Encode(b, m) |
| 100 |
if err != nil { |
| 101 |
log.Println(err) |
| 102 |
os.Exit(1) |
| 103 |
} |
| 104 |
err = b.Flush() |
| 105 |
if err != nil { |
| 106 |
log.Println(err) |
| 107 |
os.Exit(1) |
| 108 |
} |
| 109 |
fmt.Println("Wrote out.png OK.") |
| 110 |
} |