Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package main;
  2.  
  3. import javafx.application.Application;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.canvas.Canvas;
  7. import javafx.scene.canvas.GraphicsContext;
  8. import javafx.stage.Stage;
  9.  
  10. public class Turtle extends Application {
  11.  
  12. double angle = 90;
  13. double oldX = 0;
  14. double oldY = 300;
  15. double x = 0;
  16. double y = 300;
  17.  
  18. GraphicsContext gc;
  19.  
  20. public static void main(String[] args) {
  21. launch(args);
  22. }
  23.  
  24. @Override
  25. public void start(Stage primaryStage) {
  26. primaryStage.setTitle("Schildkroeten und Fraktale <3");
  27. Group root = new Group();
  28. Canvas canvas = new Canvas(600, 600);
  29. gc = canvas.getGraphicsContext2D();
  30. root.getChildren().add(canvas);
  31. primaryStage.setScene(new Scene(root));
  32. primaryStage.show();
  33.  
  34. //hier Methoden aufrufen
  35. koch(600,5);
  36.  
  37. }
  38.  
  39. private void _zeroKoch(double length){
  40. forward(length / 3);
  41. turn(-60);
  42. forward(length / 3);
  43. turn(120);
  44. forward(length / 3);
  45. turn(-60);
  46. forward(length / 3);
  47. }
  48.  
  49. public void koch(double length, int depth){
  50. if(depth == 1) {
  51. _zeroKoch(length);
  52. return;
  53. }
  54.  
  55. koch(length / 3, depth - 1);
  56. turn(-60);
  57. koch(length / 3, depth - 1);
  58. turn(120);
  59. koch(length / 3, depth - 1);
  60. turn(-60);
  61. koch(length / 3, depth - 1);
  62. }
  63.  
  64. public void turn( double degree ){
  65. if ( (angle += degree) > 360 )
  66. angle %= 360;
  67. }
  68.  
  69. public void forward( double step ){
  70. oldX = x;
  71. oldY = y;
  72.  
  73. x += step * Math.sin( Math.toRadians( angle ) );
  74. y -= step * Math.cos( Math.toRadians( angle ) );
  75.  
  76. gc.strokeLine( (int) x, (int) y, (int) oldX, (int) oldY );
  77. }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement