예제 #1
0
        private static void Clamp <T>(T expectL, T expectR, T l, T r, T targetL, T targetR)
        {
            var          expect = new Interval <T>(expectL, expectR);
            Interval <T> i = new Interval <T>(l, r), target = new Interval <T>(targetL, targetR);

            Assert.AreEqual(expect, i.Clamp(target));
        }
예제 #2
0
        public object Coerce(object context, object value)
        {
            if (value == null)
            {
                return(null);
            }

            // Initialize functions to access the panel.
            if (_getMaxima == null)
            {
                Type type = context.GetType();

                // Allow accessing properties.
                MethodInfo getMaxima = type.GetProperty("Maxima" + _axisName).GetGetMethod();
                _getMaxima = DelegateHelper.CreateOpenInstanceDelegate <Func <object, object> >(getMaxima, DelegateHelper.CreateOptions.Downcasting);
                MethodInfo getMinimumSize = type.GetProperty("MinimumSize" + _axisName).GetGetMethod();
                _getMinimumSize = DelegateHelper.CreateOpenInstanceDelegate <Func <object, object> >(getMinimumSize, DelegateHelper.CreateOptions.Downcasting);
                MethodInfo getMaximumSize = type.GetProperty("MaximumSize" + _axisName).GetGetMethod();
                _getMaximumSize = DelegateHelper.CreateOpenInstanceDelegate <Func <object, object> >(getMaximumSize, DelegateHelper.CreateOptions.Downcasting);

                // Allow accessing protected conversion functions.
                MethodInfo convertToInternalInterval = type.GetMethod(_convertToInternalIntervalMethod, Reflection.ReflectionHelper.InstanceMembers);
                _convertToInternalInterval =
                    DelegateHelper.CreateOpenInstanceDelegate <Func <object, object, Interval <double> > >(convertToInternalInterval, DelegateHelper.CreateOptions.Downcasting);
                MethodInfo convertToInterval = type.GetMethod(_convertToIntervalMethod, Reflection.ReflectionHelper.InstanceMembers);
                _convertToInterval =
                    DelegateHelper.CreateOpenInstanceDelegate <Func <object, Interval <double>, object> >(convertToInterval, DelegateHelper.CreateOptions.Downcasting);
                MethodInfo convertToInternalSize = type.GetMethod(_convertToInternalSizeMethod, Reflection.ReflectionHelper.InstanceMembers);
                _convertToInternalSize =
                    DelegateHelper.CreateOpenInstanceDelegate <Func <object, object, double> >(convertToInternalSize, DelegateHelper.CreateOptions.Downcasting);
            }

            // Limit size of the desired interval.
            Interval <double> setInterval   = _convertToInternalInterval(context, value);
            object            min           = _getMinimumSize(context);
            double            minimumSize   = _convertToInternalSize(context, min);
            double            tooSmallRatio = minimumSize / setInterval.Size;

            if (tooSmallRatio > 1)
            {
                setInterval = setInterval.Scale(tooSmallRatio);
            }
            object max         = _getMaximumSize(context);
            double maximumSize = _convertToInternalSize(context, max);
            double tooBigRatio = maximumSize / setInterval.Size;

            if (tooBigRatio < 1)
            {
                setInterval = setInterval.Scale(tooBigRatio);
            }

            // Limit how far the time line goes.
            Interval <double> limitInterval = _convertToInternalInterval(context, _getMaxima(context));
            Interval <double> limited       = setInterval.Clamp(limitInterval);

            return(_convertToInterval(context, limited));
        }
        static void ClampIntervalTestHelper <T, TSize>(
            T startA, bool startAIncluded, TSize additionA, bool endAIncluded,
            T startB, bool startBIncluded, TSize additionB, bool endBIncluded,
            Interval <T, TSize> clamped)
            where T : IComparable <T>
            where TSize : IComparable <TSize>
        {
            // Fully included intervals.
            Interval <T, TSize>  intervalA = NewInterval(startA, additionA, startAIncluded, endAIncluded);
            Interval <T, TSize>  intervalB = NewInterval(startB, additionB, startBIncluded, endBIncluded);
            IInterval <T, TSize> result    = intervalA.Clamp(intervalB);

            Assert.Equal(clamped, result);
        }
        static void ClampValueTestHelper <T, TSize>(T start, TSize addition, T actual, T clamped)
            where T : IComparable <T>
            where TSize : IComparable <TSize>
        {
            T end = AddSize(start, addition);

            // Fully included interval.
            var interval = new Interval <T, TSize>(start, end);

            Assert.Equal(clamped, interval.Clamp(actual));

            // Reversed intervals.
            var reversed = new Interval <T, TSize>(end, start);

            Assert.Equal(clamped, reversed.Clamp(actual));

            // Excluded borders.
            // TODO: For now it does not matter whether start or end is included. Do we want to take this into account?
            var excludedInterval = new Interval <T, TSize>(start, false, end, false);

            Assert.Equal(clamped, excludedInterval.Clamp(actual));
        }