op/clip: smoother implementation of Arc for large angles

Signed-off-by: Sebastien Binet <s@sbinet.org>
This commit is contained in:
Sebastien Binet
2020-08-29 14:59:33 +00:00
committed by Elias Naur
parent 1584f3a64a
commit f2cd504a9f
3 changed files with 17 additions and 37 deletions
+1
View File
@@ -61,6 +61,7 @@ func TestPaintArc(t *testing.T) {
p.Line(f32.Pt(0, 25)) p.Line(f32.Pt(0, 25))
p.Arc(f32.Pt(-10, 5), f32.Pt(10, 15), -math.Pi) p.Arc(f32.Pt(-10, 5), f32.Pt(10, 15), -math.Pi)
p.Line(f32.Pt(0, 25)) p.Line(f32.Pt(0, 25))
p.Arc(f32.Pt(10, 10), f32.Pt(10, 10), 2*math.Pi)
p.Line(f32.Pt(-10, 0)) p.Line(f32.Pt(-10, 0))
p.Arc(f32.Pt(-10, 0), f32.Pt(-40, 0), -math.Pi) p.Arc(f32.Pt(-10, 0), f32.Pt(-40, 0), -math.Pi)
p.Line(f32.Pt(-10, 0)) p.Line(f32.Pt(-10, 0))
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

+16 -37
View File
@@ -194,16 +194,9 @@ func arcFrom(f1, f2, p f32.Point) (c f32.Point, rx, ry, start, alpha float64) {
// An electronic version may be found at: // An electronic version may be found at:
// http://spaceroots.org/documents/ellipse/elliptical-arc.pdf // http://spaceroots.org/documents/ellipse/elliptical-arc.pdf
func (p *Path) arc(alpha float64, c f32.Point, rx, ry, beg, delta float64) { func (p *Path) arc(alpha float64, c f32.Point, rx, ry, beg, delta float64) {
const n = 16
var ( var (
n = math.Round(20 * math.Pi / math.Abs(delta)) θ = delta / n
θ = delta / n
sinθ64, cosθ64 = math.Sincos(θ)
sinθ, cosθ = float32(sinθ64), float32(cosθ64)
b = (cosθ - 1) / sinθ
)
var (
ref f32.Affine2D // transform from absolute frame to ellipse-based one ref f32.Affine2D // transform from absolute frame to ellipse-based one
rot f32.Affine2D // rotation matrix for each segment rot f32.Affine2D // rotation matrix for each segment
inv f32.Affine2D // transform from ellipse-based frame to absolute one inv f32.Affine2D // transform from ellipse-based frame to absolute one
@@ -215,43 +208,29 @@ func (p *Path) arc(alpha float64, c f32.Point, rx, ry, beg, delta float64) {
Y: float32(1 / ry), Y: float32(1 / ry),
}) })
inv = ref.Invert() inv = ref.Invert()
rot = rot.Rotate(f32.Point{}, float32(θ)) rot = rot.Rotate(f32.Point{}, float32(0.5*θ))
// Instead of invoking math.Sincos for every segment, compute a rotation // Instead of invoking math.Sincos for every segment, compute a rotation
// matrix once and apply for each segment. // matrix once and apply for each segment.
// Before applying the rotation matrix rot, transform the coordinates // Before applying the rotation matrix rot, transform the coordinates
// to a frame centered to the ellipse (and warped into a unit circle), then rotate. // to a frame centered to the ellipse (and warped into a unit circle), then rotate.
// Finally, transform back into the original frame. // Finally, transform back into the original frame.
// step := func(p f32.Point) f32.Point {
// Also compute the control point C according to
// https://pomax.github.io/bezierinfo/#circles.
// If S is the starting point, S' is the orthogonal
// tangent, θ is clockwise:
//
// C = S + b*S', b = (cos θ - 1)/sin θ
//
// We apply the same original <-> ellipse frame transformation to the
// control point as well.
rotate := func(p f32.Point) (end, ctl f32.Point) {
q := ref.Transform(p) q := ref.Transform(p)
t := f32.Pt(-q.Y, q.X) q = rot.Transform(q)
q = inv.Transform(q)
end = rot.Transform(q) return q
ctl = q.Add(t.Mul(b))
end = inv.Transform(end)
ctl = inv.Transform(ctl)
return end, ctl
} }
var ( for i := 0; i < n; i++ {
ctl f32.Point p0 := p.pen
end = p.pen p1 := step(p0)
) p2 := step(p1)
for i := 0; i < int(n); i++ { ctl := f32.Pt(
end, ctl = rotate(p.pen) 2*p1.X-0.5*(p0.X+p2.X),
p.quadTo(ctl, end) 2*p1.Y-0.5*(p0.Y+p2.Y),
)
p.quadTo(ctl, p2)
} }
} }