当使用贝塞尔曲线逼近方法绘制2D圆弧时,如果已知圆心点、起始和结束角度以及半径,如何计算两个控制点?
当使用贝塞尔曲线逼近方法绘制2D圆弧时,如果已知圆心点、起始和结束角度以及半径,如何计算两个控制点?
这是一个8年前的问题,但最近我遇到了同样的问题,所以我想分享一下我的解决方案。我花了很多时间尝试使用Aleksas Riškus的文章中的解决方案(9),但在没有进行谷歌搜索之前,我无法从中获得任何有意义的数字。后来我得知,方程式中似乎存在一些打字错误。根据这篇博客文章中列出的更正,给定弧线的起始点和结束点(分别为[x1,y1]和[x4,y4])以及圆的中心点([xc,yc]),可以如下导出用于三次贝塞尔曲线的控制点([x2,y2]和[x3,y3]):
ax = x1 - xc
ay = y1 - yc
bx = x4 - xc
by = y4 - yc
q1 = ax * ax + ay * ay
q2 = q1 + ax * bx + ay * by
k2 = (4/3) * (sqrt(2 * q1 * q2) - q2) / (ax * by - ay * bx)
x2 = xc + ax - k2 * ay
y2 = yc + ay + k2 * ax
x3 = xc + bx + k2 * by
y3 = yc + by - k2 * bx
在Aleksas Riškus的文章《Approximation of a cubic bezier curve by circular arcs and vice versa》1中提供了一个很好的解释。
简而言之,使用Bezier曲线可以达到最小误差为1.96×10^-4,对于大多数应用程序来说这相当不错。
对于正象限弧,使用以下点:
p0 = [0, radius]
p1 = [radius * K, radius]
p2 = [radius, radius * K]
p3 = [radius, 0]
K是所谓的“魔数”,是一个无理数。它可以近似如下:
K = 0.5522847498
<svg xmlns="http://www.w3.org/2000/svg" viewBox="10 20 80 80">
<style>text{font-size:40%;font-style:italic;text-anchor:middle}tspan{font-size:50%;font-style:normal}</style>
<rect x="10" y="20" width="80" height="80" fill="none" stroke="gray"></rect>
<path stroke="gray" stroke-dasharray="3,2" fill="none" d="M25,30 62.6,31.62 80,65 22.19,95.13 25,30 80,65 M22.19,95.13 62.6,31.62"></path>
<path stroke="black" fill="none" d="M25,30A65.19 65.19 0 0 1 80,65"></path>
<circle r="1" fill="red" cx="25" cy="30"></circle>
<circle r="1" fill="green" cx="80" cy="65"></circle>
<circle r="1" fill="magenta" cx="22.19" cy="95.13"></circle>
<circle r="1" fill="darkgreen" cx="52.5" cy="47.5"></circle>
<circle r="1" fill="yellow" cx="57.19" cy="40.13"></circle>
<circle r="1" fill="maroon" cx="62.6" cy="31.62"></circle>
<circle r="1" fill="orange" cx="48.27" cy="31"></circle>
<circle r="1" fill="teal" cx="69.24" cy="44.35"></circle>
<text x="25" y="28">P<tspan>0</tspan></text>
<text x="48.27" y="29">P<tspan>1</tspan></text>
<text x="71.24" y="42.35">P<tspan>2</tspan></text>
<text x="83" y="63">P<tspan>3</tspan></text>
<text x="62.6" y="29.62">P<tspan>E</tspan></text>
<text x="59.19" y="47.13">P<tspan>H</tspan></text>
<text x="54.5" y="54.5">P<tspan>M</tspan></text>
</svg>
让PE成为切线于P0和P3的弧线的交点。为了使曲线切于弧线,P1必须位于线段P0PE上,而P2必须位于P3PE上。让k成为比率P0P1/P0PE(也等于P3P2/P3PE):
P1 = (1 - k)P0 + k PE
P2 = (1 - k)P3 + k PE
我们还有以下比例(进行一些比例计算):
PM = (P0 + P3) / 2
PH = PM / cos(x) = PM sec(x) = (P0 + P3) sec(x) / 2
PE = PH / cos(x) = PM sec(x)^2 = (P0 + P3) sec(x)^2 / 2
为了简化计算,我将所有向量点都视为基于中心,但最终这并不重要。
通用的四点贝塞尔曲线由以下公式给出:
C(t) = t^3 P3 + 3(1 - t)t^2 P2 + 3(1 - t)^2 t P1 + (1 - t)^3 P0
我们必须有C(1/2) = PH,所以
C(1/2) = (P0 + 3 P1 + 3 P2 + P3) / 8
= ((P0 + P3) + 3(1 - k)P0 + 3 k PE + 3(1 - k)P3 + 3 k PE) / 8
= ((P0 + P3) + 3(1 - k)(P0 + P3) + 6 k PE) / 8
= (P0 + P3)(1 + 3(1 - k) + 3 k sec(x)^2) / 8
这是我们用来找到k的方程式(乘以8):
8 C(1/2) = 8 PH
=> (P0 + P3)(4 - 3 k + 3 k sec(x)^2) = 4(P0 + P3) sec(x)
我们可以消除向量(P0 + P3),然后我们得到:
4 - 3 k + 3 k sec(x)^2 = 4 sec(x)
=> 3 k (sec(x)^2 - 1) = 4(sec(x) - 1)
=> k = 4 / ( 3 * (sec(x) + 1) )
现在你知道应该把控制点放在哪里了。太好了!
如果你有x = pi/4,那么你会得到k = 0.552... 你可能在其他地方看到过这个值。
当处理椭圆弧时,你只需相应地缩放点的坐标即可。
如果你需要处理更大的角度,建议将它们分成更多的曲线。这实际上是一些软件在绘制弧线时所做的,因为计算Bézier曲线有时比使用正弦和余弦更快。
Raphael 2.1.0支持Arc->Cubic (path2curve函数),在修复S和T路径规范化的错误后,它似乎现在可以正常工作。我更新了*随机路径生成器*,使其只生成弧线,因此可以轻松测试所有可能的路径组合:
测试一下,如果有某些路径失败了,请告诉我。
编辑:刚刚意识到这是三年前的帖子...
'use strict';
module.exports = function (angleStart, angleEnd, center, radius) {
// assuming angleStart and angleEnd are in degrees
const angleStartRadians = angleStart * Math.PI / 180;
const angleEndRadians = angleEnd * Math.PI / 180;
// Finding the coordinates of the control points in a simplified case where the center of the circle is at [0,0]
const relControlPoints = getRelativeControlPoints(angleStartRadians, angleEndRadians, radius);
return {
pointStart: getPointAtAngle(angleStartRadians, center, radius),
pointEnd: getPointAtAngle(angleEndRadians, center, radius),
// To get the absolute control point coordinates we just translate by the center coordinates
controlPoint1: {
x: center.x + relControlPoints[0].x,
y: center.y + relControlPoints[0].y
},
controlPoint2: {
x: center.x + relControlPoints[1].x,
y: center.y + relControlPoints[1].y
}
};
};
function getRelativeControlPoints(angleStart, angleEnd, radius) {
// factor is the commonly reffered parameter K in the articles about arc to cubic bezier approximation
const factor = getApproximationFactor(angleStart, angleEnd);
// Distance from [0, 0] to each of the control points. Basically this is the hypotenuse of the triangle [0,0], a control point and the projection of the point on Ox
const distToCtrPoint = Math.sqrt(radius * radius * (1 + factor * factor));
// Angle between the hypotenuse and Ox for control point 1.
const angle1 = angleStart + Math.atan(factor);
// Angle between the hypotenuse and Ox for control point 2.
const angle2 = angleEnd - Math.atan(factor);
return [
{
x: Math.cos(angle1) * distToCtrPoint,
y: Math.sin(angle1) * distToCtrPoint
},
{
x: Math.cos(angle2) * distToCtrPoint,
y: Math.sin(angle2) * distToCtrPoint
}
];
}
function getPointAtAngle(angle, center, radius) {
return {
x: center.x + radius * Math.cos(angle),
y: center.y + radius * Math.sin(angle)
};
}
// Calculating K as done in https://pomax.github.io/bezierinfo/#circles_cubic
function getApproximationFactor(angleStart, angleEnd) {
let arc = angleEnd - angleStart;
// Always choose the smaller arc
if (Math.abs(arc) > Math.PI) {
arc -= Math.PI * 2;
arc %= Math.PI * 2;
}
return (4 / 3) * Math.tan(arc / 4);
}
Swift解决方案基于@k88lawrence 答案
适用于弧度 <= PI / 2
func controls(center: CGPoint, start: CGPoint, end: CGPoint) -> (CGPoint, CGPoint) {
let ax = start.x - center.x
let ay = start.y - center.y
let bx = end.x - center.x
let by = end.y - center.y
let q1 = (ax * ax) + (ay * ay)
let q2 = q1 + (ax * bx) + (ay * by)
let k2 = 4 / 3 * (sqrt(2 * q1 * q2) - q2) / ((ax * by) - (ay * bx))
let control1 = CGPoint(x: center.x + ax - (k2 * ay), y: center.y + ay + (k2 * ax))
let control2 = CGPoint(x: center.x + bx + (k2 * by), y: center.y + by - (k2 * bx))
return (control1, control2)
}