master
Chloe Fontenot 🏳️‍⚧️ 2023-01-31 12:25:35 +07:00
parent 601b7ebc0c
commit 5fcdbf27fd
2 changed files with 196 additions and 0 deletions

@ -0,0 +1,196 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.calebfontenot.mp2_calebfontenot;
/**
*
* @author caleb
*/
public class Circle2D {
private double x;
private double y;
private double radius;
public Circle2D()
{
this.x = this.y = 0;
this.radius = 1;
}
public Circle2D(double x, double y, double radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
/**
* Get the value of x
*
* @return the value of x
*/
public double getX()
{
return x;
}
/**
* Get the value of y
*
* @return the value of y
*/
public double getY()
{
return y;
}
/**
* Get the value of radius
*
* @return the value of radius
*/
public double getRadius()
{
return radius;
}
public double getArea()
{
return Math.PI * this.radius * this.radius;
}
public double getPerimeter()
{
return 2 * Math.PI * this.radius;
}
@Override
public String toString()
{
return "Circle2D{" + "x=" + x + ", y=" + y + ", radius=" + radius + '}';
}
@Override
public int hashCode()
{
int hash = 5;
return hash;
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Circle2D other = (Circle2D) obj;
if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
return false;
}
if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
return false;
}
return Double.doubleToLongBits(this.radius) == Double.doubleToLongBits(other.radius);
}
/**
* Determines if a point is inside the circle.
*
* @param x
* @param y
* @return true if the specified point (x, y) is inside the circle, otherwise false.
*/
public boolean contains(double x, double y)
{
double d = distance(x, y, this.x, this.y);
return d <= radius;
}
public boolean contains(Circle2D c)
{
double d = distance(c.x, c.y, this.x, this.y);
return d <= radius;
}
/**
* Determines if this circle overlaps with parameter circle.
*
* @param circle to compare for overlapping.
* @return true
*/
public boolean overlaps(Circle2D c)
{
// Compare this to the paremeter
double overlap = Math.sqrt((Math.pow((this.x - c.x), 2) + Math.pow((this.y + c.y), 2)));
if (overlap <= (this.radius - c.radius)) {
return true;
} else if (overlap <= (c.radius - this.radius)) {
return true;
} else if (overlap < (this.radius + c.radius)) {
return true;
} else {
return false;
}
}
/**
*
* @param x1 x of point 1
* @param y1 x of point 1
* @param x2 x of point 1
* @param y2 x of point 1
* @return the distance between the two points
*/
private static double distance(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x1 - x2) * (x1) * (x1 - x2)
+ (y1 - y2) * (y1 - y2));
}
/**
* Sort the array in ascending order by perimeter i=size;
*
* @param array the array to be used for sorting, not altered.
* @return a new sorted array;
*/
public static Circle2D[] sortCirclesByPerimeter(Circle2D[] array)
{
return null;
}
/**
* Sort the array in ascending order by perimeter size. Returns a new sorted array. You compare every circle with all other circles. If it overlaps with all others it should be placed first.
*
* @param array The array used for sorting.
* @return
*/
public static Circle2D[] sortCirclesByNumberOfTimesOverlapping(Circle2D[] array)
{
return null;
}
public static void main(String[] args)
{
Circle2D c1 = new Circle2D(2, 2, 5.5);
System.out.println("Area is " + c1.getArea());
System.out.println("Perimeter is " + c1.getPerimeter());
//System.out.println(c1.contains(3, 3));
//System.out.println(c1.contains(new Circle2D(4, 5, 10.5)));
//System.out.println(c1.overlaps(new Circle2D(3, 5, 2.3)));
//System.out.println(c1.overlaps(new Circle2D(-8.7, 5, 2.3)));
//System.out.println(c1.overlaps(new Circle2D(-8.7, 5, 2.3)));
System.out.println(c1.contains(new Circle2D(-1.9, 1.8, 2.3)));
System.out.println(c1.overlaps(new Circle2D(-1.9, 1.8, 2.3)));
}
}