Advertisement
pteacher

Fractals Tree and Serpinski triangle

Feb 22nd, 2022
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1.     public void draw() {
  2.         // drawChessBoard(20, 20);
  3.         // drawStar1(200, 8);
  4.         // drawStar2(50, 8);
  5.         // drawStar3(50, 10);
  6.         // serpinski(width / 2, height - 100, 600);
  7.         tree(width / 2, height - 100, 200);
  8.     }
  9.  
  10.     public void serpinski(float x, float y, int side) {
  11.         // TODO: Change the code so it will draw a Serpinski Carpet
  12.         //  https://media.springernature.com/lw685/springer-static/image/art%3A10.1007%2Fs00209-019-02319-4/MediaObjects/209_2019_2319_Fig1_HTML.png
  13.         line(x - side / 2, y, x + side / 2, y);
  14.         line(x - side / 2, y, x, (float) (y -  Math.sqrt(3/2) * side));
  15.         line(x + side / 2, y, x, (float) (y -  Math.sqrt(3/2) * side));
  16.         if (side > 10) {
  17.             serpinski(x - side / 4, y, side / 2);
  18.             serpinski(x + side / 4, y, side / 2);
  19.             serpinski(x, (int) (y -  (Math.sqrt(3/2) * side) / 2), side / 2);
  20.         }
  21.     }
  22.  
  23.     public void tree(float x, float y, int size) {
  24.         // TODO: Change the code so it will look as http://www.lukaszkroenke.net/content/tree_simple.png
  25.         line(x, y, x, y - size);
  26.         line(x, y - size, x + (float) Math.cos(-45 * Math.PI / 180) * size, y - size + (float) Math.sin(-45 * Math.PI / 180) * size);
  27.         line(x, y - size, x + (float) Math.cos(-135 * Math.PI / 180) * size,y - size + (float) Math.sin(-135 * Math.PI / 180) * size);
  28.         if (size > 10) {
  29.             tree(x + (float) Math.cos(-45 * Math.PI / 180) * size, y - size + (float) Math.sin(-45 * Math.PI / 180) * size, size / 2);
  30.             tree(x + (float) Math.cos(-135 * Math.PI / 180) * size,y - size + (float) Math.sin(-135 * Math.PI / 180) * size, size / 2);
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement