Beispiel #1
0
 public EdgePointer(int ymax, double x, double m, EdgePointer next)
 {
     this.ymax = ymax;
     this.x    = x;
     this.m    = m;
     this.next = next;
 }
Beispiel #2
0
        void FillPoly(Polygon p)
        {
            EdgePointer[] ET = new EdgePointer[2000];
            int           ymin, ymax, x, ystart = p.points[0].Y;
            double        m;
            int           count = p.points.Count;

            for (int i = 0; i < p.points.Count; i++)
            {
                ymin = p.points[i].Y;
                ymax = p.points[(i + 1) % p.points.Count].Y;
                x    = p.points[i].X;
                m    = (double)(ymin - ymax) / (double)(x - p.points[(i + 1) % p.points.Count].X);
                m    = 1 / m;
                if (ymax < ymin)
                {
                    int t = ymin;
                    ymin = ymax;
                    ymax = t;
                    x    = p.points[(i + 1) % p.points.Count].X;
                }
                else if (ymax == ymin)
                {
                    count--;
                    continue;
                }
                if (ET[ymin] == null)
                {
                    ET[ymin] = new EdgePointer(ymax, x, m, null);
                }
                else
                {
                    EdgePointer pt = ET[ymin];
                    while (pt.next != null)
                    {
                        pt = pt.next;
                    }
                    pt.next = new EdgePointer(ymax, x, m, null);
                }
                if (ystart > ymin)
                {
                    ystart = ymin;
                }
            }

            List <EdgePointer> AET = new List <EdgePointer>();

            while ((count != 0 || AET.Count != 0) && ystart < DrawArea.Height)
            {
                EdgePointer pty = ET[ystart];
                while (pty != null)
                {
                    AET.Add(pty);
                    pty = pty.next;
                    count--;
                }
                AET.Sort((x1, x2) => (int)(x1.x - x2.x));
                for (int i = 0; i < AET.Count; i += 2)
                {
                    int xa1 = (int)AET[i].x + 1;
                    int xa2 = (int)AET[i + 1].x;
                    if (InterpolationCheckBox.Checked)
                    {
                        Color Left  = GetColor(xa1, ystart);
                        Color Right = GetColor(xa2, ystart);
                        DrawPixel(xa1, ystart, Left);
                        DrawPixel(xa2, ystart, Right);
                        for (int j = xa1 + 1; j < xa2; j++)
                        {
                            DrawPixel(j, ystart, InterpolateColor(xa1, xa2, j, Left, Right));
                        }
                    }
                    else
                    {
                        for (int j = xa1; j <= xa2; j++)
                        {
                            DrawPixel(j, ystart, GetColor(j, ystart));
                        }
                    }
                }
                ystart++;
                AET.RemoveAll(x1 => x1.ymax == ystart);
                foreach (var e in AET)
                {
                    e.x += e.m;
                }
            }
        }