Esempio n. 1
0
        public bool Solve(int MaxRounds = 1024)
        {
            AxisAlignedBox2d TargetBounds = Target.Bounds;

            Box2d fitBounds = new Box2d(Fit.GetBounds());

            InputTranslate = -fitBounds.Center;
            int N = Fit.VertexCount;

            Polygon2d FitCentered = new Polygon2d(Fit);

            for (int k = 0; k < N; ++k)
            {
                FitCentered[k] = Fit[k] + InputTranslate;
            }
            fitBounds = new Box2d(FitCentered.GetBounds());


            Polygon2d FitXForm = new Polygon2d(FitCentered);

            Random r = new Random(31337);

            int  ri     = 0;
            bool solved = false;

            while (ri++ < MaxRounds && solved == false)
            {
                Vector2d center = random_point_in_target(r, TargetBounds.SampleT, Target.Contains);

                Matrix2d rotate = Matrix2d.Identity;
                if (EnableRotate)
                {
                    Util.gDevAssert(EnableRandomRotations == false);
                    double rotAngle = ValidRotationsDeg[r.Next() % ValidRotationsDeg.Length];
                    rotate.SetToRotationDeg(rotAngle);
                }

                for (int k = 0; k < N; ++k)
                {
                    FitXForm[k] = rotate * (ApplyScale * FitCentered[k]) + center;
                }

                if (!Target.Outer.Contains(FitXForm))
                {
                    continue;
                }

                if (Target.Intersects(FitXForm))
                {
                    continue;
                }

                FitBounds = fitBounds;
                FitBounds.RotateAxes(rotate);
                FitBounds.Translate(center);
                FitRotation = rotate;
                FitPolygon  = FitXForm;
                solved      = true;
            }

            return(solved);
        }