Skip to content
Snippets Groups Projects
Commit 822f11f1 authored by Vitalii Bortsov's avatar Vitalii Bortsov
Browse files

reseni 06

parent 119d6bc8
No related branches found
No related tags found
1 merge request!8submit-06
Pipeline #
package cz.muni.fi.pb162.project.geometry;
import java.util.Arrays;
/**
* Class for working with vertices of polygons
*
* @author Vitalii Bortsov
*/
public class ArrayPolygon extends SimplePolygon {
private final Vertex2D[] arrayOfVertices;
/**
* Constructor of the array with vertices of the polygon
*
* @param vertices coordinates of the vertices of the polygon;
* must not be null,
* must not contain null-element,
* must has at least 3 elements.
*/
public ArrayPolygon(Vertex2D[] vertices) {
if(vertices == null) {
throw new IllegalArgumentException("Array must not be null");
}
if(Arrays.asList(vertices).contains(null)) {
throw new IllegalArgumentException("Array must not contain a null element");
}
if(vertices.length < 3) {
throw new IllegalArgumentException("Array must has at least 3 elements");
}
arrayOfVertices = Arrays.copyOf(vertices, vertices.length);
}
@Override
public Vertex2D getVertex(int index) {
if(index < 0) {
throw new IllegalArgumentException("index must be positive");
}
return arrayOfVertices[index % arrayOfVertices.length];
}
@Override
public int getNumVertices() {
return arrayOfVertices.length;
}
@Override
public boolean equals(Object object) {
if(object == null) {
return false;
}
if(this.getClass() != object.getClass()) {
return false;
}
ArrayPolygon secondArray = (ArrayPolygon) object;
final int length = this.arrayOfVertices.length;
if (length != secondArray.arrayOfVertices.length) {
return false;
}
for (int i = 0; i < length; i++) {
if(!this.arrayOfVertices[i].equals(secondArray.arrayOfVertices[i])) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
return Arrays.hashCode(arrayOfVertices);
}
}
package cz.muni.fi.pb162.project.geometry;
import cz.muni.fi.pb162.project.utils.SimpleMath;
/**
* Class for working with Simple polygons
*
* @author Vitalii Bortsov
*/
abstract class SimplePolygon implements Polygon {
/**
* Method returns the difference between the largest and smallest Y coordinates in an polygon
*
* @return height of the polygon
*/
public double getHeight() {
return SimpleMath.maxY(this) - SimpleMath.minY(this);
}
/**
* Method returns the difference between the largest and smallest X coordinates in an polygon
*
* @return width of the polygon
*/
public double getWidth() {
return SimpleMath.maxX(this) - SimpleMath.minX(this);
}
@Override
public String toString() {
final int verticesNum = this.getNumVertices();
StringBuilder string = new StringBuilder("Polygon: vertices =");
for (int i = 0; i < verticesNum; i++) {
string.append(" ").append(this.getVertex(i));
}
return string.toString();
}
}
......@@ -6,8 +6,7 @@ import cz.muni.fi.pb162.project.utils.SimpleMath;
* Class for creating a triangle from 3 vertices
* @author Vitalii Bortsov
*/
public class Triangle implements Measurable {
private final Vertex2D[] arrayOfVertices;
public class Triangle extends ArrayPolygon implements Measurable {
private final Triangle[] arrayOfTriangles = {null, null, null};
/**
......@@ -17,7 +16,7 @@ public class Triangle implements Measurable {
* @param v3 3rd vertex in new triangle
*/
public Triangle(Vertex2D v1, Vertex2D v2, Vertex2D v3) {
arrayOfVertices = new Vertex2D[] {v1, v2, v3};
super(new Vertex2D[] {v1, v2, v3});
}
/**
......@@ -32,25 +31,13 @@ public class Triangle implements Measurable {
this.divide(depth);
}
/**
*
* @param index of Vertex in range 0 - 2
* @return Vertex on input index or NULL if input index is out of range
*/
public Vertex2D getVertex(int index) {
if(index > 2 || index < 0) {
return null;
}
return arrayOfVertices[index];
}
/**
*
* @return string with info about vertices in triangle
*/
@Override
public String toString() {
return "Triangle: vertices=" + arrayOfVertices[0] + " " + arrayOfVertices[1] + " " + arrayOfVertices[2];
return "Triangle: vertices=" + getVertex(0) + " " + getVertex(1) + " " + getVertex(2);
}
/**
......@@ -70,9 +57,9 @@ public class Triangle implements Measurable {
return false;
}
for(int index = 0; index < 3; index++) {
Vertex2D point1 = arrayOfVertices[index];
Vertex2D point2 = arrayOfVertices[index].createMiddle(arrayOfVertices[(index + 1) % 3]);
Vertex2D point3 = arrayOfVertices[index].createMiddle(arrayOfVertices[(index + 2) % 3]);
Vertex2D point1 = getVertex(index);
Vertex2D point2 = getVertex(index).createMiddle(getVertex((index + 1) % 3));
Vertex2D point3 = getVertex(index).createMiddle(getVertex((index + 2) % 3));
Triangle triangle = new Triangle(point1, point2, point3);
arrayOfTriangles[index] = triangle;
}
......@@ -99,9 +86,9 @@ public class Triangle implements Measurable {
* @return true if the triangle is equilateral
*/
public boolean isEquilateral() {
double side1 = arrayOfVertices[0].distance(arrayOfVertices[1]);
double side2 = arrayOfVertices[1].distance(arrayOfVertices[2]);
double side3 = arrayOfVertices[0].distance(arrayOfVertices[2]);
double side1 = getVertex(0).distance(getVertex(1));
double side2 = getVertex(1).distance(getVertex(2));
double side3 = getVertex(0).distance(getVertex(2));
return Math.abs(side1 - side2) < 0.001 &&
Math.abs(side1 - side3) < 0.001 &&
Math.abs(side2 - side3) < 0.001;
......
......@@ -59,4 +59,18 @@ public class Vertex2D {
return Math.sqrt(Math.pow((this.xcoord - targetVertex.xcoord), 2)
+ Math.pow((this.ycoord - targetVertex.ycoord), 2));
}
@Override
public boolean equals(Object object) {
if (object instanceof Vertex2D) {
Vertex2D secondVertex = (Vertex2D) object;
return (this.xcoord == secondVertex.xcoord) && (this.ycoord == secondVertex.ycoord);
}
return false;
}
@Override
public int hashCode() {
return Double.hashCode(xcoord) * 31 + Double.hashCode(ycoord);
}
}
package cz.muni.fi.pb162.project.utils;
import cz.muni.fi.pb162.project.geometry.Triangle;
import cz.muni.fi.pb162.project.geometry.Polygon;
/**
* Class with simple geometry methods.
......@@ -8,16 +8,17 @@ import cz.muni.fi.pb162.project.geometry.Triangle;
*/
public class SimpleMath {
/**
* Method that finds minimum x coordinate in Triangle.
* Method that finds minimum x coordinate in n-gon.
*
* @param triangle Triangle object. Must not be null.
* @param polygon Polygon object. Must not be null.
* @return minimum X coordinate.
*/
public static double minX(Triangle triangle) {
double xCoord = triangle.getVertex(0).getX();
public static double minX(Polygon polygon) {
final int verticesNum = polygon.getNumVertices();
double xCoord = polygon.getVertex(0).getX();
double minX = xCoord;
for(int i = 1; i < 3; i++) {
xCoord = triangle.getVertex(i).getX();
for(int i = 1; i < verticesNum; i++) {
xCoord = polygon.getVertex(i).getX();
if(xCoord < minX) {
minX = xCoord;
}
......@@ -26,16 +27,17 @@ public class SimpleMath {
}
/**
* Method that finds minimum y coordinate in Triangle.
* Method that finds minimum y coordinate in n-gon.
*
* @param triangle Triangle object. Must not be null.
* @param polygon Polygon object. Must not be null.
* @return minimum Y coordinate.
*/
public static double minY(Triangle triangle) {
double yCoord = triangle.getVertex(0).getY();
public static double minY(Polygon polygon) {
final int verticesNum = polygon.getNumVertices();
double yCoord = polygon.getVertex(0).getY();
double minY = yCoord;
for(int i = 1; i < 3; i++) {
yCoord = triangle.getVertex(i).getY();
for(int i = 1; i < verticesNum; i++) {
yCoord = polygon.getVertex(i).getY();
if(yCoord < minY) {
minY = yCoord;
}
......@@ -44,16 +46,17 @@ public class SimpleMath {
}
/**
* Method that finds maximum x coordinate in Triangle.
* Method that finds maximum x coordinate in n-gon.
*
* @param triangle Triangle object. Must not be null.
* @param polygon Polygon object. Must not be null.
* @return maximum X coordinate.
*/
public static double maxX(Triangle triangle) {
double xCoord = triangle.getVertex(0).getX();
public static double maxX(Polygon polygon) {
final int verticesNum = polygon.getNumVertices();
double xCoord = polygon.getVertex(0).getX();
double maxX = xCoord;
for(int i = 1; i < 3; i++) {
xCoord = triangle.getVertex(i).getX();
for(int i = 1; i < verticesNum; i++) {
xCoord = polygon.getVertex(i).getX();
if(xCoord > maxX) {
maxX = xCoord;
}
......@@ -62,16 +65,17 @@ public class SimpleMath {
}
/**
* Method that finds maximum y coordinate in Triangle.
* Method that finds maximum y coordinate in n-gon.
*
* @param triangle Triangle object. Must not be null.
* @param polygon Polygon object. Must not be null.
* @return maximum Y coordinate.
*/
public static double maxY(Triangle triangle) {
double yCoord = triangle.getVertex(0).getY();
public static double maxY(Polygon polygon) {
final int verticesNum = polygon.getNumVertices();
double yCoord = polygon.getVertex(0).getY();
double maxY = yCoord;
for(int i = 1; i < 3; i++) {
yCoord = triangle.getVertex(i).getY();
for(int i = 1; i < verticesNum; i++) {
yCoord = polygon.getVertex(i).getY();
if(yCoord > maxY) {
maxY = yCoord;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment