algorithms-for-computing-li.../src/main/java/Presenter/Presenter.java

212 lines
5.8 KiB
Java

package Presenter;
import Model.Arrangement;
import Model.Coordinates;
import View.MainFrame;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
*
* @Author: Armin Wolf
* @Email: a_wolf28@uni-muenster.de
* @Date: 28.05.2017.
*/
public class Presenter {
private Arrangement model;
private MainFrame view;
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};
// Float[] x = {18f,24f,30f,34f,38f};
// Float[] y = {18f,26f,30f,40f,70f};
// Double[] x = {1d,3d,4d,5d,8d};
// Double[] y = {4d,2d,1d,0d,0d};
view.logHeading("Dualen Geraden");
for (int j = 0; j < 7; j++) {
Coordinates p = new Coordinates(x[j], y[j]);
view.log("f(x) = " + p.getX() + "x + " + p.getY());
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 (Coordinates p : model.getNodes()) {
LinkedList<String> rowEntry = new LinkedList<>();
rowEntry.add(p.getX().toString());
rowEntry.add(p.getY().toString());
rows.add(rowEntry);
}
view.logHeading("Koordinaten der Punkte");
view.createTable(heading, rows);
view.logSuccess("Berechnung wurde Erfolgreich durchgeführt");
}
public void getDataFromModel() {
}
public void setDataByModel() {
}
public void getActionsFromView() {
}
public void startArrangementVisualization() {
view.createArrangement();
}
public void startScatterPlotVisualization() {
view.createPlot();
}
private void extractBounds() {
// Coordinates pmax = Collections.max(model.getLines());
// Coordinates pmin = Collections.min(model.getLines());
//
// max = pmax.getX() >= pmax.getY() ? pmax.getX() : pmax.getY();
// min = pmin.getX() <= pmin.getY() ? pmin.getX() : pmin.getY();
LinkedList<Double> xCoordinates = new LinkedList<>();
LinkedList<Double> yCoordinates = new LinkedList<>();
for (Coordinates point : model.getNodes()) {
xCoordinates.add(point.getX());
yCoordinates.add(point.getY());
System.out.println(point.getX() + ", " + point.getY());
}
Collections.sort(xCoordinates);
Collections.sort(yCoordinates);
double xmin, xmax;
double ymin, ymax;
xmin = xCoordinates.getFirst();
xmax = xCoordinates.getLast();
ymin = yCoordinates.getFirst();
ymax = yCoordinates.getLast();
System.out.println("x: "+xmin+", "+xmax+"\t"+ymin+", "+ymax);
for (Coordinates p : model.getLines()) {
p.setX(scale(p.getX(), xmin, xmax, 0, 100));
p.setY(scale(p.getY(), ymin, ymax, 0, 100));
view.log(p.getX() + ", "+p.getY());
}
}
public double scale(
double x,
double old_min, double old_max,
double new_min, double new_max) {
double old_range = Math.abs(old_max - old_min);
double new_range = Math.abs(new_max - new_min);
return (new_min + x - old_min) * (new_range / old_range);
}
public Coordinates calcIntersection(Coordinates a, Coordinates b) {
Coordinates p1;
Coordinates p2;
if (a.compareTo(b) > 0) {
p1 = a;
p2 = b;
} else {
p1 = b;
p2 = a;
}
Double x = (p1.getY() - p2.getY()) / (p2.getX() - p1.getX());
Double y = ((p1.getX() * p2.getY()) - (p2.getX() * p1.getY())) / (p1.getX() - p2.getX());
System.out.printf("RESULT: (%3.3f, %3.3f)\n", x, y);
return new Coordinates(x, y);
}
public void calcArrangementNodes() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
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)));
}
}
extractBounds();
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/***************************************************************************************************************************
* 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<Coordinates> getLines() {
return this.model.getLines();
}
public void setLines(LinkedList<Coordinates> 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;
}
}