从几个点绘制曲线
我想知道如何将点连接在一起形成曲线。我的图表中有 20 个点,想知道如何连接它们。我尝试使用 GeneralPath 对象,但想知道是否有更好的方法?
I would like to know how to join point together to form a curve. I have 20 points in a diagram and would like to know how to join to them. I tried with GeneralPath object, but would like to know if there is a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
GeneralPath 当然是最直接的。创建路径,为第一个点调用 moveTo,然后为每个后续点调用 lineTo。然后将其绘制到 Graphics2D 对象。
GeneralPath is certainly the most straightforward. Create your path, call moveTo for your first point, then call lineTo for each subsequent point. Then draw it to a Graphics2D object.
听起来您需要 Catmull-Rom 曲线。请参阅http://www.mvps.org/directx/articles/catmull/更多详细信息,以及 http://johnsogg.blogspot.com/2010/01/cardinal-splines-and-catmull-rom.html 进行实现。
It sounds like you need a Catmull-Rom curve instead. See http://www.mvps.org/directx/articles/catmull/ for more details, and http://johnsogg.blogspot.com/2010/01/cardinal-splines-and-catmull-rom.html for an implementation.
GeneralPath 是一种很好的方法,应该可以很好地满足您的要求,除非您遗漏了其他内容。
Path2D
是一个可以更精确的新类,但如果您不需要这种精度,那么它比GeneralPath
没有任何优势。GeneralPath
is a fine approach and should handle your requirement just fine unless you are leaving something else out.Path2D
is a new class that can be more precise but if you don't need that precision there is no advantage to it overGeneralPath
.贝塞尔想象了一条基于多项式元素的曲线:(
^
是“幂”而不是“XOR”)。他实际上用t
替换了a
,用1-t
替换了b
。因此公式将是(t + (1 - t))^3
(是的,它等于 1)。此时,我们的公式
有4部分。选择4点。
现在,创建参数方程,将公式的每个部分乘以一个坐标,如下所示:
这是三次贝塞尔曲线的参数方程。
您想要 20 次方贝塞尔曲线吗? “简单地”开发
(t + (1-t))^20
。帕斯卡三角应该对你有帮助。
Bézier imagined a curve based on the polynomial element:
(
^
being "to the power" and not "XOR"). He actually replaceda
byt
andb
by1-t
. So that the formula would be(t + (1 - t))^3
(Yes, it's equal to 1).At this point, we have the formula
There are 4 parts. Choose 4 points.
Now, create parametric equations, multiplying every part of the formula by a coordinate, like this:
This is the parametric equation of the cubic Bézier.
You want a 20th power Bézier? "simply" develop
(t + (1-t))^20
.Pascal Triangle should help you.
的 GeneralPath 方法
要构建曲线而不仅仅是直线,您可以使用创建贝塞尔曲线 。但是要计算控制点
x1, y1, x2, y2
,您需要进行一些数学计算,或者下载一些插值库。您也可以检查这个问题,它有一个实现一些插值算法的源代码的链接。
To build a curve, and not just lines, you can use method of
GeneralPath
which creates Bezier curve. But to calculate control points
x1, y1, x2, y2
you need to put some maths, or download some interpolation library.Also you can check this question, it has a links to source code implementing some interpolation algorithms.