1 /** 2 * Copyright 2010 JogAmp Community. All rights reserved. 3 * Copyright (c) 2010 JogAmp Community. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, are 6 * permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED 16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 17 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * 25 * The views and conclusions contained in the software and documentation are those of the 26 * authors and should not be interpreted as representing official policies, either expressed 27 * or implied, of JogAmp Community. 28 */ 29 30 package com.jogamp.nativewindow.util; 31 32 public class Point implements Cloneable, PointImmutable { 33 int x; 34 int y; 35 Point(final int x, final int y)36 public Point(final int x, final int y) { 37 this.x=x; 38 this.y=y; 39 } 40 Point()41 public Point() { 42 this(0, 0); 43 } 44 45 @Override cloneMutable()46 public Object cloneMutable() { 47 return clone(); 48 } 49 50 @Override clone()51 public Object clone() { 52 try { 53 return super.clone(); 54 } catch (final CloneNotSupportedException ex) { 55 throw new InternalError(); 56 } 57 } 58 59 @Override compareTo(final PointImmutable d)60 public int compareTo(final PointImmutable d) { 61 final int sq = x*y; 62 final int xsq = d.getX()*d.getY(); 63 64 if(sq > xsq) { 65 return 1; 66 } else if(sq < xsq) { 67 return -1; 68 } 69 return 0; 70 } 71 72 @Override equals(final Object obj)73 public boolean equals(final Object obj) { 74 if(this == obj) { return true; } 75 if (obj instanceof Point) { 76 final Point p = (Point)obj; 77 return y == p.y && x == p.x; 78 } 79 return false; 80 } 81 82 @Override getX()83 public final int getX() { 84 return x; 85 } 86 87 @Override getY()88 public final int getY() { 89 return y; 90 } 91 92 @Override hashCode()93 public int hashCode() { 94 // 31 * x == (x << 5) - x 95 int hash = 31 + x; 96 hash = ((hash << 5) - hash) + y; 97 return hash; 98 } 99 100 @Override toString()101 public String toString() { 102 return x + " / " + y; 103 } 104 set(final int x, final int y)105 public final void set(final int x, final int y) { this.x = x; this.y = y; } setX(final int x)106 public final void setX(final int x) { this.x = x; } setY(final int y)107 public final void setY(final int y) { this.y = y; } 108 109 /** 110 * Translate this instance's x- and y-components, 111 * i.e. add the values of the given delta point to them. 112 * @param pd delta point 113 * @return this instance for scaling 114 */ translate(final Point pd)115 public final Point translate(final Point pd) { 116 x += pd.x ; 117 y += pd.y ; 118 return this; 119 } 120 121 /** 122 * Translate this instance's x- and y-components, 123 * i.e. add the given deltas to them. 124 * @param dx delta for x 125 * @param dy delta for y 126 * @return this instance for scaling 127 */ translate(final int dx, final int dy)128 public final Point translate(final int dx, final int dy) { 129 x += dx ; 130 y += dy ; 131 return this; 132 } 133 134 /** 135 * Scale this instance's x- and y-components, 136 * i.e. multiply them by the given scale factors. 137 * @param sx scale factor for x 138 * @param sy scale factor for y 139 * @return this instance for scaling 140 */ scale(final int sx, final int sy)141 public final Point scale(final int sx, final int sy) { 142 x *= sx ; 143 y *= sy ; 144 return this; 145 } 146 147 /** 148 * Scale this instance's x- and y-components, 149 * i.e. multiply them by the given scale factors. 150 * <p> 151 * The product is rounded back to integer. 152 * </p> 153 * @param sx scale factor for x 154 * @param sy scale factor for y 155 * @return this instance for scaling 156 */ scale(final float sx, final float sy)157 public final Point scale(final float sx, final float sy) { 158 x = (int)(x * sx + 0.5f); 159 y = (int)(y * sy + 0.5f); 160 return this; 161 } 162 163 /** 164 * Inverse scale this instance's x- and y-components, 165 * i.e. divide them by the given scale factors. 166 * @param sx inverse scale factor for x 167 * @param sy inverse scale factor for y 168 * @return this instance for scaling 169 */ scaleInv(final int sx, final int sy)170 public final Point scaleInv(final int sx, final int sy) { 171 x /= sx ; 172 y /= sy ; 173 return this; 174 } 175 /** 176 * Inverse scale this instance's x- and y-components, 177 * i.e. divide them by the given scale factors. 178 * <p> 179 * The product is rounded back to integer. 180 * </p> 181 * @param sx inverse scale factor for x 182 * @param sy inverse scale factor for y 183 * @return this instance for scaling 184 */ scaleInv(final float sx, final float sy)185 public final Point scaleInv(final float sx, final float sy) { 186 x = (int)(x / sx + 0.5f); 187 y = (int)(y / sy + 0.5f); 188 return this; 189 } 190 } 191