/
polygon.go
55 lines (48 loc) · 1.3 KB
/
polygon.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
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package svg
import (
"cogentcore.org/core/math32"
)
// Polygon is a SVG polygon
type Polygon struct {
Polyline
}
func (g *Polygon) SVGName() string { return "polygon" }
func (g *Polygon) Render(sv *SVG) {
sz := len(g.Points)
if sz < 2 {
return
}
vis, pc := g.PushTransform(sv)
if !vis {
return
}
pc.DrawPolygon(g.Points)
pc.FillStrokeClear()
g.BBoxes(sv)
if mrk := sv.MarkerByName(g, "marker-start"); mrk != nil {
pt := g.Points[0]
ptn := g.Points[1]
ang := math32.Atan2(ptn.Y-pt.Y, ptn.X-pt.X)
mrk.RenderMarker(sv, pt, ang, g.Paint.StrokeStyle.Width.Dots)
}
if mrk := sv.MarkerByName(g, "marker-end"); mrk != nil {
pt := g.Points[sz-1]
ptp := g.Points[sz-2]
ang := math32.Atan2(pt.Y-ptp.Y, pt.X-ptp.X)
mrk.RenderMarker(sv, pt, ang, g.Paint.StrokeStyle.Width.Dots)
}
if mrk := sv.MarkerByName(g, "marker-mid"); mrk != nil {
for i := 1; i < sz-1; i++ {
pt := g.Points[i]
ptp := g.Points[i-1]
ptn := g.Points[i+1]
ang := 0.5 * (math32.Atan2(pt.Y-ptp.Y, pt.X-ptp.X) + math32.Atan2(ptn.Y-pt.Y, ptn.X-pt.X))
mrk.RenderMarker(sv, pt, ang, g.Paint.StrokeStyle.Width.Dots)
}
}
g.RenderChildren(sv)
pc.PopTransform()
}