Guest User

hcurve.java

a guest
Aug 17th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.41 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.applet.*;
  4.  
  5. public class hcurve extends Applet {
  6.  
  7.     HControls controls;
  8.  
  9.     public void init() {
  10.         setLayout(new BorderLayout());
  11.         HCanvas c = new HCanvas();
  12.         add("North", controls = new HControls(c));
  13.         add("Center", c);
  14.     }
  15.  
  16.     public void start() {
  17.         controls.enable();
  18.     }
  19.  
  20.     public void stop() {
  21.         controls.disable();
  22.     }
  23.  
  24.     public boolean handleEvent(Event e) {
  25.         if (e.id == Event.WINDOW_DESTROY) {
  26.             System.exit(0);
  27.         }
  28.         return false;
  29.     }
  30.  
  31.     public static void main(String args[]) {
  32.         Frame f = new Frame("hcurve");
  33.         hcurve hcurve = new hcurve();
  34.  
  35.         hcurve.init();
  36.         hcurve.start();
  37.  
  38.         f.add("Center", hcurve);
  39.         f.resize(300, 300);
  40.         f.show();
  41.     }
  42. }
  43.  
  44. class rect {
  45.  
  46.     public int x, y, g, h;
  47.     public rect lo, ro, lu, ru, sp;
  48.  
  49.     public int xcor(int i) {
  50.         if (x == 1) {
  51.             return 0;
  52.         } else if (y == 1) {
  53.             return i;
  54.         } else if (2 * lo.y == y) {
  55.             if (i < lo.g - lo.h) {
  56.                 return lo.x - 1 - lo.xcor(i + lo.h);
  57.             } else if (i < lu.g + lo.g - lo.h) {
  58.                 return lu.x - 1 - lu.xcor(lu.g + lo.g - lo.h - i - 1);
  59.             } else if (i < ru.g + lu.g + lo.g - lo.h) {
  60.                 return lu.x + ru.xcor(i - lu.g - lo.g + lo.h);
  61.             } else if (i < g - lo.h) {
  62.                 return lo.x + ro.xcor(g - lo.h - i - 1);
  63.             } else {
  64.                 return lo.x - 1 - lo.xcor(i + lo.h - g);
  65.             }
  66.         } else if (2 * lo.x == x) {
  67.             return sp.ycor(g - i);
  68.         } else if (i < lo.h) {
  69.             return lo.xcor(i);
  70.         } else if (i < lu.g + lo.h) {
  71.             return lu.x - 1 - lu.xcor(lu.g + lo.h - i - 1);
  72.         } else if (i < lu.g + ru.g + lo.h) {
  73.             return lu.x + ru.xcor(i - lu.g - lo.h);
  74.         } else if (i < g - lo.g + lo.h) {
  75.             return lo.x + ro.xcor(g - lo.g + lo.h - i - 1);
  76.         } else {
  77.             return lo.xcor(i + lo.g - g);
  78.         }
  79.     }
  80.  
  81.     public int ycor(int i) {
  82.         if (x == 1) {
  83.             return i;
  84.         } else if (y == 1) {
  85.             return 0;
  86.         } else if (2 * lo.y == y) {
  87.             if (i < lo.g - lo.h) {
  88.                 return lo.y - 1 - lo.ycor(i + lo.h);
  89.             } else if (i < lu.g + lo.g - lo.h) {
  90.                 return lo.y + lu.ycor(lu.g + lo.g - lo.h - i - 1);
  91.             } else if (i < ru.g + lu.g + lo.g - lo.h) {
  92.                 return ro.y + ru.ycor(i - lu.g - lo.g + lo.h);
  93.             } else if (i < g - lo.h) {
  94.                 return ro.y - 1 - ro.ycor(g - lo.h - i - 1);
  95.             } else {
  96.                 return lo.y - 1 - lo.ycor(i + lo.h - g);
  97.             }
  98.         } else if (2 * lo.x == x) {
  99.             return sp.xcor(g - i);
  100.         } else if (i < lo.h) {
  101.             return lo.ycor(i);
  102.         } else if (i < lu.g + lo.h) {
  103.             return lo.y + lu.ycor(lu.g + lo.h - i - 1);
  104.         } else if (i < lu.g + ru.g + lo.h) {
  105.             return ro.y + ru.ycor(i - lu.g - lo.h);
  106.         } else if (i < g - lo.g + lo.h) {
  107.             return ro.y - 1 - ro.ycor(g - lo.g + lo.h - i - 1);
  108.         } else {
  109.             return lo.ycor(i + lo.g - g);
  110.         }
  111.     }
  112. };
  113.  
  114. class HCanvas extends Canvas {
  115.  
  116.     int f = 30;
  117.     int n = 16;
  118.     int m = 64;
  119.     int w = 30;
  120.     int wa = 0;
  121.     int filled = 0;
  122.     Font font;
  123.     boolean bfel[] = new boolean[257];
  124.     boolean bfeld[][] = new boolean[257][257];
  125.     rect fel[] = new rect[257];
  126.     rect feld[][] = new rect[257][257];
  127.  
  128.     int xco(int i, int n) {
  129.         int acht = 2 * (n / 4) * (n / 4);
  130.         int halb = 2 * (n / 4);
  131.         if (i < 2) {
  132.             return 0;
  133.         } else if (i >= n * n / 2) {
  134.             return (n - xco((i - n * n / 2), n) - 1);
  135.         } else if (i < acht) {
  136.             return xco(i, halb);
  137.         } else if (i >= n * n / 2 - acht) {
  138.             return n - halb + xco(i - n * n / 2 + acht, halb);
  139.         } else if (2 * halb == n) {
  140.             return halb - xco(n * n / 2 - acht - 1 - i, halb) - 1;
  141.         } else {
  142.             return n - halb - xcod(n * n / 2 - acht - 1 - i, n - halb) - 1;
  143.         }
  144.     }
  145.  
  146.     int xcod(int i, int n) {
  147.         int acht = 2 * (n / 4) * (n / 4);
  148.         int halb = 2 * (n / 4);
  149.         if (n <= 4) {
  150.             if (i == 0) {
  151.                 return 0;
  152.             } else if (i < 1 + n) {
  153.                 return i - 1;
  154.             } else if (i < 2 * n) {
  155.                 return 2 * n - i;
  156.             } else {
  157.                 return (i + 6 - 3 * n) / 2;
  158.             }
  159.         } else if (n == 6) {
  160.             if (i < 4) {
  161.                 return 0;
  162.             } else if (i < 7) {
  163.                 return 1;
  164.             } else if (i < 10) {
  165.                 return 2;
  166.             } else if (i < 16) {
  167.                 return (i - 4) / 2;
  168.             } else if (i < 23) {
  169.                 return (27 - i) / 2;
  170.             } else if (i < 26) {
  171.                 return 1;
  172.             } else if (i < 28) {
  173.                 return 2;
  174.             } else if (i < 31) {
  175.                 return i - 25;
  176.             } else {
  177.                 return 36 - i;
  178.             }
  179.         } else if (i < acht - 4) {
  180.             return xcod(i, halb);
  181.         } else if (i >= n * n - acht - 4) {
  182.             return xcod(i + 2 * acht - n * n, halb);
  183.         } else if ((i >= n * n / 2 - acht - 4) && (i < n * n / 2 + acht - 4)) {
  184.             return (n - halb + xco((i - n * n / 2 + acht + 4), halb));
  185.         } else if (i >= n * n / 2 - 4) {
  186.             return n - xco(i - n * n / 2 + 4, n) - 1;
  187.         } else if (2 * halb == n) {
  188.             return halb - xco(n * n / 2 - acht - 5 - i, halb) - 1;
  189.         } else {
  190.             return n - halb - xcod(n * n / 2 - acht - 5 - i, n - halb) - 1;
  191.         }
  192.     }
  193.  
  194.     int yco(int i, int n) {
  195.         int acht = 2 * (n / 4) * (n / 4);
  196.         int halb = 2 * (n / 4);
  197.         if (i < 2) {
  198.             return i;
  199.         } else if (i >= n * n / 2) {
  200.             return (n - yco((i - n * n / 2), n) - 1);
  201.         } else if (i < acht) {
  202.             return yco(i, halb);
  203.         } else if (i >= n * n / 2 - acht) {
  204.             return n - halb + yco(i - n * n / 2 + acht, halb);
  205.         } else if (2 * halb == n) {
  206.             return halb + yco(n * n / 2 - acht - 1 - i, halb);
  207.         } else {
  208.             return halb + ycod(n * n / 2 - acht - 1 - i, n - halb);
  209.         }
  210.     }
  211.  
  212.     int ycod(int i, int n) {
  213.         int acht = 2 * (n / 4) * (n / 4);
  214.         int halb = 2 * (n / 4);
  215.         if (n <= 4) {
  216.             if (i == 0) {
  217.                 return n - 2;
  218.             } else if (i < 1 + n) {
  219.                 return n - 1;
  220.             } else if (i < 2 * n) {
  221.                 return n - 2;
  222.             } else if (i < 10) {
  223.                 return (9 - i) * (n - 3);
  224.             } else if (i < 12) {
  225.                 return i - 10;
  226.             } else {
  227.                 return 13 - i;
  228.             }
  229.         } else if (n == 6) {
  230.             if (i < 4) {
  231.                 return i + 2;
  232.             } else if (i < 7) {
  233.                 return 9 - i;
  234.             } else if (i < 10) {
  235.                 return i - 4;
  236.             } else if (i < 12) {
  237.                 return 15 - i;
  238.             } else if (i < 14) {
  239.                 return i - 8;
  240.             } else if (i < 18) {
  241.                 return 19 - i;
  242.             } else if (i < 20) {
  243.                 return i - 16;
  244.             } else if (i < 22) {
  245.                 return 23 - i;
  246.             } else if (i < 24) {
  247.                 return 2;
  248.             } else if (i < 25) {
  249.                 return 1;
  250.             } else if (i < 27) {
  251.                 return 0;
  252.             } else if (i < 31) {
  253.                 return 1;
  254.             } else {
  255.                 return 0;
  256.             }
  257.         } else if (i < acht - 4) {
  258.             return ycod(i, halb);
  259.         } else if (i >= n * n - acht - 4) {
  260.             return ycod(i + 2 * acht - n * n, halb);
  261.         } else if ((i >= n * n / 2 - acht - 4) && (i < n * n / 2 + acht - 4)) {
  262.             return (n - halb + yco((i - n * n / 2 + acht + 4), halb));
  263.         } else if (i >= n * n / 2 - 4) {
  264.             return n - yco(i - n * n / 2 + 4, n) - 1;
  265.         } else if (2 * halb == n) {
  266.             return halb + yco(n * n / 2 - acht - 5 - i, halb);
  267.         } else {
  268.             return halb + ycod(n * n / 2 - acht - 5 - i, n - halb);
  269.         }
  270.     }
  271.  
  272.     int xcor(int i, int n) {
  273.         if (i < 2) {
  274.             return 0;
  275.         } else if (i >= n * n / 2) {
  276.             return (n - xcor((i - n * n / 2), n) - 1);
  277.         } else {
  278.             switch (8 * i / n / n) {
  279.                 case 0:
  280.                     return xcor(i, n / 2);
  281.                 case 1:
  282.                     return xcor(n * n / 4 - 1 - i, n / 2);
  283.                 case 2:
  284.                     return n / 2 - xcor(3 * n * n / 8 - 1 - i, n / 2) - 1;
  285.                 case 3:
  286.                     return n / 2 + xcor(i - 3 * n * n / 8, n / 2);
  287.             }
  288.         }
  289.         return 0;
  290.     }
  291.  
  292.     int ycor(int i, int n) {
  293.         if (i < 2) {
  294.             return i;
  295.         } else if (i >= n * n / 2) {
  296.             return (n - ycor((i - n * n / 2), n) - 1);
  297.         } else {
  298.             switch (8 * i / n / n) {
  299.                 case 0:
  300.                     return ycor(i, n / 2);
  301.                 case 1:
  302.                     return n - ycor(n * n / 4 - 1 - i, n / 2) - 1;
  303.                 case 2:
  304.                     return n / 2 + ycor(3 * n * n / 8 - 1 - i, n / 2);
  305.                 case 3:
  306.                     return n / 2 + ycor(i - 3 * n * n / 8, n / 2);
  307.             }
  308.         }
  309.         return 0;
  310.     }
  311.  
  312.     rect fels(int xm) {
  313.         rect r;
  314.         if (bfel[xm]) {
  315.             return fel[xm];
  316.         } else {
  317.             r = new rect();
  318.             fel[xm] = r;
  319.             bfel[xm] = true;
  320.             r.g = xm;
  321.             r.x = xm;
  322.             r.y = 1;
  323.             r.h = 1;
  324.             r.lo = felds(0, 0);
  325.             r.ro = felds(0, 0);
  326.             r.lu = felds(0, 0);
  327.             r.ru = felds(0, 0);
  328.             return r;
  329.         }
  330.     }
  331.  
  332.     rect felds(int xm, int ym) {
  333.         rect r;
  334.         int xn, yn;
  335.         if (bfeld[xm][ym]) {
  336.             return feld[xm][ym];
  337.         } else {
  338.             r = new rect();
  339.             feld[xm][ym] = r;
  340.             bfeld[xm][ym] = true;
  341.             r.x = xm;
  342.             r.y = ym;
  343.             xn = xm / 2;
  344.             yn = ym / 2;
  345.             r.g = xm * ym;
  346.             if (xn == (xn / 2) * 2) {
  347.                 xn = xm - xn;
  348.             }
  349.             if (yn == (yn / 2) * 2) {
  350.                 yn = ym - yn;
  351.             }
  352.             if ((xm - xn) * (ym - yn) == 0) {
  353.                 r.h = xm * ym - 1;
  354.                 if (xm * ym == 0) {
  355.                     r.h += 1;
  356.                 }
  357.                 r.lo = felds(0, 0);
  358.                 r.ro = felds(0, 0);
  359.                 r.lu = felds(0, 0);
  360.                 r.ru = felds(0, 0);
  361.             } else {
  362.                 r.lo = felds(xn, yn);
  363.                 r.ro = felds(xm - xn, yn);
  364.                 r.lu = felds(xn, ym - yn);
  365.                 r.ru = felds(xm - xn, ym - yn);
  366.                 if (xm == 3) {
  367.                     if (2 * yn != ym) {
  368.                         r.lo = fels(3);
  369.                         r.ro = felds(0, 1);
  370.                         r.lu = felds(0, ym - 1);
  371.                         r.ru = felds(3, ym - 1);
  372.                     } else if (2 * (yn / 2) != yn) {
  373.                         r.lo = felds(3, yn);
  374.                         r.ro = felds(0, yn);
  375.                         r.lu = felds(0, yn);
  376.                         r.ru = felds(3, yn);
  377.                     } else {
  378.                         r.lo = felds(3, yn);
  379.                         r.ro = felds(0, yn);
  380.                         r.lu = felds(3, yn);
  381.                         r.ru = felds(0, yn);
  382.                     }
  383.                 } else if ((xm == 2) && (2 * yn == ym)) {
  384.                     r.lo = felds(2, yn);
  385.                     r.ro = felds(0, yn);
  386.                     r.lu = felds(0, ym - yn);
  387.                     r.ru = felds(2, ym - yn);
  388.                 } else if ((ym == 3) && (2 * xn != xm)) {
  389.                     r.lo = fels(xm);
  390.                     r.ro = felds(0, 1);
  391.                     r.lu = felds(0, ym - 1);
  392.                     r.ru = felds(xm, ym - 1);
  393.                 }
  394.                 if (2 * r.lo.y == ym) {
  395.                     r.h = r.lo.g - r.lo.h;
  396.                 } else {
  397.                     r.h = r.lo.h;
  398.                 }
  399.                 r.h = r.h + r.lu.g + r.ru.h;
  400.                 if ((xm == 3) && (2 * (yn / 2) == yn) && (2 * yn == ym)) {
  401.                     r.h = r.h - r.lu.h + 1;
  402.                 }
  403.             }
  404.             if ((2 * r.lo.x == xm) && (2 * r.lo.y != ym)) {
  405.                 r.sp = felds(ym, xm);
  406.                 r.h = r.sp.g - r.sp.h;
  407.             }
  408.             return r;
  409.         }
  410.     }
  411.  
  412.     public void paintframe(Graphics g, rect r, int xb, int yb) {
  413.         if (r.x * r.y > 1) {
  414.             g.setColor(Color.pink);
  415.             g.drawRect(xb * f + f / 2 - 1, yb * f + f / 2 - 1, (r.x - 1) * f + 2, (r.y - 1) * f + 2);
  416.             if ((2 * r.lo.x != r.x) || (2 * r.lo.y == r.y)) {
  417.                 paintframe(g, r.lo, xb, yb);
  418.                 paintframe(g, r.lu, xb, yb + r.lo.y);
  419.                 paintframe(g, r.ro, xb + r.lo.x, yb);
  420.                 paintframe(g, r.ru, xb + r.lu.x, yb + r.ro.y);
  421.             }
  422.         }
  423.     }
  424.  
  425.     public void paint(Graphics g) {
  426.         Rectangle rr = bounds();
  427.         int xa = 0;
  428.         int ya = 0;
  429.         int x = 0;
  430.         int y = 0;
  431.         int xn, yn, ww;
  432.         rect r;
  433.         if (n > 256) {
  434.             n = 256;
  435.         }
  436.         if (w > 256) {
  437.             w = 256;
  438.         }
  439.         if (n > w) {
  440.             ww = n;
  441.         } else {
  442.             ww = w;
  443.         }
  444.         if (filled < 2) {
  445.             w = n;
  446.         } else {
  447.             r = felds(w, n);
  448.         }
  449.         if (f * w > rr.width) {
  450.             f = rr.width / w;
  451.         }
  452.         if (f * n > rr.height) {
  453.             f = rr.height / n;
  454.         }
  455.         setBackground(Color.white);
  456.         if (filled > 2) {
  457.             paintframe(g, feld[w][n], 0, 0);
  458.         }
  459.         g.setColor(Color.red);
  460.         for (int i = 0; i <= 2 * ((n * w) / 2); i++) {
  461.             switch (filled) {
  462.                 case 0:
  463.                     x = xco(i, n);
  464.                     y = yco(i, n);
  465.                     break;
  466.                 case 1:
  467.                     x = xcor(i, n);
  468.                     y = ycor(i, n);
  469.                     break;
  470.                 default:
  471.                     x = feld[w][n].xcor(i);
  472.                     y = feld[w][n].ycor(i);
  473.                     break;
  474.             }
  475.             if (m == i) {
  476.                 g.setColor(Color.blue);
  477.             }
  478.             if (n * w / 2 + 1 == i) {
  479.                 g.setColor(Color.green);
  480.             }
  481.             g.drawLine(xa * f + f / 2, (n - 1 - ya) * f + f / 2, x * f + f / 2, (n - 1 - y) * f + f / 2);
  482.             xa = x;
  483.             ya = y;
  484.         }
  485.     }
  486.  
  487.     public void redraw(int filled, int faktor, int groesse, int marke, int w) {
  488.         this.filled = filled;
  489.         this.f = faktor;
  490.         this.n = groesse;
  491.         this.m = marke;
  492.         this.w = w;
  493.         repaint();
  494.     }
  495. }
  496.  
  497. class HControls extends Panel {
  498.  
  499.     Choice hwahl;
  500.     TextField e, a, w, s;
  501.     HCanvas canvas;
  502.  
  503.     public HControls(HCanvas canvas) {
  504.         this.canvas = canvas;
  505.         add(hwahl = new Choice());
  506.         hwahl.addItem("Fill even square");
  507.         hwahl.addItem("Fill 2^ square");
  508.         hwahl.addItem("Fill rectangle");
  509.         add(new Label("Squaresize"));
  510.         add(e = new TextField("16", 4));
  511.         add(new Label("Rectangle width"));
  512.         add(w = new TextField("30", 4));
  513.         add(new Label("Switch to blue at"));
  514.         add(a = new TextField("64", 4));
  515.         add(new Button("+"));
  516.         add(new Button("-"));
  517.         add(new Label("Maximal edge length"));
  518.         add(s = new TextField("30", 4));
  519.     }
  520.  
  521.     public boolean action(Event ev, Object arg) {
  522.         if ((ev.target == e) || (ev.target == w) || (ev.target == a) || (ev.target == s)) {
  523.             String label = (String) arg;
  524.             canvas.redraw(hwahl.getSelectedIndex(),
  525.                     Integer.parseInt(s.getText().trim()),
  526.                     Integer.parseInt(e.getText().trim()),
  527.                     Integer.parseInt(a.getText().trim()),
  528.                     Integer.parseInt(w.getText().trim()));
  529.             return true;
  530.         } else if ("+".equals(arg)) {
  531.             a.setText(String.valueOf(Integer.parseInt(a.getText().trim()) + 1));
  532.             canvas.redraw(hwahl.getSelectedIndex(),
  533.                     Integer.parseInt(s.getText().trim()),
  534.                     Integer.parseInt(e.getText().trim()),
  535.                     Integer.parseInt(a.getText().trim()),
  536.                     Integer.parseInt(w.getText().trim()));
  537.             return true;
  538.         } else if ("-".equals(arg)) {
  539.             a.setText(String.valueOf(Integer.parseInt(a.getText().trim()) - 1));
  540.             canvas.redraw(hwahl.getSelectedIndex(),
  541.                     Integer.parseInt(s.getText().trim()),
  542.                     Integer.parseInt(e.getText().trim()),
  543.                     Integer.parseInt(a.getText().trim()),
  544.                     Integer.parseInt(w.getText().trim()));
  545.             return true;
  546.         }
  547.         return false;
  548.     }
  549. }
Advertisement
Add Comment
Please, Sign In to add comment