示例#1
0
        /// <summary>
        /// Rasterizes this polygon into a given matrix using the given value (label).
        /// </summary>
        public static void RasterizePolygon(Polygon2d polygon, Matrix <int> intoMatrix, int value)
        {
            var lines = polygon.GetEdgeLineArray();

            for (int y = 0; y < intoMatrix.Size.Y; y++)
            {
                var scanLine      = new Line2d(new V2d(0, y), new V2d(intoMatrix.Size.X, y));
                var intersections = new List <int>();
                foreach (var l in lines)
                {
                    if (scanLine.Intersects(l, out V2d p))
                    {
                        intersections.Add((int)(p.X + 0.5));
                    }
                }
                intersections.Sort();

                if (intersections.Count % 2 == 1)
                {
                    intersections.Add((int)intoMatrix.Size.X);
                }
                for (int i = 0; i < intersections.Count; i += 2)
                {
                    for (int x = intersections[i]; x < intersections[i + 1]; x++)
                    {
                        intoMatrix[x, y] = value;
                    }
                }
            }
        }