Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dot;
- public class Dot {
- private double x;
- private double y;
- private double radius;
- private int r;
- private int g;
- private int b;
- /**
- * Constructs a black dot at (0.0, 0.0) with radius 1.0
- */
- public Dot() {
- this(0.0, 0.0, 0.0, 1, 0, 0);
- }
- /**
- * Constructs a black dot with radius 1.0 at the given location
- */
- public Dot(double x, double y) {
- this(x, y, 1.0, 0, 0, 0);
- }
- /**
- * Constructs a black dot at the given location and radius
- */
- public Dot(double x, double y, double radius) {
- this(x, y, radius, 1, 0, 0);
- }
- /**
- * Constructs a dot at the given location and radius, and a color specified by
- * r, g, b values.
- */
- public Dot(double x, double y, double radius, int r, int g, int b) {
- if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || radius <= 0) {
- throw new IllegalArgumentException();
- }
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.r = r;
- this.g = g;
- this.b = b;
- }
- /**
- * Returns the x-coordinate
- */
- public double getX() {
- return this.x;
- }
- /**
- * Sets the x-coordinate
- */
- public void setX(double x) {
- this.x = x;
- }
- /**
- * Doubles the radius of this dot.
- */
- public void grow() {
- this.radius *= 2;
- }
- /**
- * Translates this dot by increasing the x- and y-coordinates by dx and dy,
- * respectively.
- */
- public void translate(double dx, double dy) {
- this.x += dx;
- this.y += dy;
- }
- /**
- * Halves the radius of this dot.
- */
- public void shrink() {
- this.radius /= 2;
- }
- /**
- * Gets the y-coordinate
- */
- public double getY() {
- return this.y;
- }
- /**
- * Sets the y-coordinate
- */
- public void setY(double y) {
- this.y = y;
- }
- /**
- * Gets the radius
- */
- public double getRadius() {
- return this.radius;
- }
- /**
- * Sets the radius to a specified value
- */
- public void setRadius(double newRadius) {
- this.radius = newRadius;
- }
- /**
- * Gets the red component of this dot's color
- */
- public int getRed() {
- return this.r;
- }
- /**
- * Gets the green component of this dot's color
- */
- public int getGreen() {
- return this.g;
- }
- /**
- * Returns a new dot that is halfway between this dot and the specified dot.
- */
- public Dot getMidDot(Dot that) {
- return new Dot((this.x + that.x) / 2, (this.y + that.y) / 2, (this.radius + that.radius) / 2,
- (this.r + that.r) / 2, (this.g + that.g) / 2, (this.b + that.b) / 2);
- }
- /**
- * Gets the blue component of this dot's color
- */
- public int getBlue() {
- return this.b;
- }
- /**
- * Returns the brightness of a dot, defined by the average of the red, green,
- * and blue components of the dot
- */
- public int getBrightness() {
- return (this.b + this.g + this.r) / 3;
- }
- /**
- * Returns the distance between this dot's center and a point specified by (x,
- * y)
- */
- public double getDistanceTo(double x, double y) {
- return Math.sqrt(Math.pow((this.x - x), 2) + Math.pow((this.y - y), 2));
- }
- /**
- * Returns the distance between two dots' centers
- */
- public double getDistanceTo(Dot that) {
- return Math.sqrt(Math.pow((this.x - that.x), 2) + Math.pow((this.y - that.y), 2));
- }
- /**
- * Returns the distance between this dot's center and the point (0, 0)
- */
- public double getDistanceToOrigin() {
- return Math.sqrt(Math.pow((this.x - 0), 2) + Math.pow((this.y - 0), 2));
- }
- /**
- * Sets the color of this point.
- */
- public void setColor(int r, int g, int b) {
- if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255) {
- throw new IllegalArgumentException();
- }
- this.r = r;
- this.g = g;
- this.b = b;
- }
- /**
- * Brightens this dot's color by doubling its red, green, and blue components.
- */
- public void brighten() {
- this.r *= 2;
- this.g *= 2;
- this.b *= 2;
- }
- /**
- * Darken this dot's color by cutting its red, green, and blue components in
- * half.
- */
- public void darken() {
- this.r /= 2;
- this.g /= 2;
- this.b /= 2;
- }
- /*
- *
- * Returns a String representation of this Dot in the form: (47.375, 18.500),
- * radius=72.139, color=(255, 0, 0) Note that all double values are rounded to
- * three places.
- */
- @Override
- public String toString() {
- return "(" + x + ", " + y + "), radius=" + radius + ", color=(" + r + "," + g + "," + b + ")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement