コード例 #1
0
        public static IInterval <TPoint, TPointComparer> Intersect <TPoint, TPointComparer>(
            this IBoundaryInterval <TPoint, TPointComparer> interval,
            IBoundaryInterval <TPoint, TPointComparer> otherInterval,
            TPointComparer pointComparer)
            where TPoint : notnull
            where TPointComparer : IComparer <TPoint>, new()
        {
            var maxLowerBoundary = interval.LowerBoundary()
                                   .Compare(
                otherInterval.LowerBoundary(),
                pointComparer) >= 0
                ? interval.LowerBoundary()
                : otherInterval.LowerBoundary();

            var minLowerBoundary = interval.UpperBoundary()
                                   .Compare(
                otherInterval.UpperBoundary(),
                pointComparer) <= 0
                ? interval.UpperBoundary()
                : otherInterval.UpperBoundary();

            return(IntervalFactory.Build(
                       lowerBoundary: maxLowerBoundary,
                       upperBoundary: minLowerBoundary,
                       pointComparer: pointComparer));
        }
コード例 #2
0
        public static List <Interval <TPoint> > Exclude <TPoint>(
            this Interval <TPoint> interval,
            HashSet <TPoint> pointSet,
            IComparer <TPoint> comparer)
            where TPoint : notnull
        {
            var orderedExclusions = pointSet
                                    .OrderBy(
                key => key,
                comparer: comparer)
                                    .Where(
                p => interval.Contains(
                    point: p,
                    comparer: comparer));

            var lowerBound = interval.LowerBound;
            var upperBound = interval.UpperBound;

            var intervalList = new List <IInterval <TPoint> >();

            foreach (var exclude in orderedExclusions)
            {
                intervalList.Add(
                    item: IntervalFactory
                    .Build(
                        lowerBound: lowerBound,
                        upperBound: new OpenUpperBound <TPoint>(
                            point: exclude),
                        comparer: comparer));

                lowerBound = new OpenLowerBound <TPoint>(
                    point: exclude);
            }

            intervalList
            .Add(
                item: IntervalFactory
                .Build(
                    lowerBound: lowerBound,
                    upperBound: upperBound,
                    comparer: Comparer <TPoint> .Default));

            return(intervalList
                   .OfType <Interval <TPoint> >()
                   .ToList());
        }
コード例 #3
0
        public static IInterval <TPoint> Intersect <TPoint>(
            this Interval <TPoint> leftInterval,
            Interval <TPoint> rightInterval,
            IComparer <TPoint> comparer)
            where TPoint : notnull
        {
            var lowerBoundComparer = new LowerBoundComparer <TPoint>(comparer);
            var upperBoundComparer = new UpperBoundComparer <TPoint>(comparer);

            return(IntervalFactory.Build(
                       lowerBound: lowerBoundComparer.Compare(leftInterval.LowerBound, rightInterval.LowerBound) >= 0
                    ? leftInterval.LowerBound
                    : rightInterval.LowerBound,
                       upperBound: upperBoundComparer.Compare(leftInterval.UpperBound, rightInterval.UpperBound) <= 0
                    ? leftInterval.UpperBound
                    : rightInterval.UpperBound,
                       comparer: comparer));
        }
コード例 #4
0
        public void IntersectWithOpenIntervalIsOpenInterval(
            int lowerBoundaryPoint,
            int upperBoundaryPoint)
        {
            var leftInterval = IntervalFactory.Build(
                new LowerOpenBoundary <int, IntComparer>(lowerBoundaryPoint),
                new UpperOpenBoundary <int, IntComparer>(upperBoundaryPoint),
                pointComparer: new IntComparer());

            InfinityInterval <int, IntComparer> rightInterval = default;

            var intersection = leftInterval.Intersect(
                rightInterval,
                pointComparer: new IntComparer());

            Assert.Equal(
                expected: leftInterval,
                actual: intersection);
        }