Skip to content

Commit fbbc851

Browse files
authored
Add files via upload
1 parent 077c7eb commit fbbc851

File tree

5 files changed

+516
-516
lines changed

5 files changed

+516
-516
lines changed
Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
1-
package aima.gui.fx.views;
2-
3-
import java.util.Optional;
4-
import java.util.function.Function;
5-
6-
import javafx.scene.canvas.Canvas;
7-
import javafx.scene.canvas.GraphicsContext;
8-
import javafx.scene.paint.Color;
9-
import javafx.scene.paint.Paint;
10-
11-
/**
12-
* Controller class which provides functionality for using a canvas as function
13-
* plotter.
14-
*
15-
* @author Ruediger Lunde
16-
*/
17-
public class FunctionPlotterCtrl {
18-
private Canvas canvas;
19-
private Optional<Function<Double, Double>> function = Optional.empty();
20-
private double minX;
21-
private double maxX = 1;
22-
private double minY;
23-
private double maxY = 1;
24-
25-
public FunctionPlotterCtrl(Canvas canvas) {
26-
this.canvas = canvas;
27-
canvas.widthProperty().addListener((obs, o, n) -> update());
28-
canvas.heightProperty().addListener((obs, o, n) -> update());
29-
}
30-
31-
public Optional<Function<Double, Double>> getFunction() {
32-
return function;
33-
}
34-
35-
public void setFunction(Function<Double, Double> function) {
36-
this.function = Optional.of(function);
37-
update();
38-
}
39-
40-
public void setLimits(double minX, double maxX, double minY, double maxY) {
41-
this.minX = minX;
42-
this.minY = minY;
43-
this.maxX = maxX;
44-
this.maxY = maxY;
45-
46-
}
47-
48-
public void update() {
49-
GraphicsContext gc = canvas.getGraphicsContext2D();
50-
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
51-
52-
gc.setStroke(Color.GRAY);
53-
gc.strokeLine(0.01 * canvas.getWidth(), yToScreen(0), canvas.getWidth() * 0.99, yToScreen(0));
54-
gc.strokeLine(xToScreen(0), 0.01 * canvas.getHeight(), xToScreen(0), 0.99 * canvas.getHeight());
55-
gc.strokeLine(xToScreen(1), yToScreen(0) - 5, xToScreen(1), yToScreen(0) + 5);
56-
gc.strokeLine(xToScreen(0) - 5, yToScreen(1), xToScreen(0) + 5, yToScreen(1));
57-
58-
if (function.isPresent()) {
59-
gc.setStroke(Color.BLACK);
60-
double lastVal = function.get().apply(minX);
61-
for (int i = 1; i < canvas.getWidth(); i++) {
62-
double newVal = function.get().apply(screenToX(i));
63-
gc.strokeLine(i - 1, yToScreen(lastVal), i, yToScreen(newVal));
64-
lastVal = newVal;
65-
}
66-
}
67-
}
68-
69-
public void setMarker(double x, Optional<Paint> fill) {
70-
if (function.isPresent()) {
71-
double y = function.get().apply(x);
72-
GraphicsContext gc = canvas.getGraphicsContext2D();
73-
gc.setStroke(Color.RED);
74-
if (fill.isPresent()) {
75-
gc.setFill(fill.get());
76-
gc.fillOval(xToScreen(x) - 10, yToScreen(y) - 10, 20, 20);
77-
} else
78-
gc.strokeOval(xToScreen(x) - 10, yToScreen(y) - 10, 20, 20);
79-
}
80-
}
81-
82-
/* Returns pixel width. */
83-
public double getDeltaX() {
84-
return (maxX - minX) / canvas.getWidth();
85-
}
86-
87-
private double yToScreen(double y) {
88-
return (1.1 - (y - minY) / (maxY - minY)) * canvas.getHeight() * 0.8;
89-
}
90-
91-
private double screenToX(double xScreen) {
92-
return xScreen / canvas.getWidth() * (maxX - minX) + minX;
93-
}
94-
95-
private double xToScreen(double x) {
96-
return (x - minX) * canvas.getWidth() / (maxX - minX);
97-
}
98-
}
1+
package aima.gui.fx.views;
2+
3+
import java.util.Optional;
4+
import java.util.function.Function;
5+
6+
import javafx.scene.canvas.Canvas;
7+
import javafx.scene.canvas.GraphicsContext;
8+
import javafx.scene.paint.Color;
9+
import javafx.scene.paint.Paint;
10+
11+
/**
12+
* Controller class which provides functionality for using a canvas as function
13+
* plotter.
14+
*
15+
* @author Ruediger Lunde
16+
*/
17+
public class FunctionPlotterCtrl {
18+
private Canvas canvas;
19+
private Optional<Function<Double, Double>> function = Optional.empty();
20+
private double minX;
21+
private double maxX = 1;
22+
private double minY;
23+
private double maxY = 1;
24+
25+
public FunctionPlotterCtrl(Canvas canvas) {
26+
this.canvas = canvas;
27+
canvas.widthProperty().addListener((obs, o, n) -> update());
28+
canvas.heightProperty().addListener((obs, o, n) -> update());
29+
}
30+
31+
public Optional<Function<Double, Double>> getFunction() {
32+
return function;
33+
}
34+
35+
public void setFunction(Function<Double, Double> function) {
36+
this.function = Optional.of(function);
37+
update();
38+
}
39+
40+
public void setLimits(double minX, double maxX, double minY, double maxY) {
41+
this.minX = minX;
42+
this.minY = minY;
43+
this.maxX = maxX;
44+
this.maxY = maxY;
45+
46+
}
47+
48+
public void update() {
49+
GraphicsContext gc = canvas.getGraphicsContext2D();
50+
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
51+
52+
gc.setStroke(Color.GRAY);
53+
gc.strokeLine(0.01 * canvas.getWidth(), yToScreen(0), canvas.getWidth() * 0.99, yToScreen(0));
54+
gc.strokeLine(xToScreen(0), 0.01 * canvas.getHeight(), xToScreen(0), 0.99 * canvas.getHeight());
55+
gc.strokeLine(xToScreen(1), yToScreen(0) - 5, xToScreen(1), yToScreen(0) + 5);
56+
gc.strokeLine(xToScreen(0) - 5, yToScreen(1), xToScreen(0) + 5, yToScreen(1));
57+
58+
if (function.isPresent()) {
59+
gc.setStroke(Color.BLACK);
60+
double lastVal = function.get().apply(minX);
61+
for (int i = 1; i < canvas.getWidth(); i++) {
62+
double newVal = function.get().apply(screenToX(i));
63+
gc.strokeLine(i - 1, yToScreen(lastVal), i, yToScreen(newVal));
64+
lastVal = newVal;
65+
}
66+
}
67+
}
68+
69+
public void setMarker(double x, Optional<Paint> fill) {
70+
if (function.isPresent()) {
71+
double y = function.get().apply(x);
72+
GraphicsContext gc = canvas.getGraphicsContext2D();
73+
gc.setStroke(Color.RED);
74+
if (fill.isPresent()) {
75+
gc.setFill(fill.get());
76+
gc.fillOval(xToScreen(x) - 10, yToScreen(y) - 10, 20, 20);
77+
} else
78+
gc.strokeOval(xToScreen(x) - 10, yToScreen(y) - 10, 20, 20);
79+
}
80+
}
81+
82+
/* Returns pixel width. */
83+
public double getDeltaX() {
84+
return (maxX - minX) / canvas.getWidth();
85+
}
86+
87+
private double yToScreen(double y) {
88+
return (1.1 - (y - minY) / (maxY - minY)) * canvas.getHeight() * 0.8;
89+
}
90+
91+
private double screenToX(double xScreen) {
92+
return xScreen / canvas.getWidth() * (maxX - minX) + minX;
93+
}
94+
95+
private double xToScreen(double x) {
96+
return (x - minX) * canvas.getWidth() / (maxX - minX);
97+
}
98+
}

0 commit comments

Comments
 (0)