Commit 921093d0 authored by Georgii Aksenov's avatar Georgii Aksenov
Browse files

Iteration04

parent 2129399f
Pipeline #77130 passed with stage
in 33 seconds
package cz.muni.fi.pb162.project.demo;
import cz.muni.fi.pb162.project.geometry.Triangle;
import cz.muni.fi.pb162.project.geometry.Square;
import cz.muni.fi.pb162.project.geometry.Vertex2D;
/**
......@@ -12,16 +12,13 @@ import cz.muni.fi.pb162.project.geometry.Vertex2D;
public class Demo {
/**
* prints info for two vertices of fixed coordinates.
* This is the Demo main method.
*
* @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));
Square square = new Square(new Vertex2D(0, 0), 100);
System.out.println(triangle.toString());
System.out.println(square.toString());
}
}
......@@ -5,7 +5,7 @@ package cz.muni.fi.pb162.project.geometry;
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Circle {
public class Circle implements Measurable, Circular {
private final Vertex2D center;
private final double radius;
......@@ -27,10 +27,12 @@ public class Circle {
this(new Vertex2D(0, 0), 1);
}
@Override
public Vertex2D getCenter() {
return center;
}
@Override
public double getRadius() {
return radius;
}
......@@ -39,4 +41,20 @@ public class Circle {
public String toString() {
return "Circle: center=" + center.toString() + ", radius=" + radius;
}
/**
* @return diameter.
*/
@Override
public double getWidth() {
return radius * 2;
}
/**
* @return diameter.
*/
@Override
public double getHeight() {
return getWidth();
}
}
package cz.muni.fi.pb162.project.geometry;
/**
* This class represents a Snowman.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Snowman {
public static final int NUMBER_OF_BALLS = 4;
private static final double DEFAULT_REDUCTION_FACTOR = 0.8;
private final Circular[] balls = new Circular[NUMBER_OF_BALLS];
/**
* Builds Snowman from NUMBER_OF_BALLS parts, with base of given lowerSphere.
* Each consecutive ball is a Circle shrunk by reductionFactor.
* @param lowerSphere base of Snowball.
* @param reductionFactor reduction factor for consecutive balls.
*/
public Snowman(Circular lowerSphere, double reductionFactor) {
if (reductionFactor <= 0 || reductionFactor > 1) {
reductionFactor = DEFAULT_REDUCTION_FACTOR;
}
balls[0] = lowerSphere;
for (int i = 1; i < NUMBER_OF_BALLS; i++) {
balls[i] = nextCircle(balls[i - 1], reductionFactor);
}
}
public Circular[] getBalls() {
return balls;
}
private Circular nextCircle(Circular prev, double reductionFactor) {
double radius = prev.getRadius() * reductionFactor;
Vertex2D center = new Vertex2D(prev.getCenter().getX(), prev.getCenter().getY() + prev.getRadius() + radius);
return new Circle(center, radius);
}
}
package cz.muni.fi.pb162.project.geometry;
import static java.util.stream.Collectors.joining;
import static java.util.stream.IntStream.range;
/**
* This class represents a Square.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Square implements Circular {
private final Vertex2D center;
private final double radius;
/**
* @param center of square.
* @param diameter of circumscribed circle.
*/
public Square(Vertex2D center, double diameter) {
this.center = center;
this.radius = diameter / 2;
}
/**
* @param circumscribed circular.
*/
public Square(Circular circumscribed) {
this(circumscribed.getCenter(), circumscribed.getRadius() * 2);
}
@Override
public Vertex2D getCenter() {
return center;
}
@Override
public double getRadius() {
return radius;
}
/**
* Returns the index-th vertex if the index is in range 0..3, otherwise null
* 0 = left vertex
* 1 = lower vertex
* 2 = right vertex
* 3 = upper vertex
*
* @param index is the index of the vector
* @return the index-th vertex
*/
public Vertex2D getVertex(int index) {
switch (index) {
case 0:
return new Vertex2D(center.getX() - radius, center.getY());
case 1:
return new Vertex2D(center.getX(), center.getY() - radius);
case 2:
return new Vertex2D(center.getX() + radius, center.getY());
case 3:
return new Vertex2D(center.getX(), center.getY() + radius);
default:
return null;
}
}
@Override
public String toString() {
return "Square: vertices=" + range(0, 4).boxed()
.map(index -> getVertex(index).toString())
.collect(joining(" "));
}
}
package cz.muni.fi.pb162.project.geometry;
import static cz.muni.fi.pb162.project.utils.SimpleMath.maxX;
import static cz.muni.fi.pb162.project.utils.SimpleMath.maxY;
import static cz.muni.fi.pb162.project.utils.SimpleMath.minX;
import static cz.muni.fi.pb162.project.utils.SimpleMath.minY;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
......@@ -8,10 +12,11 @@ import static java.util.stream.Collectors.joining;
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Triangle {
public class Triangle implements Measurable {
private static final double TOLERATED_DEVIATION = 0.001;
private final Vertex2D[] vertices;
private final Triangle[] subTriangles = new Triangle[3];
/**
......@@ -38,6 +43,10 @@ public class Triangle {
this.vertices = new Vertex2D[]{v1, v2, v3};
}
public Vertex2D[] getVertices() {
return vertices;
}
/**
* Returns the index-th vertex if the index is in range 0..2, otherwise null
*
......@@ -139,4 +148,24 @@ public class Triangle {
private boolean isValid(int index) {
return index >= 0 && index <= 2;
}
/**
* Calculates width as length of a projection to the X axis.
*
* @return width
*/
@Override
public double getWidth() {
return maxX(this) - minX(this);
}
/**
* Calculates height as length of a projection to the Y axis.
*
* @return height
*/
@Override
public double getHeight() {
return maxY(this) - minY(this);
}
}
package cz.muni.fi.pb162.project.utils;
import cz.muni.fi.pb162.project.geometry.Measurable;
import cz.muni.fi.pb162.project.geometry.Triangle;
/**
* This is a Util class for printing information about Measurable objects.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class Gauger {
/**
* @param measured an object being measured
*/
public static void printMeasurement(Measurable measured) {
System.out.printf("Width: %s%n", measured.getWidth());
System.out.printf("Height: %s%n", measured.getHeight());
}
/**
* @param triangle a triangle being measured
*/
public static void printMeasurement(Triangle triangle) {
System.out.println(triangle.toString());
printMeasurement((Measurable) triangle);
}
}
package cz.muni.fi.pb162.project.utils;
import cz.muni.fi.pb162.project.geometry.Triangle;
import cz.muni.fi.pb162.project.geometry.Vertex2D;
import static java.util.Arrays.stream;
import static java.util.Comparator.naturalOrder;
/**
* This is a Util class for work with geometry shapes.
*
* @author Georgii Aksenov <xaksenov@fi.muni.cz>
*/
public class SimpleMath {
/**
* @param triangle to work with.
* @return minimum of the X coordinates.
*/
public static double minX(Triangle triangle) {
return stream(triangle.getVertices()).map(Vertex2D::getX)
.min(naturalOrder())
.orElseThrow(IllegalArgumentException::new);
}
/**
* @param triangle to work with.
* @return minimum of the Y coordinates.
*/
public static double minY(Triangle triangle) {
return stream(triangle.getVertices()).map(Vertex2D::getY)
.min(naturalOrder())
.orElseThrow(IllegalArgumentException::new);
}
/**
* @param triangle to work with.
* @return maximum of the X coordinates.
*/
public static double maxX(Triangle triangle) {
return stream(triangle.getVertices()).map(Vertex2D::getX)
.max(naturalOrder())
.orElseThrow(IllegalArgumentException::new);
}
/**
* @param triangle to work with.
* @return maximum of the Y coordinates.
*/
public static double maxY(Triangle triangle) {
return stream(triangle.getVertices()).map(Vertex2D::getY)
.max(naturalOrder())
.orElseThrow(IllegalArgumentException::new);
}
}
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