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).

1  
!7
2  
3  
import static java.lang.Math.*;
4  
import java.awt.geom.*;
5  
6  
p-substance {
7  
  showPackedFrame(new PenroseTiling);
8  
  hideConsole();
9  
}
10  
11  
sclass PenroseTiling extends JPanel {
12  
  // ignores missing hash code
13  
  class Tile {
14  
      double x, y, angle, size;
15  
      Type type;
16  
17  
      Tile(Type t, double x, double y, double a, double s) {
18  
          type = t;
19  
          this.x = x;
20  
          this.y = y;
21  
          angle = a;
22  
          size = s;
23  
      }
24  
25  
      @Override
26  
      public boolean equals(Object o) {
27  
          if (o instanceof Tile) {
28  
              Tile t = (Tile) o;
29  
              return type == t.type && x == t.x && y == t.y && angle == t.angle;
30  
          }
31  
          return false;
32  
      }
33  
  }
34  
35  
  enum Type {
36  
      Kite, Dart
37  
  }
38  
39  
  static final double G = (1 + sqrt(5)) / 2; // golden ratio
40  
  static final double T = toRadians(36); // theta
41  
42  
  new L<Tile> tiles;
43  
44  
  *() {
45  
    int w = 700, h = 450;
46  
    setPreferredSize(new Dimension(w, h));
47  
    setBackground(Color.white);
48  
49  
    tiles = deflateTiles(setupPrototiles(w, h), 5);
50  
  }
51  
52  
  L<Tile> setupPrototiles(int w, int h) {
53  
    new L<Tile> proto;
54  
55  
    // sun
56  
    for (double a = PI / 2 + T; a < 3 * PI; a += 2 * T)
57  
        proto.add(new Tile(Type.Kite, w / 2, h / 2, a, w / 2.5));
58  
59  
    ret proto;
60  
  }
61  
62  
  List<Tile> deflateTiles(List<Tile> tls, int generation) {
63  
      if (generation <= 0)
64  
          return tls;
65  
66  
      List<Tile> next = new ArrayList<>();
67  
68  
      for (Tile tile : tls) {
69  
          double x = tile.x, y = tile.y, a = tile.angle, nx, ny;
70  
          double size = tile.size / G;
71  
72  
          if (tile.type == Type.Dart) {
73  
              next.add(new Tile(Type.Kite, x, y, a + 5 * T, size));
74  
75  
              for (int i = 0, sign = 1; i < 2; i++, sign *= -1) {
76  
                  nx = x + cos(a - 4 * T * sign) * G * tile.size;
77  
                  ny = y - sin(a - 4 * T * sign) * G * tile.size;
78  
                  next.add(new Tile(Type.Dart, nx, ny, a - 4 * T * sign, size));
79  
              }
80  
81  
          } else {
82  
83  
              for (int i = 0, sign = 1; i < 2; i++, sign *= -1) {
84  
                  next.add(new Tile(Type.Dart, x, y, a - 4 * T * sign, size));
85  
86  
                  nx = x + cos(a - T * sign) * G * tile.size;
87  
                  ny = y - sin(a - T * sign) * G * tile.size;
88  
                  next.add(new Tile(Type.Kite, nx, ny, a + 3 * T * sign, size));
89  
              }
90  
          }
91  
      }
92  
      // remove duplicates
93  
      tls = uniquify(next);
94  
95  
      return deflateTiles(tls, generation - 1);
96  
  }
97  
98  
  void drawTiles(Graphics2D g) {
99  
      double[][] dist = {{G, G, G}, {-G, -1, -G}};
100  
      for (Tile tile : tiles) {
101  
          double angle = tile.angle - T;
102  
          Path2D path = new Path2D.Double();
103  
          path.moveTo(tile.x, tile.y);
104  
105  
          int ord = tile.type.ordinal();
106  
          for (int i = 0; i < 3; i++) {
107  
              double x = tile.x + dist[ord][i] * tile.size * cos(angle);
108  
              double y = tile.y - dist[ord][i] * tile.size * sin(angle);
109  
              path.lineTo(x, y);
110  
              angle += T;
111  
          }
112  
          path.closePath();
113  
          g.setColor(ord == 0 ? Color.orange : Color.yellow);
114  
          g.fill(path);
115  
          g.setColor(Color.darkGray);
116  
          g.draw(path);
117  
      }
118  
  }
119  
120  
  @Override
121  
  public void paintComponent(Graphics og) {
122  
      super.paintComponent(og);
123  
      Graphics2D g = (Graphics2D) og;
124  
      g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
125  
              RenderingHints.VALUE_ANTIALIAS_ON);
126  
      drawTiles(g);
127  
  }
128  
}

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

Comments [hide]

ID Author/Program Comment Date
1375 stefan From https://rosettacode.org/wiki/Penrose_tiling 2018-09-11 21:46:07

add comment

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: 669 / 1464
Version history: 5 change(s)
Referenced in: [show references]