예제 #1
0
        public void FillPolygon(Polygon3D p)
        {
            //Tried using fill lines and storing them in the polygon. Had issues with concave polygons where it would draw outside of them because I didn't
            //have mechanism like the parity bit flip. This is attempt two....
            int Y        = (int)(p.OrderByDescending(i => i.Y).First().Y);
            int lowestY  = (int)(p.OrderByDescending(i => i.Y).Last().Y);
            int lowestX  = (int)(p.OrderByDescending(i => i.X).Last().X);
            int highestX = (int)(p.OrderByDescending(i => i.X).First().X);

            while (Y >= lowestY)
            {
                List <Vector4 <double> > Intersections = new List <Vector4 <double> >();

                List <Line3D> lines = p.GetLines();
                for (int i = 0; i < lines.Count; i++)
                {
                    Line3D           scanLine = new Line3D(new Vector4 <double>(lowestX, Y, 1, 1), new Vector4 <double>(highestX, Y, 1, 1));
                    Vector4 <double> intPoint = Intersect(scanLine, lines[i]);
                    if (null != intPoint) // they interesected
                    {
                        if ((int)System.Math.Round(intPoint.Y) != lines[i].MaxY)
                        {
                            Intersections.Add(intPoint);
                        }
                        else
                        {
                            //we need to do something about the corners causing issues with rounding.
                        }
                    }
                }
                //sort items by x value so we can easily create lines

                var sorted = Intersections.OrderBy(i => i.X).ToList();

                while (sorted.Count > 1)
                {
                    //Start at least x. Not using a parity bit. but this functions exactly the same way.
                    //Y stays constant while drawing. X is incremented
                    int x = (int)System.Math.Round(sorted[0].X, MidpointRounding.AwayFromZero);
                    //Start drawing and remove first point.
                    sorted.RemoveAt(0);
                    x++;
                    while (x < sorted[0].X)
                    {
                        WritePixel(x, Y, true);
                        x++;
                    }
                    sorted.RemoveAt(0);
                }
                Y--;
            }
        }