From 1c903b9cac0b4ea2325ce91061284e5a3fef57a2 Mon Sep 17 00:00:00 2001 From: AlexeyRogS Date: Sat, 22 Feb 2020 23:06:12 +0300 Subject: [PATCH 1/2] Lecture02 --- .../src/main/java/ru/atom/geometry/Bar.java | 52 +++++++++++++++++++ .../main/java/ru/atom/geometry/Geometry.java | 5 +- .../src/main/java/ru/atom/geometry/Point.java | 23 ++++++-- 3 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 lecture02/src/main/java/ru/atom/geometry/Bar.java 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..b2b67040da --- /dev/null +++ b/lecture02/src/main/java/ru/atom/geometry/Bar.java @@ -0,0 +1,52 @@ +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; + return !(bar.leftCorner.getX() > rightCorner.getX() || bar.rightCorner.getX() < leftCorner.getX() || + Math.max(bar.leftCorner.getY(), bar.rightCorner.getY()) < Math.min(leftCorner.getY(), rightCorner.getY()) || + Math.min(bar.leftCorner.getY(), bar.rightCorner.getY()) > 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..ea72c936f2 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Point.java +++ b/lecture02/src/main/java/ru/atom/geometry/Point.java @@ -3,10 +3,20 @@ /** * 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 +29,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); } } From 2d379f2fb4a94cd0c521c64fb7b30e10d4f3fa60 Mon Sep 17 00:00:00 2001 From: AlexeyRogS Date: Sat, 22 Feb 2020 23:20:07 +0300 Subject: [PATCH 2/2] Lecture02 --- .../src/main/java/ru/atom/geometry/Bar.java | 31 ++++++++++--------- .../src/main/java/ru/atom/geometry/Point.java | 13 +++++--- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/lecture02/src/main/java/ru/atom/geometry/Bar.java b/lecture02/src/main/java/ru/atom/geometry/Bar.java index b2b67040da..279e6e65c8 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Bar.java +++ b/lecture02/src/main/java/ru/atom/geometry/Bar.java @@ -1,17 +1,16 @@ package ru.atom.geometry; -public class Bar implements Collider{ +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){ + if (firstCornerX < secondCornerX) { left = new Point(firstCornerX, firstCornerY); right = new Point(secondCornerX, secondCornerY); - } - else{ + } else { left = new Point(secondCornerX, secondCornerY); right = new Point(firstCornerX, firstCornerY); } @@ -26,26 +25,30 @@ public boolean equals(Object o) { // 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())); + 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 == 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; + return leftCorner.getX() <= point.getX() && point.getX() <= rightCorner.getX() + && point.getY() <= maxY && point.getY() >= minY; } - if (other.getClass() == getClass()){ + if (other.getClass() == getClass()) { Bar bar = (Bar) other; - return !(bar.leftCorner.getX() > rightCorner.getX() || bar.rightCorner.getX() < leftCorner.getX() || - Math.max(bar.leftCorner.getY(), bar.rightCorner.getY()) < Math.min(leftCorner.getY(), rightCorner.getY()) || - Math.min(bar.leftCorner.getY(), bar.rightCorner.getY()) > Math.max(leftCorner.getY(), rightCorner.getY())); + 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/Point.java b/lecture02/src/main/java/ru/atom/geometry/Point.java index ea72c936f2..818ba59ce9 100644 --- a/lecture02/src/main/java/ru/atom/geometry/Point.java +++ b/lecture02/src/main/java/ru/atom/geometry/Point.java @@ -9,14 +9,19 @@ public class Point implements Collider { // and methods - public Point(int x, int y){ + public Point(int x, int y) { this.x = x; this.y = y; } - public int getX() { return x; } + public int getX() { + return x; + } + + public int getY() { + return y; + } - public int getY() { return y; } /** * @param o - other object to check equality with * @return true if two points are equal and not null. @@ -33,7 +38,7 @@ public boolean equals(Object o) { } @Override - public boolean isColliding(Collider other){ + public boolean isColliding(Collider other) { Point point = (Point) other; return this.equals(point); }