Record Class Vec2
java.lang.Object
java.lang.Record
cit244.api.geom.Vec2
- Record Components:
x- horizontal componenty- vertical component
An immutable two-dimensional vector, also usable as a position.
Coordinates use the same units as the values supplied to them: typically pixels for positions and pixels per second for velocities. Operations return new vectors; they never modify this one. Components must be finite.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds another vector component-wise.doubledistanceTo(Vec2 other) Returns the Euclidean distance to another position.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.doublelength()Returns the vector's Euclidean length (magnitude).static Vec2Blends between two vectors.scaled(double factor) Multiplies both components by a scalar.Subtracts another vector component-wise.final StringtoString()Returns a string representation of this record class.translated(double dx, double dy) Returns this vector displaced by the given horizontal and vertical amounts.doublex()Returns the value of thexrecord component.doubley()Returns the value of theyrecord component.
-
Field Details
-
ZERO
The zero vector, with both components equal to zero.
-
-
Constructor Details
-
Vec2
public Vec2(double x, double y) Constructs a vector from its two components.- Throws:
IllegalArgumentException- if either component is NaN or infinite
-
-
Method Details
-
add
-
subtract
-
scaled
Multiplies both components by a scalar.- Parameters:
factor- scale factor- Returns:
- the scaled vector
-
translated
Returns this vector displaced by the given horizontal and vertical amounts. Equivalent to adding (dx, dy).- Parameters:
dx- horizontal displacementdy- vertical displacement- Returns:
- the translated vector
-
length
public double length()Returns the vector's Euclidean length (magnitude).- Returns:
- nonnegative length
-
distanceTo
Returns the Euclidean distance to another position.- Parameters:
other- destination position- Returns:
- nonnegative distance
-
lerp
Blends between two vectors.t = 0givesa,t = 1givesb, andt = 0.5gives the midpoint. Values outside [0, 1] continue along the same line, likeMathf.lerp(double, double, double).To ease the motion, shape
tfirst:Vec2.lerp(start, end, Curve.SMOOTHSTEP.apply(t)).- Parameters:
a- starting vectorb- ending vectort- interpolation factor- Returns:
- interpolated vector
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with thecomparemethod from their corresponding wrapper classes. -
x
-
y
-