我的问题的答案可能很明显,我只是想得到确认。我有一个曲线与大量的点,必须优化。在这条曲线上,我可能需要做各种操作: 1.添加画布;2.在画布上移动;3.在画布上拉伸;4.画布盘;5.画布缩放
为了做到这一点,我有一个原始的折线存储所有的原始点。然后,我有第二条折线,它存储了优化的点集。
首先,我的优化看起来很简单,即: 1.点是双vars,但在屏幕的最后是整数。因此,如果转换为整数的point1和points2是相同的,那么我就放弃point2。2.如果一个段有开始和结束在画布上(例如,由于已经平移或缩放),我放弃这两个点。
我对所有的问题都这么做。这是非常有效的,因为一个例子螺旋有100万点可以减少到4000点左右!!
在此之后,我总是显示优化曲线。
关于在曲线上执行的各种操作。
这些都是对的吗?
谢谢你帮助帕特里克
发布于 2015-08-28 07:59:58
您可以使用Douglas-Ramer-Peucker算法来减少折线中的点数:
public static IList<Point> ReducePoints(IList<Point> points, double tolerance)
{
var indexes = new List<int>(2);
indexes.Add(0);
indexes.Add(points.Count - 1);
ReducePoints(points, tolerance, indexes, 0, points.Count - 1);
return indexes.OrderBy(i => i).Select(i => points[i]).ToList();
}
private static void ReducePoints(
IList<Point> points, double tolerance, List<int> indexes, int first, int last)
{
if (first + 1 < last)
{
var dx = points[first].X - points[last].X;
var dy = points[first].Y - points[last].Y;
var length = Math.Sqrt(dx * dx + dy * dy);
var maxDistance = 0d;
var farthest = 0;
for (var index = first + 1; index < last; index++)
{
var dxi = points[first].X - points[index].X;
var dyi = points[first].Y - points[index].Y;
// perpendicular distance from line segment first to last
var distance = Math.Abs(dx * dyi - dxi * dy) / length;
if (distance > maxDistance)
{
maxDistance = distance;
farthest = index;
}
}
if (maxDistance > tolerance)
{
indexes.Add(farthest);
ReducePoints(points, tolerance, indexes, first, farthest);
ReducePoints(points, tolerance, indexes, farthest, last);
}
}
}https://stackoverflow.com/questions/32222043
复制相似问题