-
Notifications
You must be signed in to change notification settings - Fork 204
/
canvas.go
703 lines (608 loc) · 17.8 KB
/
canvas.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright ©2015 The gonum 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 draw
import (
"fmt"
"image/color"
"math"
"strings"
"github.com/gonum/plot/vg"
"github.com/gonum/plot/vg/vgeps"
"github.com/gonum/plot/vg/vgimg"
"github.com/gonum/plot/vg/vgpdf"
"github.com/gonum/plot/vg/vgsvg"
)
// A Canvas is a vector graphics canvas along with
// an associated Rectangle defining a section of the canvas
// to which drawing should take place.
type Canvas struct {
vg.Canvas
Rectangle
}
// TextStyle describes what text will look like.
type TextStyle struct {
// Color is the text color.
Color color.Color
// Font is the font description.
Font vg.Font
}
// LineStyle describes what a line will look like.
type LineStyle struct {
// Color is the color of the line.
Color color.Color
// Width is the width of the line.
Width vg.Length
Dashes []vg.Length
DashOffs vg.Length
}
// A GlyphStyle specifies the look of a glyph used to draw
// a point on a plot.
type GlyphStyle struct {
// Color is the color used to draw the glyph.
color.Color
// Radius specifies the size of the glyph's radius.
Radius vg.Length
// Shape draws the shape of the glyph.
Shape GlyphDrawer
}
// A GlyphDrawer wraps the DrawGlyph function.
type GlyphDrawer interface {
// DrawGlyph draws the glyph at the given
// point, with the given color and radius.
DrawGlyph(*Canvas, GlyphStyle, Point)
}
// DrawGlyph draws the given glyph to the draw
// area. If the point is not within the Canvas
// or the sty.Shape is nil then nothing is drawn.
func (c *Canvas) DrawGlyph(sty GlyphStyle, pt Point) {
if sty.Shape == nil || !c.Contains(pt) {
return
}
c.SetColor(sty.Color)
sty.Shape.DrawGlyph(c, sty, pt)
}
// DrawGlyphNoClip draws the given glyph to the draw
// area. If the sty.Shape is nil then nothing is drawn.
func (c *Canvas) DrawGlyphNoClip(sty GlyphStyle, pt Point) {
if sty.Shape == nil {
return
}
c.SetColor(sty.Color)
sty.Shape.DrawGlyph(c, sty, pt)
}
// Rectangle returns the rectangle surrounding this glyph,
// assuming that it is drawn centered at 0,0
func (g GlyphStyle) Rectangle() Rectangle {
return Rectangle{
Min: Point{-g.Radius, -g.Radius},
Max: Point{+g.Radius, +g.Radius},
}
}
// CircleGlyph is a glyph that draws a solid circle.
type CircleGlyph struct{}
// DrawGlyph implements the GlyphDrawer interface.
func (CircleGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
var p vg.Path
p.Move(pt.X+sty.Radius, pt.Y)
p.Arc(pt.X, pt.Y, sty.Radius, 0, 2*math.Pi)
p.Close()
c.Fill(p)
}
// RingGlyph is a glyph that draws the outline of a circle.
type RingGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (RingGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
c.SetLineStyle(LineStyle{Color: sty.Color, Width: vg.Points(0.5)})
var p vg.Path
p.Move(pt.X+sty.Radius, pt.Y)
p.Arc(pt.X, pt.Y, sty.Radius, 0, 2*math.Pi)
p.Close()
c.Stroke(p)
}
const (
cosπover4 = vg.Length(.707106781202420)
sinπover6 = vg.Length(.500000000025921)
cosπover6 = vg.Length(.866025403769473)
)
// SquareGlyph is a glyph that draws the outline of a square.
type SquareGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (SquareGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
c.SetLineStyle(LineStyle{Color: sty.Color, Width: vg.Points(0.5)})
x := (sty.Radius-sty.Radius*cosπover4)/2 + sty.Radius*cosπover4
var p vg.Path
p.Move(pt.X-x, pt.Y-x)
p.Line(pt.X+x, pt.Y-x)
p.Line(pt.X+x, pt.Y+x)
p.Line(pt.X-x, pt.Y+x)
p.Close()
c.Stroke(p)
}
// BoxGlyph is a glyph that draws a filled square.
type BoxGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (BoxGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
x := (sty.Radius-sty.Radius*cosπover4)/2 + sty.Radius*cosπover4
var p vg.Path
p.Move(pt.X-x, pt.Y-x)
p.Line(pt.X+x, pt.Y-x)
p.Line(pt.X+x, pt.Y+x)
p.Line(pt.X-x, pt.Y+x)
p.Close()
c.Fill(p)
}
// TriangleGlyph is a glyph that draws the outline of a triangle.
type TriangleGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (TriangleGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
c.SetLineStyle(LineStyle{Color: sty.Color, Width: vg.Points(0.5)})
r := sty.Radius + (sty.Radius-sty.Radius*sinπover6)/2
var p vg.Path
p.Move(pt.X, pt.Y+r)
p.Line(pt.X-r*cosπover6, pt.Y-r*sinπover6)
p.Line(pt.X+r*cosπover6, pt.Y-r*sinπover6)
p.Close()
c.Stroke(p)
}
// PyramidGlyph is a glyph that draws a filled triangle.
type PyramidGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (PyramidGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
r := sty.Radius + (sty.Radius-sty.Radius*sinπover6)/2
var p vg.Path
p.Move(pt.X, pt.Y+r)
p.Line(pt.X-r*cosπover6, pt.Y-r*sinπover6)
p.Line(pt.X+r*cosπover6, pt.Y-r*sinπover6)
p.Close()
c.Fill(p)
}
// PlusGlyph is a glyph that draws a plus sign
type PlusGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (PlusGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
c.SetLineStyle(LineStyle{Color: sty.Color, Width: vg.Points(0.5)})
r := sty.Radius
var p vg.Path
p.Move(pt.X, pt.Y+r)
p.Line(pt.X, pt.Y-r)
c.Stroke(p)
p = vg.Path{}
p.Move(pt.X-r, pt.Y)
p.Line(pt.X+r, pt.Y)
c.Stroke(p)
}
// CrossGlyph is a glyph that draws a big X.
type CrossGlyph struct{}
// DrawGlyph implements the Glyph interface.
func (CrossGlyph) DrawGlyph(c *Canvas, sty GlyphStyle, pt Point) {
c.SetLineStyle(LineStyle{Color: sty.Color, Width: vg.Points(0.5)})
r := sty.Radius * cosπover4
var p vg.Path
p.Move(pt.X-r, pt.Y-r)
p.Line(pt.X+r, pt.Y+r)
c.Stroke(p)
p = vg.Path{}
p.Move(pt.X-r, pt.Y+r)
p.Line(pt.X+r, pt.Y-r)
c.Stroke(p)
}
// New returns a new (bounded) draw.Canvas.
func New(c vg.CanvasSizer) Canvas {
w, h := c.Size()
return NewCanvas(c, w, h)
}
// NewFormattedCanvas creates a new vg.CanvasWriterTo with the specified
// image format.
//
// Supported formats are:
//
// eps, jpg|jpeg, pdf, png, svg, and tif|tiff.
func NewFormattedCanvas(w, h vg.Length, format string) (vg.CanvasWriterTo, error) {
var c vg.CanvasWriterTo
switch format {
case "eps":
c = vgeps.New(w, h)
case "jpg", "jpeg":
c = vgimg.JpegCanvas{Canvas: vgimg.New(w, h)}
case "pdf":
c = vgpdf.New(w, h)
case "png":
c = vgimg.PngCanvas{Canvas: vgimg.New(w, h)}
case "svg":
c = vgsvg.New(w, h)
case "tif", "tiff":
c = vgimg.TiffCanvas{Canvas: vgimg.New(w, h)}
default:
return nil, fmt.Errorf("unsupported format: %q", format)
}
return c, nil
}
// NewCanvas returns a new (bounded) draw.Canvas of the given size.
func NewCanvas(c vg.Canvas, w, h vg.Length) Canvas {
return Canvas{
Canvas: c,
Rectangle: Rectangle{
Min: Point{0, 0},
Max: Point{w, h},
},
}
}
// Center returns the center point of the area
func (c *Canvas) Center() Point {
return Point{
X: (c.Max.X-c.Min.X)/2 + c.Min.X,
Y: (c.Max.Y-c.Min.Y)/2 + c.Min.Y,
}
}
// Contains returns true if the Canvas contains the point.
func (c *Canvas) Contains(p Point) bool {
return c.ContainsX(p.X) && c.ContainsY(p.Y)
}
// Contains returns true if the Canvas contains the
// x coordinate.
func (c *Canvas) ContainsX(x vg.Length) bool {
return x <= c.Max.X+slop && x >= c.Min.X-slop
}
// ContainsY returns true if the Canvas contains the
// y coordinate.
func (c *Canvas) ContainsY(y vg.Length) bool {
return y <= c.Max.Y+slop && y >= c.Min.Y-slop
}
// X returns the value of x, given in the unit range,
// in the drawing coordinates of this draw area.
// A value of 0, for example, will return the minimum
// x value of the draw area and a value of 1 will
// return the maximum.
func (c *Canvas) X(x float64) vg.Length {
return vg.Length(x)*(c.Max.X-c.Min.X) + c.Min.X
}
// Y returns the value of x, given in the unit range,
// in the drawing coordinates of this draw area.
// A value of 0, for example, will return the minimum
// y value of the draw area and a value of 1 will
// return the maximum.
func (c *Canvas) Y(y float64) vg.Length {
return vg.Length(y)*(c.Max.Y-c.Min.Y) + c.Min.Y
}
// Crop returns a new Canvas corresponding to the receiver
// area with the given number of inches added to the minimum
// and maximum x and y values of the Canvas's Rectangle.
// Note that cropping the right and top sides of the canvas
// requires specifying negative values of right and top.
func (c Canvas) Crop(right, bottom, left, top vg.Length) Canvas {
minpt := Point{
X: c.Min.X + right,
Y: c.Min.Y + bottom,
}
maxpt := Point{
X: c.Max.X + left,
Y: c.Max.Y + top,
}
return Canvas{
Canvas: c,
Rectangle: Rectangle{Min: minpt, Max: maxpt},
}
}
// Tiles is a holder for subcanvases returned by the Tile function.
type Tiles struct {
Values []Canvas
Rows, Cols int
}
// At returns the canvas at the given row and column.
func (t Tiles) At(row, col int) Canvas {
return t.Values[row*t.Cols+col]
}
// Tiles returns new Canvases corresponding to the reciever canvas tiled
// into the given number of rows and columns (row major, starting from the
// top left corner) where top, bottom, left, right, padx,
// and pady are the padding above, below, to the left, to the right, between columns,
// and between rows of the tiles, respectively.
func (c Canvas) Tiles(rows, cols int, top, bottom, left, right, padx, pady vg.Length) *Tiles {
o := &Tiles{
Values: make([]Canvas, 0, rows*cols),
Rows: rows,
Cols: cols,
}
tileH := (c.Max.Y - c.Min.Y - top - bottom - vg.Length(rows-1)*pady) / vg.Length(rows)
tileW := (c.Max.X - c.Min.X - left - right - vg.Length(cols-1)*padx) / vg.Length(cols)
for j := 0; j < rows; j++ {
ymax := c.Max.Y - top - vg.Length(j)*(pady+tileH)
ymin := ymax - tileH
for i := 0; i < cols; i++ {
xmin := c.Min.X + left + vg.Length(i)*(padx+tileW)
xmax := xmin + tileW
o.Values = append(o.Values, Canvas{
Canvas: vg.Canvas(c),
Rectangle: Rectangle{
Min: Point{X: xmin, Y: ymin},
Max: Point{X: xmax, Y: ymax},
},
})
}
}
return o
}
// SetLineStyle sets the current line style
func (c *Canvas) SetLineStyle(sty LineStyle) {
c.SetColor(sty.Color)
c.SetLineWidth(sty.Width)
var dashDots []vg.Length
for _, dash := range sty.Dashes {
dashDots = append(dashDots, dash)
}
c.SetLineDash(dashDots, sty.DashOffs)
}
// StrokeLines draws a line connecting a set of points
// in the given Canvas.
func (c *Canvas) StrokeLines(sty LineStyle, lines ...[]Point) {
if len(lines) == 0 {
return
}
c.SetLineStyle(sty)
for _, l := range lines {
if len(l) == 0 {
continue
}
var p vg.Path
p.Move(l[0].X, l[0].Y)
for _, pt := range l[1:] {
p.Line(pt.X, pt.Y)
}
c.Stroke(p)
}
}
// StrokeLine2 draws a line between two points in the given
// Canvas.
func (c *Canvas) StrokeLine2(sty LineStyle, x0, y0, x1, y1 vg.Length) {
c.StrokeLines(sty, []Point{{x0, y0}, {x1, y1}})
}
// ClipLineXY returns a slice of lines that
// represent the given line clipped in both
// X and Y directions.
func (c *Canvas) ClipLinesXY(lines ...[]Point) [][]Point {
return c.ClipLinesY(c.ClipLinesX(lines...)...)
}
// ClipLineX returns a slice of lines that
// represent the given line clipped in the
// X direction.
func (c *Canvas) ClipLinesX(lines ...[]Point) (clipped [][]Point) {
var lines1 [][]Point
for _, line := range lines {
ls := clipLine(isLeft, Point{c.Max.X, c.Min.Y}, Point{-1, 0}, line)
lines1 = append(lines1, ls...)
}
for _, line := range lines1 {
ls := clipLine(isRight, Point{c.Min.X, c.Min.Y}, Point{1, 0}, line)
clipped = append(clipped, ls...)
}
return
}
// ClipLineY returns a slice of lines that
// represent the given line clipped in the
// Y direction.
func (c *Canvas) ClipLinesY(lines ...[]Point) (clipped [][]Point) {
var lines1 [][]Point
for _, line := range lines {
ls := clipLine(isAbove, Point{c.Min.X, c.Min.Y}, Point{0, -1}, line)
lines1 = append(lines1, ls...)
}
for _, line := range lines1 {
ls := clipLine(isBelow, Point{c.Min.X, c.Max.Y}, Point{0, 1}, line)
clipped = append(clipped, ls...)
}
return
}
// clipLine performs clipping of a line by a single
// clipping line specified by the norm, clip point,
// and in function.
func clipLine(in func(Point, Point) bool, clip, norm Point, pts []Point) (lines [][]Point) {
var l []Point
for i := 1; i < len(pts); i++ {
cur, next := pts[i-1], pts[i]
curIn, nextIn := in(cur, clip), in(next, clip)
switch {
case curIn && nextIn:
l = append(l, cur)
case curIn && !nextIn:
l = append(l, cur, isect(cur, next, clip, norm))
lines = append(lines, l)
l = []Point{}
case !curIn && !nextIn:
// do nothing
default: // !curIn && nextIn
l = append(l, isect(cur, next, clip, norm))
}
if nextIn && i == len(pts)-1 {
l = append(l, next)
}
}
if len(l) > 1 {
lines = append(lines, l)
}
return
}
// FillPolygon fills a polygon with the given color.
func (c *Canvas) FillPolygon(clr color.Color, pts []Point) {
if len(pts) == 0 {
return
}
c.SetColor(clr)
var p vg.Path
p.Move(pts[0].X, pts[0].Y)
for _, pt := range pts[1:] {
p.Line(pt.X, pt.Y)
}
p.Close()
c.Fill(p)
}
// ClipPolygonXY returns a slice of lines that
// represent the given polygon clipped in both
// X and Y directions.
func (c *Canvas) ClipPolygonXY(pts []Point) []Point {
return c.ClipPolygonY(c.ClipPolygonX(pts))
}
// ClipPolygonX returns a slice of lines that
// represent the given polygon clipped in the
// X direction.
func (c *Canvas) ClipPolygonX(pts []Point) []Point {
return clipPoly(isLeft, Point{c.Max.X, c.Min.Y}, Point{-1, 0},
clipPoly(isRight, Point{c.Min.X, c.Min.Y}, Point{1, 0}, pts))
}
// ClipPolygonY returns a slice of lines that
// represent the given polygon clipped in the
// Y direction.
func (c *Canvas) ClipPolygonY(pts []Point) []Point {
return clipPoly(isBelow, Point{c.Min.X, c.Max.Y}, Point{0, 1},
clipPoly(isAbove, Point{c.Min.X, c.Min.Y}, Point{0, -1}, pts))
}
// clipPoly performs clipping of a polygon by a single
// clipping line specified by the norm, clip point,
// and in function.
func clipPoly(in func(Point, Point) bool, clip, norm Point, pts []Point) (clipped []Point) {
for i := 0; i < len(pts); i++ {
j := i + 1
if i == len(pts)-1 {
j = 0
}
cur, next := pts[i], pts[j]
curIn, nextIn := in(cur, clip), in(next, clip)
switch {
case curIn && nextIn:
clipped = append(clipped, cur)
case curIn && !nextIn:
clipped = append(clipped, cur, isect(cur, next, clip, norm))
case !curIn && !nextIn:
// do nothing
default: // !curIn && nextIn
clipped = append(clipped, isect(cur, next, clip, norm))
}
}
return
}
// slop is some slop for floating point equality
const slop = 3e-8 // ≈ √1⁻¹⁵
func isLeft(p, clip Point) bool {
return p.X <= clip.X+slop
}
func isRight(p, clip Point) bool {
return p.X >= clip.X-slop
}
func isBelow(p, clip Point) bool {
return p.Y <= clip.Y+slop
}
func isAbove(p, clip Point) bool {
return p.Y >= clip.Y-slop
}
// isect returns the intersection of a line p0→p1 with the
// clipping line specified by the clip point and normal.
func isect(p0, p1, clip, norm Point) Point {
// t = (norm · (p0 - clip)) / (norm · (p0 - p1))
t := p0.minus(clip).dot(norm) / p0.minus(p1).dot(norm)
// p = p0 + t*(p1 - p0)
return p1.minus(p0).scale(t).plus(p0)
}
// FillText fills lines of text in the draw area.
// The text is offset by its width times xalign and
// its height times yalign. x and y give the bottom
// left corner of the text befor e it is offset.
func (c *Canvas) FillText(sty TextStyle, x, y vg.Length, xalign, yalign float64, txt string) {
txt = strings.TrimRight(txt, "\n")
if len(txt) == 0 {
return
}
c.SetColor(sty.Color)
ht := sty.Height(txt)
y += ht*vg.Length(yalign) - sty.Font.Extents().Ascent
nl := textNLines(txt)
for i, line := range strings.Split(txt, "\n") {
xoffs := vg.Length(xalign) * sty.Font.Width(line)
n := vg.Length(nl - i)
c.FillString(sty.Font, x+xoffs, y+n*sty.Font.Size, line)
}
}
// Width returns the width of lines of text
// when using the given font.
func (sty TextStyle) Width(txt string) (max vg.Length) {
txt = strings.TrimRight(txt, "\n")
for _, line := range strings.Split(txt, "\n") {
if w := sty.Font.Width(line); w > max {
max = w
}
}
return
}
// Height returns the height of the text when using
// the given font.
func (sty TextStyle) Height(txt string) vg.Length {
nl := textNLines(txt)
if nl == 0 {
return vg.Length(0)
}
e := sty.Font.Extents()
return e.Height*vg.Length(nl-1) + e.Ascent
}
// Rectangle returns a rectangle giving the bounds of
// this text assuming that it is drawn at 0, 0
func (sty TextStyle) Rectangle(txt string) Rectangle {
return Rectangle{Max: Point{sty.Width(txt), sty.Height(txt)}}
}
// textNLines returns the number of lines in the text.
func textNLines(txt string) int {
txt = strings.TrimRight(txt, "\n")
if len(txt) == 0 {
return 0
}
n := 1
for _, r := range txt {
if r == '\n' {
n++
}
}
return n
}
// A Rectangle represents a rectangular region of 2d space.
type Rectangle struct {
Min Point
Max Point
}
// Size returns the width and height of a Rectangle.
func (r Rectangle) Size() Point {
return Point{
X: r.Max.X - r.Min.X,
Y: r.Max.Y - r.Min.Y,
}
}
// Path returns the path of a Rect specified by its
// upper left corner, width and height.
func (r Rectangle) Path() (p vg.Path) {
p.Move(r.Min.X, r.Min.Y)
p.Line(r.Max.X, r.Min.Y)
p.Line(r.Max.X, r.Max.Y)
p.Line(r.Min.X, r.Max.Y)
p.Close()
return
}
// A Point is a location in 2d space.
//
// Points are used for drawing, not for data. For
// data, see the XYer interface.
type Point struct {
X, Y vg.Length
}
// dot returns the dot product of two points.
func (p Point) dot(q Point) vg.Length {
return p.X*q.X + p.Y*q.Y
}
// plus returns the component-wise sum of two points.
func (p Point) plus(q Point) Point {
return Point{p.X + q.X, p.Y + q.Y}
}
// minus returns the component-wise difference of two points.
func (p Point) minus(q Point) Point {
return Point{p.X - q.X, p.Y - q.Y}
}
// scale returns the component-wise product of a point and a scalar.
func (p Point) scale(s vg.Length) Point {
return Point{p.X * s, p.Y * s}
}