Last active
August 29, 2015 14:07
-
-
Save jwgmeligmeyling/c83c92db5bf2d05c646d to your computer and use it in GitHub Desktop.
Circle interface using Java default methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.tudelft.ti2206.bubbles; | |
import java.awt.Point; | |
/** | |
* Circle | |
* | |
* @author Jan-Willem Gmelig Meyling | |
* | |
*/ | |
public interface Circle { | |
/** | |
* Get the radius for this {@code Circle} | |
* | |
* @return the radius for this {@code Circle} | |
*/ | |
int getRadius(); | |
/** | |
* Get the center for this {@code Circle} | |
* | |
* @return the center for this {@code Circle} | |
*/ | |
Point getCenter(); | |
/** | |
* Set the center for this {@code Circle} | |
* | |
* @param center | |
*/ | |
void setCenter(Point center); | |
/** | |
* Get the diameter of this {@code Circle} | |
* | |
* @return the diameter of this {@code Circle} | |
*/ | |
default int getDiameter() { | |
return getRadius() * 2; | |
} | |
/** | |
* Get the surface for this {@code Circle} | |
* | |
* @return the surface for this {@code Circle} | |
*/ | |
default double getSurface() { | |
return Math.PI * getRadius() * getRadius(); | |
} | |
/** | |
* Translate this circle | |
* | |
* @param dx | |
* @param dy | |
*/ | |
default void translate(int dx, int dy) { | |
Point center = getCenter(); | |
center.translate(dx, dy); | |
setCenter(center); | |
} | |
/** | |
* Get the distance between two circles | |
* | |
* @param other | |
* another {@code Circle} | |
* @return the distance between this {@code Circle} and the other | |
* {@code Circle} | |
*/ | |
default double getDistance(final Circle other) { | |
return getCenter().distance(other.getCenter()); | |
} | |
/** | |
* Check if two circles intersect | |
* | |
* @param other | |
* another {@code Circle} | |
* @return true if this {@code Circle} intersects the other {@code Circle} | |
*/ | |
default boolean intersect(final Circle other) { | |
return getDistance(other) <= (getRadius() + other.getRadius()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment