001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.geometry.euclidean.oned;
018    
019    import org.apache.commons.math3.geometry.partitioning.Region.Location;
020    
021    
022    /** This class represents a 1D interval.
023     * @see IntervalsSet
024     * @version $Id: Interval.java 1422195 2012-12-15 06:45:18Z psteitz $
025     * @since 3.0
026     */
027    public class Interval {
028    
029        /** The lower bound of the interval. */
030        private final double lower;
031    
032        /** The upper bound of the interval. */
033        private final double upper;
034    
035        /** Simple constructor.
036         * @param lower lower bound of the interval
037         * @param upper upper bound of the interval
038         */
039        public Interval(final double lower, final double upper) {
040            this.lower = lower;
041            this.upper = upper;
042        }
043    
044        /** Get the lower bound of the interval.
045         * @return lower bound of the interval
046         * @since 3.1
047         */
048        public double getInf() {
049            return lower;
050        }
051    
052        /** Get the lower bound of the interval.
053         * @return lower bound of the interval
054         * @deprecated as of 3.1, replaced by {@link #getInf()}
055         */
056        @Deprecated
057        public double getLower() {
058            return getInf();
059        }
060    
061        /** Get the upper bound of the interval.
062         * @return upper bound of the interval
063         * @since 3.1
064         */
065        public double getSup() {
066            return upper;
067        }
068    
069        /** Get the upper bound of the interval.
070         * @return upper bound of the interval
071         * @deprecated as of 3.1, replaced by {@link #getSup()}
072         */
073        @Deprecated
074        public double getUpper() {
075            return getSup();
076        }
077    
078        /** Get the size of the interval.
079         * @return size of the interval
080         * @since 3.1
081         */
082        public double getSize() {
083            return upper - lower;
084        }
085    
086        /** Get the length of the interval.
087         * @return length of the interval
088         * @deprecated as of 3.1, replaced by {@link #getSize()}
089         */
090        @Deprecated
091        public double getLength() {
092            return getSize();
093        }
094    
095        /** Get the barycenter of the interval.
096         * @return barycenter of the interval
097         * @since 3.1
098         */
099        public double getBarycenter() {
100            return 0.5 * (lower + upper);
101        }
102    
103        /** Get the midpoint of the interval.
104         * @return midpoint of the interval
105         * @deprecated as of 3.1, replaced by {@link #getBarycenter()}
106         */
107        @Deprecated
108        public double getMidPoint() {
109            return getBarycenter();
110        }
111    
112        /** Check a point with respect to the interval.
113         * @param point point to check
114         * @param tolerance tolerance below which points are considered to
115         * belong to the boundary
116         * @return a code representing the point status: either {@link
117         * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
118         * @since 3.1
119         */
120        public Location checkPoint(final double point, final double tolerance) {
121            if (point < lower - tolerance || point > upper + tolerance) {
122                return Location.OUTSIDE;
123            } else if (point > lower + tolerance && point < upper - tolerance) {
124                return Location.INSIDE;
125            } else {
126                return Location.BOUNDARY;
127            }
128        }
129    
130    }