2017-05-31 123 views
0

我需要像素化/从封闭的2D多边形获得点。不勾勒,但用“像素”体素填充,以检索它们的位置作为点。 现在我有用于线栅格的C#代码,有没有类似于多边形的方法?Bresenhams多边形C#

回答

0

只需使用DrawLine()绘制一个多边形。没有绘制多边形的具体算法。

void DrawPoly(IEnumerable<Point> points) 
{ 
    endpoints = points.Skip(1).Concat(new []{points.First()}); 
    pairs = points.Zip(endpoints, Tuple.Create); 

    for(var pair in pairs) 
    { 
     DrawLine(pair.Item1, pair.Item2); 
    } 
} 

void DrawLine(Point p1, Point p2) 
{ 
    // Your Bresenham code here 
} 

据编辑,你要填充的多边形。如果您想手动执行此操作,请尝试使用these之一。

听起来好像你想要一个包含多边形的所有坐标的大列表。有可能有更好的方法来解决潜在的问题。但是,您有两种选择:

  • 在绘制每个点时存储它。这要求您重写上述算法。
  • 使用现有库绘制多边形。然后,把得到的图像和附加坐标图像矩阵,它压扁成一维列表,然后过滤掉非黑值:

    /* (0,0), (0,1), ..., (w,h) */ 
    grid = Enumerable.Range(0, width) 
        .SelectMany(x => Enumerable.Range(0, height) 
         .Select(y => new Point(x, y))); 
    
    flattened = image.SelectMany(p => p) 
        .Zip(grid, (a,b) => new {PixelValue = a, Coordinate = b}); 
    
    filledPoints = flattened.Where(p => p.PixelValue == 0) 
        .Select(p => p.Coordinate); 
    
+0

也许我的问题是不准确的,我正在寻找如何用像素填充多边形并检索它们的位置。像体素化一样。 –

+1

听起来像是在寻找[多边形填充算法](https://www.tutorialspoint.com/computer_graphics/polygon_filling_algorithm.htm)。那里列出了一些。如果要提取所有点的列表,可以:1.在绘制点时存储点,或2.将坐标附加到图像矩阵,将其平铺到一维列表中,然后执行一个'.Where (p => p.PixelValue == 0)'。 –

+0

如果我有绘制多边形并填充它的简单函数,如何附加图像矩阵?见下面我的功能 –

0

如何我这件事情与矩阵连接存储填充像素?

public static List<Tuple<int, int>> PixelizePolygon(PaintEventArgs e) 
{ 
    List<Tuple<int,int>> pixels = new List<Tuple<int, int>>(); 

    // Create solid brush. 
    SolidBrush blueBrush = new SolidBrush(Color.Blue); 

    // Create points that define polygon. 
    PointF point1 = new PointF(50.0F, 50.0F); 
    PointF point2 = new PointF(100.0F, 25.0F); 
    PointF point3 = new PointF(200.0F, 5.0F); 
    PointF point4 = new PointF(250.0F, 50.0F); 
    PointF point5 = new PointF(300.0F, 100.0F); 
    PointF point6 = new PointF(350.0F, 200.0F); 
    PointF point7 = new PointF(250.0F, 250.0F); 
    PointF[] curvePoints = { point1, point2, point3, point4, point5, point6, point7 }; 

    // Define fill mode. 
    FillMode newFillMode = FillMode.Winding; 

    // Fill polygon to screen. 
    e.Graphics.FillPolygon(blueBrush, curvePoints, newFillMode); 



    return pixels; 

}