public bool Clip(Vertices clipVertices, Vector2 position)
        {
            Rot       rot = new Rot(0);
            Transform t   = new Transform(ref position, ref rot);

            //Transform shape
            Transform thistransform;

            Body.GetTransform(out thistransform);

            //Transform the shape
            Vertices transformedshape = new Vertices(clipVertices.Count);

            foreach (Vector2 v in clipVertices)
            {
                Vector2 newv = v;
                newv = MathUtils.Mul(ref t, ref newv);
                newv = MathUtils.MulT(ref thistransform, ref newv);
                transformedshape.Add(newv);
            }

            PolyClipError   error;
            List <Vertices> result = YuPengClipper.Difference(Vertices, transformedshape, out error);

            if (error != PolyClipError.None)
            {
                return(false);
            }

            //Need to check if the entire shape was cut,
            //so we can destroy/erase it
            if (result.Count == 0)
            {
                return(false);
            }

            //The shape was split up,
            //so create a new DestructableBody for each piece
            if (result.Count > 1)
            {
                //Create a new destructable body for each extra shape
                for (int i = 1; i < result.Count; i++)
                {
                    DestructableBody db = new DestructableBody(_world, result[i]);
                    db.Body.Position = Body.Position;
                }
            }

            //Set Shape
            Vertices newshape = result[0];

            SetShape(newshape);

            return(true);
        }
Пример #2
0
        public override void Keyboard(KeyboardManager keyboardManager)
        {
            // Add Circles
            if (keyboardManager.IsNewKeyPress(Keys.Q))
            {
                AddCircle(3, 8);
            }

            // Add Circles
            if (keyboardManager.IsNewKeyPress(Keys.W))
            {
                AddCircle(4, 16);
            }

            // Add Circles
            if (keyboardManager.IsNewKeyPress(Keys.E))
            {
                AddCircle(5, 32);
            }

            // Add Rectangle
            if (keyboardManager.IsNewKeyPress(Keys.A))
            {
                AddRectangle(4, 8);
            }

            // Add Rectangle
            if (keyboardManager.IsNewKeyPress(Keys.S))
            {
                AddRectangle(5, 2);
            }

            // Add Rectangle
            if (keyboardManager.IsNewKeyPress(Keys.D))
            {
                AddRectangle(2, 5);
            }

            // Perform a Union
            if (keyboardManager.IsNewKeyPress(Keys.Space))
            {
                if (_subject != null && _clip != null)
                {
                    DoBooleanOperation(YuPengClipper.Union(_subject, _clip, out _err));
                }
            }

            // Perform a Subtraction
            if (keyboardManager.IsNewKeyPress(Keys.Back))
            {
                if (_subject != null && _clip != null)
                {
                    DoBooleanOperation(YuPengClipper.Difference(_subject, _clip, out _err));
                }
            }

            // Perform a Intersection
            if (keyboardManager.IsNewKeyPress(Keys.LeftShift))
            {
                if (_subject != null && _clip != null)
                {
                    DoBooleanOperation(YuPengClipper.Intersect(_subject, _clip, out _err));
                }
            }

            // Select Subject
            if (keyboardManager.IsNewKeyPress(Keys.D1))
            {
                if (_selected != null)
                {
                    if (_clip == _selected)
                    {
                        _clip = null;
                    }

                    _subject = _selected;
                }
            }

            // Select Clip
            if (keyboardManager.IsNewKeyPress(Keys.D2))
            {
                if (_selected != null)
                {
                    if (_subject == _selected)
                    {
                        _subject = null;
                    }

                    _clip = _selected;
                }
            }
        }
Пример #3
0
        protected override void OnKeyDown(KeyEventArgs e)
        {
            // Add Circles
            if (e.KeyCode == Keys.Q)
            {
                AddCircle(3, 8);
            }

            // Add Circles
            if (e.KeyCode == Keys.W)
            {
                AddCircle(4, 16);
            }

            // Add Circles
            if (e.KeyCode == Keys.E)
            {
                AddCircle(5, 32);
            }

            // Add Rectangle
            if (e.KeyCode == Keys.A)
            {
                AddRectangle(4, 8);
            }

            // Add Rectangle
            if (e.KeyCode == Keys.S)
            {
                AddRectangle(5, 2);
            }

            // Add Rectangle
            if (e.KeyCode == Keys.D)
            {
                AddRectangle(2, 5);
            }

            // Perform a Union
            if (e.KeyCode == Keys.Space)
            {
                if (_subject != null && _clip != null)
                {
                    PolyClipError _err;
                    DoBooleanOperation(YuPengClipper.Union(_subject, _clip, out _err));
                }
            }

            // Perform a Subtraction
            if (e.KeyCode == Keys.Back)
            {
                if (_subject != null && _clip != null)
                {
                    PolyClipError _err;
                    DoBooleanOperation(YuPengClipper.Difference(_subject, _clip, out _err));
                }
            }

            // Perform a Intersection
            if (e.KeyCode == Keys.LeftShift)
            {
                if (_subject != null && _clip != null)
                {
                    PolyClipError _err;
                    DoBooleanOperation(YuPengClipper.Intersect(_subject, _clip, out _err));
                }
            }

            // Select Subject
            if (e.KeyCode == Keys.D1)
            {
                if (_selected != null)
                {
                    if (_clip == _selected)
                    {
                        _clip = null;
                    }

                    _subject = _selected;
                }
            }

            // Select Clip
            if (e.KeyCode == Keys.D2)
            {
                if (_selected != null)
                {
                    if (_subject == _selected)
                    {
                        _subject = null;
                    }

                    _clip = _selected;
                }
            }
        }
Пример #4
0
        protected virtual void MergePolygons(IEnumerable <Polygon> polygons, ObstacleCollection obstacles)
        {
            var filledPolygons = new List <Vertices>();
            var mergedPolygons = new List <Vertices>();

            foreach (var polygon in polygons)
            {
                if (polygon.CheckIfFilled(Map, Layer, obstacles))
                {
                    filledPolygons.Add(polygon);
                }
                else
                {
                    AddLineObstacles(polygon, obstacles, Layer);
                }
            }

            while (filledPolygons.Count > 0)
            {
                var polygon = filledPolygons.First();
                var changed = true;
                while (changed)
                {
                    changed = false;
                    var index = 0;
                    while (index < filledPolygons.Count)
                    {
                        var polygon2         = filledPolygons[index];
                        var error            = PolyClipError.None;
                        var combinedPolygons = polygon == polygon2 ? new List <Vertices> {
                            polygon
                        } : YuPengClipper.Union(polygon2, polygon, out error);
                        if (combinedPolygons.Count == 0)
                        {
                            //sometimes a polygon is a subset of the other polygon. Try to check that...
                            Vertices biggerPolygon;
                            if (VerticesEx.IsPolygonSubsetOf(polygon, polygon2, out biggerPolygon))
                            {
                                combinedPolygons = new List <Vertices> {
                                    biggerPolygon
                                };
                                error = PolyClipError.None; //we could recover it...
                            }
                        }
                        //if (combinedPolygons.Count > 2)
                        //    //the polygons intersect at several points, we ignore them as this would lead to holes...
                        //if (error != PolyClipError.None)
                        //    //some error occurred, so we igore this union process...
                        if (combinedPolygons.Count == 1) //they intersect
                        {
                            filledPolygons.Remove(polygon2);
                            changed = true;
                            polygon = SimplifyTools.CollinearSimplify(combinedPolygons[0]);
                        }
                        else //combinedPolygons.Count > 1 --> they don't intersect (or may do at several points, but we ignore it )
                        {
                            index++;
                        }
                    }
                }
                mergedPolygons.Add(polygon);
            }

            foreach (var mergedPolygon in mergedPolygons)
            {
                AddPolygonObstacle(mergedPolygon, VerticesEx.IsRectangle(mergedPolygon), obstacles, Layer);
            }
        }