Class Interval

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.index.bintree;
14  
15 /**
16  * Represents an (1-dimensional) closed interval on the Real number line.
17  *
18  * @version 1.7
19  */
20 public class Interval {
21  
22   public double minmax;
23  
24   public Interval()
25   {
26     min = 0.0;
27     max = 0.0;
28   }
29  
30   public Interval(double min, double max)
31   {
32     init(min, max);
33   }
34   public Interval(Interval interval)
35   {
36     init(interval.min, interval.max);
37   }
38   public void init(double min, double max)
39   {
40     this.min = min;
41     this.max = max;
42     if (min > max) {
43       this.min = max;
44       this.max = min;
45     }
46   }
47   public double getMin() { return min; }
48   public double getMax() { return max; }
49   public double getWidth() { return max - min; }
50  
51   public void expandToInclude(Interval interval)
52   {
53     if (interval.max > max) max = interval.max;
54     if (interval.min < min) min = interval.min;
55   }
56   public boolean overlaps(Interval interval)
57   {
58     return overlaps(interval.min, interval.max);
59   }
60  
61   public boolean overlaps(double min, double max)
62   {
63     if (this.min > max || this.max < min) return false;
64     return true;
65   }
66  
67   public boolean contains(Interval interval)
68   {
69     return contains(interval.min, interval.max);
70   }
71   public boolean contains(double min, double max)
72   {
73     return (min >= this.min && max <= this.max);
74   }
75   public boolean contains(double p)
76   {
77     return (p >= this.min && p <= this.max);
78   }
79  
80   public String toString()
81   {
82     return "[" + min + ", " + max + "]";
83   }
84 }
85