/* CASA Framework for ActionScript 3.0 Copyright (c) 2008, Contributors of CASA Framework All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the CASA Framework nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.casaframework.math { import org.casaframework.math.Percent; /** Creates a standardized way of describing and storing an extent of variation/a value range. @author Aaron Clinger @author Mike Creighton @version 02/26/08 */ public class Range { protected var _endValue:Number; protected var _startValue:Number; /** Creates and defines a Range object. @param startValue: Beginning value of the range. @param endValue: Ending value of the range. @usageNote You are not required to define the range in the contructor you can do it at any point by calling {@link #setRange}. */ public function Range(startValue:Number, endValue:Number) { super(); this.setRange(startValue, endValue); } /** Defines or redefines range. @param startValue: Beginning value of the range. @param endValue: Ending value of the range. */ public function setRange(startValue:Number, endValue:Number):void { this.startValue = startValue; this.endValue = endValue; } /** The start value of the range. */ public function get startValue():Number { return this._startValue; } public function set startValue(value:Number):void { this._startValue = value; } /** The end value of the range. */ public function get endValue():Number { return this._endValue; } public function set endValue(value:Number):void { this._endValue = value; } /** The minimum or smallest value of the range. */ public function get minValue():Number { return Math.min(this.startValue, this.endValue); } /** Tthe maximum or largest value of the range. */ public function get maxValue():Number { return Math.max(this.startValue, this.endValue); } /** Determines if value is included in the range including the range's start and end values. @return Returns {@code true} if value was included in range; otherwise {@code false}. */ public function isWithinRange(value:Number):Boolean { return !(value > this.maxValue || value < this.minValue); } /** Calculates the percent of the range. @param percent: A {@link Percent} object. @return The value the percent represent of the range. */ public function getValueOfPercent(percent:Percent):Number { var min:Number; var max:Number; var val:Number; var per:Percent = percent.clone(); if (this.startValue <= this.endValue) { min = this.startValue; max = this.endValue; } else { per.decimalPercentage = 1 - per.decimalPercentage; min = this.endValue; max = this.startValue; } val = Math.abs(max - min) * per.decimalPercentage + min; return val; } /** Returns the percentage the value represents out of the range. @param value: An integer. @return A Percent object. */ public function getPercentOfValue(value:Number):Percent { var min:Number; var max:Number; var per:Percent; if (this.startValue <= this.endValue) { min = this.startValue; max = this.endValue; per = new Percent((value - min) / (max - min)); } else { min = this.endValue; max = this.startValue; per = new Percent(1 - (value - min) / (max - min)); } return per; } /** Determines if the range specified by the {@code range} parameter is equal to this range object. @param percent: A Range object. @return Returns {@code true} if ranges are identical; otherwise {@code false}. */ public function equals(range:Range):Boolean { return this.startValue == range.startValue && this.endValue == range.endValue; } /** Determines if this range and the range specified by the {@code range} parameter overlap. @param A Range object. @return Returns {@code true} if this range contains any value of the range specified; otherwise {@code false}. */ public function overlaps(range:Range):Boolean { if (this.equals(range) || this.contains(range) || range.contains(this) || this.isWithinRange(range.startValue) || this.isWithinRange(range.endValue)) return true; return false; } /** Determines if this range contains the range specified by the {@code range} parameter. @param A Range object. @return Returns {@code true} if this range contains all values of the range specified; otherwise {@code false}. */ public function contains(range:Range):Boolean { return this.startValue <= range.startValue && this.endValue >= range.endValue; } /** @return A new range object with the same values as this range. */ public function clone():Range { return new Range(this.startValue, this.endValue); } } }