Interval.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.geometry.euclidean.oned;

  22. import org.hipparchus.geometry.partitioning.Region.Location;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;


  25. /** This class represents a 1D interval.
  26.  * @see IntervalsSet
  27.  */
  28. public class Interval {

  29.     /** The lower bound of the interval. */
  30.     private final double lower;

  31.     /** The upper bound of the interval. */
  32.     private final double upper;

  33.     /** Simple constructor.
  34.      * @param lower lower bound of the interval
  35.      * @param upper upper bound of the interval
  36.      */
  37.     public Interval(final double lower, final double upper) {
  38.         if (upper < lower) {
  39.             throw new MathIllegalArgumentException(LocalizedCoreFormats.ENDPOINTS_NOT_AN_INTERVAL,
  40.                                                 upper, lower, true);
  41.         }
  42.         this.lower = lower;
  43.         this.upper = upper;
  44.     }

  45.     /** Get the lower bound of the interval.
  46.      * @return lower bound of the interval
  47.      */
  48.     public double getInf() {
  49.         return lower;
  50.     }

  51.     /** Get the upper bound of the interval.
  52.      * @return upper bound of the interval
  53.      */
  54.     public double getSup() {
  55.         return upper;
  56.     }

  57.     /** Get the size of the interval.
  58.      * @return size of the interval
  59.      */
  60.     public double getSize() {
  61.         return upper - lower;
  62.     }

  63.     /** Get the barycenter of the interval.
  64.      * @return barycenter of the interval
  65.      */
  66.     public double getBarycenter() {
  67.         return 0.5 * (lower + upper);
  68.     }

  69.     /** Check a point with respect to the interval.
  70.      * @param point point to check
  71.      * @param tolerance tolerance below which points are considered to
  72.      * belong to the boundary
  73.      * @return a code representing the point status: either {@link
  74.      * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
  75.      */
  76.     public Location checkPoint(final double point, final double tolerance) {
  77.         if (point < lower - tolerance || point > upper + tolerance) {
  78.             return Location.OUTSIDE;
  79.         } else if (point > lower + tolerance && point < upper - tolerance) {
  80.             return Location.INSIDE;
  81.         } else {
  82.             return Location.BOUNDARY;
  83.         }
  84.     }

  85. }