2010-06-30 07:10:17 +02:00
|
|
|
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by your choice of either the
|
2010-08-03 03:07:23 +02:00
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
2010-06-30 07:10:17 +02:00
|
|
|
|
|
|
|
// 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"
|
2011-10-06 04:55:02 +02:00
|
|
|
"image/color"
|
2011-07-10 07:05:40 +02:00
|
|
|
"image/draw"
|
2010-06-30 07:10:17 +02:00
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
|
2015-08-12 06:30:01 +02:00
|
|
|
"github.com/golang/freetype/raster"
|
2010-06-30 07:10:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
const (
|
|
|
|
n = 17
|
|
|
|
r = 256 * 80
|
|
|
|
)
|
2010-07-01 01:42:43 +02:00
|
|
|
s := raster.Fix32(r * math.Sqrt(2) / 2)
|
|
|
|
t := raster.Fix32(r * math.Tan(math.Pi/8))
|
2010-06-30 07:10:17 +02:00
|
|
|
|
2011-09-14 14:02:41 +02:00
|
|
|
m := image.NewRGBA(image.Rect(0, 0, 800, 600))
|
2012-05-07 14:52:01 +02:00
|
|
|
draw.Draw(m, m.Bounds(), image.NewUniform(color.RGBA{63, 63, 63, 255}), image.ZP, draw.Src)
|
2010-06-30 07:10:17 +02:00
|
|
|
mp := raster.NewRGBAPainter(m)
|
|
|
|
mp.SetColor(image.Black)
|
|
|
|
z := raster.NewRasterizer(800, 600)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
2010-07-01 01:42:43 +02:00
|
|
|
cx := raster.Fix32(25600 + 51200*(i%4))
|
|
|
|
cy := raster.Fix32(2560 + 32000*(i/4))
|
2012-05-07 14:52:01 +02:00
|
|
|
c := raster.Point{X: cx, Y: cy}
|
2010-06-30 07:10:17 +02:00
|
|
|
theta := math.Pi * (0.5 + 0.5*float64(i)/(n-1))
|
2010-07-01 01:42:43 +02:00
|
|
|
dx := raster.Fix32(r * math.Cos(theta))
|
|
|
|
dy := raster.Fix32(r * math.Sin(theta))
|
2012-05-07 14:52:01 +02:00
|
|
|
d := raster.Point{X: dx, Y: dy}
|
2010-06-30 07:10:17 +02:00
|
|
|
// Draw a quarter-circle approximated by two quadratic segments,
|
|
|
|
// with each segment spanning 45 degrees.
|
|
|
|
z.Start(c)
|
2012-05-07 14:52:01 +02:00
|
|
|
z.Add1(c.Add(raster.Point{X: r, Y: 0}))
|
|
|
|
z.Add2(c.Add(raster.Point{X: r, Y: t}), c.Add(raster.Point{X: s, Y: s}))
|
|
|
|
z.Add2(c.Add(raster.Point{X: t, Y: r}), c.Add(raster.Point{X: 0, Y: r}))
|
2010-06-30 07:10:17 +02:00
|
|
|
// Add another quadratic segment whose angle ranges between 0 and 90 degrees.
|
|
|
|
// For an explanation of the magic constants 22, 150, 181 and 256, read the
|
|
|
|
// comments in the freetype/raster package.
|
2012-05-07 14:52:01 +02:00
|
|
|
dot := 256 * d.Dot(raster.Point{X: 0, Y: r}) / (r * r)
|
2010-07-01 01:42:43 +02:00
|
|
|
multiple := raster.Fix32(150 - 22*(dot-181)/(256-181))
|
2012-05-07 14:52:01 +02:00
|
|
|
z.Add2(c.Add(raster.Point{X: dx, Y: r + dy}.Mul(multiple)), c.Add(d))
|
2010-06-30 07:10:17 +02:00
|
|
|
// Close the curve.
|
|
|
|
z.Add1(c)
|
|
|
|
}
|
|
|
|
z.Rasterize(mp)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
2010-07-01 01:42:43 +02:00
|
|
|
cx := raster.Fix32(25600 + 51200*(i%4))
|
|
|
|
cy := raster.Fix32(2560 + 32000*(i/4))
|
2010-06-30 07:10:17 +02:00
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
theta := math.Pi * float64(j) / (n - 1)
|
2010-07-01 01:42:43 +02:00
|
|
|
dx := raster.Fix32(r * math.Cos(theta))
|
|
|
|
dy := raster.Fix32(r * math.Sin(theta))
|
2011-10-06 04:55:02 +02:00
|
|
|
m.Set(int((cx+dx)/256), int((cy+dy)/256), color.RGBA{255, 255, 0, 255})
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save that RGBA image to disk.
|
2011-04-11 02:48:00 +02:00
|
|
|
f, err := os.Create("out.png")
|
2010-06-30 07:10:17 +02:00
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
log.Println(err)
|
2010-06-30 07:10:17 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b := bufio.NewWriter(f)
|
|
|
|
err = png.Encode(b, m)
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
log.Println(err)
|
2010-06-30 07:10:17 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = b.Flush()
|
|
|
|
if err != nil {
|
2010-10-15 05:02:11 +02:00
|
|
|
log.Println(err)
|
2010-06-30 07:10:17 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
fmt.Println("Wrote out.png OK.")
|
|
|
|
}
|