Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.6 kB
1
Indexable
Never
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class LagrangeInterpolation extends Application {

    @Override
    public void start(Stage primaryStage) {
        HBox hbox = new HBox();
        VBox vbox = new VBox();
        hbox.setPadding(new Insets(10));
        hbox.setSpacing(10);
        vbox.setSpacing(10);

        TextField x0Field = new TextField();
        TextField y0Field = new TextField();
        TextField x1Field = new TextField();
        TextField y1Field = new TextField();
        TextField x2Field = new TextField();
        TextField y2Field = new TextField();

        Label x0Label = new Label("x0:");
        Label y0Label = new Label("y0:");
        Label x1Label = new Label("x1:");
        Label y1Label = new Label("y1:");
        Label x2Label = new Label("x2:");
        Label y2Label = new Label("y2:");

        hbox.getChildren().addAll(x0Label, x0Field, y0Label, y0Field);
        hbox.getChildren().addAll(x1Label, x1Field, y1Label, y1Field);
        hbox.getChildren().addAll(x2Label, x2Field, y2Label, y2Field);

        Button calculateButton = new Button("Calculate");
        hbox.getChildren().add(calculateButton);

        LineChart<Number, Number> lineChart = new LineChart<>(new NumberAxis(), new NumberAxis());
        lineChart.setPrefSize(600, 400);
        lineChart.setCreateSymbols(false);
        lineChart.setAnimated(false);

        calculateButton.setOnAction(event -> {
            double x0 = Double.parseDouble(x0Field.getText());
            double y0 = Double.parseDouble(y0Field.getText());
            double x1 = Double.parseDouble(x1Field.getText());
            double y1 = Double.parseDouble(y1Field.getText());
            double x2 = Double.parseDouble(x2Field.getText());
            double y2 = Double.parseDouble(y2Field.getText());

            lineChart.getData().clear();

            List<Double> xValues = List.of(x0, x1, x2);
            List<Double> yValues = List.of(y0, y1, y2);

            XYChart.Series<Number, Number> series = new XYChart.Series<>();
            series.setName("Lagrange Polynomial");

            for (double x = -10; x <= 10; x += 0.1) {
                double y = 0;
                for (int i = 0; i < xValues.size(); i++) {
                    double numerator = 1;
                    double denominator = 1;
                    for (int j = 0; j < xValues.size(); j++) {
                        if (j != i) {
                            numerator *= x - xValues.get(j);
                            denominator *= xValues.get(i) - xValues.get(j);
                        }
                    }
                    y += numerator / denominator * yValues.get(i);
                }
                series.getData().add(new XYChart.Data<>(x, y));
            }

            lineChart.getData().add(series);
        });

        vbox.getChildren().addAll(hbox, lineChart);

        Scene scene = new Scene(vbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Leave a Comment