绘制点集合的简单方法

本文关键字:简单 方法 集合 点集 绘制 | 更新日期: 2023-09-27 18:26:28

是否有任何内置的方法来绘制点的集合?目前我是这样做的:

public static Bitmap Draw(this IEnumerable<Point> points)
{
    var horizontallySortedPoints = points.OrderBy(p => p.X).ToArray();
    var verticallySortedPoints = points.OrderBy(p => p.Y).ToArray();
    var leftmost = horizontallySortedPoints.First();
    var rightmost = horizontallySortedPoints.Last();
    var topmost = verticallySortedPoints.First();
    var bottommost = verticallySortedPoints.Last();
    var i = new Bitmap(rightmost.X - leftmost.X + 1, bottommost.Y - topmost.Y + 1);
    foreach (var item in points)
        i.SetPixel(item.X, item.Y, Color.Red);
    return i;
}

这似乎工作量很大。我必须找到自己最左边和最右边的点,计算宽度等等。我不是在说性能或其他什么。只是知道框架是否有办法实现这一点。

绘制点集合的简单方法

没有简单的方法。我最终也做了同样的事情(有一些修复):

public static Bitmap Draw(this IEnumerable<Point> points, 
                          Color paintColor = default(Color))
{
    return points.Draw(paintColor, Color.White);
}
public static Bitmap Draw(this IEnumerable<Point> points, Color paintColor, 
                          Color backColor)
{
    return points.Draw(paintColor, backColor, (ps, leftmost, topmost) => ps);
}
public static Bitmap DrawRelative(this IEnumerable<Point> points,
                                  Color paintColor = default(Color))
{
    return points.DrawRelative(paintColor, Color.White);
}
public static Bitmap DrawRelative(this IEnumerable<Point> points,
                                  Color paintColor, Color backColor)
{
    return points.Draw(paintColor, backColor, 
                       (ps, leftmost, topmost) => ps.Select(p => new Point(p.X - leftmost.X, p.Y - topmost.Y)));
}
static Bitmap Draw(this IEnumerable<Point> points, 
                   Color paintColor, Color backColor, 
                   Func<IEnumerable<Point>, Point, Point, IEnumerable<Point>> transformer)
{
    points = points.ToArray();
    Func<IEnumerable<Point>, IEnumerable<Point>> horizontallSorter = ps => ps.OrderBy(p => p.X);
    Func<IEnumerable<Point>, IEnumerable<Point>> verticallSorter = ps => ps.OrderBy(p => p.Y);
    var leftmost = horizontallSorter(points).First();
    var topmost = verticallSorter(points).First();
    var relativePoints = transformer(points, leftmost, topmost).ToArray();
    var rightmost = horizontallSorter(relativePoints).Last();
    var bottommost = verticallSorter(relativePoints).Last();
    var img = new Bitmap(rightmost.X + 1, bottommost.Y + 1);
    using (var g = Graphics.FromImage(img))
    {
        g.Clear(backColor);
    }
    foreach (var item in relativePoints)
        img.SetPixel(item.X, item.Y, paintColor);
    return img;
}

Draw方法沿着points输入中指定的精确坐标绘制。DrawRelative通过沿着X和Y轴从第一个非白色点开始绘制,即所有点都被相对地移动,使得左/上边缘没有空白。

是的,有一种更简单的方法可以做到这一点。诀窍是,Graphics类中没有绘制点的方法,但有一个绘制矩形的方法。所以你所要做的就是为每个点定义许多矩形,然后使用FillRectangles(Brush, Rectangle[])方法来绘制你的点。看看这个代码片段:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Random rnd = new Random();
    Rectangle[] myrects = new Rectangle[1000];
    for (int i = 0; i < 1000; i++) 
        myrects[i] = new Rectangle(rnd.Next(0, this.Width), rnd.Next(0, this.Height), 1, 1);
    g.FillRectangles(Brushes.Red, myrects);
}

请记住,必须使用FillRectangles方法,而不是DrawRectangles。后者在与普通Pens.Red一起使用时将绘制一个宽度为2px的矩形。