private bool StartWithPolygonSegment(Polygon polygon, Segment segment)
        {
            if (!Arithmetic.IsSquare(segment.QuadratOfLength))
            {
                return(false);
            }

            var destSegment    = new Segment(new Vector(0, 0), new Vector(Arithmetic.Sqrt(segment.QuadratOfLength), 0));
            var transpOperator = TransposeOperator.ConstructOperator(segment, destSegment);

            var fixedPolygon = transpOperator.TransposePolygon(polygon);

            if (SegmentsMatrix.TryAddPolygon(fixedPolygon))
            {
                SetPolygon(fixedPolygon);
                var success = DoIt();
                if (!success)
                {
                    RemovePolygon(fixedPolygon);
                }
                return(success);
            }
            fixedPolygon = fixedPolygon.Reflect(destSegment);
            if (SegmentsMatrix.TryAddPolygon(fixedPolygon))
            {
                SetPolygon(fixedPolygon);
                var success = DoIt();
                if (!success)
                {
                    RemovePolygon(fixedPolygon);
                }
                return(success);
            }
            return(false);
        }
        private void DoRound(Polygon polygon, Polygon startPolygon, Segment startSegment, Segment segment, List <List <Polygon> > result,
                             Stack <Polygon> stack, Vector vertex, int maxCount, int deep)
        {
            if (result.Count > maxCount)
            {
                return;
            }

            if (deep > 10)
            {
                Console.WriteLine("");
            }

            if (startSegment.Equals(segment) && stack.Count > 0 || IsOnSquareBound(segment))
            {
                result.Add(stack.ToList());
                return;
            }

            if (Segments.ContainsKey(segment))
            {
                var skipPolygons = Segments[segment].Where(p => !UsedPolygonsInStack.Contains(p) || polygon != startPolygon && p == startPolygon).ToList();
                if (skipPolygons.Count > 0)
                {
                    var skipSegment = skipPolygons[0].Segments.First(s => IsEndOfSegment(vertex, s) && !s.Equals(segment));
                    UsedPolygonsInStack.Add(skipPolygons[0]);
                    DoRound(skipPolygons[0], startPolygon, startSegment, skipSegment, result, stack, vertex, maxCount, deep + 1);
                    UsedPolygonsInStack.Remove(skipPolygons[0]);
                    return;
                }
            }

            var originalSegment  = GivenPolygons[polygon.Id].Segments.First(s => segment.Id == s.Id);
            var trOperator       = TransposeOperator.ConstructOperator(originalSegment, segment);
            var possiblePolygons = GivenGraph[originalSegment]
                                   .Select(p => (p.Id == polygon.Id) ^ polygon.IsReflected ? p.Reflect(originalSegment) : p)
                                   .Select(p => trOperator.TransposePolygon(p));

            foreach (var pPolygon in possiblePolygons)
            {
                if (!SegmentsMatrix.TryAddPolygon(pPolygon))
                {
                    continue;
                }
                var newSegment = pPolygon.Segments.Where(s => s.Start.Equals(vertex) || s.End.Equals(vertex)).FirstOrDefault(s => !s.Equals(segment));
                if (newSegment == null)
                {
                    SegmentsMatrix.RemovePolygon(pPolygon);
                    return;
                }

                stack.Push(pPolygon);
                DoRound(pPolygon, startPolygon, startSegment, newSegment, result, stack, vertex, maxCount, deep + 1);
                stack.Pop();
                SegmentsMatrix.RemovePolygon(pPolygon);
            }
        }
Beispiel #3
0
        public static TransposeOperator ConstructOperator(Segment source, Segment destination)
        {
            var sVector = source.End - source.Start;
            var dVector = destination.End - destination.Start;

            var cos = GeometryExtensions.GetCos(sVector, dVector);
            var sin = GeometryExtensions.GetSin(sVector, dVector);

            var matrix = new TransposeOperator();

            matrix[0, 0] = matrix[1, 1] = cos;
            matrix[1, 0] = sin;
            matrix[0, 1] = -sin;

            var newDVector = matrix.Apply(source.Start);

            matrix.MoveVector = destination.Start - newDVector;
            return(matrix);
        }