• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 AndroidPlot.com
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 
17 
18 package com.androidplot;
19 
20 /**
21  * A one dimensional region represented by a starting and ending value.
22  */
23 public class LineRegion {
24     private Number minVal;
25     private Number maxVal;
26 
LineRegion(Number val1, Number v2)27     public LineRegion(Number val1, Number v2) {
28         if (val1.doubleValue() < v2.doubleValue()) {
29             this.setMinVal(val1);
30             this.setMaxVal(v2);
31         } else {
32             this.setMinVal(v2);
33             this.setMaxVal(val1);
34         }
35     }
36 
measure(Number val1, Number val2)37     public static Number measure(Number val1, Number val2) {
38         return new LineRegion(val1, val2).length();
39     }
40 
length()41     public Number length() {
42         return maxVal.doubleValue() - minVal.doubleValue();
43     }
44 
45     /**
46      * Tests whether a value is within the given range
47      * @param value
48      * @return
49      */
contains(Number value)50     public boolean contains(Number value) {
51         return value.doubleValue() >= minVal.doubleValue() && value.doubleValue() <= maxVal.doubleValue();
52     }
53 
intersects(LineRegion lineRegion)54     public boolean intersects(LineRegion lineRegion) {
55         return intersects(lineRegion.getMinVal(), lineRegion.getMaxVal());
56     }
57 
58      /**
59      * Tests whether this segment intersects another
60      * @param line2Min
61      * @param line2Max
62      * @return
63      */
intersects(Number line2Min, Number line2Max)64     public  boolean intersects(Number line2Min, Number line2Max) {
65 
66         //double l1min = getMinVal() == null ? Double.NEGATIVE_INFINITY : getMinVal().doubleValue();
67         //double l1max = getMaxVal() == null ? Double.POSITIVE_INFINITY : getMaxVal().doubleValue();
68 
69         //double l2min = line2Min == null ? Double.NEGATIVE_INFINITY : line2Min.doubleValue();
70         //double l2max = line2Max == null ? Double.POSITIVE_INFINITY : line2Max.doubleValue();
71 
72 
73         // is this line completely within line2?
74         if(line2Min.doubleValue() <= this.minVal.doubleValue() && line2Max.doubleValue() >= this.maxVal.doubleValue()) {
75             return true;
76         // is line1 partially within line2
77         } else return contains(line2Min) || contains(line2Max);
78     }
79 
getMinVal()80     public Number getMinVal() {
81         return minVal;
82     }
83 
setMinVal(Number minVal)84     public void setMinVal(Number minVal) {
85         if(minVal == null) {
86             throw new NullPointerException("Region values can never be null.");
87         }
88         this.minVal = minVal;
89     }
90 
getMaxVal()91     public Number getMaxVal() {
92         return maxVal;
93     }
94 
setMaxVal(Number maxVal)95     public void setMaxVal(Number maxVal) {
96         if(maxVal == null) {
97             throw new NullPointerException("Region values can never be null.");
98         }
99         this.maxVal = maxVal;
100     }
101 }
102