vector: Better interpolation of Bézier curves

Updates #844
This commit is contained in:
Hajime Hoshi 2021-07-06 03:26:52 +09:00
parent 035759703b
commit 315f87896b

View File

@ -18,8 +18,6 @@
package vector
import (
"math"
"github.com/hajimehoshi/ebiten/v2"
)
@ -51,46 +49,76 @@ func (p *Path) LineTo(x, y float32) {
p.cur = point{x: x, y: y}
}
// nseg returns a number of segments based on the given two points (x0, y0) and (x1, y1).
func nseg(x0, y0, x1, y1 float32) int {
distx := x1 - x0
if distx < 0 {
distx = -distx
}
disty := y1 - y0
if disty < 0 {
disty = -disty
}
dist := distx
if dist < disty {
dist = disty
}
return int(math.Ceil(float64(dist)))
}
// QuadTo adds a quadratic Bézier curve to the path.
func (p *Path) QuadTo(cpx, cpy, x, y float32) {
// TODO: Split more appropriate number of segments.
c := p.cur
num := nseg(c.x, c.y, x, y)
for t := float32(0.0); t <= 1; t += 1.0 / float32(num) {
xf := (1-t)*(1-t)*c.x + 2*t*(1-t)*cpx + t*t*x
yf := (1-t)*(1-t)*c.y + 2*t*(1-t)*cpy + t*t*y
p.LineTo(xf, yf)
p.quadTo(cpx, cpy, x, y, 0)
}
// isPointCloseToSegment detects the distance between a segment (x0, y0)-(x1, y1) and a point (x, y) is less than allow.
func isPointCloseToSegment(x, y, x0, y0, x1, y1 float32, allow float32) bool {
// Line passing through (x0, y0) and (x1, y1) in the form of ax + by + c = 0
a := y1 - y0
b := -(x1 - x0)
c := (x1-x0)*y0 - (y1-y0)*x0
// The distance between a line ax+by+c=0 and (x0, y0) is
// |ax0 + by0 + c| / √(a² + b²)
return allow*allow*(a*a+b*b) > (a*x+b*y+c)*(a*x+b*y+c)
}
func (p *Path) quadTo(x1, y1, x2, y2 float32, level int) {
if level > 10 {
return
}
x0 := p.cur.x
y0 := p.cur.y
if isPointCloseToSegment(x1, y1, x0, y0, x2, y2, 1) {
p.LineTo(x2, y2)
return
}
x01 := (x0 + x1) / 2
y01 := (y0 + y1) / 2
x12 := (x1 + x2) / 2
y12 := (y1 + y2) / 2
x012 := (x01 + x12) / 2
y012 := (y01 + y12) / 2
p.quadTo(x01, y01, x012, y012, level+1)
p.quadTo(x12, y12, x2, y2, level+1)
}
// CubicTo adds a cubic Bézier curve to the path.
func (p *Path) CubicTo(cp0x, cp0y, cp1x, cp1y, x, y float32) {
// TODO: Split more appropriate number of segments.
c := p.cur
num := nseg(c.x, c.y, x, y)
for t := float32(0.0); t <= 1; t += 1.0 / float32(num) {
xf := (1-t)*(1-t)*(1-t)*c.x + 3*(1-t)*(1-t)*t*cp0x + 3*(1-t)*t*t*cp1x + t*t*t*x
yf := (1-t)*(1-t)*(1-t)*c.y + 3*(1-t)*(1-t)*t*cp0y + 3*(1-t)*t*t*cp1y + t*t*t*y
p.LineTo(xf, yf)
p.cubicTo(cp0x, cp0y, cp1x, cp1y, x, y, 0)
}
func (p *Path) cubicTo(x1, y1, x2, y2, x3, y3 float32, level int) {
if level > 10 {
return
}
x0 := p.cur.x
y0 := p.cur.y
if isPointCloseToSegment(x1, y1, x0, y0, x3, y3, 1) && isPointCloseToSegment(x2, y2, x0, y0, x3, y3, 1) {
p.LineTo(x3, y3)
return
}
x01 := (x0 + x1) / 2
y01 := (y0 + y1) / 2
x12 := (x1 + x2) / 2
y12 := (y1 + y2) / 2
x23 := (x2 + x3) / 2
y23 := (y2 + y3) / 2
x012 := (x01 + x12) / 2
y012 := (y01 + y12) / 2
x123 := (x12 + x23) / 2
y123 := (y12 + y23) / 2
x0123 := (x012 + x123) / 2
y0123 := (y012 + y123) / 2
p.cubicTo(x01, y01, x012, y012, x0123, y0123, level+1)
p.cubicTo(x123, y123, x23, y23, x3, y3, level+1)
}
// AppendVerticesAndIndices appends vertices and indices for this path and returns them.