/
draw.go
88 lines (73 loc) · 2.4 KB
/
draw.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
"image"
"image/color"
)
// DrawLine draws the line represented by the Points
func DrawLine(p Points, width, height float64, name string) {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
gc := draw2dimg.NewGraphicContext(dest)
// Set some properties
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)
paths := []*draw2d.Path{}
// Draw a path
for idx, point := range p[:len(p)-1] {
path := new(draw2d.Path)
path.MoveTo(float64(point.X), height-float64(point.Y))
path.LineTo(float64(p[idx+1].X), height-float64(p[idx+1].Y))
paths = append(paths, path)
}
gc.Stroke(paths...)
gc.FillStroke()
// Save to file
draw2dimg.SaveToPngFile(name, dest)
}
// DrawClosedLine draws the line represented by the Points and closes it.
func DrawClosedLine(p Points, width, height float64, name string) {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
gc := draw2dimg.NewGraphicContext(dest)
// Set some properties
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)
paths := []*draw2d.Path{}
// Draw a path
for idx, point := range p[:len(p)] {
path := new(draw2d.Path)
path.MoveTo(float64(point.X), height-float64(point.Y))
if idx < len(p)-1 {
path.LineTo(float64(p[idx+1].X), height-float64(p[idx+1].Y))
} else {
path.LineTo(float64(p[0].X), height-float64(p[0].Y))
}
paths = append(paths, path)
}
gc.Stroke(paths...)
gc.FillStroke()
// Save to file
draw2dimg.SaveToPngFile(name, dest)
}
// DrawPolygon draws the polygon represented by Points on the canvas
func DrawPolygon(p Points, width, height float64, name string) {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
gc := draw2dimg.NewGraphicContext(dest)
// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)
// Draw a closed shape
gc.MoveTo(float64(p[0].X), height-float64(p[0].Y))
for _, point := range p[1:] {
gc.LineTo(float64(point.X), height-float64(point.Y))
gc.MoveTo(float64(point.X), height-float64(point.Y))
}
gc.Close()
gc.FillStroke()
// Save to file
draw2dimg.SaveToPngFile(name, dest)
}