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

iteration01 + iteration02

(cherry picked from commit 5f8f9312)
parent 3458cd3b
package cz.muni.fi.pb162.project;
package cz.muni.fi.pb162.project.demo;
import cz.muni.fi.pb162.project.geometry.Triangle;
import cz.muni.fi.pb162.project.geometry.Vertex2D;
/**
* Class for running main method.
*
* @author TODO: put your name here
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Demo {
/**
* Runs the code.
* prints info for two vertices of fixed coordinates.
*
* @param args command line arguments, will be ignored
*/
public static void main(String[] args) {
Triangle triangle = new Triangle(
new Vertex2D(-100.0, 0),
new Vertex2D(0, 100),
new Vertex2D(100, -100));
System.out.println("Hello world!");
System.out.println(triangle.toString());
}
}
package cz.muni.fi.pb162.project.geometry;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
/**
* This class represents a triangle in 2D space.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Triangle {
private final Vertex2D[] vertices;
private final Triangle[] subTriangles = new Triangle[3];
/**
* Checkstyle asks for the Javadoc here.
* This is a constructor.
* It sets corners ¯\_(ツ)_/¯
*
* @param v1 is the first corner
* @param v2 is the second corner
* @param v3 is the third corner
*/
public Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3) {
this.vertices = new Vertex2D[]{v1, v2, v3};
}
/**
* Returns the index-th vertex if the index is in range 0..2, otherwise null
*
* @param index is the index of the vector
* @return the index-th vertex
*/
public Vertex2D getVertex(int index) {
if (index < 0 || index > 2) {
return null;
}
return vertices[index];
}
/**
* Sets the index-th vertex (if the index is in range 0..2)
*
* @param index is the index of the vector to be set
* @param vertex to be set
*/
public void setVertex(int index, Vertex2D vertex) {
if (!isValid(index)) {
return;
}
vertices[index] = vertex;
}
@Override
public String toString() {
return "Triangle: vertices=" + stream(vertices)
.map(Vertex2D::toString)
.collect(joining(" "));
}
/**
* 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.
*
* @return false if triangle was already divided, otherwise true
*/
public boolean divide() {
if (isDivided()) {
return false;
}
subTriangles[0] = createSubTriangle(0, 1, 2);
subTriangles[1] = createSubTriangle(1, 0, 2);
subTriangles[2] = createSubTriangle(2, 0, 1);
return true;
}
/**
* @return true if the triangle was already divided.
*/
public boolean isDivided() {
return subTriangles[0] != null;
}
/**
* Returns the index-th subtriangle if the index is in range 0..2, otherwise null
*
* @param index is the index of the vector
* @return the index-th subtriangle
*/
public Triangle getSubTriangle(int index) {
if (!isValid(index) || !isDivided()) {
return null;
}
return subTriangles[index];
}
private Triangle createSubTriangle(int cornerIndex, int oppositeIndex1, int oppositeIndex2) {
Vertex2D corner = vertices[cornerIndex];
Vertex2D opposite1 = vertices[oppositeIndex1];
Vertex2D opposite2 = vertices[oppositeIndex2];
return new Triangle(corner, corner.createMiddle(opposite1), corner.createMiddle(opposite2));
}
private boolean isValid(int index) {
return index >= 0 && index <= 2;
}
}
package cz.muni.fi.pb162.project.geometry;
import static java.lang.String.format;
/**
* This class represents a point in 2D space.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Vertex2D {
private final double x;
private final double y;
/**
* Checkstyle asks for the Javadoc here.
* This is a constructor.
* It sets coordinates ¯\_(ツ)_/¯
*
* @param x is the x coordinate
* @param y is the y coordinate
*/
public Vertex2D(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
/**
* To get string representation.
*
* @return string representation.
*/
@Override
public String toString() {
return format("[%s, %s]", Double.valueOf(x).toString(), Double.valueOf(y).toString());
}
/**
* To get string representation.
*
* @param opposite the opposite point to create middle against.
* @return string representation.
*/
public Vertex2D createMiddle(Vertex2D opposite) {
return new Vertex2D((x + opposite.x) / 2, (y + opposite.y) / 2);
}
}
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