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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.geometry.euclidean.oned;
23
24 import org.hipparchus.geometry.partitioning.Region.Location;
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27
28
29 /** This class represents a 1D interval.
30 * @see IntervalsSet
31 */
32 public class Interval {
33
34 /** The lower bound of the interval. */
35 private final double lower;
36
37 /** The upper bound of the interval. */
38 private final double upper;
39
40 /** Simple constructor.
41 * @param lower lower bound of the interval
42 * @param upper upper bound of the interval
43 */
44 public Interval(final double lower, final double upper) {
45 if (upper < lower) {
46 throw new MathIllegalArgumentException(LocalizedCoreFormats.ENDPOINTS_NOT_AN_INTERVAL,
47 upper, lower, true);
48 }
49 this.lower = lower;
50 this.upper = upper;
51 }
52
53 /** Get the lower bound of the interval.
54 * @return lower bound of the interval
55 */
56 public double getInf() {
57 return lower;
58 }
59
60 /** Get the upper bound of the interval.
61 * @return upper bound of the interval
62 */
63 public double getSup() {
64 return upper;
65 }
66
67 /** Get the size of the interval.
68 * @return size of the interval
69 */
70 public double getSize() {
71 return upper - lower;
72 }
73
74 /** Get the barycenter of the interval.
75 * @return barycenter of the interval
76 */
77 public double getBarycenter() {
78 return 0.5 * (lower + upper);
79 }
80
81 /** Check a point with respect to the interval.
82 * @param point point to check
83 * @param tolerance tolerance below which points are considered to
84 * belong to the boundary
85 * @return a code representing the point status: either {@link
86 * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY}
87 */
88 public Location checkPoint(final double point, final double tolerance) {
89 if (point < lower - tolerance || point > upper + tolerance) {
90 return Location.OUTSIDE;
91 } else if (point > lower + tolerance && point < upper - tolerance) {
92 return Location.INSIDE;
93 } else {
94 return Location.BOUNDARY;
95 }
96 }
97
98 }