c# - 帮助分析软件/程序如何构建贝塞尔曲线

标签 c# delphi bezier

我正在尝试了解由 cambridgesoft 开发的业界领先的化学工具 ChemDraw 如何构建贝塞尔曲线,以便我可以手动转换来自其他程序/例程(例如自制的 Delphi/C# 实用程序)的贝塞尔曲线点) 转换为 ChemDraw 可识别的曲线数据。在开始之前,我必须承认我在询问某些黑盒的内部工作原理,因此对任何麻烦表示歉意并感谢任何帮助!

我在ChemDraw中制作了四种最简单的贝塞尔曲线,并保存为.CDXML文件,最后粘贴了其中的曲线部分。 .CDXML 文件和相应的图片都可以从 fileserve 下载。 Download Bezier_curve_ChemDraw_sample here . ChemDraw trial edition can be downloaded here .

问题

问题 1. 以“线”类型的曲线点为例,我在制作这条曲线时总共使用了两个点。为什么 ChemDraw 为它存储 6 个点? ChemDraw 如何准确解释这些点?其他三种类型也存在同样的问题,最多使用四个显式点。

问题2、如果在ChemDraw中显示“使用三点的贝塞尔曲线-第一种类型”的内容,可以看到第一个点不能是(12.22, 104.25)。我的意思是,它的X坐标明显大于12。为什么会这样? ChemDraw 是否会对数据进行一些转换? “使用四点的贝塞尔曲线”也存在同样的问题。

问题 3. CurvePoints 属性末尾的额外空白字符似乎有所不同。究竟有什么区别?

有人能给我讲讲这些问题吗?

ChemDraw .CDXML 文件中的曲线部分

============行==============
Line

 <curve  
 id="2"  
 Z="1"  
 ArrowheadType="Solid"  
 CurvePoints="143.47 116.25 143.47 116.25 143.47 116.25 300.22 117.75 300.22 117.75 300.22 117.75"  
 />  

============使用三点的贝塞尔曲线-第一种类型==============
Curve - three points - drag the starting point

 <curve  
 id="2"  
 Z="1"  
 ArrowheadType="Solid"  
 CurvePoints="12.22 104.25 121.72 106.5 231.22 108.75 230.47 204 230.47 204 230.47 204"  
 />  

============使用三点的贝塞尔曲线-第二种==============
Curve - three points - drag the stopping point

 <curve  
 id="2"  
 Z="1"  
 ArrowheadType="Solid"  
 CurvePoints="134.47 97.5 134.47 97.5 134.47 97.5 231.22 60.75 229.72 109.5 228.22 158.25"  
 />  

============ 使用四点的贝塞尔曲线 ==============
Curve - three points - drag both the starting and the stopping points

 <curve  
 id="2"  
 Z="1"  
 ArrowheadType="Solid"  
 CurvePoints="5.47 93.75 123.22 93.75 240.97 93.75 351.22 177.75 236.47 177.75 121.72 177.75"   
 />  

生成相同曲线的示例 C# 程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForms_2_DrawBezier
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Text = "Draw Bezier Curve";
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // DrawBezier_1(this, e);
            // DrawBezier_2(this, e);
            // DrawBezier_3(this, e);
             DrawBezier_4(this, e);
        }

        private void DrawBezier_1(object sender, PaintEventArgs e)
        {
            Pen l_pen = new Pen(Color.Black);
            PointF l_pt1 = new PointF(143.47f, 116.25f);
            PointF l_pt2 = new PointF(l_pt1.X, l_pt1.Y);
            PointF l_pt3 = new PointF(l_pt1.X, l_pt1.Y);
            PointF l_pt4 = new PointF(300.22f, 117.75f);
            PointF l_pt5 = new PointF(l_pt4.X, l_pt4.Y);
            PointF l_pt6 = new PointF(l_pt4.X, l_pt4.Y);

            PointF[] l_pts = new PointF[6];
            l_pts[0] = l_pt1;
            l_pts[1] = l_pt2;
            l_pts[2] = l_pt3;
            l_pts[3] = l_pt4;
            l_pts[4] = l_pt5;
            l_pts[5] = l_pt6;

            // e.Graphics.DrawBezier(l_pen, l_pt1, l_pt2, l_pt5, l_pt6);

            e.Graphics.DrawBezier(l_pen, l_pt1, l_pt3, l_pt4, l_pt6); // As suggested by Dani

            // e.Graphics.DrawBeziers(l_pen, l_pts);
        }

        private void DrawBezier_2(object sender, PaintEventArgs e)
        {
            Pen l_pen = new Pen(Color.Black);
            PointF l_pt1 = new PointF(12.22f, 104.25f);
            PointF l_pt2 = new PointF(121.72f, 106.5f);
            PointF l_pt3 = new PointF(231.22f, 108.75f);
            PointF l_pt4 = new PointF(230.47f, 204f);
            PointF l_pt5 = new PointF(l_pt4.X, l_pt4.Y);
            PointF l_pt6 = new PointF(l_pt4.X, l_pt4.Y);

            PointF[] l_pts = new PointF[6];
            l_pts[0] = l_pt1;
            l_pts[1] = l_pt2;
            l_pts[2] = l_pt3;
            l_pts[3] = l_pt4;
            l_pts[4] = l_pt5;
            l_pts[5] = l_pt6;

            // e.Graphics.DrawBezier(l_pen, l_pt1, l_pt2, l_pt3, l_pt4);

            e.Graphics.DrawBezier(l_pen, l_pt1, l_pt3, l_pt4, l_pt6); // As suggested by Dani

            // e.Graphics.DrawBeziers(l_pen, l_pts);
        }

        private void DrawBezier_3(object sender, PaintEventArgs e)
        {
            Pen l_pen = new Pen(Color.Black);
            PointF l_pt1 = new PointF(134.47f, 97.5f);
            PointF l_pt2 = new PointF(l_pt1.X, l_pt1.Y);
            PointF l_pt3 = new PointF(l_pt1.X, l_pt1.Y);
            PointF l_pt4 = new PointF(231.22f, 60.75f);
            PointF l_pt5 = new PointF(229.72f, 109.5f);
            PointF l_pt6 = new PointF(228.22f, 158.25f);

            PointF[] l_pts = new PointF[6];
            l_pts[0] = l_pt1;
            l_pts[1] = l_pt2;
            l_pts[2] = l_pt3;
            l_pts[3] = l_pt4;
            l_pts[4] = l_pt5;
            l_pts[5] = l_pt6;

            // e.Graphics.DrawBezier(l_pen, l_pt3, l_pt4, l_pt5, l_pt6);

            e.Graphics.DrawBezier(l_pen, l_pt1, l_pt3, l_pt4, l_pt6); // As suggested by Dani

            // e.Graphics.DrawBeziers(l_pen, l_pts);
        }

        private void DrawBezier_4(object sender, PaintEventArgs e)
        {
            Pen l_pen = new Pen(Color.Black);
            PointF l_pt1 = new PointF(5.47f, 93.75f);
            PointF l_pt2 = new PointF(123.22f, 93.75f);
            PointF l_pt3 = new PointF(240.97f, 93.75f);
            PointF l_pt4 = new PointF(351.22f, 177.75f);
            PointF l_pt5 = new PointF(236.47f, 177.75f);
            PointF l_pt6 = new PointF(121.72f, 177.75f);

            PointF[] l_pts = new PointF[6];
            l_pts[0] = l_pt1;
            l_pts[1] = l_pt2;
            l_pts[2] = l_pt3;
            l_pts[3] = l_pt4;
            l_pts[4] = l_pt5;
            l_pts[5] = l_pt6;

            // e.Graphics.DrawBezier(l_pen, l_pt1, l_pt4, l_pt5, l_pt6);

            e.Graphics.DrawBezier(l_pen, l_pt1, l_pt3, l_pt4, l_pt6); // As suggested by Dani

            // e.Graphics.DrawBeziers(l_pen, l_pts);
        }
    }
}

但是,除了第一个 Line 类型之外,我无法让这个 C# 实用程序生成与 ChemDraw 显示的相同的曲线。
C# DrawBezier_1 screen-capture
C# DrawBezier_2 screen-capture C# DrawBezier_3 screen-capture C# DrawBezier_4 screen-capture

最佳答案

它使用标准贝塞尔曲线算法,可以取任意数量的点来构造曲线。看Bézier curve

编辑:
C# DrawBezier 仅支持二次(四点)贝塞尔曲线。你的四点贝塞尔曲线将被翻译成 C# 使用:

float[] CurvePoints = GetCurvePoints();

DrawBezier(Pen, CurvePoints[0], CurvePoints[1], CurvePoints[4], CurvePoints[5],
                CurvePoints[6], CurvePoints[7], CurvePoints[10], CurvePoints[11]);

GetCurvePoints() 只是为您提供来自 CurvePoints xml 属性的点列表。

编辑 2:
重现最后一条曲线所有曲线的代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Collections.Generic;

public class Bezier : Form
{
    static public void Main ()
    {
        Application.Run (new Bezier ());
    }

    protected override void OnPaint (PaintEventArgs e)
    {
        // The input with all points
        string CurveDataString = "5.47 93.75 123.22 93.75 240.97 93.75 351.22 177.75 236.47 177.75 121.72 177.75";

        string[] CurveDataStringParts = CurveDataString.Split(' ');

        int[] Keep = {2, 3, 4, 5, 6, 7, 8, 9};
        float[] CurveData = (from i in Keep select float.Parse(CurveDataStringParts[i])).ToArray();

        e.Graphics.DrawBezier(Pens.Black, CurveData[0], CurveData[1], CurveData[2], CurveData[3],
                                          CurveData[4], CurveData[5], CurveData[6], CurveData[7]);

        for(int i = 0; i < CurveData.Length; i += 2)
        {
            e.Graphics.FillEllipse(Brushes.Black, new RectangleF(CurveData[i] - 2, CurveData[i + 1] - 2, 4, 4));
        }

        base.OnPaint (e);
    }
}

结果:
Bezier example

关于c# - 帮助分析软件/程序如何构建贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7140789/

相关文章:

delphi - 为引用的命名空间创建别名

dart - 路径 quadraticBezierTo : curve pass the control point

c# - 如何防止拆箱 - 读取任意 sql 行时装箱内存开销

C# 变量不会更新

android - 如果 Android 中的 HttpGet 操作持续时间过长,如何避免出现错误 10053 (WSAECONNABORTED)?

delphi - 为静态数组中的记录正确分配/释放内存

python - 贝塞尔曲线和 matplotlib

python - 更改 Python Bezier 包中多边形的不透明度

C# - 简单验证 - DialogResult

c# - 将字符串从 datagridview 传递到另一种形式的文本框