Libraryless. Click here for Pure Java version (1288L/9K/28K).
!636 !standard functions !721 // thread { !* ctr !quicknew import static java.lang.Math.*; !image classes !FrameWithImages main { !include #1000522 // helper functions // St = Structure static abstract class St { abstract double def(double x, double y); } static class SConst extends St { double value; *(double *value) {} double def(double x, double y) { return value; } } static class SX extends St { double def(double x, double y) { return x; } } static class SY extends St { double def(double x, double y) { return y; } } static class SSin extends St { St a; *(St *a) {} double def(double x, double y) { return sin(a.def(x, y)); } } static class SCos extends St { St a; *(St *a) {} double def(double x, double y) { return cos(a.def(x, y)); } } static class SPlus extends St { St a, b; *(St *a, St *b) {} double def(double x, double y) { return a.def(x, y) + b.def(x, y); } } static class SMinus extends St { St a, b; *(St *a, St *b) {} double def(double x, double y) { return a.def(x, y) - b.def(x, y); } } static class SMul extends St { St a, b; *(St *a, St *b) {} double def(double x, double y) { return a.def(x, y) * b.def(x, y); } } static class SDiv extends St { St a, b; *(St *a, St *b) {} double def(double x, double y) { return a.def(x, y) / b.def(x, y); } } static class SSwitch extends St { St a, b, c; *(St *a, St *b, St *c) {} double def(double x, double y) { return a.def(x, y) < 0 ? b.def(x, y) : c.def(x, y); } } static St randomConst() { int n = random(2); double d; if (n == 0) d = random(-10.0, 10.0); else d = random(0.0, 1.0); return new SConst(d); } static St makeStructure(int budget) { if (budget < 1) fail("no budget"); int n = random(min(3, budget)); if (n == 0) return makeStructure1(); if (n == 1) return makeStructure2(budget); return makeStructure3(budget); } static St makeStructure1() { int n = random(3); if (n == 0) return randomConst(); else if (n == 1) return new SX(); else return new SY(); } static St makeStructure2(int budget) { int n = random(6); if (n == 0) return new SSin(makeStructure(budget-1)); else if (n == 1) return new SCos(makeStructure(budget-1)); // split budget among parts int b1 = 1+random(budget-1), b2 = budget-b1; //System.out.println((budget+1) + " => " + b1 + "/" + b2); St a = makeStructure(b1), b = makeStructure(b2); if (n == 2) return new SPlus(a, b); if (n == 3) return new SMinus(a, b); if (n == 4) return new SMul(a, b); return new SDiv(a, b); } static St makeStructure3(int budget) { // split budget among parts int b1 = 1+random(budget-2); int b2 = 1+random(budget-1-b1); int b3 = budget-b1-b2; St a = makeStructure(b1), b = makeStructure(b2), c = makeStructure(b3); return new SSwitch(a, b, c); } static RGBImage render(St st, int w, int h) { RGBImage img = new RGBImage(w, h, Color.white); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { double nx = x/(double)(w-1); double ny = y/(double)(h-1); double d = st.def(nx, ny); d = min(1, max(0, d)); img.setPixel(x, y, new RGB(d, d, d)); } return img; } static FrameWithImages fwi; static int budget = 4; static int numImages = 8; static int delay = 100; psvm { fwi = new FrameWithImages(numImages); fwi.hop(); final int w = 50, h = 50; for (int i = 0; i < args.length; i++) { String a = args[i]; if (a.equals("budget")) budget = Integer.parseInt(args[++i]); } fwi.setInnerSize(w, h); thread { int n = 0; while (true) { St struct = makeStructure(budget); RGBImage img = render(struct, w, h); fwi.setImage(n, img); n = (n+1) % numImages; System.out.println(structure(struct)); Thread.sleep(delay); } } } static String structure(Object o) { if (o == null) return "null"; String name = o.getClass().getName(); new StringBuilder buf; if (o instanceof Collection) { for (Object x : (Collection) o) { if (buf.length() != 0) buf.append(", "); buf.append(structure(x)); } return "{" + buf + "}"; } if (o.getClass().isArray()) { int n = Array.getLength(o); for (int i = 0; i < n; i++) { if (buf.length() != 0) buf.append(", "); buf.append(structure(Array.get(o, i))); } return "{" + buf + "}"; } if (o instanceof String) return quote((String) o); // Need more cases? This should cover all library classes... if (name.startsWith("java.") || name.startsWith("javax.")) return String.valueOf(o); String shortName = o.getClass().getName().replaceAll("^main\\$", ""); // TODO: go to superclasses too Field[] fields = o.getClass().getDeclaredFields(); int numFields = 0; String fieldName = ""; for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; Object value; try { value = field.get(o); } catch (Exception e) { value = "?"; } fieldName = field.getName(); // insert special cases here if (value != null) { if (buf.length() != 0) buf.append(", "); buf.append(fieldName + "=" + structure(value)); } ++numFields; } String b = buf.toString(); if (numFields == 1) b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one String s = shortName; if (buf.length() != 0) s += "(" + b + ")"; return s; } }
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
2 comment(s) hidden. show
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: | 821 / 819 |
Referenced in: | [show references] |