Example #1
0
        public static Event Register(EventType gameEvent, FunctionWithArguments function)
        {
            var newEvent = new Event(gameEvent, function);

            newEvent.IsRegistered = true;
            RegisteredEvents.Registered[gameEvent].Add(newEvent);
            return(newEvent);
        }
Example #2
0
        public double DfDx(FunctionWithArguments Func, object Arguments, double x, double h)
        {
            double XPrev    = x - h;
            double XNext    = x + h;
            double FuncPrev = Func(XPrev, Arguments);
            double FuncNext = Func(XNext, Arguments);

            return((FuncNext - FuncPrev) / (2.0D * h));
        }
Example #3
0
        public double DfDx(FunctionWithArguments Func, object Arguments, double x, double InitialStepSize, double Precision)
        {
            double precision = Precision; // precision to evaluate derivative
            double h = InitialStepSize;   // initial step size
            double c = 10.0D;             // factor do divide h (i.e., h_new =h_old/c)
            double eps = 1.0E-100D;       // small number
            int    maxSteps = 100;        // maximum times to divid h
            double devOld, devNew;        // old derivative
            double calcPrecision;         // new derivative

            // calculate initial derivative
            devOld = this.DfDx(Func, Arguments, x, h);
            devNew = devOld;

            for (int i = 0; i < maxSteps; i++)
            {
                // reduce h
                h /= c;
                // calculate new derivative
                devNew = this.DfDx(Func, Arguments, x, h);
                //calculate precision
                if (Math.Abs(devNew) > eps)
                {
                    calcPrecision = Math.Abs((devNew - devOld) / devNew);
                }
                else
                {
                    // don't divide by devNew if devNew is small
                    calcPrecision = Math.Abs(devNew - devOld);
                }
                // stop if precision is met
                if (calcPrecision < precision)
                {
                    //Console.Write("number of steps = "+i.ToString()+"\n");
                    //Console.Write("Calculated precision = "+calcPrecision.ToString()+"\n");
                    return(devNew);
                }
                devOld = devNew;
            }
            return(devNew);
        }
    public static void Main(string[] args)
    {
        FunctionWithNoArgumentsAndNoResult simpleFunction = delegate() {
            System.Console.WriteLine("Simple function.");
        };

        FunctionWithNoArgumentsAndNoResult functionWithLocalVariable = delegate() {
            int i = 4;
            System.Console.WriteLine(string.Format("A function with a local variable: {0}", i));
        };

        FunctionWithResult functionReturningValue = delegate() {
            System.Console.WriteLine("A function returning value.");
            return(9);
        };

        FunctionWithArguments functionWithArguments = delegate(int number, string text) {
            System.Console.WriteLine("A function with some arguments:\n"
                                     + string.Format("number: {0}\n", number)
                                     + string.Format("text: {0}", text));
        };

        int j;

        System.Console.WriteLine("Functions:\n");

        simpleFunction();
        System.Console.WriteLine();

        functionWithLocalVariable();
        System.Console.WriteLine();

        j = functionReturningValue();
        System.Console.WriteLine(string.Format("returned value: {0}\n", j));

        functionWithArguments(6, "orange");
        System.Console.WriteLine();
    }
Example #5
0
 Event(EventType gameEvent, FunctionWithArguments function)
 {
     this.GameEvent = gameEvent;
     this.Function  = function;
 }