c# - 从曲线c#中提取点坐标(x,y)
问题描述
我有一条曲线,我使用方法 graphics.drawcurve(pen, points,tension) 在 c# 中的图片框上绘制
无论如何我可以提取曲线覆盖的所有点(x,y坐标)吗?并将它们保存到数组或列表中,或者任何东西都会很棒,所以我可以在不同的东西中使用它们。
我的代码:
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);
}
提前谢谢了。
解决方案
如果你真的想要一个像素坐标列表,你仍然可以让 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}");
}
}
}
}
}
这种方法将样条曲线绘制成一条路径,然后使用将该路径展平为一组足够密集的线段的内置功能(大多数矢量绘图程序也是如此),然后从线网格中提取路径点进入PointF
s 列表。
GDI+ 设备渲染的人工制品(平滑、抗锯齿)在此过程中丢失。
推荐阅读
- javascript - 错误 TS:“元素”类型上不存在属性“数据集”
- javascript - 我将 Chartjs 与插件 chartjs 数据源一起使用,但无法指定要使用的数据部分
- r - 错误:稀疏逻辑矩阵的“'which' 参数不符合逻辑”
- asp.net-mvc - 默认内容安全策略 ASP MVC
- sql - 如何在sql server的同一列上多次使用case语句
- java - 为什么 Hibernate 在 save() 上加入不相关的实体
- python - Twitter Stream 的最有效方式是什么?
- react-native - 如何使用 reactnavigation 5 访问 React.Component 中的当前导航?
- facebook - 使用 Facebook graph api 在我的 Facebook 页面上发帖 未公开显示
- ruby - Ruby `add_item':nil:NilClass (NoMethodError) 的未定义方法`>'