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

completed lecture 2 practice #1021

Open
wants to merge 1 commit 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
74 changes: 74 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,74 @@
package ru.atom.geometry;

public class Bar implements Collider {

private final int leftX;
private final int bottomY;
private final int rightX;
private final int topY;

public Bar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) {
this.leftX = Math.min(firstCornerX, secondCornerX);
this.bottomY = Math.min(firstCornerY, secondCornerY);
this.rightX = Math.max(firstCornerX, secondCornerX);
this.topY = Math.max(firstCornerY, secondCornerY);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Bar)) {
return false;
}
Bar other = (Bar) obj;
return this.leftX == other.leftX && this.bottomY == other.bottomY
&& this.rightX == other.rightX && this.topY == other.topY;
}

/**
* Checks if val is inside range
*
* @param val of interest
* @param left boundary
* @param right boundary
* @return true if val is inside else false
*/
private boolean isInside(int val, int left, int right) {
return val >= left && val <= right;
}

public int width() {
return rightX - leftX;
}

public int height() {
return topY - bottomY;
}

private int centerX() {
return leftX + (rightX - leftX) / 2;
}

private int centerY() {
return bottomY + (topY - bottomY) / 2;
}

@Override
public boolean isColliding(Collider other) {
if (other instanceof Point) {
Point point = (Point) other;
return isInside(point.getX(), leftX, rightX) && isInside(point.getY(), bottomY, topY);
}
if (other instanceof Bar) {
Bar bar = (Bar) other;
int meanWidth = (this.width() + bar.width()) / 2;
int meanHeight = (this.height() + bar.height()) / 2;
int centersXDist = Math.abs(this.centerX() - bar.centerX());
int centersYDist = Math.abs(this.centerY() - bar.centerY());
return centersXDist <= meanWidth && centersYDist <= meanHeight;
}
return false;
}
}
4 changes: 2 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,14 @@ 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();
return new Point(x, y);
}
}
27 changes: 22 additions & 5 deletions lecture02/src/main/java/ru/atom/geometry/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@
/**
* Template class for
*/
public class Point /* super class and interfaces here if necessary */ {
// fields
// and methods
public class Point implements Collider {

private final int x;
private final int y;

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
Expand All @@ -18,8 +32,11 @@ public boolean equals(Object o) {

// cast from Object to Point
Point point = (Point) o;
return x == point.x && y == point.y;
}

// your code here
throw new UnsupportedOperationException();
@Override
public boolean isColliding(Collider other) {
return this.equals(other);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package ru.atom.geometry;

import org.junit.Ignore;
import org.junit.Test;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;

@Ignore
public class BarBarCollisionTest {
@Test
public void barSelfCollide() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package ru.atom.geometry;

import org.junit.Ignore;
import org.junit.Test;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;

@Ignore
public class BarPointCollisionTest {
@Test
public void pointInsideBar() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package ru.atom.geometry;

import org.junit.Ignore;
import org.junit.Test;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;

@Ignore
public class PointPointCollisionTest {
@Test
public void pointSelfCollide() {
Expand Down