JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
Vec2i.java
Go to the documentation of this file.
1/**
2 * Copyright 2022-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.math;
30
31/**
32 * 2D Vector based upon two integer components.
33 */
34public final class Vec2i {
35 private int x;
36 private int y;
37
38 public Vec2i() {}
39
40 public Vec2i(final Vec2i o) {
41 set(o);
42 }
43
44 public Vec2i copy() {
45 return new Vec2i(this);
46 }
47
48 public Vec2i(final int[/*2*/] xy) {
49 set(xy);
50 }
51
52 public Vec2i(final int x, final int y) {
53 set(x, y);
54 }
55
56 /** this = o, returns this. */
57 public void set(final Vec2i o) {
58 this.x = o.x;
59 this.y = o.y;
60 }
61
62 /** this = { x, y }, returns this. */
63 public void set(final int x, final int y) {
64 this.x = x;
65 this.y = y;
66 }
67
68 /** this = xy, returns this. */
69 public Vec2i set(final int[/*2*/] xy) {
70 this.x = xy[0];
71 this.y = xy[1];
72 return this;
73 }
74
75 /** xy[0..1] = this.{x, y}, returns this. */
76 public Vec2i toArray(final int[/*2*/] xy) {
77 xy[0] = this.x;
78 xy[1] = this.y;
79 return this;
80 }
81
82 /** xy = this, returns xy. */
83 public int[] get(final int[/*2*/] xy) {
84 xy[0] = this.x;
85 xy[1] = this.y;
86 return xy;
87 }
88
89 public int x() { return x; }
90 public int y() { return y; }
91
92 public void setX(final int x) { this.x = x; }
93 public void setY(final int y) { this.y = y; }
94
95 /** Return true if all components are zero. */
96 public boolean isZero() {
97 return 0 == x && 0 == y;
98 }
99
100 /**
101 * Return the length of this vector, a.k.a the <i>norm</i> or <i>magnitude</i>
102 */
103 public int length() {
104 return (int) Math.sqrt(lengthSq());
105 }
106
107 /**
108 * Return the squared length of this vector, a.k.a the squared <i>norm</i> or squared <i>magnitude</i>
109 */
110 public int lengthSq() {
111 return x*x + y*y;
112 }
113
114 /**
115 * Return the squared distance between this vector and the given one.
116 * <p>
117 * When comparing the relative distance between two points it is usually sufficient to compare the squared
118 * distances, thus avoiding an expensive square root operation.
119 * </p>
120 */
121 public int distSq(final Vec2i o) {
122 final int dx = x - o.x;
123 final int dy = y - o.y;
124 return dx*dx + dy*dy;
125 }
126
127 /**
128 * Return the distance between this vector and the given one.
129 */
130 public int dist(final Vec2i o) {
131 return (int)Math.sqrt(distSq(o));
132 }
133
134 /**
135 * Equals check.
136 * @param o comparison value
137 * @return true if all components are equal
138 */
139 public boolean isEqual(final Vec2i o) {
140 if( this == o ) {
141 return true;
142 } else {
143 return x == o.x && y == o.y;
144 }
145 }
146
147 @Override
148 public boolean equals(final Object o) {
149 if( o instanceof Vec2i ) {
150 return isEqual((Vec2i)o);
151 } else {
152 return false;
153 }
154 }
155
156 @Override
157 public String toString() {
158 return x + " / " + y;
159 }
160}
2D Vector based upon two integer components.
Definition: Vec2i.java:34
boolean equals(final Object o)
Definition: Vec2i.java:148
boolean isEqual(final Vec2i o)
Equals check.
Definition: Vec2i.java:139
void setX(final int x)
Definition: Vec2i.java:92
boolean isZero()
Return true if all components are zero.
Definition: Vec2i.java:96
Vec2i(final int x, final int y)
Definition: Vec2i.java:52
Vec2i(final int[] xy)
Definition: Vec2i.java:48
Vec2i(final Vec2i o)
Definition: Vec2i.java:40
int length()
Return the length of this vector, a.k.a the norm or magnitude
Definition: Vec2i.java:103
int dist(final Vec2i o)
Return the distance between this vector and the given one.
Definition: Vec2i.java:130
Vec2i toArray(final int[] xy)
xy[0..1] = this.
Definition: Vec2i.java:76
int distSq(final Vec2i o)
Return the squared distance between this vector and the given one.
Definition: Vec2i.java:121
int lengthSq()
Return the squared length of this vector, a.k.a the squared norm or squared magnitude
Definition: Vec2i.java:110
void setY(final int y)
Definition: Vec2i.java:93