Circle2D work
This commit is contained in:
parent
d03c3039f7
commit
f5c3a529cd
@ -13,11 +13,31 @@ public class Circle2D {
|
|||||||
private double x;
|
private double x;
|
||||||
private double y;
|
private double y;
|
||||||
private double radius;
|
private double radius;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the value of name
|
||||||
|
*
|
||||||
|
* @return the value of name
|
||||||
|
*/
|
||||||
|
public String getName()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
public Circle2D()
|
public Circle2D()
|
||||||
{
|
{
|
||||||
this.x = this.y = 0;
|
this.x = this.y = 0;
|
||||||
this.radius = 1;
|
this.radius = 1;
|
||||||
|
this.name = "Default Circle";
|
||||||
|
}
|
||||||
|
|
||||||
|
public Circle2D(double x, double y, double radius, String name)
|
||||||
|
{
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.radius = radius;
|
||||||
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Circle2D(double x, double y, double radius)
|
public Circle2D(double x, double y, double radius)
|
||||||
@ -70,7 +90,7 @@ public class Circle2D {
|
|||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
return "Circle2D{" + "x=" + x + ", y=" + y + ", radius=" + radius + '}';
|
return name + "{" + "x=" + x + ", y=" + y + ", radius=" + radius + " }";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -153,9 +173,30 @@ public class Circle2D {
|
|||||||
* @param array the array to be used for sorting, not altered.
|
* @param array the array to be used for sorting, not altered.
|
||||||
* @return a new sorted array;
|
* @return a new sorted array;
|
||||||
*/
|
*/
|
||||||
public static Circle2D[] sortCirclesByPerimeter(Circle2D[] array)
|
public static Circle2D[] sortCirclesByPerimeter(Circle2D[] oldArray)
|
||||||
{
|
{
|
||||||
return null;
|
Circle2D[] array = new Circle2D[oldArray.length];
|
||||||
|
System.arraycopy(oldArray, 0, array, 0, oldArray.length);
|
||||||
|
for (int i = 0; i < array.length - 1; ++i) {
|
||||||
|
for (int j = i + 1; j < array.length; ++j) {
|
||||||
|
double perimeter1 = array[i].getPerimeter();
|
||||||
|
double perimeter2 = array[j].getPerimeter();
|
||||||
|
if (perimeter1 > perimeter2) {
|
||||||
|
Circle2D temp = array[i];
|
||||||
|
array[i] = array[j];
|
||||||
|
array[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void printPerimeter(Circle2D[] array)
|
||||||
|
{
|
||||||
|
for (Circle2D circle : array) {
|
||||||
|
System.out.println(circle.name + " " + circle.getPerimeter());
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -164,34 +205,77 @@ public class Circle2D {
|
|||||||
* @param array The array used for sorting.
|
* @param array The array used for sorting.
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Circle2D[] sortCirclesByNumberOfTimesOverlapping(Circle2D[] array)
|
public static Circle2D[] sortCirclesByNumberOfTimesOverlapping(Circle2D[] oldArray)
|
||||||
{
|
{
|
||||||
return null;
|
Circle2D[] array = new Circle2D[oldArray.length];
|
||||||
|
|
||||||
|
int idealCount = 0;
|
||||||
|
int actualCount = 0;
|
||||||
|
int arrayIndex = 0;
|
||||||
|
for (int i = 0; i < oldArray.length; ++i) {
|
||||||
|
actualCount = 0;
|
||||||
|
for (int j = 0; j < oldArray.length; ++j) {
|
||||||
|
boolean doesOverlap = oldArray[i].overlaps(oldArray[j]);
|
||||||
|
if (doesOverlap) {
|
||||||
|
++actualCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (actualCount == idealCount) {
|
||||||
|
array[arrayIndex] = oldArray[i];
|
||||||
|
System.out.println(oldArray[i]);
|
||||||
|
arrayIndex++;
|
||||||
|
}
|
||||||
|
idealCount++;
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(Circle2D[] array)
|
||||||
|
{
|
||||||
|
for (Circle2D circle : array) {
|
||||||
|
System.out.println(circle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void print(Circle2D[] array, Circle2D c1)
|
||||||
|
{
|
||||||
|
for (Circle2D circle : array) {
|
||||||
|
System.out.println(circle);
|
||||||
|
System.out.println("c1.contains: " + c1.contains(circle));
|
||||||
|
System.out.println("c1.overlaps: " + c1.overlaps(circle));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Circle2D[] createArrayOfCircle()
|
||||||
|
{
|
||||||
|
Circle2D[] circleArray = new Circle2D[8];
|
||||||
|
circleArray[0] = new Circle2D(0, 0, 1, "c1");
|
||||||
|
circleArray[1] = new Circle2D(1, 1, 2, "c2");
|
||||||
|
circleArray[2] = new Circle2D(3.2, -2.2, 1.2, "c3");
|
||||||
|
circleArray[3] = new Circle2D(4.2, 0, 4, "c4");
|
||||||
|
circleArray[4] = new Circle2D(-7, 5, 4.5, "c5");
|
||||||
|
circleArray[5] = new Circle2D(19, 1, 10, "c6");
|
||||||
|
circleArray[6] = new Circle2D(1, 8.8, 3, "c7");
|
||||||
|
circleArray[7] = new Circle2D(4.1, 9.1, 1.3, "c8");
|
||||||
|
return circleArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
{
|
{
|
||||||
Circle2D c1 = new Circle2D(2, 2, 5.5);
|
Circle2D c1 = new Circle2D(2, 2, 5.5);
|
||||||
System.out.println(c1);
|
//System.out.println(c1);
|
||||||
System.out.println("Area is " + c1.getArea());
|
//System.out.println("Area is " + c1.getArea());
|
||||||
System.out.println("Perimeter is " + c1.getPerimeter());
|
//System.out.println("Perimeter is " + c1.getPerimeter());
|
||||||
//System.out.println(c1.contains(3, 3));
|
Circle2D[] circleArray = createArrayOfCircle();
|
||||||
//System.out.println(c1.contains(new Circle2D(4, 5, 10.5)));
|
System.out.println();
|
||||||
//System.out.println(c1.overlaps(new Circle2D(3, 5, 2.3)));
|
printPerimeter(circleArray);
|
||||||
//System.out.println(c1.overlaps(new Circle2D(-8.7, 5, 2.3)));
|
Circle2D[] sortedArray = sortCirclesByPerimeter(circleArray);
|
||||||
//System.out.println(c1.overlaps(new Circle2D(-8.7, 5, 2.3)));
|
printPerimeter(sortedArray);
|
||||||
Circle2D c2 = new Circle2D(-1.9, 1.8, 1.8);
|
print(circleArray);
|
||||||
Circle2D c3 = new Circle2D(-1.9, 1.8, 0.8);
|
Circle2D[] overlapArray = Circle2D.sortCirclesByNumberOfTimesOverlapping(circleArray);
|
||||||
Circle2D c4 = new Circle2D(-6.2, 1.8, 1.8);
|
print(overlapArray);
|
||||||
System.out.println(c2);
|
|
||||||
System.out.println("c1.contains: " + c1.contains(c2));
|
|
||||||
System.out.println("c1.overlaps: " + c1.overlaps(c2));
|
|
||||||
System.out.println(c3);
|
|
||||||
System.out.println("c1.contains: " + c1.contains(c3));
|
|
||||||
System.out.println("c1.overlaps: " + c1.overlaps(c3));
|
|
||||||
System.out.println(c4);
|
|
||||||
System.out.println("c1.contains: " + c1.contains(c4));
|
|
||||||
System.out.println("c1.overlaps: " + c1.overlaps(c4));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user