added some nonstandard performance flags
This commit is contained in:
parent
bb244c4868
commit
99b9763f00
2 changed files with 32 additions and 11 deletions
13
canvas.go
13
canvas.go
|
@ -117,6 +117,19 @@ const (
|
||||||
Bottom
|
Bottom
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Performance is a nonstandard setting to improve the
|
||||||
|
// performance of the rendering in some circumstances.
|
||||||
|
// Disabling self intersections will lead to incorrect
|
||||||
|
// rendering of self intersecting polygons, but will
|
||||||
|
// yield better performance when not using the polygons
|
||||||
|
// are not self intersecting. Assuming convex polygons
|
||||||
|
// will break concave polygons, but improve performance
|
||||||
|
// even further
|
||||||
|
var Performance = struct {
|
||||||
|
IgnoreSelfIntersections bool
|
||||||
|
AssumeConvex bool
|
||||||
|
}{}
|
||||||
|
|
||||||
// New creates a new canvas with the given viewport coordinates.
|
// New creates a new canvas with the given viewport coordinates.
|
||||||
// While all functions on the canvas use the top left point as
|
// While all functions on the canvas use the top left point as
|
||||||
// the origin, since GL uses the bottom left coordinate, the
|
// the origin, since GL uses the bottom left coordinate, the
|
||||||
|
|
16
path2d.go
16
path2d.go
|
@ -46,6 +46,10 @@ func (p *Path2D) MoveTo(x, y float64) {
|
||||||
|
|
||||||
// LineTo (see equivalent function on canvas type)
|
// LineTo (see equivalent function on canvas type)
|
||||||
func (p *Path2D) LineTo(x, y float64) {
|
func (p *Path2D) LineTo(x, y float64) {
|
||||||
|
p.lineTo(x, y, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Path2D) lineTo(x, y float64, checkSelfIntersection bool) {
|
||||||
count := len(p.p)
|
count := len(p.p)
|
||||||
if count > 0 && isSamePoint(p.p[len(p.p)-1].pos, vec{x, y}, 0.1) {
|
if count > 0 && isSamePoint(p.p[len(p.p)-1].pos, vec{x, y}, 0.1) {
|
||||||
return
|
return
|
||||||
|
@ -72,10 +76,11 @@ func (p *Path2D) LineTo(x, y float64) {
|
||||||
newp.flags |= pathSelfIntersects
|
newp.flags |= pathSelfIntersects
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(p.p) < 4 {
|
if len(p.p) < 4 || Performance.AssumeConvex {
|
||||||
newp.flags |= pathIsConvex
|
newp.flags |= pathIsConvex
|
||||||
} else if prev.flags&pathIsConvex > 0 {
|
} else if prev.flags&pathIsConvex > 0 {
|
||||||
cuts := false
|
cuts := false
|
||||||
|
if checkSelfIntersection && !Performance.IgnoreSelfIntersections {
|
||||||
b0, b1 := prev.pos, vec{x, y}
|
b0, b1 := prev.pos, vec{x, y}
|
||||||
for i := 1; i < count; i++ {
|
for i := 1; i < count; i++ {
|
||||||
a0, a1 := p.p[i-1].pos, p.p[i].pos
|
a0, a1 := p.p[i-1].pos, p.p[i].pos
|
||||||
|
@ -85,6 +90,7 @@ func (p *Path2D) LineTo(x, y float64) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if cuts {
|
if cuts {
|
||||||
newp.flags |= pathSelfIntersects
|
newp.flags |= pathSelfIntersects
|
||||||
} else {
|
} else {
|
||||||
|
@ -104,6 +110,8 @@ func (p *Path2D) LineTo(x, y float64) {
|
||||||
|
|
||||||
// Arc (see equivalent function on canvas type)
|
// Arc (see equivalent function on canvas type)
|
||||||
func (p *Path2D) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise bool) {
|
func (p *Path2D) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise bool) {
|
||||||
|
checkSelfIntersection := len(p.p) > 0
|
||||||
|
|
||||||
lastWasMove := len(p.p) == 0 || p.p[len(p.p)-1].flags&pathMove != 0
|
lastWasMove := len(p.p) == 0 || p.p[len(p.p)-1].flags&pathMove != 0
|
||||||
|
|
||||||
startAngle = math.Mod(startAngle, math.Pi*2)
|
startAngle = math.Mod(startAngle, math.Pi*2)
|
||||||
|
@ -123,16 +131,16 @@ func (p *Path2D) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise b
|
||||||
if anticlockwise {
|
if anticlockwise {
|
||||||
for a := startAngle; a > endAngle; a -= step {
|
for a := startAngle; a > endAngle; a -= step {
|
||||||
s, c := math.Sincos(a)
|
s, c := math.Sincos(a)
|
||||||
p.LineTo(x+radius*c, y+radius*s)
|
p.lineTo(x+radius*c, y+radius*s, checkSelfIntersection)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for a := startAngle; a < endAngle; a += step {
|
for a := startAngle; a < endAngle; a += step {
|
||||||
s, c := math.Sincos(a)
|
s, c := math.Sincos(a)
|
||||||
p.LineTo(x+radius*c, y+radius*s)
|
p.lineTo(x+radius*c, y+radius*s, checkSelfIntersection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
s, c := math.Sincos(endAngle)
|
s, c := math.Sincos(endAngle)
|
||||||
p.LineTo(x+radius*c, y+radius*s)
|
p.lineTo(x+radius*c, y+radius*s, checkSelfIntersection)
|
||||||
|
|
||||||
if lastWasMove {
|
if lastWasMove {
|
||||||
p.p[len(p.p)-1].flags |= pathIsConvex
|
p.p[len(p.p)-1].flags |= pathIsConvex
|
||||||
|
|
Loading…
Reference in a new issue