Example #1
0
 public static void Fill(Shape s, DrawParams dParams, Brush fillBrush)
 {
     Point[] pArray = new Point[s.Points.Count];
     for (int i = 0; i < s.Points.Count; i++)
     {
         pArray[i] = dParams.Trans.TransPoint(s.Points[i]);
     }
     dParams.Graphics.FillPolygon(fillBrush, pArray);
 }
Example #2
0
        public Shape TransShape(Shape s)
        {
            Shape ret = new Shape();
            int nPoint = s.Points.Count;
            for (int i = 0; i < nPoint; i++)
            {
                ret.Points.Add(TransPoint(s.Points[i]));
            }

            foreach (Shape s1 in s.GetShapes())
            {
                ret.AddNestedShape(TransShape(s1));
            }
            return ret;
        }
Example #3
0
        public void AddNestedShape(Shape s1)
        {
            foreach (Shape s2 in shapes)
            {
                if (s2.CanContain(s1))
                {
                    s2.AddNestedShape(s1);
                    return;
                }
            }

            int nShapes = shapes.Count;
            for (int i = (nShapes-1); i >= 0; i--)
            {
                if (s1.CanContain(shapes[i]))
                {
                    s1.AddNestedShape(shapes[i]);
                    shapes.RemoveAt(i);
                }
            }
            shapes.Add(s1);
        }
Example #4
0
        public static void Draw(Shape s, DrawParams dParams, bool fill = false)
        {
            if (fill)
            {
                Fill(s, dParams, dParams.FillBrush);
                foreach (Shape s2 in s.GetShapes())
                {
                    Draw(s2, dParams);
                    Fill(s2, dParams, new SolidBrush(Color.White));
                }
            }

            Point[] pArray = new Point[s.Points.Count];
            for (int i = 0; i < s.Points.Count; i++)
            {
                pArray[i] = dParams.Trans.TransPoint(s.Points[i]);
            }

            for (int i = 0; i < s.Points.Count; i++)
            {
                dParams.Graphics.DrawLine(dParams.Pen, pArray[i], pArray[(i + 1) % s.Points.Count]);
            }
        }
Example #5
0
        private bool ShapeIsValid(Shape s)
        {
            if (s.Points.Count < 4)
                return false;

            int left = 0;
            int right = 0;

            for (int i = 0; i < s.Points.Count; i++)
            {
                ILine l1 = new Line(s.Points[i], s.Points[(i + 1) % s.Points.Count]);
                ILine l2 = new Line(s.Points[(i + 1) % s.Points.Count], s.Points[(i + 2) % s.Points.Count]);

                switch (l1.RelativeDirection(l2))
                {
                    case Line.Direction.Left: left++;
                        break;
                    case Line.Direction.Right: right++;
                        break;
                    default:
                        break;
                }
            }
            return ((right - left) == 4);
        }
Example #6
0
 private bool ShapeAlreadyExists(Shape newShape)
 {
     foreach (Shape s in shapes)
     {
         if (s.EqualsOrContains(newShape))
         {
             return true;
         }
     }
     return false;
 }
Example #7
0
 private bool ResolveDuplicateLines(Shape s, List<ILine> lines)
 {
     bool ret = false;
     List<ILine> l = s.GetLines();
     int nLine = l.Count;
     for (int i = 0; i < nLine; i++)
     {
         for (int j = i+1; j < nLine; j++)
         {
             if (l[i].IsCongruentTo(l[j]))
             {
                 KillLine(l[i], lines);
                 ret = true;
             }
         }
     }
     return ret;
 }
Example #8
0
        private void MapShape(MyLineQueue q, List<ILine> lines)
        {
            Shape s = new Shape();
            try
            {
                ILine start = q.Dequeue();
                int qSize = q.Count;

                FollowLine(start, s, q, lines);

                if (ShapeAlreadyExists(s))
                {
                    int nRemove = q.Count - qSize;
                    if (nRemove > 0)
                        q.Remove(nRemove);
                }
                else if (ResolveDuplicateLines(s, lines))
                {
                    int remove = q.Count - qSize;
                    if (remove > 0)
                        q.Remove(remove);
                }
                else if (!ShapeIsValid(s))
                {
                    int remove = q.Count - qSize;
                    if (remove > 0)
                    {
                        q.Remove(remove);
                    }
                    ILine li = (s.GetLines())[0];
                    q.Enqueue(Line.Flip(li), shapes);
                }
                else
                {
                    AddShape(s);
                }
            }
            catch
            {
            }
        }
Example #9
0
        private void FollowLine(ILine start, Shape s, MyLineQueue q, List<ILine> lines)
        {
            // q.Remove(start);

            if (!s.AddPoint(start.Point1))
                return;

            List<ILine> l = new List<ILine>();

            for (int i = 0; i < lines.Count; i++)
            {
                if (!lines[i].IsCongruentTo(start))
                {
                    if (lines[i].HasPoint(start.Point2))
                    {
                        if (start.Point2 != lines[i].Point1)
                        {
                            lines[i] = Line.Flip(lines[i]);
                        }

                        l.Add(lines[i]);
                    }
                }
            }

            if (l.Count == 1)
            {
                FollowLine(l[0], s, q, lines);
            }
            else
            {
                int iR = -1;
                int iL = -1;
                int iU = -1;
                for (int i = 0; i < l.Count; i++)
                {
                    switch (start.RelativeDirection(l[i]))
                    {
                        case Line.Direction.Right: iR = i; break;
                        case Line.Direction.Left: iL = i; break;
                        case Line.Direction.Up: iU = i; break;
                        case Line.Direction.Down: iU = i; break;
                        case Line.Direction.Undef: iU = i; break;
                        default: break;
                    }
                }

                if (iR > -1)
                {
                    if (iU > -1)
                        q.Enqueue(l[iU], shapes);
                    if (iL > -1)
                        q.Enqueue(l[iL], shapes);

                    FollowLine(l[iR], s, q, lines);

                }
                else if (iU > -1)
                {
                    if (iL > -1)
                        q.Enqueue(l[iL], shapes);
                    FollowLine(l[iU], s, q, lines);
                }
                else if (iL > -1)
                {
                    FollowLine(l[iL], s, q, lines);
                }
            }
            return;
        }
Example #10
0
 private void AddShape(Shape s)
 {
     int nShape = shapes.Count;
     for (int i = nShape-1; i >= 0; i--)
     {
         if (shapes[i].CanContain(s))
         {
             shapes[i].AddNestedShape(s);
             return;
         }
         else if (s.CanContain(shapes[i]))
         {
             s.AddNestedShape(shapes[i]);
             shapes.RemoveAt(i);
         }
     }
     shapes.Add(s);
 }
Example #11
0
        public bool IsAdjacentTo(Shape s)
        {
            List<ILine> lines1 = this.GetLines();
            int nLine = lines1.Count;

            for (int i = 0; i < nLine; ++i)
            {
                if (s.Contains(lines1[i]))
                    return true;
            }
            return false;
        }
Example #12
0
        public bool EqualsOrContains(Shape newShape)
        {
            if (this.Equals(newShape))
                return true;

            foreach (Shape s in shapes)
            {
                if (s.EqualsOrContains(newShape))
                    return true;
            }
            return false;
        }
Example #13
0
        public bool Equals(Shape s)
        {
            if (this.points.Count != s.points.Count)
                return false;

            foreach (Point p in this.points)
            {
                if (!s.Exists(p))
                    return false;
            }
            return true;
        }
Example #14
0
        public bool CanContain(Shape s)
        {
            if (this.LeftEdge > s.LeftEdge
                || this.RightEdge < s.RightEdge
                || this.TopEdge < s.TopEdge
                || this.BottomEdge > s.BottomEdge)
                return false;

            if (this.GetAreaOfSpace() <= s.GetAreaOfSpace())
                return false;

            Point rightest = ShapeMaker.InvalidPoint;

            foreach (Point ps in s.Points)
            {
                if (ps.X == s.RightEdge)
                {
                    rightest = ps;
                    break;
                }
            }
            if (rightest == ShapeMaker.InvalidPoint)
                return false;

            ILine ray = new Line(rightest, new Point(Int32.MaxValue, rightest.Y));

            int intersections = 0;

            foreach (ILine l in this.GetLines())
            {
                if (Line.Crosses(ray, l))
                    intersections++;
            }
            return (intersections % 2) != 0;
        }