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 * http://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 package org.apache.fontbox.util; 18 19 import java.awt.Point; 20 21 /** 22 * This is an implementation of a bounding box. This was originally written for the 23 * AMF parser. 24 * 25 * @author Ben Litchfield (ben@benlitchfield.com) 26 * @version $Revision: 1.1 $ 27 */ 28 public class BoundingBox 29 { 30 private float lowerLeftX; 31 private float lowerLeftY; 32 private float upperRightX; 33 private float upperRightY; 34 35 /** 36 * Getter for property lowerLeftX. 37 * 38 * @return Value of property lowerLeftX. 39 */ 40 public float getLowerLeftX() 41 { 42 return lowerLeftX; 43 } 44 45 /** 46 * Setter for property lowerLeftX. 47 * 48 * @param lowerLeftXValue New value of property lowerLeftX. 49 */ 50 public void setLowerLeftX(float lowerLeftXValue) 51 { 52 this.lowerLeftX = lowerLeftXValue; 53 } 54 55 /** 56 * Getter for property lowerLeftY. 57 * 58 * @return Value of property lowerLeftY. 59 */ 60 public float getLowerLeftY() 61 { 62 return lowerLeftY; 63 } 64 65 /** 66 * Setter for property lowerLeftY. 67 * 68 * @param lowerLeftYValue New value of property lowerLeftY. 69 */ 70 public void setLowerLeftY(float lowerLeftYValue) 71 { 72 this.lowerLeftY = lowerLeftYValue; 73 } 74 75 /** 76 * Getter for property upperRightX. 77 * 78 * @return Value of property upperRightX. 79 */ 80 public float getUpperRightX() 81 { 82 return upperRightX; 83 } 84 85 /** 86 * Setter for property upperRightX. 87 * 88 * @param upperRightXValue New value of property upperRightX. 89 */ 90 public void setUpperRightX(float upperRightXValue) 91 { 92 this.upperRightX = upperRightXValue; 93 } 94 95 /** 96 * Getter for property upperRightY. 97 * 98 * @return Value of property upperRightY. 99 */ 100 public float getUpperRightY() 101 { 102 return upperRightY; 103 } 104 105 /** 106 * Setter for property upperRightY. 107 * 108 * @param upperRightYValue New value of property upperRightY. 109 */ 110 public void setUpperRightY(float upperRightYValue) 111 { 112 this.upperRightY = upperRightYValue; 113 } 114 115 /** 116 * This will get the width of this rectangle as calculated by 117 * upperRightX - lowerLeftX. 118 * 119 * @return The width of this rectangle. 120 */ 121 public float getWidth() 122 { 123 return getUpperRightX() - getLowerLeftX(); 124 } 125 126 /** 127 * This will get the height of this rectangle as calculated by 128 * upperRightY - lowerLeftY. 129 * 130 * @return The height of this rectangle. 131 */ 132 public float getHeight() 133 { 134 return getUpperRightY() - getLowerLeftY(); 135 } 136 137 /** 138 * Checks if a point is inside this rectangle. 139 * 140 * @param x The x coordinate. 141 * @param y The y coordinate. 142 * 143 * @return true If the point is on the edge or inside the rectangle bounds. 144 */ 145 public boolean contains( float x, float y ) 146 { 147 return x >= lowerLeftX && x <= upperRightX && 148 y >= lowerLeftY && y <= upperRightY; 149 } 150 151 /** 152 * Checks if a point is inside this rectangle. 153 * 154 * @param point The point to check 155 * 156 * @return true If the point is on the edge or inside the rectangle bounds. 157 */ 158 public boolean contains( Point point ) 159 { 160 return contains( (float)point.getX(), (float)point.getY() ); 161 } 162 163 /** 164 * This will return a string representation of this rectangle. 165 * 166 * @return This object as a string. 167 */ 168 public String toString() 169 { 170 return "[" + getLowerLeftX() + "," + getLowerLeftY() + "," + 171 getUpperRightX() + "," + getUpperRightY() +"]"; 172 } 173 174 }