From cfbbc2ecda64b9d7a3a0d9c778ed20deba3a40e3 Mon Sep 17 00:00:00 2001 From: Rafael-Dev-21 <57507185+Rafael-Dev-21@users.noreply.github.com> Date: Tue, 13 Oct 2020 23:23:58 -0300 Subject: [PATCH] Add files via upload --- .../rafael/math/geometry/plane/Circle.java | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/main/java/br/com/rafael/math/geometry/plane/Circle.java diff --git a/src/main/java/br/com/rafael/math/geometry/plane/Circle.java b/src/main/java/br/com/rafael/math/geometry/plane/Circle.java new file mode 100644 index 0000000..2a76d91 --- /dev/null +++ b/src/main/java/br/com/rafael/math/geometry/plane/Circle.java @@ -0,0 +1,147 @@ +package br.com.rafael.math.geometry.plane; +/** + * Copyright© 2020 Victor Rafael Pereira Alves This file is part of MathUtils. + * + *

MathUtils is free software: you can redistribute it and/or modify it under the terms of the + * Lesser GNU General Public License as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + *

MathUtils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser + * GNU General Public License for more details. + * + *

You should have received a copy of the Lesser GNU General Public License along with MathUtils. + * If not, see <https://www.gnu.org/licenses/>. + */ +import br.com.rafael.math.geometry.plane.Circle; +import br.com.rafael.math.geometry.plane.Point2D; + +import java.io.Serializable; + +public class Circle implements Cloneable, Comparable, Serializable { + + private static final long serialVersionUID = 0l; + + private Point2D _point; + private double _radius; + + public Circle() { + _point = new Point2D(); + _radius = 0; + } + + public Circle(double radius) { + this(0, 0, radius); + } + + public Circle(double x, double y) { + this(x, y, 0); + } + + public Circle(double x, double y, double radius) { + this(new Point2D(x, y), radius); + } + + public Circle(Point2D point) { + this(point, 0); + } + + public Circle(Point2D point, double radius) { + _point = point; + _radius = radius; + } + + public void setX(double x) { + _point.setX(x); + } + + public void setY(double y) { + _point.setY(y); + } + + public double getX() { + return _point.getX(); + } + + public double getY() { + return _point.getY(); + } + + public void setPosition(float x, float y) { + _point = new Point2D(x, y); + } + + public Point2D getPosition() { + return _point.clone(); + } + + public void setRadius(double r) { + _radius = r; + } + + public double getRadius() { + return _radius; + } + + public double getArea() { + return _radius * _radius * Math.PI; + } + + public double getPerimeter() { + return _radius * Math.PI * 2; + } + + public boolean isCollide(Circle other) { + return _point.distance(other._point) < _radius + other._radius; + } + + public boolean isCollide(Point2D point) { + return _point.distance(point) < _radius; + } + + @Override + public Circle clone() { + try { + Circle clone = (Circle) super.clone(); + clone._point = _point.clone(); + + return clone; + } catch (CloneNotSupportedException e) { + throw new AssertionError(); + } + } + + @Override + public int compareTo(Circle other) { + return (int) (_radius - other._radius); + } + + @Override + public boolean equals(Object other) { + if (this == other) return true; + if (!(other instanceof Circle)) return false; + Circle circle = (Circle) other; + return this._radius == circle._radius && this._point.equals(circle._point); + } + + @Override + public int hashCode() { + int result = 17; + + result = result * 31 + _point.hashCode(); + result = result * 31 + hashDouble(_radius); + + return result; + } + + public int hashDouble(double d) { + long longBits = Double.doubleToLongBits(d); + + return (int) (longBits ^ (longBits >>> 32)); + } + + @Override + public String toString() { + return "{point: " + _point + ", radius: " + _radius + "}"; + } +}