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

192 lines
3.9 KiB
Java

package de.wwwu.awolf.model;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 12.06.2017.
*/
public class Line {
private final Double MAX = 9999d;
private final Double MIN = -9999d;
private Double m;
private Double b;
private Double x1;
private Double x2;
private Double y1;
private Double y2;
private String id;
/**
* Konstruktor
*
* @param m Steigung
* @param b y-Achsenabschnitt
* @param id id
*/
public Line(double m, double b, String id) {
this.m = m;
this.b = b;
this.x1 = MIN;
this.y1 = (MIN * m) + b;
this.x2 = MAX * 0.5;
this.y2 = ((MAX * 0.5) * m) + b;
this.id = id;
}
/**
* Konstruktor
*
* @param m Steigung
* @param b y-Achsenabschnitt
*/
public Line(double m, double b) {
this.m = m;
this.b = b;
this.x1 = MIN;
this.y1 = (MIN * m) + b;
this.x2 = MAX * 0.5;
this.y2 = ((MAX * 0.5) * m) + 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.x2 = x2;
this.y1 = y1;
this.y2 = y2;
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;
}
/**
* @return x-Koordiante des Startpunkts
*/
public Double getX1() {
return x1;
}
/**
* @return x-Koordiante des Endpunkts
*/
public Double getX2() {
return x2;
}
/**
* @return y-Koordiante des Startpunkts
*/
public Double getY1() {
return y1;
}
/**
* @return y-Koordiante des Endpunkts
*/
public Double getY2() {
return y2;
}
/**
* Setzt die Koordianten des Segments. Aus dem Segment wird eine Gerade berechnet.
*
* @param x1 x-Koordiante des Startpunkts
* @param y1 y-Koordiante des Endpunkts
* @param x2 x-Koordinate des Startpunkts
* @param y2 y-Koordinte des Endpunkts
*/
public void setEndPoints(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.m = (y2 - y1) / (x2 - x1);
this.b = y2 - (x2 * m);
}
/**
* 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 other.getM().equals(this.getM()) && other.getB().equals(this.getB());
} else {
return super.equals(obj);
}
}
@Override
public int hashCode() {
return super.hashCode() + this.getM().hashCode() + this.getB().hashCode();
}
@Override
public String toString() {
return "Line m: " + this.getM() + ", b: " + this.getB();
}
}