main.go 3.3 KB
Newer Older
1 2
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
// Use of this source code is governed by your choice of either the
3 4
// FreeType License or the GNU General Public License version 2 (or
// any later version), both of which can be found in the LICENSE file.
5

6 7 8 9 10
// +build ignore
//
// This build tag means that "go install github.com/golang/freetype/..."
// doesn't install this example program. Use "go run main.go" to run it.

11 12 13 14 15 16 17 18 19 20 21
// This program visualizes the quadratic approximation to the circle, used to
// implement round joins when stroking paths. The approximation is used in the
// stroking code for arcs between 0 and 45 degrees, but is visualized here
// between 0 and 90 degrees. The discrepancy between the approximation and the
// true circle is clearly visible at angles above 65 degrees.
package main

import (
	"bufio"
	"fmt"
	"image"
N
Nigel Tao 已提交
22
	"image/color"
23
	"image/draw"
24 25 26 27 28
	"image/png"
	"log"
	"math"
	"os"

N
Nigel Tao 已提交
29
	"github.com/golang/freetype/raster"
30
	"golang.org/x/image/math/fixed"
31 32
)

33
// pDot returns the dot product p·q.
34
func pDot(p, q fixed.Point26_6) fixed.Int52_12 {
35 36
	px, py := int64(p.X), int64(p.Y)
	qx, qy := int64(q.X), int64(q.Y)
37
	return fixed.Int52_12(px*qx + py*qy)
38 39
}

40 41 42
func main() {
	const (
		n = 17
43
		r = 64 * 80
44
	)
45 46
	s := fixed.Int26_6(r * math.Sqrt(2) / 2)
	t := fixed.Int26_6(r * math.Tan(math.Pi/8))
47

48
	m := image.NewRGBA(image.Rect(0, 0, 800, 600))
49
	draw.Draw(m, m.Bounds(), image.NewUniform(color.RGBA{63, 63, 63, 255}), image.ZP, draw.Src)
50 51 52 53 54
	mp := raster.NewRGBAPainter(m)
	mp.SetColor(image.Black)
	z := raster.NewRasterizer(800, 600)

	for i := 0; i < n; i++ {
55 56 57
		cx := fixed.Int26_6(6400 + 12800*(i%4))
		cy := fixed.Int26_6(640 + 8000*(i/4))
		c := fixed.Point26_6{X: cx, Y: cy}
58
		theta := math.Pi * (0.5 + 0.5*float64(i)/(n-1))
59 60 61
		dx := fixed.Int26_6(r * math.Cos(theta))
		dy := fixed.Int26_6(r * math.Sin(theta))
		d := fixed.Point26_6{X: dx, Y: dy}
62 63 64
		// Draw a quarter-circle approximated by two quadratic segments,
		// with each segment spanning 45 degrees.
		z.Start(c)
65 66 67 68 69 70 71 72 73
		z.Add1(c.Add(fixed.Point26_6{X: r, Y: 0}))
		z.Add2(c.Add(fixed.Point26_6{X: r, Y: t}), c.Add(fixed.Point26_6{X: s, Y: s}))
		z.Add2(c.Add(fixed.Point26_6{X: t, Y: r}), c.Add(fixed.Point26_6{X: 0, Y: r}))
		// Add another quadratic segment whose angle ranges between 0 and 90
		// degrees. For an explanation of the magic constants 128, 150, 181 and
		// 256, read the comments in the freetype/raster package.
		dot := 256 * pDot(d, fixed.Point26_6{X: 0, Y: r}) / (r * r)
		multiple := fixed.Int26_6(150-(150-128)*(dot-181)/(256-181)) >> 2
		z.Add2(c.Add(fixed.Point26_6{X: dx, Y: r + dy}.Mul(multiple)), c.Add(d))
74 75 76 77 78 79
		// Close the curve.
		z.Add1(c)
	}
	z.Rasterize(mp)

	for i := 0; i < n; i++ {
80 81
		cx := fixed.Int26_6(6400 + 12800*(i%4))
		cy := fixed.Int26_6(640 + 8000*(i/4))
82 83
		for j := 0; j < n; j++ {
			theta := math.Pi * float64(j) / (n - 1)
84 85 86
			dx := fixed.Int26_6(r * math.Cos(theta))
			dy := fixed.Int26_6(r * math.Sin(theta))
			m.Set(int((cx+dx)/64), int((cy+dy)/64), color.RGBA{255, 255, 0, 255})
87 88 89 90
		}
	}

	// Save that RGBA image to disk.
91
	f, err := os.Create("out.png")
92
	if err != nil {
93
		log.Println(err)
94 95 96 97 98 99
		os.Exit(1)
	}
	defer f.Close()
	b := bufio.NewWriter(f)
	err = png.Encode(b, m)
	if err != nil {
100
		log.Println(err)
101 102 103 104
		os.Exit(1)
	}
	err = b.Flush()
	if err != nil {
105
		log.Println(err)
106 107 108 109
		os.Exit(1)
	}
	fmt.Println("Wrote out.png OK.")
}