diff --git a/lecture02/src/main/java/ru/atom/geometry/Bar.java b/lecture02/src/main/java/ru/atom/geometry/Bar.java new file mode 100644 index 0000000000..279e6e65c8 --- /dev/null +++ b/lecture02/src/main/java/ru/atom/geometry/Bar.java @@ -0,0 +1,55 @@ +package ru.atom.geometry; + +public class Bar implements Collider { + private Point leftCorner; + private Point rightCorner; + + public Bar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) { + Point left = null; + Point right = null; + if (firstCornerX < secondCornerX) { + left = new Point(firstCornerX, firstCornerY); + right = new Point(secondCornerX, secondCornerY); + } else { + left = new Point(secondCornerX, secondCornerY); + right = new Point(firstCornerX, firstCornerY); + } + this.leftCorner = left; + this.rightCorner = right; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + // cast from Object to Point + Bar bar = (Bar) o; + + return leftCorner.getX() == bar.leftCorner.getX() && rightCorner.getX() == bar.rightCorner.getX() + && ((leftCorner.getY() == bar.leftCorner.getY() && rightCorner.getY() == bar.rightCorner.getY()) + || (leftCorner.getY() == bar.rightCorner.getY() && rightCorner.getY() == bar.leftCorner.getY())); + } + + @Override + public boolean isColliding(Collider other) { + if (other == this) { + return true; + } + if (other.getClass() == Point.class) { + Point point = (Point) other; + int maxY = Math.max(leftCorner.getY(), rightCorner.getY()); + int minY = Math.min(leftCorner.getY(), rightCorner.getY()); + return leftCorner.getX() <= point.getX() && point.getX() <= rightCorner.getX() + && point.getY() <= maxY && point.getY() >= minY; + } + if (other.getClass() == getClass()) { + Bar bar = (Bar) other; + int maxBarY = Math.max(bar.leftCorner.getY(), bar.rightCorner.getY()); + int minBarY = Math.min(bar.leftCorner.getY(), bar.rightCorner.getY()); + return !(bar.leftCorner.getX() > rightCorner.getX() || bar.rightCorner.getX() < leftCorner.getX() + || maxBarY < Math.min(leftCorner.getY(), rightCorner.getY()) + || minBarY > Math.max(leftCorner.getY(), rightCorner.getY())); + } + return false; + } +} diff --git a/lecture02/src/main/java/ru/atom/geometry/Geometry.java b/lecture02/src/main/java/ru/atom/geometry/Geometry.java index 79a8c95465..fa018e4255 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Geometry.java +++ b/lecture02/src/main/java/ru/atom/geometry/Geometry.java @@ -22,7 +22,7 @@ private Geometry() { * @return new Bar */ public static Collider createBar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) { - throw new UnsupportedOperationException(); + return new Bar(firstCornerX, firstCornerY, secondCornerX, secondCornerY); } /** @@ -30,6 +30,7 @@ public static Collider createBar(int firstCornerX, int firstCornerY, int secondC * @return new Point */ public static Collider createPoint(int x, int y) { - throw new UnsupportedOperationException(); + // cast from Object to Point + return new Point(x, y); } } diff --git a/lecture02/src/main/java/ru/atom/geometry/Point.java b/lecture02/src/main/java/ru/atom/geometry/Point.java index 6f13561350..818ba59ce9 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Point.java +++ b/lecture02/src/main/java/ru/atom/geometry/Point.java @@ -3,10 +3,25 @@ /** * Template class for */ -public class Point /* super class and interfaces here if necessary */ { - // fields +public class Point implements Collider { + private int x; + private int y; + // and methods + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + /** * @param o - other object to check equality with * @return true if two points are equal and not null. @@ -19,7 +34,12 @@ public boolean equals(Object o) { // cast from Object to Point Point point = (Point) o; - // your code here - throw new UnsupportedOperationException(); + return x == point.x && y == point.y; + } + + @Override + public boolean isColliding(Collider other) { + Point point = (Point) other; + return this.equals(point); } }