Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

128
LINES

< > BotCompany Repo | #1018239 // Penrose Tiling (OK)

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (5071L/36K).

!7

import static java.lang.Math.*;
import java.awt.geom.*;

p-substance {
  showPackedFrame(new PenroseTiling);
  hideConsole();
}

sclass PenroseTiling extends JPanel {
  // ignores missing hash code
  class Tile {
      double x, y, angle, size;
      Type type;

      Tile(Type t, double x, double y, double a, double s) {
          type = t;
          this.x = x;
          this.y = y;
          angle = a;
          size = s;
      }

      @Override
      public boolean equals(Object o) {
          if (o instanceof Tile) {
              Tile t = (Tile) o;
              return type == t.type && x == t.x && y == t.y && angle == t.angle;
          }
          return false;
      }
  }

  enum Type {
      Kite, Dart
  }

  static final double G = (1 + sqrt(5)) / 2; // golden ratio
  static final double T = toRadians(36); // theta

  new L<Tile> tiles;

  *() {
    int w = 700, h = 450;
    setPreferredSize(new Dimension(w, h));
    setBackground(Color.white);

    tiles = deflateTiles(setupPrototiles(w, h), 5);
  }

  L<Tile> setupPrototiles(int w, int h) {
    new L<Tile> proto;

    // sun
    for (double a = PI / 2 + T; a < 3 * PI; a += 2 * T)
        proto.add(new Tile(Type.Kite, w / 2, h / 2, a, w / 2.5));

    ret proto;
  }

  List<Tile> deflateTiles(List<Tile> tls, int generation) {
      if (generation <= 0)
          return tls;

      List<Tile> next = new ArrayList<>();

      for (Tile tile : tls) {
          double x = tile.x, y = tile.y, a = tile.angle, nx, ny;
          double size = tile.size / G;

          if (tile.type == Type.Dart) {
              next.add(new Tile(Type.Kite, x, y, a + 5 * T, size));

              for (int i = 0, sign = 1; i < 2; i++, sign *= -1) {
                  nx = x + cos(a - 4 * T * sign) * G * tile.size;
                  ny = y - sin(a - 4 * T * sign) * G * tile.size;
                  next.add(new Tile(Type.Dart, nx, ny, a - 4 * T * sign, size));
              }

          } else {

              for (int i = 0, sign = 1; i < 2; i++, sign *= -1) {
                  next.add(new Tile(Type.Dart, x, y, a - 4 * T * sign, size));

                  nx = x + cos(a - T * sign) * G * tile.size;
                  ny = y - sin(a - T * sign) * G * tile.size;
                  next.add(new Tile(Type.Kite, nx, ny, a + 3 * T * sign, size));
              }
          }
      }
      // remove duplicates
      tls = uniquify(next);

      return deflateTiles(tls, generation - 1);
  }

  void drawTiles(Graphics2D g) {
      double[][] dist = {{G, G, G}, {-G, -1, -G}};
      for (Tile tile : tiles) {
          double angle = tile.angle - T;
          Path2D path = new Path2D.Double();
          path.moveTo(tile.x, tile.y);

          int ord = tile.type.ordinal();
          for (int i = 0; i < 3; i++) {
              double x = tile.x + dist[ord][i] * tile.size * cos(angle);
              double y = tile.y - dist[ord][i] * tile.size * sin(angle);
              path.lineTo(x, y);
              angle += T;
          }
          path.closePath();
          g.setColor(ord == 0 ? Color.orange : Color.yellow);
          g.fill(path);
          g.setColor(Color.darkGray);
          g.draw(path);
      }
  }

  @Override
  public void paintComponent(Graphics og) {
      super.paintComponent(og);
      Graphics2D g = (Graphics2D) og;
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
      drawTiles(g);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

1 comment(s) hidden. show

Snippet ID: #1018239
Snippet name: Penrose Tiling (OK)
Eternal ID of this version: #1018239/6
Text MD5: 41604ed531381b7e96ce768884bac0cd
Transpilation MD5: 95e60fe0d4b7e778f87a66bfae5813cb
Author: stefan
Category: javax / gui
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-11 21:40:02
Source code size: 3578 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 668 / 1460
Version history: 5 change(s)
Referenced in: [show references]