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 */ 017package org.apache.commons.math3.geometry.euclidean.oned; 018 019import org.apache.commons.math3.geometry.partitioning.Region.Location; 020 021 022/** This class represents a 1D interval. 023 * @see IntervalsSet 024 * @since 3.0 025 */ 026public class Interval { 027 028 /** The lower bound of the interval. */ 029 private final double lower; 030 031 /** The upper bound of the interval. */ 032 private final double upper; 033 034 /** Simple constructor. 035 * @param lower lower bound of the interval 036 * @param upper upper bound of the interval 037 */ 038 public Interval(final double lower, final double upper) { 039 this.lower = lower; 040 this.upper = upper; 041 } 042 043 /** Get the lower bound of the interval. 044 * @return lower bound of the interval 045 * @since 3.1 046 */ 047 public double getInf() { 048 return lower; 049 } 050 051 /** Get the lower bound of the interval. 052 * @return lower bound of the interval 053 * @deprecated as of 3.1, replaced by {@link #getInf()} 054 */ 055 @Deprecated 056 public double getLower() { 057 return getInf(); 058 } 059 060 /** Get the upper bound of the interval. 061 * @return upper bound of the interval 062 * @since 3.1 063 */ 064 public double getSup() { 065 return upper; 066 } 067 068 /** Get the upper bound of the interval. 069 * @return upper bound of the interval 070 * @deprecated as of 3.1, replaced by {@link #getSup()} 071 */ 072 @Deprecated 073 public double getUpper() { 074 return getSup(); 075 } 076 077 /** Get the size of the interval. 078 * @return size of the interval 079 * @since 3.1 080 */ 081 public double getSize() { 082 return upper - lower; 083 } 084 085 /** Get the length of the interval. 086 * @return length of the interval 087 * @deprecated as of 3.1, replaced by {@link #getSize()} 088 */ 089 @Deprecated 090 public double getLength() { 091 return getSize(); 092 } 093 094 /** Get the barycenter of the interval. 095 * @return barycenter of the interval 096 * @since 3.1 097 */ 098 public double getBarycenter() { 099 return 0.5 * (lower + upper); 100 } 101 102 /** Get the midpoint of the interval. 103 * @return midpoint of the interval 104 * @deprecated as of 3.1, replaced by {@link #getBarycenter()} 105 */ 106 @Deprecated 107 public double getMidPoint() { 108 return getBarycenter(); 109 } 110 111 /** Check a point with respect to the interval. 112 * @param point point to check 113 * @param tolerance tolerance below which points are considered to 114 * belong to the boundary 115 * @return a code representing the point status: either {@link 116 * Location#INSIDE}, {@link Location#OUTSIDE} or {@link Location#BOUNDARY} 117 * @since 3.1 118 */ 119 public Location checkPoint(final double point, final double tolerance) { 120 if (point < lower - tolerance || point > upper + tolerance) { 121 return Location.OUTSIDE; 122 } else if (point > lower + tolerance && point < upper - tolerance) { 123 return Location.INSIDE; 124 } else { 125 return Location.BOUNDARY; 126 } 127 } 128 129}