Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.applet.*;
- public class hcurve extends Applet {
- HControls controls;
- public void init() {
- setLayout(new BorderLayout());
- HCanvas c = new HCanvas();
- add("North", controls = new HControls(c));
- add("Center", c);
- }
- public void start() {
- controls.enable();
- }
- public void stop() {
- controls.disable();
- }
- public boolean handleEvent(Event e) {
- if (e.id == Event.WINDOW_DESTROY) {
- System.exit(0);
- }
- return false;
- }
- public static void main(String args[]) {
- Frame f = new Frame("hcurve");
- hcurve hcurve = new hcurve();
- hcurve.init();
- hcurve.start();
- f.add("Center", hcurve);
- f.resize(300, 300);
- f.show();
- }
- }
- class rect {
- public int x, y, g, h;
- public rect lo, ro, lu, ru, sp;
- public int xcor(int i) {
- if (x == 1) {
- return 0;
- } else if (y == 1) {
- return i;
- } else if (2 * lo.y == y) {
- if (i < lo.g - lo.h) {
- return lo.x - 1 - lo.xcor(i + lo.h);
- } else if (i < lu.g + lo.g - lo.h) {
- return lu.x - 1 - lu.xcor(lu.g + lo.g - lo.h - i - 1);
- } else if (i < ru.g + lu.g + lo.g - lo.h) {
- return lu.x + ru.xcor(i - lu.g - lo.g + lo.h);
- } else if (i < g - lo.h) {
- return lo.x + ro.xcor(g - lo.h - i - 1);
- } else {
- return lo.x - 1 - lo.xcor(i + lo.h - g);
- }
- } else if (2 * lo.x == x) {
- return sp.ycor(g - i);
- } else if (i < lo.h) {
- return lo.xcor(i);
- } else if (i < lu.g + lo.h) {
- return lu.x - 1 - lu.xcor(lu.g + lo.h - i - 1);
- } else if (i < lu.g + ru.g + lo.h) {
- return lu.x + ru.xcor(i - lu.g - lo.h);
- } else if (i < g - lo.g + lo.h) {
- return lo.x + ro.xcor(g - lo.g + lo.h - i - 1);
- } else {
- return lo.xcor(i + lo.g - g);
- }
- }
- public int ycor(int i) {
- if (x == 1) {
- return i;
- } else if (y == 1) {
- return 0;
- } else if (2 * lo.y == y) {
- if (i < lo.g - lo.h) {
- return lo.y - 1 - lo.ycor(i + lo.h);
- } else if (i < lu.g + lo.g - lo.h) {
- return lo.y + lu.ycor(lu.g + lo.g - lo.h - i - 1);
- } else if (i < ru.g + lu.g + lo.g - lo.h) {
- return ro.y + ru.ycor(i - lu.g - lo.g + lo.h);
- } else if (i < g - lo.h) {
- return ro.y - 1 - ro.ycor(g - lo.h - i - 1);
- } else {
- return lo.y - 1 - lo.ycor(i + lo.h - g);
- }
- } else if (2 * lo.x == x) {
- return sp.xcor(g - i);
- } else if (i < lo.h) {
- return lo.ycor(i);
- } else if (i < lu.g + lo.h) {
- return lo.y + lu.ycor(lu.g + lo.h - i - 1);
- } else if (i < lu.g + ru.g + lo.h) {
- return ro.y + ru.ycor(i - lu.g - lo.h);
- } else if (i < g - lo.g + lo.h) {
- return ro.y - 1 - ro.ycor(g - lo.g + lo.h - i - 1);
- } else {
- return lo.ycor(i + lo.g - g);
- }
- }
- };
- class HCanvas extends Canvas {
- int f = 30;
- int n = 16;
- int m = 64;
- int w = 30;
- int wa = 0;
- int filled = 0;
- Font font;
- boolean bfel[] = new boolean[257];
- boolean bfeld[][] = new boolean[257][257];
- rect fel[] = new rect[257];
- rect feld[][] = new rect[257][257];
- int xco(int i, int n) {
- int acht = 2 * (n / 4) * (n / 4);
- int halb = 2 * (n / 4);
- if (i < 2) {
- return 0;
- } else if (i >= n * n / 2) {
- return (n - xco((i - n * n / 2), n) - 1);
- } else if (i < acht) {
- return xco(i, halb);
- } else if (i >= n * n / 2 - acht) {
- return n - halb + xco(i - n * n / 2 + acht, halb);
- } else if (2 * halb == n) {
- return halb - xco(n * n / 2 - acht - 1 - i, halb) - 1;
- } else {
- return n - halb - xcod(n * n / 2 - acht - 1 - i, n - halb) - 1;
- }
- }
- int xcod(int i, int n) {
- int acht = 2 * (n / 4) * (n / 4);
- int halb = 2 * (n / 4);
- if (n <= 4) {
- if (i == 0) {
- return 0;
- } else if (i < 1 + n) {
- return i - 1;
- } else if (i < 2 * n) {
- return 2 * n - i;
- } else {
- return (i + 6 - 3 * n) / 2;
- }
- } else if (n == 6) {
- if (i < 4) {
- return 0;
- } else if (i < 7) {
- return 1;
- } else if (i < 10) {
- return 2;
- } else if (i < 16) {
- return (i - 4) / 2;
- } else if (i < 23) {
- return (27 - i) / 2;
- } else if (i < 26) {
- return 1;
- } else if (i < 28) {
- return 2;
- } else if (i < 31) {
- return i - 25;
- } else {
- return 36 - i;
- }
- } else if (i < acht - 4) {
- return xcod(i, halb);
- } else if (i >= n * n - acht - 4) {
- return xcod(i + 2 * acht - n * n, halb);
- } else if ((i >= n * n / 2 - acht - 4) && (i < n * n / 2 + acht - 4)) {
- return (n - halb + xco((i - n * n / 2 + acht + 4), halb));
- } else if (i >= n * n / 2 - 4) {
- return n - xco(i - n * n / 2 + 4, n) - 1;
- } else if (2 * halb == n) {
- return halb - xco(n * n / 2 - acht - 5 - i, halb) - 1;
- } else {
- return n - halb - xcod(n * n / 2 - acht - 5 - i, n - halb) - 1;
- }
- }
- int yco(int i, int n) {
- int acht = 2 * (n / 4) * (n / 4);
- int halb = 2 * (n / 4);
- if (i < 2) {
- return i;
- } else if (i >= n * n / 2) {
- return (n - yco((i - n * n / 2), n) - 1);
- } else if (i < acht) {
- return yco(i, halb);
- } else if (i >= n * n / 2 - acht) {
- return n - halb + yco(i - n * n / 2 + acht, halb);
- } else if (2 * halb == n) {
- return halb + yco(n * n / 2 - acht - 1 - i, halb);
- } else {
- return halb + ycod(n * n / 2 - acht - 1 - i, n - halb);
- }
- }
- int ycod(int i, int n) {
- int acht = 2 * (n / 4) * (n / 4);
- int halb = 2 * (n / 4);
- if (n <= 4) {
- if (i == 0) {
- return n - 2;
- } else if (i < 1 + n) {
- return n - 1;
- } else if (i < 2 * n) {
- return n - 2;
- } else if (i < 10) {
- return (9 - i) * (n - 3);
- } else if (i < 12) {
- return i - 10;
- } else {
- return 13 - i;
- }
- } else if (n == 6) {
- if (i < 4) {
- return i + 2;
- } else if (i < 7) {
- return 9 - i;
- } else if (i < 10) {
- return i - 4;
- } else if (i < 12) {
- return 15 - i;
- } else if (i < 14) {
- return i - 8;
- } else if (i < 18) {
- return 19 - i;
- } else if (i < 20) {
- return i - 16;
- } else if (i < 22) {
- return 23 - i;
- } else if (i < 24) {
- return 2;
- } else if (i < 25) {
- return 1;
- } else if (i < 27) {
- return 0;
- } else if (i < 31) {
- return 1;
- } else {
- return 0;
- }
- } else if (i < acht - 4) {
- return ycod(i, halb);
- } else if (i >= n * n - acht - 4) {
- return ycod(i + 2 * acht - n * n, halb);
- } else if ((i >= n * n / 2 - acht - 4) && (i < n * n / 2 + acht - 4)) {
- return (n - halb + yco((i - n * n / 2 + acht + 4), halb));
- } else if (i >= n * n / 2 - 4) {
- return n - yco(i - n * n / 2 + 4, n) - 1;
- } else if (2 * halb == n) {
- return halb + yco(n * n / 2 - acht - 5 - i, halb);
- } else {
- return halb + ycod(n * n / 2 - acht - 5 - i, n - halb);
- }
- }
- int xcor(int i, int n) {
- if (i < 2) {
- return 0;
- } else if (i >= n * n / 2) {
- return (n - xcor((i - n * n / 2), n) - 1);
- } else {
- switch (8 * i / n / n) {
- case 0:
- return xcor(i, n / 2);
- case 1:
- return xcor(n * n / 4 - 1 - i, n / 2);
- case 2:
- return n / 2 - xcor(3 * n * n / 8 - 1 - i, n / 2) - 1;
- case 3:
- return n / 2 + xcor(i - 3 * n * n / 8, n / 2);
- }
- }
- return 0;
- }
- int ycor(int i, int n) {
- if (i < 2) {
- return i;
- } else if (i >= n * n / 2) {
- return (n - ycor((i - n * n / 2), n) - 1);
- } else {
- switch (8 * i / n / n) {
- case 0:
- return ycor(i, n / 2);
- case 1:
- return n - ycor(n * n / 4 - 1 - i, n / 2) - 1;
- case 2:
- return n / 2 + ycor(3 * n * n / 8 - 1 - i, n / 2);
- case 3:
- return n / 2 + ycor(i - 3 * n * n / 8, n / 2);
- }
- }
- return 0;
- }
- rect fels(int xm) {
- rect r;
- if (bfel[xm]) {
- return fel[xm];
- } else {
- r = new rect();
- fel[xm] = r;
- bfel[xm] = true;
- r.g = xm;
- r.x = xm;
- r.y = 1;
- r.h = 1;
- r.lo = felds(0, 0);
- r.ro = felds(0, 0);
- r.lu = felds(0, 0);
- r.ru = felds(0, 0);
- return r;
- }
- }
- rect felds(int xm, int ym) {
- rect r;
- int xn, yn;
- if (bfeld[xm][ym]) {
- return feld[xm][ym];
- } else {
- r = new rect();
- feld[xm][ym] = r;
- bfeld[xm][ym] = true;
- r.x = xm;
- r.y = ym;
- xn = xm / 2;
- yn = ym / 2;
- r.g = xm * ym;
- if (xn == (xn / 2) * 2) {
- xn = xm - xn;
- }
- if (yn == (yn / 2) * 2) {
- yn = ym - yn;
- }
- if ((xm - xn) * (ym - yn) == 0) {
- r.h = xm * ym - 1;
- if (xm * ym == 0) {
- r.h += 1;
- }
- r.lo = felds(0, 0);
- r.ro = felds(0, 0);
- r.lu = felds(0, 0);
- r.ru = felds(0, 0);
- } else {
- r.lo = felds(xn, yn);
- r.ro = felds(xm - xn, yn);
- r.lu = felds(xn, ym - yn);
- r.ru = felds(xm - xn, ym - yn);
- if (xm == 3) {
- if (2 * yn != ym) {
- r.lo = fels(3);
- r.ro = felds(0, 1);
- r.lu = felds(0, ym - 1);
- r.ru = felds(3, ym - 1);
- } else if (2 * (yn / 2) != yn) {
- r.lo = felds(3, yn);
- r.ro = felds(0, yn);
- r.lu = felds(0, yn);
- r.ru = felds(3, yn);
- } else {
- r.lo = felds(3, yn);
- r.ro = felds(0, yn);
- r.lu = felds(3, yn);
- r.ru = felds(0, yn);
- }
- } else if ((xm == 2) && (2 * yn == ym)) {
- r.lo = felds(2, yn);
- r.ro = felds(0, yn);
- r.lu = felds(0, ym - yn);
- r.ru = felds(2, ym - yn);
- } else if ((ym == 3) && (2 * xn != xm)) {
- r.lo = fels(xm);
- r.ro = felds(0, 1);
- r.lu = felds(0, ym - 1);
- r.ru = felds(xm, ym - 1);
- }
- if (2 * r.lo.y == ym) {
- r.h = r.lo.g - r.lo.h;
- } else {
- r.h = r.lo.h;
- }
- r.h = r.h + r.lu.g + r.ru.h;
- if ((xm == 3) && (2 * (yn / 2) == yn) && (2 * yn == ym)) {
- r.h = r.h - r.lu.h + 1;
- }
- }
- if ((2 * r.lo.x == xm) && (2 * r.lo.y != ym)) {
- r.sp = felds(ym, xm);
- r.h = r.sp.g - r.sp.h;
- }
- return r;
- }
- }
- public void paintframe(Graphics g, rect r, int xb, int yb) {
- if (r.x * r.y > 1) {
- g.setColor(Color.pink);
- g.drawRect(xb * f + f / 2 - 1, yb * f + f / 2 - 1, (r.x - 1) * f + 2, (r.y - 1) * f + 2);
- if ((2 * r.lo.x != r.x) || (2 * r.lo.y == r.y)) {
- paintframe(g, r.lo, xb, yb);
- paintframe(g, r.lu, xb, yb + r.lo.y);
- paintframe(g, r.ro, xb + r.lo.x, yb);
- paintframe(g, r.ru, xb + r.lu.x, yb + r.ro.y);
- }
- }
- }
- public void paint(Graphics g) {
- Rectangle rr = bounds();
- int xa = 0;
- int ya = 0;
- int x = 0;
- int y = 0;
- int xn, yn, ww;
- rect r;
- if (n > 256) {
- n = 256;
- }
- if (w > 256) {
- w = 256;
- }
- if (n > w) {
- ww = n;
- } else {
- ww = w;
- }
- if (filled < 2) {
- w = n;
- } else {
- r = felds(w, n);
- }
- if (f * w > rr.width) {
- f = rr.width / w;
- }
- if (f * n > rr.height) {
- f = rr.height / n;
- }
- setBackground(Color.white);
- if (filled > 2) {
- paintframe(g, feld[w][n], 0, 0);
- }
- g.setColor(Color.red);
- for (int i = 0; i <= 2 * ((n * w) / 2); i++) {
- switch (filled) {
- case 0:
- x = xco(i, n);
- y = yco(i, n);
- break;
- case 1:
- x = xcor(i, n);
- y = ycor(i, n);
- break;
- default:
- x = feld[w][n].xcor(i);
- y = feld[w][n].ycor(i);
- break;
- }
- if (m == i) {
- g.setColor(Color.blue);
- }
- if (n * w / 2 + 1 == i) {
- g.setColor(Color.green);
- }
- g.drawLine(xa * f + f / 2, (n - 1 - ya) * f + f / 2, x * f + f / 2, (n - 1 - y) * f + f / 2);
- xa = x;
- ya = y;
- }
- }
- public void redraw(int filled, int faktor, int groesse, int marke, int w) {
- this.filled = filled;
- this.f = faktor;
- this.n = groesse;
- this.m = marke;
- this.w = w;
- repaint();
- }
- }
- class HControls extends Panel {
- Choice hwahl;
- TextField e, a, w, s;
- HCanvas canvas;
- public HControls(HCanvas canvas) {
- this.canvas = canvas;
- add(hwahl = new Choice());
- hwahl.addItem("Fill even square");
- hwahl.addItem("Fill 2^ square");
- hwahl.addItem("Fill rectangle");
- add(new Label("Squaresize"));
- add(e = new TextField("16", 4));
- add(new Label("Rectangle width"));
- add(w = new TextField("30", 4));
- add(new Label("Switch to blue at"));
- add(a = new TextField("64", 4));
- add(new Button("+"));
- add(new Button("-"));
- add(new Label("Maximal edge length"));
- add(s = new TextField("30", 4));
- }
- public boolean action(Event ev, Object arg) {
- if ((ev.target == e) || (ev.target == w) || (ev.target == a) || (ev.target == s)) {
- String label = (String) arg;
- canvas.redraw(hwahl.getSelectedIndex(),
- Integer.parseInt(s.getText().trim()),
- Integer.parseInt(e.getText().trim()),
- Integer.parseInt(a.getText().trim()),
- Integer.parseInt(w.getText().trim()));
- return true;
- } else if ("+".equals(arg)) {
- a.setText(String.valueOf(Integer.parseInt(a.getText().trim()) + 1));
- canvas.redraw(hwahl.getSelectedIndex(),
- Integer.parseInt(s.getText().trim()),
- Integer.parseInt(e.getText().trim()),
- Integer.parseInt(a.getText().trim()),
- Integer.parseInt(w.getText().trim()));
- return true;
- } else if ("-".equals(arg)) {
- a.setText(String.valueOf(Integer.parseInt(a.getText().trim()) - 1));
- canvas.redraw(hwahl.getSelectedIndex(),
- Integer.parseInt(s.getText().trim()),
- Integer.parseInt(e.getText().trim()),
- Integer.parseInt(a.getText().trim()),
- Integer.parseInt(w.getText().trim()));
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment