예제 #1
0
    public static RenderQueue fromString(string commands)
    {
        RenderQueue queue = new RenderQueue();

        foreach (char c in commands)
        {
            queue.enqueue(dictionary[c]);
        }

        return(queue);
    }
    private void updatePosition()
    {
        RenderCommand rc         = RenderCommand.NONE;
        bool          updatedPos = false;

        while (!(word.empty()) && !updatedPos)
        {
            rc = word.dequeue();
            switch (rc)
            {
            case RenderCommand.FORWARD:
            case RenderCommand.FORWARD2:
                float x;
                float y;

                double theta = Math.Atan2(pos.y, pos.x);
                x = (float)(forward * Math.Cos(theta));
                y = (float)(forward * Math.Sin(theta));

                //Debug.Log("THETA: X: " + x + " Y: " + y);

                double r = Math.Sqrt(x * x + y * y);
                x = (float)(r * Math.Cos(startHeading));
                y = (float)(r * Math.Sin(startHeading));
                //Debug.Log("R: X: " + x + " Y: " + y);

                pos.x += x;
                pos.y += y;

                updatedPos = true;
                break;

            case RenderCommand.RIGHT:
                startHeading += (turn * Math.PI / 180.0);
                break;

            case RenderCommand.LEFT:
                startHeading += (-turn * Math.PI / 180.0);
                break;

            case RenderCommand.PUSH:
            case RenderCommand.POP:
            case RenderCommand.IGNORE:
            default:
                break;
            }

            word.enqueue(rc);
        }
    }
예제 #3
0
    /**
     * Perform the rewriting for the given number of generations.
     * @param seed starting word
     * @param numGenerations how many times to rewrite the word with our rules
     * @return the nth-generation word
     */
    public RenderQueue rewrite(RenderQueue seed, int numGenerations)
    {
        // Implementation is just to treat the seed as the "previous" output
        // queue and then move the output queue to be the input queue for
        // the next generation, apply the rules to any axioms for which we
        // have rules (the other axioms are copied over to the output
        // directly). Repeat this process for the given number of generations.
        RenderQueue output = seed.copy();
        RenderQueue input;

        for (int gen = 0; gen < numGenerations; gen++)
        {
            input  = output; // last generation's output is this one's input
            output = new RenderQueue();
            while (!input.empty())
            {
                RenderCommand nextInput = input.dequeue();
                // look through the from-list to find a rule to invoke
                bool ruleFound = false;
                for (int i = 0; !ruleFound && i < from.Length; i++)
                {
                    if (from[i] == nextInput)
                    {
                        ruleFound = true;
                        // append a copy of this rule's right side onto the
                        // end of the output queue
                        output.append(to[i]);
                    }
                }
                // if no rule was found, then just copy this command to output
                if (!ruleFound)
                {
                    output.enqueue(nextInput);
                }
            }
        }
        return(output);
    }