0

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

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;
}

这似乎需要做很多工作。我必须找到最左边和最右边的点,计算宽度等。我不是在谈论性能或任何东西。只知道框架是否有办法做到这一点。

4

2 回答 2

0

是的,有一种更简单的方法可以做到这一点。诀窍在于,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.

于 2013-08-06T01:46:13.257 回答
0

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

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方法沿着输入中指定的精确坐标绘制pointsDrawRelative从沿 X 和 Y 轴的第一个非白点开始绘制,即所有点都相对移动,因此左/上边缘没有空白。

于 2013-09-13T05:51:13.227 回答