All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.psjava.geometry.data.Segment2D Maven / Gradle / Ivy

package org.psjava.geometry.data;

import org.psjava.javautil.EqualityTester;
import org.psjava.javautil.StrictEqualityTester;

public class Segment2D implements EqualityTester> {

	public static  Segment2D create(Point2D p1, Point2D p2) {
		return new Segment2D(p1, p2);
	}

	private final Point2D p1;
	private final Point2D p2;

	private Segment2D(Point2D p1, Point2D p2) {
		if (p1.equals(p2))
			throw new IllegalArgumentException("Segment : two points are same");
		this.p1 = p1;
		this.p2 = p2;
	}

	public String toString() {
		return "Segment(" + p1 + "-" + p2 + ")";
	}

	public Point2D p1() {
		return p1;
	}

	public Point2D p2() {
		return p2;
	}

	public final boolean equals(Object obj) {
		return StrictEqualityTester.areEqual(this, obj, this);
	}

	@Override
	public boolean areEqual(Segment2D o1, Segment2D o2) {
		return (o2.p1.equals(o1.p1) && o2.p2.equals(o1.p2)) || (o2.p1.equals(o1.p2) && o2.p2.equals(o1.p1));
	}

	public final int hashCode() {
		return p1.hashCode() ^ p2.hashCode();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy