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 lecture02 #1035

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
70 changes: 70 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,70 @@
package ru.atom.geometry;

import java.util.Objects;

public class Bar implements Collider {

private final Point lowerLeft;
private final Point upperRight;

public Bar(int firstCornerX, int firstCornerY, int secondCornerX, int secondCornerY) {
lowerLeft = new Point(Math.min(firstCornerX, secondCornerX), Math.min(firstCornerY, secondCornerY));
upperRight = new Point(Math.max(firstCornerX, secondCornerX), Math.max(firstCornerY, secondCornerY));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Bar otherBar = (Bar) o;
return Objects.equals(lowerLeft, otherBar.lowerLeft) && Objects.equals(upperRight, otherBar.upperRight);
}

@Override
public boolean isColliding(Collider other) {
if (other instanceof Point) {
return this.contains((Point) other);
}
if (other instanceof Bar) {
return this.intersects((Bar) other);
}
return false;
}

private boolean intersects(Bar other) {
double thisCenterX = average(lowerLeft.getX(), upperRight.getX());
double otherCenterX = average(other.lowerLeft.getX(), other.upperRight.getX());
if (Math.abs(thisCenterX - otherCenterX) > average(this.width(), other.width())) {
return false;
}

double thisCenterY = average(lowerLeft.getY(), upperRight.getY());
double otherCenterY = average(other.lowerLeft.getY(), other.upperRight.getY());
return Math.abs(thisCenterY - otherCenterY) <= average(this.height(), other.height());
}

public int width() {
return upperRight.getX() - lowerLeft.getX();
}

public int height() {
return upperRight.getY() - lowerLeft.getY();
}

private double average(int from, int to) {
return (from + to) / 2.0;
}

private boolean contains(Point point) {
return isInside(point.getX(), lowerLeft.getX(), upperRight.getX())
&& isInside(point.getY(), lowerLeft.getY(), upperRight.getY());
}

private boolean isInside(int value, int start, int end) {
return value >= start && value <= end;
}
}
20 changes: 11 additions & 9 deletions lecture02/src/main/java/ru/atom/geometry/Geometry.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package ru.atom.geometry;

/**
* ^ Y
* |
* |
* |
* | X
* .---------->
* ^ Y
* |
* |
* |
* | X
* .---------->
*/

public final class Geometry {

private Geometry() {
}

Expand All @@ -19,17 +19,19 @@ private Geometry() {
* Like selection bar in desktop, this bar is defined by two opposite corners
* Bar is not oriented
* (It is not relevant, which opposite corners you choose to define bar)
*
* @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);
}
}
31 changes: 26 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 @@ -19,7 +33,14 @@ public boolean equals(Object o) {
// cast from Object to Point
Point point = (Point) o;

// your code here
throw new UnsupportedOperationException();
return this.x == point.x && this.y == point.y;
}

@Override
public boolean isColliding(Collider other) {
if (other instanceof Point) {
return this.equals(other);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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,11 @@
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