org.joml.Circlef Maven / Gradle / Ivy
Show all versions of joml Show documentation
/*
* The MIT License
*
* Copyright (c) 2017-2020 JOML
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.joml;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* Represents a 2D circle using single-precision floating-point numbers.
*
* @author Kai Burjack
*/
public class Circlef implements Externalizable {
/**
* The x coordiante of the circle's center.
*/
public float x;
/**
* The y coordiante of the circle's center.
*/
public float y;
/**
* The radius of the circle.
*/
public float r;
/**
* Create a new {@link Circlef} with center position (0, 0, 0)
and radius 0
.
*/
public Circlef() {
}
/**
* Create a new {@link Circlef} as a copy of the given source
.
*
* @param source
* the {@link Circlef} to copy from
*/
public Circlef(Circlef source) {
this.x = source.x;
this.y = source.y;
this.r = source.r;
}
/**
* Create a new {@link Circlef} with center position (x, y)
and radius r
.
*
* @param x
* the x coordinate of the circle's center
* @param y
* the y coordinate of the circle's center
* @param r
* the radius of the circle
*/
public Circlef(float x, float y, float r) {
this.x = x;
this.y = y;
this.r = r;
}
/**
* Translate this
by the given vector xy
.
*
* @param xy
* the vector to translate by
* @return this
*/
public Circlef translate(Vector2fc xy) {
return translate(xy.x(), xy.y(), this);
}
/**
* Translate this
by the given vector xy
and store the result in dest
.
*
* @param xy
* the vector to translate by
* @param dest
* will hold the result
* @return dest
*/
public Circlef translate(Vector2fc xy, Circlef dest) {
return translate(xy.x(), xy.y(), dest);
}
/**
* Translate this
by the vector (x, y)
.
*
* @param x
* the x coordinate to translate by
* @param y
* the y coordinate to translate by
* @return this
*/
public Circlef translate(float x, float y) {
return translate(x, y, this);
}
/**
* Translate this
by the vector (x, y)
and store the result in dest
.
*
* @param x
* the x coordinate to translate by
* @param y
* the y coordinate to translate by
* @param dest
* will hold the result
* @return dest
*/
public Circlef translate(float x, float y, Circlef dest) {
dest.x = this.x + x;
dest.y = this.y + y;
return dest;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Float.floatToIntBits(r);
result = prime * result + Float.floatToIntBits(x);
result = prime * result + Float.floatToIntBits(y);
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Circlef other = (Circlef) obj;
if (Float.floatToIntBits(r) != Float.floatToIntBits(other.r))
return false;
if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x))
return false;
if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y))
return false;
return true;
}
/**
* Return a string representation of this circle.
*
* This method creates a new {@link DecimalFormat} on every invocation with the format string "0.000E0;-
".
*
* @return the string representation
*/
public String toString() {
return Runtime.formatNumbers(toString(Options.NUMBER_FORMAT));
}
/**
* Return a string representation of this circle by formatting the vector components with the given {@link NumberFormat}.
*
* @param formatter
* the {@link NumberFormat} used to format the components with
* @return the string representation
*/
public String toString(NumberFormat formatter) {
return "(" + formatter.format(x) + " " + formatter.format(y) + " " + formatter.format(r) + ")";
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeFloat(x);
out.writeFloat(y);
out.writeFloat(r);
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
x = in.readFloat();
y = in.readFloat();
r = in.readFloat();
}
}