Commit 2bc653f9 authored by Georgii Aksenov's avatar Georgii Aksenov
Browse files

Iteration-02

parent e9d91a5a
Pipeline #69710 passed with stage
in 10 seconds
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;
/**
......@@ -16,26 +17,11 @@ public class Demo {
* @param args command line arguments, will be ignored
*/
public static void main(String[] args) {
Vertex2D v1 = newVertex(2, 3);
Vertex2D v2 = newVertex(1, 1);
Triangle triangle = new Triangle(
new Vertex2D(-100.0, 0),
new Vertex2D(0, 100),
new Vertex2D(100, -100));
v1.move(v2);
System.out.println(v1.getInfo());
System.out.println(v2.getInfo());
}
/**
* Creates new instance of Vertex2D.
*
* @param x coordinate for new vector
* @param y coordinate for new vector
* @return new Vertex2D.
*/
private static Vertex2D newVertex(double x, double y) {
Vertex2D vertex = new Vertex2D();
vertex.setX(x);
vertex.setY(y);
return vertex;
System.out.println(triangle.toString());
}
}
......@@ -104,4 +104,4 @@ public final class Draw extends JFrame {
int a2 = (int) Math.rint(HALF_HEIGHT - triangle.getVertex(index).getY());
return new AbstractMap.SimpleEntry<>(a1, a2);
}
}
\ No newline at end of file
}
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 double x = 0;
private double y = 0;
private final double x;
private final double y;
public double getX() {
return x;
/**
* 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 void setX(double x) {
this.x = x;
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
/**
* To get string representation.
*
* @return string representation.
*/
public String getInfo() {
return String.format("[%.1f, %.1f]", x, y);
}
/**
* To get sum of coordinates.
*
* @return sum of coordinates.
*/
public double sumCoordinates() {
return x + y;
@Override
public String toString() {
return format("[%s, %s]", Double.valueOf(x).toString(), Double.valueOf(y).toString());
}
/**
* Moves the vertex.
* To get string representation.
*
* @param vertex is added to this vertex.
* @param opposite the opposite point to create middle against.
* @return string representation.
*/
public void move(Vertex2D vertex) {
x += vertex.x;
y += vertex.y;
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