Пример #1
0
func roundCapper(p Adder, halfWidth fixed.Int26_6, pivot, n1 fixed.Point26_6) {
	// The cubic Bézier approximation to a circle involves the magic number
	// (√2 - 1) * 4/3, which is approximately 141/256.
	const k = 141
	e := pRot90CCW(n1)
	side := pivot.Add(e)
	start, end := pivot.Sub(n1), pivot.Add(n1)
	d, e := n1.Mul(k), e.Mul(k)
	p.Add3(start.Add(e), side.Sub(d), side)
	p.Add3(side.Add(d), end.Add(e), end)
}
Пример #2
0
// addArc adds a circular arc from pivot+n0 to pivot+n1 to p. The shorter of
// the two possible arcs is taken, i.e. the one spanning <= 180 degrees. The
// two vectors n0 and n1 must be of equal length.
func addArc(p Adder, pivot, n0, n1 fixed.Point26_6) {
	// r2 is the square of the length of n0.
	r2 := pDot(n0, n0)
	if r2 < epsilon {
		// The arc radius is so small that we collapse to a straight line.
		p.Add1(pivot.Add(n1))
		return
	}
	// We approximate the arc by 0, 1, 2 or 3 45-degree quadratic segments plus
	// a final quadratic segment from s to n1. Each 45-degree segment has
	// control points {1, 0}, {1, tan(π/8)} and {1/√2, 1/√2} suitably scaled,
	// rotated and translated. tan(π/8) is approximately 106/256.
	const tpo8 = 106
	var s fixed.Point26_6
	// We determine which octant the angle between n0 and n1 is in via three
	// dot products. m0, m1 and m2 are n0 rotated clockwise by 45, 90 and 135
	// degrees.
	m0 := pRot45CW(n0)
	m1 := pRot90CW(n0)
	m2 := pRot90CW(m0)
	if pDot(m1, n1) >= 0 {
		if pDot(n0, n1) >= 0 {
			if pDot(m2, n1) <= 0 {
				// n1 is between 0 and 45 degrees clockwise of n0.
				s = n0
			} else {
				// n1 is between 45 and 90 degrees clockwise of n0.
				p.Add2(pivot.Add(n0).Add(m1.Mul(tpo8)), pivot.Add(m0))
				s = m0
			}
		} else {
			pm1, n0t := pivot.Add(m1), n0.Mul(tpo8)
			p.Add2(pivot.Add(n0).Add(m1.Mul(tpo8)), pivot.Add(m0))
			p.Add2(pm1.Add(n0t), pm1)
			if pDot(m0, n1) >= 0 {
				// n1 is between 90 and 135 degrees clockwise of n0.
				s = m1
			} else {
				// n1 is between 135 and 180 degrees clockwise of n0.
				p.Add2(pm1.Sub(n0t), pivot.Add(m2))
				s = m2
			}
		}
	} else {
		if pDot(n0, n1) >= 0 {
			if pDot(m0, n1) >= 0 {
				// n1 is between 0 and 45 degrees counter-clockwise of n0.
				s = n0
			} else {
				// n1 is between 45 and 90 degrees counter-clockwise of n0.
				p.Add2(pivot.Add(n0).Sub(m1.Mul(tpo8)), pivot.Sub(m2))
				s = pNeg(m2)
			}
		} else {
			pm1, n0t := pivot.Sub(m1), n0.Mul(tpo8)
			p.Add2(pivot.Add(n0).Sub(m1.Mul(tpo8)), pivot.Sub(m2))
			p.Add2(pm1.Add(n0t), pm1)
			if pDot(m2, n1) <= 0 {
				// n1 is between 90 and 135 degrees counter-clockwise of n0.
				s = pNeg(m1)
			} else {
				// n1 is between 135 and 180 degrees counter-clockwise of n0.
				p.Add2(pm1.Sub(n0t), pivot.Sub(m0))
				s = pNeg(m0)
			}
		}
	}
	// The final quadratic segment has two endpoints s and n1 and the middle
	// control point is a multiple of s.Add(n1), i.e. it is on the angle
	// bisector of those two points. The multiple ranges between 128/256 and
	// 150/256 as the angle between s and n1 ranges between 0 and 45 degrees.
	//
	// When the angle is 0 degrees (i.e. s and n1 are coincident) then
	// s.Add(n1) is twice s and so the middle control point of the degenerate
	// quadratic segment should be half s.Add(n1), and half = 128/256.
	//
	// When the angle is 45 degrees then 150/256 is the ratio of the lengths of
	// the two vectors {1, tan(π/8)} and {1 + 1/√2, 1/√2}.
	//
	// d is the normalized dot product between s and n1. Since the angle ranges
	// between 0 and 45 degrees then d ranges between 256/256 and 181/256.
	d := 256 * pDot(s, n1) / r2
	multiple := fixed.Int26_6(150-(150-128)*(d-181)/(256-181)) >> 2
	p.Add2(pivot.Add(s.Add(n1).Mul(multiple)), pivot.Add(n1))
}