Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // Дано квадратное уравнение вида
            // x^2+2x-3=0
            var quadratic = new Quadratic()
            {
                A = 1,
                B = 2,
                C = -3
            };

            // Инструмент для решения квадратного уравнения
            var resolver = new QuadraticSolver();

            // Результат решения квадратного уравнения
            var roots = resolver.Solve(quadratic);

            // Вывод результата решения задачи
            if (roots.HasRoots)
            {
                Console.WriteLine($"x1 = {roots.Root1}, x2 = {roots.Root2}");
            }
            else
            {
                Console.WriteLine("Корней нет");
            }

            Console.ReadKey();
        }
Exemplo n.º 2
0
    public void Lock(ZACommons commons, EventDriver eventDriver)
    {
        if (Mode != Modes.Locked)
        {
            return;
        }

        var shipControl = (ShipControlCommons)commons;

        // Guesstimate current target position
        var delta       = eventDriver.TimeSinceStart - LastTargetUpdate;
        var targetGuess = TargetAimPoint + TargetVelocity * delta.TotalSeconds;

        // Solve for intersection of expanding sphere + moving object
        // Note sphere may have an initial radius (to account for offset from
        // CoM) and may only start expanding after some delay (to account for
        // firing sequence + acceleration).
        var offset       = targetGuess - shipControl.ReferencePoint;
        var tVelSquared  = Vector3D.Dot(TargetVelocity, TargetVelocity);
        var a            = tVelSquared - ShellSpeed * ShellSpeed;
        var offsetDotVel = Vector3D.Dot(offset, TargetVelocity);
        var b            = 2.0 * (tVelSquared * ShellFireDelay + offsetDotVel - ShellOffset * ShellSpeed);
        var c            = Vector3D.Dot(offset, offset) + ShellFireDelay * ShellFireDelay * tVelSquared + 2.0 * ShellFireDelay * offsetDotVel - ShellOffset * ShellOffset;

        double interceptTime = 0.0;

        double s1, s2;
        int    solutions = QuadraticSolver.Solve(a, b, c, out s1, out s2);

        // Pick smallest positive intercept time
        if (solutions == 1)
        {
            if (s1 > 0.0)
            {
                interceptTime = s1;
            }
        }
        else if (solutions == 2)
        {
            if (s1 > 0.0)
            {
                interceptTime = s1;
            }
            else if (s2 > 0.0)
            {
                interceptTime = s2;
            }
        }

        var prediction = targetGuess + TargetVelocity * interceptTime;

        double yawPitchError;

        seeker.Seek(shipControl, prediction - shipControl.ReferencePoint, out yawPitchError);

        eventDriver.Schedule(1, Lock);
    }
Exemplo n.º 3
0
        private static IEquationSolutions Solve(string variable, IExpression expression, ClassificationResult classification)
        {
            if (classification.EquationType == EquationTypes.Undefined)
            {
                return(new EquationSolutions());
            }

            if (classification.SearchResult.ElementAt(0).Key != variable)
            {
                //var expression = equation.Left;
                //var classification = equation.Classification;
                //var clone = expression.Clone();
                //substitute = new Tuple<string, string>(variable, GetNewVariableForSub(equation));
                //var sub = new VariableExpression(substitute.Item2, 1);
                //clone.Substitute(ref clone, sub, classification.SearchResult.ElementAt(0).Key);

                var sols = Solve(classification.SearchResult.ElementAt(0).Key, expression, classification);
                return(sols);
            }
            else
            {
                if (classification.EquationType == EquationTypes.Linear)
                {
                    var solver = new LinearSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else if (classification.EquationType == EquationTypes.Quadratic)
                {
                    var solver = new QuadraticSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else if (classification.EquationType == EquationTypes.Trigonometric)
                {
                    var solver = new TrigonometricSolver();
                    return(solver.Solve(expression, variable, classification));
                }
                else
                {
                    return(null);
                }
            }
        }
Exemplo n.º 4
0
                public static void Run()
                {
                    QuadraticSolver quadraticSolver = new QuadraticSolver();

                    Console.Write("a:");
                    quadraticSolver.a = double.Parse(Console.ReadLine());
                    Console.Write("b:");
                    quadraticSolver.b = double.Parse(Console.ReadLine());
                    Console.Write("c:");
                    quadraticSolver.c = double.Parse(Console.ReadLine());

                    double?rootA, rootB;  //Equivalent to "Nullable<double> rootA, rootB;"
                    bool   areRootsFound = quadraticSolver.Solve(out rootA, out rootB);
                    //Usage of "var.Value" instead of "var" is recommended
                    //for nullable types (does not give access to same methods)
                    string message = (areRootsFound ?
                                      $"roots = {rootA.Value:0.000}, {rootB.Value:0.000}"
                                     : "No real root found");

                    Console.WriteLine(message);
                    Console.ReadLine();
                }
Exemplo n.º 5
0
    public void Run(ZACommons commons, EventDriver eventDriver)
    {
        var shipControl = (ShipControlCommons)commons;

        Vector3D?velocity = shipControl.LinearVelocity;

        if (velocity != null)
        {
            // Interpolate position since last update
            var delta       = eventDriver.TimeSinceStart - LastTargetUpdate;
            var targetGuess = TargetAimPoint + TargetVelocity * delta.TotalSeconds;

            // Solve for intersection of expanding sphere + moving object
            var offset = targetGuess - shipControl.ReferencePoint;
            var a      = Vector3D.Dot(TargetVelocity, TargetVelocity) - Vector3D.Dot((Vector3D)velocity, (Vector3D)velocity);
            var b      = 2.0 * Vector3D.Dot(offset, TargetVelocity);
            var c      = Vector3D.Dot(offset, offset);

            double interceptTime = 20.0;

            double s1, s2;
            int    solutions = QuadraticSolver.Solve(a, b, c, out s1, out s2);
            // Pick smallest positive intercept time
            if (solutions == 1)
            {
                if (s1 > 0.0)
                {
                    interceptTime = s1;
                }
            }
            else if (solutions == 2)
            {
                if (s1 > 0.0)
                {
                    interceptTime = s1;
                }
                else if (s2 > 0.0)
                {
                    interceptTime = s2;
                }
            }

            var prediction = targetGuess + TargetVelocity * interceptTime;

            // Determine relative vector to aim point
            var targetVector = prediction - shipControl.ReferencePoint;
            // Project onto our velocity
            var velocityNorm = Vector3D.Normalize((Vector3D)velocity);
            var forwardProj  = velocityNorm * Vector3D.Dot(targetVector, velocityNorm);
            // Use scaled rejection for oversteer
            var forwardRej = (targetVector - forwardProj) * OVERSTEER_FACTOR;
            // Add to projection to get adjusted aimpoint
            var aimPoint = forwardProj + forwardRej;

            double yawPitchError;
            seeker.Seek(shipControl, aimPoint, out yawPitchError);
        }
        else
        {
            // Can't really do crap w/o our own velocity
            shipControl.GyroControl.Reset();
        }

        eventDriver.Schedule(FramesPerRun, Run);
    }