Commit eeb07e2e authored by Georgii Aksenov's avatar Georgii Aksenov
Browse files

Iteration03

(cherry picked from commit 417eee7e)
parent d45ec99c
package cz.muni.fi.pb162.project.geometry;
/**
* This class represents a circle in 2D space.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Circle {
private final Vertex2D center;
private final double radius;
/**
* This is a constructor.
*
* @param center is the center point
* @param radius is the radius
*/
public Circle(Vertex2D center, double radius) {
this.center = center;
this.radius = radius;
}
/**
* This is a default constructor with default parameters.
*/
public Circle() {
this.center = new Vertex2D(0, 0);
this.radius = 1;
}
public Vertex2D getCenter() {
return center;
}
public double getRadius() {
return radius;
}
@Override
public String toString() {
return "Circle: center=" + center.toString() + ", radius=" + radius;
}
}
......@@ -9,13 +9,26 @@ import static java.util.stream.Collectors.joining;
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Triangle {
private static final double TOLERATED_DEVIATION = 0.001;
private final Vertex2D[] vertices;
private final Triangle[] subTriangles = new Triangle[3];
/**
* Checkstyle asks for the Javadoc here.
* This is a constructor that divides this triangle recursively.
*
* @param v1 is the first corner
* @param v2 is the second corner
* @param v3 is the third corner
* @param depth is the depth of recursive division.
*/
public Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3, int depth) {
this(v1, v2, v3);
divide(depth);
}
/**
* This is a constructor.
* It sets corners ¯\_(ツ)_/¯
*
* @param v1 is the first corner
* @param v2 is the second corner
......@@ -25,7 +38,6 @@ public class Triangle {
this.vertices = new Vertex2D[]{v1, v2, v3};
}
/**
* Returns the index-th vertex if the index is in range 0..2, otherwise null
*
......@@ -39,24 +51,41 @@ public class Triangle {
return vertices[index];
}
@Override
public String toString() {
return "Triangle: vertices=" + stream(vertices)
.map(Vertex2D::toString)
.collect(joining(" "));
}
/**
* @return true if is equilateral
*/
public boolean isEquilateral() {
double side1 = getVertex(0).distance(getVertex(1));
double side2 = getVertex(0).distance(getVertex(2));
double side3 = getVertex(1).distance(getVertex(2));
return Math.abs(side1 - side2) < TOLERATED_DEVIATION && Math.abs(side1 - side3) < TOLERATED_DEVIATION;
}
/**
* Sets the index-th vertex (if the index is in range 0..2)
* Sets inner triangles for this triangle,
* each of them is built by one of the corners of the triangle
* and the middle between this corner and the other.
*
* @param index is the index of the vector to be set
* @param vertex to be set
* @param depth is the depth of recursive division.
*/
public void setVertex(int index, Vertex2D vertex) {
if (!isValid(index)) {
public void divide(int depth) {
if (depth <= 0) {
return;
}
vertices[index] = vertex;
}
@Override
public String toString() {
return "Triangle: vertices=" + stream(vertices)
.map(Vertex2D::toString)
.collect(joining(" "));
divide();
for (Triangle sub : subTriangles) {
sub.divide(depth - 1);
}
}
/**
......
......@@ -47,9 +47,26 @@ public class Vertex2D {
* To get string representation.
*
* @param opposite the opposite point to create middle against.
* @return string representation.
* @return point at the middle.
*/
public Vertex2D createMiddle(Vertex2D opposite) {
return new Vertex2D((x + opposite.x) / 2, (y + opposite.y) / 2);
}
/**
* Calculates Euclidean distance.
*
* @param opposite the opposite point to calculate distance against.
* @return distance to opposite point.
*/
public double distance(Vertex2D opposite) {
if (opposite == null) {
return -1.0;
}
final double xDiff = x - opposite.x;
final double yDiff = y - opposite.y;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment