JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Point.java
Go to the documentation of this file.
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
30package com.jogamp.nativewindow.util;
31
32public class Point implements Cloneable, PointImmutable {
33 int x;
34 int y;
35
36 public Point(final int x, final int y) {
37 this.x=x;
38 this.y=y;
39 }
40
41 public Point() {
42 this(0, 0);
43 }
44
45 @Override
46 public Object cloneMutable() {
47 return clone();
48 }
49
50 @Override
51 public Object clone() {
52 try {
53 return super.clone();
54 } catch (final CloneNotSupportedException ex) {
55 throw new InternalError();
56 }
57 }
58
59 @Override
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
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
83 public final int getX() {
84 return x;
85 }
86
87 @Override
88 public final int getY() {
89 return y;
90 }
91
92 @Override
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
101 public String toString() {
102 return x + " / " + y;
103 }
104
105 /**
106 * Set this instance's x- and y-component.
107 * @param x value for x-component
108 * @param y value for y-component
109 * @return this instance for scaling
110 */
111 public final Point set(final int x, final int y) { this.x = x; this.y = y; return this; }
112 /**
113 * Set this instance's x--component.
114 * @param x value for x-component
115 * @return this instance for scaling
116 */
117 public final Point setX(final int x) { this.x = x; return this; }
118 /**
119 * Set this instance's y-component.
120 * @param y value for y-component
121 * @return this instance for scaling
122 */
123 public final Point setY(final int y) { this.y = y; return this; }
124
125 /**
126 * Translate this instance's x- and y-components,
127 * i.e. add the values of the given delta point to them.
128 * @param pd delta point
129 * @return this instance for scaling
130 */
131 public final Point translate(final Point pd) {
132 x += pd.x ;
133 y += pd.y ;
134 return this;
135 }
136
137 /**
138 * Translate this instance's x- and y-components,
139 * i.e. add the values of the given delta point to them.
140 * @param pd delta point
141 * @return this instance for scaling
142 */
143 public final Point translate(final PointImmutable pd) {
144 x += pd.getX() ;
145 y += pd.getY() ;
146 return this;
147 }
148
149 /**
150 * Translate this instance's x- and y-components,
151 * i.e. add the given deltas to them.
152 * @param dx delta for x
153 * @param dy delta for y
154 * @return this instance for scaling
155 */
156 public final Point translate(final int dx, final int dy) {
157 x += dx ;
158 y += dy ;
159 return this;
160 }
161
162 /**
163 * Scale this instance's x- and y-components,
164 * i.e. multiply them by the given scale factors.
165 * @param sx scale factor for x
166 * @param sy scale factor for y
167 * @return this instance for scaling
168 */
169 public final Point scale(final int sx, final int sy) {
170 x *= sx ;
171 y *= sy ;
172 return this;
173 }
174
175 /**
176 * Scale this instance's x- and y-components,
177 * i.e. multiply them by the given scale factors.
178 * <p>
179 * The product is rounded back to integer.
180 * </p>
181 * @param sx scale factor for x
182 * @param sy scale factor for y
183 * @return this instance for scaling
184 */
185 public final Point scale(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 /**
192 * Inverse scale this instance's x- and y-components,
193 * i.e. divide them by the given scale factors.
194 * @param sx inverse scale factor for x
195 * @param sy inverse scale factor for y
196 * @return this instance for scaling
197 */
198 public final Point scaleInv(final int sx, final int sy) {
199 x /= sx ;
200 y /= sy ;
201 return this;
202 }
203 /**
204 * Inverse scale this instance's x- and y-components,
205 * i.e. divide them by the given scale factors.
206 * <p>
207 * The product is rounded back to integer.
208 * </p>
209 * @param sx inverse scale factor for x
210 * @param sy inverse scale factor for y
211 * @return this instance for scaling
212 */
213 public final Point scaleInv(final float sx, final float sy) {
214 x = (int)(x / sx + 0.5f);
215 y = (int)(y / sy + 0.5f);
216 return this;
217 }
218}
final Point translate(final PointImmutable pd)
Translate this instance's x- and y-components, i.e.
Definition: Point.java:143
final Point setX(final int x)
Set this instance's x–component.
Definition: Point.java:117
final Point translate(final Point pd)
Translate this instance's x- and y-components, i.e.
Definition: Point.java:131
final Point scale(final int sx, final int sy)
Scale this instance's x- and y-components, i.e.
Definition: Point.java:169
Point(final int x, final int y)
Definition: Point.java:36
final Point setY(final int y)
Set this instance's y-component.
Definition: Point.java:123
final Point translate(final int dx, final int dy)
Translate this instance's x- and y-components, i.e.
Definition: Point.java:156
final Point scaleInv(final float sx, final float sy)
Inverse scale this instance's x- and y-components, i.e.
Definition: Point.java:213
boolean equals(final Object obj)
Checks whether two points objects are equal.
Definition: Point.java:73
final Point scale(final float sx, final float sy)
Scale this instance's x- and y-components, i.e.
Definition: Point.java:185
int compareTo(final PointImmutable d)
Definition: Point.java:60
final Point scaleInv(final int sx, final int sy)
Inverse scale this instance's x- and y-components, i.e.
Definition: Point.java:198