C#-如何绘制闭合曲线等开放曲线? [英] C# - How to draw open curves like closed curves?

查看:87
本文介绍了C#-如何绘制闭合曲线等开放曲线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DrawCurve和DrawClosedCurve在我的PictureBox上绘制曲线,如下所示:

I'm using DrawCurve and DrawClosedCurve to draw curves on my PictureBox like this:

if(isClosed) {
    g.DrawClosedCurve(
        new Pen(c, lineSize),
        points,
        tension,
        FillMode.Alternate
    );
} else {
    g.DrawCurve(
        new Pen(c, lineSize),
        points,
        tension
    );
}

问题是,我希望打开的曲线看起来像闭合的曲线,因为现在,如果我对两条曲线使用相同的张力(0.8f),则它们看起来像这样:

The problem is, I'd like the open curves to look like the closed curves, because right now, if I use the same tension (0.8f) for both curves, they look like this:

我真正想要的是它看起来像这样:

And what I really want is for it to look like this:

如何获得此结果?我需要使用DrawBezier(或DrawBeziers)代替DrawCurve吗?如果是,我该如何计算要使用的控制点?另外,必须使用张力,以便用户可以绘制不同类型的曲线.

How can I achieve this result? Would I need to use DrawBezier (or DrawBeziers) instead of DrawCurve? If yes, how can I calculate the control points to use? Also, the tension must be ued so the user can draw different types of curves.

=====编辑=====

===== EDIT =====

我认为我没有设法正确解释我想要的东西,所以我尝试考虑另一个例子...

I think I didn't manage to explain what I want properly, so I tried to think on another example...

有这组要点:

使用1.2f作为张力,DrawCurve绘制如下:

Using 1.2f as tension, the DrawCurve draws it like this:

和这样的DrawClosedCurve:

And the DrawClosedCurve like this:

现在我意识到我真的不希望像封闭曲线那样绘制开放曲线,但是我真正想要修复的是靠近第一个点和最后一个点的曲线.

And now I realized that I don't really want the open curve to be drawn like the closed curve, but what I really want to fix is the curve near the first point and the last point.

曲线首先以逆时针方向移动,然后切换到下一个点的顺时针方向.在最后一点上也发生了同样的情况,但是它从倒数第二个点开始沿顺时针方向开始,并在中途切换到下一个点的逆时针方向(这在第一个示例中也可以看到).因此,在此示例中,我希望曲线以顺时针运动开始,而不会切换到下一个点的一半,而以顺时针运动结束.

The curve starts by making an anticlockwise movement and them, switches to clockwise movement halfway to the next point. The same happens in the last point, but it starts clockwise from the penultimate point and switches to anticlockwise halfway to the next point (and this can also be seen on the first example). So, in this example, I'd like the curve to start with a clockwise movement without switching halfway to the next point and finish with a clockwise movement.

但是现在我不认为有一个简单的解决方案,因为我希望DrawCurve在除第一个和最后一个点之外的所有点上都具有正常的行为.

But now I don't think there is an easy solution for this, because I want the normal behavior from DrawCurve on all points but the first and the last...

我相信仍然可以通过使用DrawBezier来实现,但是我不知道如何计算具有张力的控制点...

I believe this can still be achieved by using DrawBezier, but I have no clue how to calculate the control points having the tension in mind...

推荐答案

DrawCurve()和DrawClosedCurve()将创建基数样条线,这是三次Hermite样条线的特殊类型,可从相邻点推断出切线向量.如果希望在第一个点或最后一个点(或在任何点)上指定自己的切向量,则应使用更通用的三次Hermite样条曲线.有关更多详细信息,请参见链接.您可以将三次Hermite样条曲线转换为三次Bezier曲线

DrawCurve() and DrawClosedCurve() will create cardinal splines, which is a special type of cubic Hermite splines where the tangent vectors are inferred from neighbor points. If you would like to be able to specify your own tangent vectors at first or last point (or at any point), you should use the more general cubic Hermite spline. Refer to link for more details. You can convert cubic Hermite spline to cubic Bezier curve as

B0 = P0
B1 = P0'/3 + B0
B2 = B3-P1'/3
B3 = P1

B0=P0
B1=P0'/3 + B0
B2=B3 - P1'/3
B3=P1

其中B0,B1,B2和B3是三次贝塞尔曲线的控制点,P0'和P1'是在P0和P1点处的切向量.

where B0,B1,B2 and B3 are the control points for cubic Bezier curve, P0' and P1' are the tangent vector at point P0 and P1.

这篇关于C#-如何绘制闭合曲线等开放曲线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆