Dzień dobry,
Piszę Sobie program w celach edukacyjnych, który będzie rysował różnego rodzaju wykresy, ówcześnie rozwiązanych równań.
Doszedłem do momentu rysowania, lecz linie nie są "smooth" wyczytałem, że trzeba zastosować krzywe Beziera, lecz w JavieFX jest to bardziej skomplikowane, pewnie tylko mi się wydaje. Wywołując konstruktor :
path.getElements().add(new CubicCurveTo(
firstControlpointX, firstControlpointY,
secondControlPointX, secondControlPointY,
endPointX, endPointY
));
Nie wiem za bardzo jak wyznaczyć punkty kontrolne. Za wszelką pomoc będę mega wdzięczny.
Załączam również kod całego programu.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class MainClass extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(final Stage stage) {
Axes axes = new Axes(1000, 1000,
-1, 1, 0.1, -1, 1, 0.1);
Chart chart = new Chart(x -> x = x * 2*x,
-1, 1, 0.1, axes);
StackPane layout = new StackPane(
chart
);
layout.setPadding(new Insets(20));
layout.setStyle("-fx-background-color: rgb(35, 39, 50);");
stage.setScene(new Scene(layout, Color.rgb(35, 39, 50)));
stage.show();
}
}
import javafx.beans.binding.Bindings;
import javafx.geometry.Side;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.Pane;
public class Axes extends Pane{
private NumberAxis xAxis;
private NumberAxis yAxis;
public Axes(int width, int height,
double xMin, double xMax, double xTickUnit,
double yMin, double yMax, double yTickUnit) {
setMinSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
setPrefSize(width, height);
setMaxSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
xAxis = new NumberAxis(xMin, xMax, xTickUnit);
xAxis.setSide(Side.BOTTOM);
xAxis.setMinorTickVisible(false);
xAxis.setPrefWidth(width);
xAxis.setLayoutY(height/2);
yAxis = new NumberAxis(yMin, yMax, yTickUnit);
yAxis.setSide(Side.LEFT);
yAxis.setMinorTickVisible(false);
yAxis.setPrefHeight(height);
yAxis.layoutXProperty().bind(Bindings.subtract((width / 2) + 1, yAxis.widthProperty()));
getChildren().setAll(xAxis, yAxis);
}
public NumberAxis getXAxis() {
return xAxis;
}
public NumberAxis getYAxis() {
return yAxis;
}
}
import java.util.function.Function;
import javafx.geometry.Point2D;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurveTo;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
public class Chart extends Pane {
public Chart(Function<Double, Double> f,
double xMin, double xMax, double xInc,
Axes axes ) {
Path path = new Path();
path.setStroke(Color.ORANGE.deriveColor(0, 1, 1,0.5));
path.setStrokeWidth(1);
path.setClip(new Rectangle(0, 0, axes.getPrefWidth(), axes.getPrefHeight()));
double x = xMin;
double y = f.apply(x);
path.getElements().add(new MoveTo(
mapX(x, axes), mapY(y, axes)
));
x+=xInc;
while(x < xMax) {
y = f.apply(x);
path.getElements().add(new LineTo(
mapX(x, axes), mapY(y, axes)
));
x+=xInc;
}
setMinSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
setPrefSize(axes.getPrefWidth(), axes.getPrefHeight());
setMaxSize(Pane.USE_PREF_SIZE, Pane.USE_PREF_SIZE);
getChildren().setAll(axes, path);
}
private double mapX(double x, Axes axes) {
double fx = axes.getPrefWidth() / 2;
double sx = axes.getPrefWidth() /
(axes.getXAxis().getUpperBound() - axes.getXAxis().getLowerBound());
return x * fx + sx;
}
private double mapY(double y, Axes axes) {
double fy = axes.getPrefHeight() / 2;
double sy = axes.getPrefHeight() /
(axes.getYAxis().getUpperBound() - axes.getYAxis().getLowerBound());
return -y * sy + fy;
}
}