Skip to content
Snippets Groups Projects
Point.java 1.08 KiB
package koz01.java2.lab05;

import static java.lang.Math.PI;
import static java.lang.Math.cos;

import java.util.Random;

import lombok.Getter;
import lombok.ToString;

@ToString
@Getter
public class Point {

	public static Point randomWithGivenDistanceFromOrigin(Point start, double distance) {
		if (distance < 0) {
			throw new IllegalArgumentException("Distance should be non negetavi but was " + distance);
		}
		final Random rnd = new Random();
		final double lambda = rnd.nextDouble(2 * PI);
		return new Point(distance*cos(lambda) + start.x, distance*Math.sin(lambda) + start.y);
	}
	
	private double x;
	
	private double y;

	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Point) {
			Point point = (Point) obj;
			return point.canEqual(this) && point.x == x && point.y == y;
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		int val = 7;
		val = val*31 + Double.hashCode(x);
		val = val*31 + Double.hashCode(y);
		return val;
	}

	protected boolean canEqual(Point point) {
		return true;
	}
	
	
}