...

Source file src/github.com/golang/freetype/example/round/main.go

Documentation: github.com/golang/freetype/example/round

     1  // Copyright 2010 The Freetype-Go Authors. All rights reserved.
     2  // Use of this source code is governed by your choice of either the
     3  // FreeType License or the GNU General Public License version 2 (or
     4  // any later version), both of which can be found in the LICENSE file.
     5  
     6  // +build example
     7  //
     8  // This build tag means that "go install github.com/golang/freetype/..."
     9  // doesn't install this example program. Use "go run main.go" to run it or "go
    10  // install -tags=example" to install it.
    11  
    12  // This program visualizes the quadratic approximation to the circle, used to
    13  // implement round joins when stroking paths. The approximation is used in the
    14  // stroking code for arcs between 0 and 45 degrees, but is visualized here
    15  // between 0 and 90 degrees. The discrepancy between the approximation and the
    16  // true circle is clearly visible at angles above 65 degrees.
    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  // pDot returns the dot product p·q.
    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  		// Draw a quarter-circle approximated by two quadratic segments,
    64  		// with each segment spanning 45 degrees.
    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  		// Add another quadratic segment whose angle ranges between 0 and 90
    70  		// degrees. For an explanation of the magic constants 128, 150, 181 and
    71  		// 256, read the comments in the freetype/raster package.
    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  		// Close the curve.
    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  	// Save that RGBA image to disk.
    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  }
   111  

View as plain text