Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lecture02 #1025

Open
wants to merge 2 commits into
base: lecture02
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lecture02/src/main/java/ru/atom/geometry/Bar.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 3 additions & 2 deletions lecture02/src/main/java/ru/atom/geometry/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ 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);
}

/**
* 2D point
* @return new Point
*/
public static Collider createPoint(int x, int y) {
throw new UnsupportedOperationException();
// cast from Object to Point
return new Point(x, y);
}
}
28 changes: 24 additions & 4 deletions lecture02/src/main/java/ru/atom/geometry/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}