Was macht diese Methode?

bob651

Aktives Mitglied
Jo.
Ich habe eine Methode, die einen Graphen erzeugt, nur verstehe ich nicht, wie genau das funktioniert?
Code:
public void sinus(double g) {
if(i > 600)
            return;
        t0 = f * t1 - t2;
        t2 = t1;
        t1 = t0;
        i++;
        //f -= 0.01;
        sinus(g);

Also t0=f * t1 - t2;
t2=t1, wobei t1 t0 ist..
Verstehe ich nicht. Soll das eine Rekursion darstellen? Bzw es ist ja eig eine.
Ich verstehe die Rolle von t2 nicht. Könnte man es nicht weglassen?
Wie entsteht hier überhaupt ein Graph? Die Formel der Sinusfunktion beinhaltet ja eig. Sin(x).
Hmm, könnt ihr mir helfen?
Bin noch ein Noob 🙂
 
also es wird eine sinuskurve erzeugt. hier der gesamte code:
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class Aufgabe2 extends Application {

    private Group root;
   
    int cursorX = 0;
    int cursorY = 200;
    double z0;
    double z1=5;    // Höhe/Tiefe des Graphen || 2= wenig, 20= viel
    double z2=0;  // ebenfalls Höhe/Tiefe des Graphen, da z2 ja z1 ist, negativ dasselbe ||z2 "modifiziert" z1, siehe z0 funktion
    int i = 0;
   
   
    public void sinus(double f) {
       
        if(i > 600)
            return;
        z0 = f * z1 - z2;
        z2 = z1;
        z1 = z0;
       
        //System.out.println(i + " " + (int)z0 );
        drawLine(i, (int)z0) ;
        i++;
        //f -= 1; ???
        sinus(f);
    }
   
    public static void main(String[] args)  {   
   
        launch(args);
       
    }

    void drawLine(int x, int y) {
        int endX =  x;
        int endY =  200 - y;
        Line line = new Line(cursorX, cursorY, endX , endY);
        cursorX = endX;
        cursorY = endY;
        line.setStroke(Color.RED);
        root.getChildren().add(line);   
    }
   
    @Override
    public void start(Stage primaryStage) throws Exception {
        // Interface
        root = new Group();
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Sinusfunktion");
       
        Rectangle panel = new Rectangle(600, 400, Color.WHITESMOKE);
        root.getChildren().add(panel);
        primaryStage.show();       
   
        sinus(1.99);
    }
}
 
Keine Ahnung was für eine Formel für den Sinus hier benutzt wird aber man hätte das auch viel einfacher iterativ machen können, das tut das gleiche:

Code:
    public void sinus(double f) {
         
        while(i < 600) {
          z0 = f * z1 - z2;
          z2 = z1;
          z1 = z0;
      
          drawLine(i, (int)z0) ;
          i++;
      }
   }
 
nichts, was anderen nicht schon ausprobiert hätten. probiere das:
http://stackoverflow.com/questions/19914476/plot-the-sine-and-cosine-functions
Und so schaut das dann:
i2vSg.png

(Oh, sehe gerade, dass das nicht JavaFX ist...das lässt sich natürlich transferieren)
 

Zurück
Oben