Gudinna Wiki VisaÄndraBifogaVersionerUppdateratSökKarta

programmera enka rekursiva frakteler med stinky

http://www.cs.columbia.edu/~evs/stinky/
http://www.cs.columbia.edu/~evs/stinky/demos/ - galleri med klassiska fraktaler skrivna i stinky

Stinky är ett mycket minimalistiskt programmeringsspråk där man styr en penna som ritar på ett papper med enkla kommandon i följd:

koden blue left step right right right right red step right right right right yellow left step right right right right reduce push black step push right right right right purple step left left left left push skapar denna bild.



Vill man skapa en egen stinky så skapa en html sida och inkludera koden nedan!

<APPLET 
 CODEBASE="http://www.cs.columbia.edu/~evs/stinky/"
 CODE="Stinky.class" 
 width=768 height=300>
<PARAM NAME="program" VALUE="blue left step right right right right red step right right right right yellow left step right right right right reduce push black step push right right right right purple step left left left left push">
</applet>


Det går även att använda stinky för att "teckna doodlar" : http://www.cs.columbia.edu/~evs/stinky/halloffame/

Förbättra programmeringsspråket

Om man vill förbättra programmeringsspråket stinky så utför ändringarna på följande kod och kompilera med java.

/* Stinky Java Applet programming language parser by Eric Siegel */

import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import java.util.Stack;
import java.util.StringTokenizer;

public class Stinky extends Applet
{

    int times;
    Stack stack;
    int depth;
    int depthLimit;
    String program;
    StringTokenizer commands;
    Dimension size;
    Graphics g;
    double stepsize;
    double angle;
    double x;
    double y;
    boolean pendown;
    boolean drawbaseonly;

    public Stinky()
    {
        times = 3;
        depthLimit = 5;
        drawbaseonly = false;
    }

    public void init()
    {
        program = getParameter("program");
        String s = getParameter("fractal");
        if(s != null && s.equalsIgnoreCase("yes"))
            drawbaseonly = true;
    }

    public void paint(Graphics g1)
    {
        size = size();
        g = g1;
        execute();
    }

    public void execute()
    {
        stack = new Stack();
        depth = 0;
        boolean flag = false;
        pendown = true;
        commands = new StringTokenizer(program);
        stepsize = size.width;
        angle = 0.0D;
        x = y = 0.0D;
        while(!flag) 
        {
            for(; commands.hasMoreTokens(); execute_command(commands.nextToken()));
            if(stack.empty())
                flag = true;
            else
                pop();
        }
    }

    public void execute_command(String s)
    {
        if(s.equalsIgnoreCase("left"))
            left();
        else
        if(s.equalsIgnoreCase("right"))
            right();
        else
        if(s.equalsIgnoreCase("step"))
            step();
        else
        if(s.equalsIgnoreCase("reduce"))
            reduce();
        else
        if(s.equalsIgnoreCase("enlarge"))
            enlarge();
        else
        if(s.equalsIgnoreCase("reduce3"))
            reduce3();
        else
        if(s.equalsIgnoreCase("push"))
            push();
        else
        if(s.equalsIgnoreCase("toggle"))
            togglepen();
        else
        if(s.equalsIgnoreCase("black"))
            change_color(Color.black);
        else
        if(s.equalsIgnoreCase("blue"))
            change_color(Color.blue);
        else
        if(s.equalsIgnoreCase("cyan"))
            change_color(Color.cyan);
        else
        if(s.equalsIgnoreCase("grey"))
            change_color(Color.gray);
        else
        if(s.equalsIgnoreCase("gray"))
            change_color(Color.gray);
        else
        if(s.equalsIgnoreCase("darkgray"))
            change_color(Color.darkGray);
        else
        if(s.equalsIgnoreCase("darkgrey"))
            change_color(Color.darkGray);
        else
        if(s.equalsIgnoreCase("lightgray"))
            change_color(Color.lightGray);
        else
        if(s.equalsIgnoreCase("lightgrey"))
            change_color(Color.lightGray);
        else
        if(s.equalsIgnoreCase("green"))
            change_color(Color.green);
        else
        if(s.equalsIgnoreCase("magenta"))
            change_color(Color.magenta);
        else
        if(s.equalsIgnoreCase("orange"))
            change_color(Color.orange);
        else
        if(s.equalsIgnoreCase("pink"))
            change_color(Color.pink);
        else
        if(s.equalsIgnoreCase("red"))
            change_color(Color.red);
        else
        if(s.equalsIgnoreCase("white"))
            change_color(Color.white);
        else
        if(s.equalsIgnoreCase("yellow"))
            change_color(Color.yellow);
        else
            System.out.println((new StringBuilder()).append("Unknown command: ").append(s).toString());
    }

    public void change_color(Color color)
    {
        g.setColor(color);
    }

    public void pop()
    {
        Situation situation = (Situation)stack.pop();
        x = situation.x;
        y = situation.y;
        angle = situation.angle;
        stepsize = situation.stepsize;
        depth = situation.depth;
        pendown = situation.pendown;
        commands = new StringTokenizer(program);
    }

    public void left()
    {
        angle -= 30D;
    }

    public void right()
    {
        angle += 30D;
    }

    public void step()
    {
        double d = x + stepsize * Math.cos((angle / 360D) * 2D * 3.1415926535897931D);
        double d1 = y + stepsize * Math.sin((angle / 360D) * 2D * 3.1415926535897931D);
        if(pendown && (!drawbaseonly || depth > depthLimit))
            g.drawLine((int)x, (int)y, (int)d, (int)d1);
        x = d;
        y = d1;
    }

    public void reduce()
    {
        stepsize /= 2D;
    }

    public void enlarge()
    {
        stepsize *= 2D;
    }

    public void reduce3()
    {
        stepsize /= 3D;
    }

    public void push()
    {
        if(depth <= depthLimit)
        {
            Situation situation = new Situation(x, y, angle, stepsize, depth + 1, pendown);
            stack.push(situation);
        }
    }

    public void togglepen()
    {
        pendown = !pendown;
    }
}


Referenser till aktuell sida


Frivillig gåva till stöd för driften av Gudinna Wiki :