1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package vector_test 6 7 import ( 8 "image" 9 "image/draw" 10 "os" 11 12 "golang.org/x/image/vector" 13 ) 14 15 func Example_draw() { 16 const ( 17 width = 30 18 height = 20 19 ) 20 21 // Define a closed shape with three edges: two linear and one quadratic. 22 // One of its vertices is at the top-left corner of the (1, 2) pixel, which 23 // is also the bottom-right corner of the (0, 1) pixel. 24 // 25 // Co-ordinates can be floating point numbers, not just integers. They can 26 // also be outside the vector.Rasterizer's dimensions. The shapes will be 27 // clipped during rasterization. 28 r := vector.NewRasterizer(width, height) 29 r.DrawOp = draw.Src 30 r.MoveTo(1, 2) 31 r.LineTo(20, 2) 32 r.QuadTo(40.5, 15, 10, 20) 33 r.ClosePath() 34 35 // Finish the rasterization: the conversion from vector graphics (shapes) 36 // to raster graphics (pixels). Co-ordinates are now integers. 37 dst := image.NewAlpha(image.Rect(0, 0, width, height)) 38 r.Draw(dst, dst.Bounds(), image.Opaque, image.Point{}) 39 40 // Visualize the pixels. 41 const asciiArt = ".++8" 42 buf := make([]byte, 0, height*(width+1)) 43 for y := 0; y < height; y++ { 44 for x := 0; x < width; x++ { 45 a := dst.AlphaAt(x, y).A 46 buf = append(buf, asciiArt[a>>6]) 47 } 48 buf = append(buf, '\n') 49 } 50 os.Stdout.Write(buf) 51 52 // Output: 53 // .............................. 54 // .............................. 55 // .8888888888888888888+......... 56 // .+88888888888888888888+....... 57 // ..888888888888888888888+...... 58 // ..+888888888888888888888+..... 59 // ...8888888888888888888888+.... 60 // ...+8888888888888888888888+... 61 // ....88888888888888888888888+.. 62 // ....+88888888888888888888888.. 63 // .....88888888888888888888888.. 64 // .....+8888888888888888888888.. 65 // ......8888888888888888888888.. 66 // ......+88888888888888888888+.. 67 // .......8888888888888888888+... 68 // .......+88888888888888888..... 69 // ........888888888888888+...... 70 // ........+88888888888+......... 71 // .........8888888++............ 72 // .........+8+++................ 73 } 74