public SimplePolygon ToPolygon(SimplePolygon _newPolygon = null)
        {
            if (_newPolygon == null)
            {
                _newPolygon = new SimplePolygon();
            }
            else
            {
                _newPolygon.Clear();
            }
            m_fullCast.Clear();
            foreach (var _segement in m_segements)
            {
                var _left   = _segement.Key;
                var _rights = _segement.Value;
                foreach (var _right in _rights)
                {
                    m_fullCast.Add(_left, _right);
                    m_fullCast.Add(_right, _left);
                }
            }
            m_oneLinkOne.Clear();

            if (m_fullCast.Count > 0)
            {
                var _left = m_fullCast.First().Key;
                m_oneLinkOne.Add(_left, _left);
                _newPolygon.AddPoint(_left);
                for (int i = 0; i < m_fullCast.Count; i++)
                {
                    var _rights = m_fullCast[_left];
                    foreach (var _right in _rights)
                    {
                        if (!m_oneLinkOne.ContainsKey(_right))
                        {
                            m_oneLinkOne.Add(_right, _right);
                            _newPolygon.AddPoint(_right);
                            _left = _right;
                            break;
                        }
                    }
                }
            }
            return(_newPolygon.MakeSimple());
        }
 public bool CrossWith(SimplePolygon _other)
 {
     return(m_sweepLineAlg.CrossWith(this, _other));
 }
Example #3
0
        public bool CrossWith(SimplePolygon _polygon1, SimplePolygon _polygon2)
        {
            // _polygon1.ValidateLoop();
            // _polygon2.ValidateLoop();
            m_rowPoints.Clear();
            List <Point2> _polygon1Points = _polygon1.Points;
            int           len1            = _polygon1Points.Count;

            for (int i = 0; i < len1; i++)
            {
                var _pI = _polygon1Points[i];
                _pI.m_polygonFlag = 0;
                m_rowPoints.Add(_pI);
            }
            List <Point2> _polygon2Points = _polygon2.Points;
            int           len2            = _polygon2Points.Count;

            for (int i = 0; i < len2; i++)
            {
                var _pI = _polygon2Points[i];
                _pI.m_polygonFlag = 1;
                m_rowPoints.Add(_pI);
            }
            //sort from left to right
            m_rowPoints.QuickSort();
            //ShowPoints("-----sorted points-----");
            //sweep from left to right
            int  _bufLen = m_rowPoints.Count;
            bool _result = false;

            for (int i = 0; i < _bufLen; i++)
            {
                Point2 _pI             = m_rowPoints[i];
                var    _sweepLineCheck = _pI.m_polygonFlag == 0 ? m_sweepLine1 : m_sweepLine0;
                if (_sweepLineCheck.InoutOf(_pI) == InOut.In)
                {
                    _sweepLineCheck.InoutOf(_pI);
                    _result = true;
                    break;
                }
                var     _sweepLineStep = _pI.m_polygonFlag == 0 ? m_sweepLine0 : m_sweepLine1;
                Point2  _pIL           = _pI.m_left;
                Point2  _pIR           = _pI.m_right;
                Vector2 _pIV           = _pI.getValue();
                Vector2 _toLeft        = _pIL.getValue() - _pIV;
                Vector2 _toRight       = _pIR.getValue() - _pIV;
                if (_toLeft.x > 0 && _toRight.x > 0)      //Open Site,add double slope
                {
                    _sweepLineStep.OpenRegion(_pI);
                }
                else if (_toLeft.x < 0 && _toRight.x < 0) //Close Site,remove double slopes
                {
                    _sweepLineStep.CloseRegion(_pI, _toLeft, _toRight);
                }
                else //Turn Site,add single slope
                {
                    _sweepLineStep.TurnReion(_pI);
                }
            }

            m_rowPoints.Clear();
            m_sweepLine0.Clear();
            m_sweepLine1.Clear();
            return(_result);
        }