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

265
LINES

< > BotCompany Repo | #1000562 // Make structure (implementation)

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

Libraryless. Click here for Pure Java version (1288L/9K/28K).

1  
!636
2  
!standard functions
3  
!721 // thread {
4  
!* ctr
5  
!quicknew
6  
7  
import static java.lang.Math.*;
8  
9  
!image classes
10  
!FrameWithImages
11  
12  
main {
13  
  !include #1000522 // helper functions
14  
15  
  // St = Structure
16  
  static abstract class St {
17  
    abstract double def(double x, double y);
18  
  }
19  
  
20  
  static class SConst extends St {
21  
    double value;
22  
    
23  
    *(double *value) {}
24  
    
25  
    double def(double x, double y) {
26  
      return value;
27  
    }
28  
  }
29  
  
30  
  static class SX extends St {
31  
    double def(double x, double y) {
32  
      return x;
33  
    }
34  
  }
35  
  
36  
  static class SY extends St {
37  
    double def(double x, double y) {
38  
      return y;
39  
    }
40  
  }
41  
  
42  
  static class SSin extends St {
43  
    St a;
44  
    *(St *a) {}
45  
    
46  
    double def(double x, double y) {
47  
      return sin(a.def(x, y));
48  
    }
49  
  }
50  
  
51  
  static class SCos extends St {
52  
    St a;
53  
    *(St *a) {}
54  
    
55  
    double def(double x, double y) {
56  
      return cos(a.def(x, y));
57  
    }
58  
  }
59  
  
60  
  static class SPlus extends St {
61  
    St a, b;
62  
    *(St *a, St *b) {}
63  
    
64  
    double def(double x, double y) {
65  
      return a.def(x, y) + b.def(x, y);
66  
    }
67  
  }
68  
  
69  
  static class SMinus extends St {
70  
    St a, b;
71  
    *(St *a, St *b) {}
72  
    
73  
    double def(double x, double y) {
74  
      return a.def(x, y) - b.def(x, y);
75  
    }
76  
  }
77  
  
78  
  static class SMul extends St {
79  
    St a, b;
80  
    *(St *a, St *b) {}
81  
    
82  
    double def(double x, double y) {
83  
      return a.def(x, y) * b.def(x, y);
84  
    }
85  
  }
86  
  
87  
  static class SDiv extends St {
88  
    St a, b;
89  
    *(St *a, St *b) {}
90  
    
91  
    double def(double x, double y) {
92  
      return a.def(x, y) / b.def(x, y);
93  
    }
94  
  }
95  
  
96  
  static class SSwitch extends St {
97  
    St a, b, c;
98  
    *(St *a, St *b, St *c) {}
99  
    
100  
    double def(double x, double y) {
101  
      return a.def(x, y) < 0 ? b.def(x, y) : c.def(x, y);
102  
    }
103  
  }
104  
  
105  
  static St randomConst() {
106  
    int n = random(2);
107  
    double d;
108  
    if (n == 0) d = random(-10.0, 10.0);
109  
    else d = random(0.0, 1.0);
110  
    return new SConst(d);
111  
  }
112  
  
113  
  static St makeStructure(int budget) {
114  
    if (budget < 1)
115  
      fail("no budget");
116  
    int n = random(min(3, budget));
117  
    if (n == 0)
118  
      return makeStructure1();
119  
    if (n == 1)
120  
      return makeStructure2(budget);
121  
    return makeStructure3(budget);
122  
  }
123  
  
124  
  static St makeStructure1() {
125  
    int n = random(3);
126  
    if (n == 0) return randomConst();
127  
    else if (n == 1) return new SX();
128  
    else return new SY();
129  
  }
130  
  
131  
  static St makeStructure2(int budget) {
132  
    int n = random(6);
133  
    if (n == 0) return new SSin(makeStructure(budget-1));
134  
    else if (n == 1) return new SCos(makeStructure(budget-1));
135  
    
136  
    // split budget among parts
137  
    int b1 = 1+random(budget-1), b2 = budget-b1;
138  
    //System.out.println((budget+1) + " => " + b1 + "/" + b2);
139  
    
140  
    St a = makeStructure(b1), b = makeStructure(b2);
141  
    if (n == 2) return new SPlus(a, b);
142  
    if (n == 3) return new SMinus(a, b);
143  
    if (n == 4) return new SMul(a, b);
144  
    return new SDiv(a, b);
145  
  }
146  
  
147  
  static St makeStructure3(int budget) {
148  
    // split budget among parts
149  
    int b1 = 1+random(budget-2);
150  
    int b2 = 1+random(budget-1-b1);
151  
    int b3 = budget-b1-b2;
152  
    St a = makeStructure(b1), b = makeStructure(b2), c = makeStructure(b3);
153  
    return new SSwitch(a, b, c);
154  
  }
155  
  
156  
  static RGBImage render(St st, int w, int h) {
157  
    RGBImage img = new RGBImage(w, h, Color.white);
158  
     for (int y = 0; y < h; y++)
159  
      for (int x = 0; x < w; x++) {
160  
        double nx = x/(double)(w-1);
161  
        double ny = y/(double)(h-1);
162  
        double d = st.def(nx, ny);
163  
        d = min(1, max(0, d));
164  
        img.setPixel(x, y, new RGB(d, d, d));
165  
      }
166  
    return img;
167  
  }
168  
  
169  
  static FrameWithImages fwi;
170  
  static int budget = 4;
171  
  static int numImages = 8;
172  
  static int delay = 100;
173  
  
174  
  psvm {
175  
    fwi = new FrameWithImages(numImages);
176  
    fwi.hop();
177  
    final int w = 50, h = 50;
178  
    
179  
    for (int i = 0; i < args.length; i++) {
180  
      String a = args[i];
181  
      if (a.equals("budget"))
182  
        budget = Integer.parseInt(args[++i]);
183  
    }
184  
    
185  
    fwi.setInnerSize(w, h);
186  
187  
    thread {
188  
      int n = 0;
189  
      while (true) {
190  
        St struct = makeStructure(budget);
191  
        RGBImage img = render(struct, w, h);
192  
        fwi.setImage(n, img);
193  
        n = (n+1) % numImages;
194  
        System.out.println(structure(struct));
195  
        Thread.sleep(delay);
196  
      }
197  
    }
198  
  }
199  
  
200  
   static String structure(Object o) {
201  
    if (o == null) return "null";
202  
    String name = o.getClass().getName();
203  
    
204  
    new StringBuilder buf;
205  
    
206  
    if (o instanceof Collection) {
207  
      for (Object x : (Collection) o) {
208  
        if (buf.length() != 0) buf.append(", ");
209  
        buf.append(structure(x));
210  
      }
211  
      return "{" + buf + "}";
212  
    }
213  
    
214  
    if (o.getClass().isArray()) {
215  
      int n = Array.getLength(o);
216  
      for (int i = 0; i < n; i++) {
217  
        if (buf.length() != 0) buf.append(", ");
218  
        buf.append(structure(Array.get(o, i)));
219  
      }
220  
      return "{" + buf + "}";
221  
    }
222  
223  
    if (o instanceof String)
224  
      return quote((String) o);
225  
    
226  
    // Need more cases? This should cover all library classes...
227  
    if (name.startsWith("java.") || name.startsWith("javax."))
228  
      return String.valueOf(o);
229  
      
230  
    String shortName = o.getClass().getName().replaceAll("^main\\$", "");
231  
232  
    // TODO: go to superclasses too
233  
    Field[] fields = o.getClass().getDeclaredFields();
234  
    int numFields = 0;
235  
    String fieldName = "";
236  
    for (Field field : fields) {
237  
      if ((field.getModifiers() & Modifier.STATIC) != 0)
238  
        continue;
239  
      Object value;
240  
      try {
241  
        value = field.get(o);
242  
      } catch (Exception e) {
243  
        value = "?";
244  
      }
245  
      
246  
      fieldName = field.getName();
247  
      
248  
      // insert special cases here
249  
250  
      if (value != null) {
251  
        if (buf.length() != 0) buf.append(", ");
252  
        buf.append(fieldName + "=" + structure(value));
253  
      }
254  
      ++numFields;
255  
    }
256  
    String b = buf.toString();
257  
    if (numFields == 1)
258  
      b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one
259  
    String s = shortName;
260  
    if (buf.length() != 0)
261  
      s += "(" + b + ")";
262  
    return s;
263  
  }
264  
  
265  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
742 #1000610 (pitcher) 2015-08-18 00:07:07
741 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #1000562
Snippet name: Make structure (implementation)
Eternal ID of this version: #1000562/1
Text MD5: d18af262f34ebd079a861e95ae1bfe29
Transpilation MD5: 4d5ab0829a86d3b1fa925f6f08860b32
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-14 17:41:11
Source code size: 6314 bytes / 265 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 822 / 820
Referenced in: [show references]