algorithms-for-computing-li.../src/main/java/de/wwwu/awolf/model/dao/Line.java

219 lines
5.1 KiB
Java

package de.wwwu.awolf.model.dao;
import java.util.Objects;
import org.apache.commons.math3.util.Precision;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 12.06.2017.
*/
public class Line implements Comparable<Line> {
private static final double EPSILON = 0.00001;
private double x1;
private double x2;
private double y1;
private double y2;
private Segment segment;
private Double m;
private Double b;
private String id;
public Line(Double m, Double b) {
this.m = m;
this.b = b;
}
/**
* Konstruktor
*
* @param x1 x-Koordiante des Startpunkts
* @param x2 x-Koordinate des Endpunkts
* @param y1 y-Koordinate des Startpunkts
* @param y2 y-Koordinate des Endpunkts
*/
public Line(double x1, double x2, double y1, double y2) {
this.x1 = x1;
this.y1 = y1;
this.y2 = y2;
this.x2 = x2;
this.m = (y2 - y1) / (x2 - x1);
this.b = y2 - (x2 * m);
}
/**
* @return Steigung der Gerade
*/
public Double getM() {
return m;
}
/**
* @param m Steigung der Gerade
*/
public void setM(double m) {
this.m = m;
}
/**
* @return y-Achsenabschnitt der Gerade
*/
public Double getB() {
return b;
}
/**
* @param b y-Achsenabschnitt der Gerade
*/
public void setB(double b) {
this.b = b;
}
/**
* @return id der Gerade
*/
public String getId() {
return id;
}
/**
* @param id id der Gerade
*/
public void setId(String id) {
this.id = id;
}
/**
* Vergleich einzelner Geradern
*
* @param obj zu vergleichende Gerade
* @return <code>true</code> falls die Geraden Gleich sind
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Line) {
Line other = (Line) obj;
return Precision.equals(other.getM(), this.getM(), EPSILON) && Precision
.equals(other.getB(), this.getB(), EPSILON);
} else {
return super.equals(obj);
}
}
@Override
public int hashCode() {
return Objects.hash(Precision.round(m, 5), Precision.round(b, 5));
}
@Override
public String toString() {
return "Line m: " + this.getM() + ", b: " + this.getB();
}
public Point intersect(Line line) {
double x = (line.b - this.b) / (this.m - line.m);
double y = this.m * x + this.b;
return new Point(x, y);
}
@Override
public int compareTo(Line line) {
if (Precision.compareTo(this.getM(), line.getM(), EPSILON) == 0) {
return this.getB().compareTo(line.getB());
} else {
return this.getM().compareTo(line.getM());
}
}
public Segment getSegment(Interval interval) {
double xLow = interval.getLower();
double yLow = Double.NEGATIVE_INFINITY;
double xHigh = interval.getUpper();
double yHigh = Double.POSITIVE_INFINITY;
if (interval.getLower() > Double.MIN_VALUE && !Double.isNaN((interval.getLower() * m + b)) && !Double.isInfinite(interval.getLower() * m + b)) {
yLow = interval.getLower() * m + b;
}
if (interval.getUpper() < Double.MAX_VALUE && !Double.isNaN(interval.getUpper() * m + b) && !Double.isInfinite(interval.getUpper() * m + b) ) {
yHigh = interval.getUpper() * m + b;
}
this.segment = new Segment(new Point(xLow, yLow), new Point(xHigh, yHigh));
return this.segment;
}
public Segment getSegment() {
this.segment = new Segment(new Point(x1, y1), new Point(x2, y2));
return this.segment;
}
public double getX1() {
return x1;
}
public double getX2() {
return x2;
}
public double getY1() {
return y1;
}
public double getY2() {
return y2;
}
public static class Segment {
private Point p_1;
private Point p_2;
double value;
Segment(Point p_1, Point p_2) {
this.p_1 = p_1;
this.p_2 = p_2;
this.calculate_value(this.first().getX());
}
public Point first() {
if(p_1.getX() <= p_2.getX()) {
return p_1;
} else {
return p_2;
}
}
public Point second() {
if(p_1.getX() <= p_2.getX()) {
return p_2;
} else {
return p_1;
}
}
public void calculate_value(double value) {
double x1 = this.first().getX();
double x2 = this.second().getX();
double y1 = this.first().getY();
double y2 = this.second().getY();
this.value = y1 + (((y2 - y1) / (x2 - x1)) * (value - x1));
}
public void set_value(double value) {
this.value = value;
}
public double get_value() {
return this.value;
}
}
}