简体   繁体   English

从曲线C#中提取点坐标(x,y)

[英]Extracting points coordinates(x,y) from a curve c#

i have a curve that i draw on a picturebox in c# using the method graphics.drawcurve(pen, points, tension) 我有一条曲线,我使用graphics.drawcurve(pen,points,tension)方法在C#中的图片框上绘制

is there anyway that i can extract all points (x,y coordinates) been covered by the curve ? 无论如何,我可以提取曲线覆盖的所有点(x,y坐标)吗? and save them into an array or list or any thing would be great, so i can use them in a different things. 并将它们保存到数组或列表中,否则任何事情都会很棒,因此我可以在其他事情中使用它们。

My code: 我的代码:

void Curved()
{
    Graphics gg = pictureBox1.CreateGraphics();
    Pen pp = new Pen(Color.Green, 1);
    int i,j;
    Point[] pointss = new Point[counter];

    for (i = 0; i < counter; i++)
    {
        pointss[i].X = Convert.ToInt32(arrayx[i]);
        pointss[i].Y = Convert.ToInt32(arrayy[i]);
    }
    gg.DrawCurve(pp, pointss, 1.0F);
}

Many thanks in advance. 提前谢谢了。

If you really want a list of pixel co-ordinates, you can still let GDI+ do the heavy lifting: 如果您确实想要一个像素坐标列表,仍然可以让GDI +承担繁重的工作:

using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace so_pointsfromcurve
{
    class Program
    {
        static void Main(string[] args)
        {
            /* some test data */
            var pointss = new Point[]
            {
                new Point(5,20),
                new Point(17,63),
                new Point(2,9)
            };
            /* instead of to the picture box, draw to a path */
            using (var path = new GraphicsPath())
            {
                path.AddCurve(pointss, 1.0F);
                /* use a unit matrix to get points per pixel */
                using (var mx = new Matrix(1, 0, 0, 1, 0, 0))
                {                    
                    path.Flatten(mx, 0.1f);
                }
                /* store points in a list */
                var list_of_points = new List<PointF>(path.PathPoints);
                /* show them */
                int i = 0;
                foreach(var point in list_of_points)
                {
                    Debug.WriteLine($"Point #{ ++i }: X={ point.X }, Y={point.Y}");
                }
            }

        }
    }
}

This approach draws the spline to a path, then uses the built-in capability of flattening that path to a sufficiently dense set of line segments (in a way most vector drawing programs do, too) and then extracts the path points from the line mesh into a list of PointF s. 此方法将样条线绘制到路径,然后使用将路径展平到足够密集的线段集的内置功能(大多数矢量绘图程序也这样做),然后从线网格中提取路径点进入PointF的列表。

The artefacts of GDI+ device rendering (smoothing, anti-aliasing) are lost in this process. 在此过程中,会丢失GDI +设备渲染的伪像(平滑,抗锯齿)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM