我需要从封闭的2d多边形中像素化/获取点。不是轮廓,而是填充"像素"体素检索他们的位置作为点。 现在我有用于行光栅化的C#代码,是否有类似多边形的方法?
答案 0 :(得分: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之一。
听起来你想要填充多边形的所有坐标的大列表。可能有更好的方法来解决潜在的问题。但是,您有两种选择:
使用现有库绘制多边形。然后,获取生成的图像并将坐标附加到图像矩阵,将其展平为1D列表,然后过滤掉非黑色值:
/* (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);
答案 1 :(得分: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;
}