File: example_test.go

package info (click to toggle)
golang-golang-x-image 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 18,044 kB
  • sloc: makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package vector_test

import (
	"image"
	"image/draw"
	"os"

	"golang.org/x/image/vector"
)

func Example_draw() {
	const (
		width  = 30
		height = 20
	)

	// Define a closed shape with three edges: two linear and one quadratic.
	// One of its vertices is at the top-left corner of the (1, 2) pixel, which
	// is also the bottom-right corner of the (0, 1) pixel.
	//
	// Co-ordinates can be floating point numbers, not just integers. They can
	// also be outside the vector.Rasterizer's dimensions. The shapes will be
	// clipped during rasterization.
	r := vector.NewRasterizer(width, height)
	r.DrawOp = draw.Src
	r.MoveTo(1, 2)
	r.LineTo(20, 2)
	r.QuadTo(40.5, 15, 10, 20)
	r.ClosePath()

	// Finish the rasterization: the conversion from vector graphics (shapes)
	// to raster graphics (pixels). Co-ordinates are now integers.
	dst := image.NewAlpha(image.Rect(0, 0, width, height))
	r.Draw(dst, dst.Bounds(), image.Opaque, image.Point{})

	// Visualize the pixels.
	const asciiArt = ".++8"
	buf := make([]byte, 0, height*(width+1))
	for y := 0; y < height; y++ {
		for x := 0; x < width; x++ {
			a := dst.AlphaAt(x, y).A
			buf = append(buf, asciiArt[a>>6])
		}
		buf = append(buf, '\n')
	}
	os.Stdout.Write(buf)

	// Output:
	// ..............................
	// ..............................
	// .8888888888888888888+.........
	// .+88888888888888888888+.......
	// ..888888888888888888888+......
	// ..+888888888888888888888+.....
	// ...8888888888888888888888+....
	// ...+8888888888888888888888+...
	// ....88888888888888888888888+..
	// ....+88888888888888888888888..
	// .....88888888888888888888888..
	// .....+8888888888888888888888..
	// ......8888888888888888888888..
	// ......+88888888888888888888+..
	// .......8888888888888888888+...
	// .......+88888888888888888.....
	// ........888888888888888+......
	// ........+88888888888+.........
	// .........8888888++............
	// .........+8+++................
}