188 lines
5.2 KiB
Java
188 lines
5.2 KiB
Java
package Presenter;
|
|
|
|
import Model.Arrangement;
|
|
import Model.Line;
|
|
import Model.Point;
|
|
import Presenter.Algorithms.LeastMedianOfSquaresEstimator;
|
|
import View.MainFrame;
|
|
|
|
import javax.swing.*;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.Observable;
|
|
import java.util.Observer;
|
|
|
|
|
|
/**
|
|
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
|
|
*
|
|
* @Author: Armin Wolf
|
|
* @Email: a_wolf28@uni-muenster.de
|
|
* @Date: 28.05.2017.
|
|
*/
|
|
public class Presenter implements Observer {
|
|
|
|
private Arrangement model;
|
|
private MainFrame view;
|
|
|
|
private LeastMedianOfSquaresEstimator lms;
|
|
|
|
private Double max;
|
|
private Double min;
|
|
|
|
public Presenter(Arrangement model, MainFrame view) {
|
|
this.model = model;
|
|
this.view = view;
|
|
// Double[] x = {1d, 2d, 3d, 4d, 10d, 12d, 18d};
|
|
// Double[] y = {9d, 15d, 19d, 20d, 45d, 55d, 78d};
|
|
Double[] x = {18d,24d,30d,34d,38d};
|
|
Double[] y = {18d,26d,30d,40d,70d};
|
|
// Double[] x = {1d,3d,4d,5d,8d};
|
|
// Double[] y = {4d,2d,1d,0d,0d};
|
|
view.logHeading("Duale Darstellung der Punkte als Geraden:");
|
|
for (int j = 0; j < x.length; j++) {
|
|
Line p = new Line(x[j], y[j]);
|
|
view.log("f(x) = " + p.getM() + "x + " + p.getB());
|
|
this.model.addLine(p);
|
|
}
|
|
|
|
calcArrangementNodes();
|
|
//print
|
|
List<String> heading = new LinkedList<>();
|
|
List<List<String>> rows = new LinkedList<>();
|
|
heading.add("X - Koordinate");
|
|
heading.add("Y - Koordinate");
|
|
for (Point p : model.getNodes()) {
|
|
LinkedList<String> rowEntry = new LinkedList<>();
|
|
rowEntry.add(p.getX().toString());
|
|
rowEntry.add(p.getY().toString());
|
|
rows.add(rowEntry);
|
|
}
|
|
view.logHeading("Schnittpunkte der Dualen Geraden:");
|
|
view.createTable(heading, rows);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
public void update(Observable o, Object arg) {
|
|
Line result = ((Line) arg);
|
|
SwingUtilities.invokeLater(()->{
|
|
getView().createPlot(result.getM(), result.getB());
|
|
getView().setLmsIsComplete(true);
|
|
getView().logSuccess("Berechnung wurde Erfolgreich durchgeführt");
|
|
getView().log("m: "+result.getM()+"\t b: "+result.getB());
|
|
});
|
|
|
|
}
|
|
|
|
|
|
public void startArrangementVisualization() {
|
|
view.createArrangement();
|
|
}
|
|
|
|
public void startScatterPlotVisualization(String[] input) {
|
|
Double constant = Double.parseDouble(input[0]);
|
|
Double error = Double.parseDouble(input[1]);
|
|
lms = new LeastMedianOfSquaresEstimator(model.getLines(), model.getNodes(), this);
|
|
lms.setConstant(constant);
|
|
lms.setQuantileError(error);
|
|
lms.addObserver(this);
|
|
lms.run();
|
|
}
|
|
|
|
|
|
public Point calcIntersection(Line a, Line b) {
|
|
Line p1 = a;
|
|
Line p2 = b;
|
|
|
|
Double x = (p1.getB() - p2.getB()) / (p2.getM() - p1.getM());
|
|
Double y = ((p1.getM() * p2.getB()) - (p2.getM() * p1.getB())) / (p1.getM() - p2.getM());
|
|
|
|
return new Point(x, y);
|
|
}
|
|
|
|
public void calcArrangementNodes() {
|
|
try {
|
|
Thread thread = new Thread(() -> {
|
|
for (int i = 0; i < getLines().size(); i++) {
|
|
for (int j = i; j < getLines().size(); j++) {
|
|
if (i != j){
|
|
model.addNode(calcIntersection(getLines().get(j), getLines().get(i)));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
thread.start();
|
|
thread.join();
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public LinkedList<LinkedList<Point>> calcArrangementLines(){
|
|
LinkedList<LinkedList<Point>> lineCoordinates = new LinkedList<>();
|
|
double x1 = -1000;
|
|
double x2 = 1000;
|
|
|
|
|
|
for (Line point : model.getLines()) {
|
|
LinkedList line = new LinkedList();
|
|
double y1 = (point.getM() * x1 + point.getB());
|
|
double y2 = (point.getM() * x2 + point.getB());
|
|
line.add(new Point(x1,y1));
|
|
line.add(new Point(x2,y2));
|
|
lineCoordinates.add(line);
|
|
}
|
|
|
|
return lineCoordinates;
|
|
}
|
|
/***************************************************************************************************************************
|
|
* Getter und Setter Methoden
|
|
***************************************************************************************************************************/
|
|
|
|
|
|
public Arrangement getModel() {
|
|
return model;
|
|
}
|
|
|
|
public void setModel(Arrangement model) {
|
|
this.model = model;
|
|
}
|
|
|
|
public MainFrame getView() {
|
|
return view;
|
|
}
|
|
|
|
public void setView(MainFrame view) {
|
|
this.view = view;
|
|
}
|
|
|
|
public LinkedList<Line> getLines() {
|
|
return this.model.getLines();
|
|
}
|
|
|
|
public void setLines(LinkedList<Line> lines) {
|
|
this.model.setLines(lines);
|
|
}
|
|
|
|
public Double getMax() {
|
|
return max;
|
|
}
|
|
|
|
public void setMax(Double max) {
|
|
this.max = max;
|
|
}
|
|
|
|
public Double getMin() {
|
|
return min;
|
|
}
|
|
|
|
public void setMin(Double min) {
|
|
this.min = min;
|
|
}
|
|
}
|