Example #1
0
        public static List <List <Vector2i> > DrawPolygonTranslate(Vector2i[] vs, int vp)
        {
            Vector2i max = vs[0];
            Vector2i min = vs[0];

            for (int i = 1; i < vp; i++)
            {
                max = Vector2i.Max(max, vs[i]);
                min = Vector2i.Min(min, vs[i]);
            }
            Vector2i[] newV = vs.Select(t =>
            {
                return(t - min);
            }).ToArray();
            Vector2i resolute          = max - min;
            ConvexPolygonScanning scan = new ConvexPolygonScanning();

            scan.SetResolution(resolute[0] + 1, resolute[1] + 1);
            scan.DrawPolygon(newV, newV.Length);
            // scan.Show();
            List <List <Vector2i> > results = new List <List <Vector2i> >();

            for (int i = scan.mMapYresb - 1; i >= 0; i--)
            {
                int             idx    = i * scan.mMapXresb;
                List <Vector2i> result = new List <Vector2i>();
                for (int j = 0; j < scan.mMapXresb; ++j)
                {
                    if (scan.mResultMap[idx + j] == 1)
                    {
                        result.Add(new Vector2i(j, i));
                    }
                }
                if (result.Count > 0)
                {
                    result = result.Select(t =>
                    {
                        return(t + min);
                    }).ToList();
                    results.Add(result);
                }
            }
            return(results);
        }
Example #2
0
        public static List <Vector2i> Scan(List <Vector2i> poly)
        {
            List <Vector2i> results = new List <Vector2i>();
            Vector2i        max     = new Vector2i(int.MinValue, int.MinValue);
            Vector2i        min     = new Vector2i(int.MaxValue, int.MaxValue);

            foreach (Vector2i v in poly)
            {
                max = Vector2i.Max(v, max);
                min = Vector2i.Min(v, min);
            }
            List <Vector2i> trans     = poly.Select(t => { return(t - min); }).ToList();
            int             height    = max[1] - min[1];
            List <ScanEdge> scanLines = new List <ScanEdge>();
            int             count     = trans.Count;
            int             count1    = count - 1;

            for (int i = 0; i < count; ++i)
            {
                Vector2i ps   = trans[i];
                int      post = (i + 1) % count;
                Vector2i pe   = trans[post];
                if (ps[1] == pe[1]) // 水平 去掉
                {
                    Vector2i hori = Vector2i.GetVector2i(ps[0], pe[0]);
                    for (int hi = hori[0]; hi <= hori[1]; ++hi)
                    {
                        results.Add(new Vector2i(hi, ps[1]));
                    }
                    continue;
                }
                int      pre    = (i + count1) % count;
                Vector2i pss    = trans[pre];
                int      ppost  = (post + 1) % count;
                Vector2i pee    = trans[ppost];
                double   dx     = (ps[0] - pe[0]) * 1.0f / (ps[1] - pe[1]);
                bool     yiG0   = ps[1] < pe[1];
                double   startX = yiG0 ? ps[0] : pe[0];
                int      ymin   = yiG0 ? ps[1] : pe[1];
                int      ymax   = yiG0 ? pe[1] : ps[1];
                if (pe[1] > ps[1])
                {
                    if (pee[1] >= pe[1])
                    {
                        ymax -= 1;
                    }
                }
                else
                {
                    if (pss[1] >= ps[1])
                    {
                        ymax -= 1;
                    }
                }
                ScanEdge ep = new ScanEdge();
                ep.mMinY   = ymin;
                ep.mMaxY   = ymax;
                ep.mStartX = startX;
                ep.mDx     = dx;
                scanLines.Add(ep);
            }

            for (int i = 0; i < height; ++i)
            {
                List <ScanEdge> tmp = scanLines.FindAll(delegate(ScanEdge line) { return(line.Include(i)); });
                tmp.Sort((e1, e2) =>
                {
                    //此处可以不考虑 斜率。 x相等时,不用考虑谁在前谁在后
                    return(e1.mStartX.CompareTo(e2.mStartX));
                });
                if (tmp.Count % 2 != 0)
                {
                    throw new Exception("必须是偶数");
                }
                for (int idx = 0; idx < tmp.Count; idx += 2)
                {
                    int next = idx + 1;
                    int x1   = (int)tmp[idx].mStartX;
                    int x2   = (int)tmp[next].mStartX;
                    if (x1 > x2)
                    {
                        continue;
                    }
                    for (int xi = x1; xi <= x2; ++xi)
                    {
                        results.Add(new Vector2i(xi, i));
                    }
                    tmp[idx].Increase();
                    tmp[next].Increase();
                }
                scanLines.RemoveAll((t) => t.mMaxY == i);
            }
            results = results.Select(t => { return(t + min); }).ToList();
            return(results);
        }