From: Randolph Wang <rywang@CS.Princeton.EDU>
Date: Mon, 23 Feb 2004 01:17:30 -0500 (EST)
To: tbroderi@Princeton.EDU
Cc: randy_class@CS.Princeton.EDU
Subject: Re: your Turtle exploits


Kevin is the dude who's most responsible for designing cs126.  I gave
them copies of the code.


------- Start of forwarded message -------
From: Kevin Wayne <wayne@CS.Princeton.EDU>
Subject: Re: spam: tbroderi@Princeton.EDU: Re: your Turtle exploits
To: rywang@CS.Princeton.EDU
Date: Sun, 22 Feb 2004 19:15:18 -0500 (EST)
Cc: chizhang@CS.Princeton.EDU, pcalamia@CS.Princeton.EDU,
        diego@CS.Princeton.EDU, dgabai@CS.Princeton.EDU,
        yshao@CS.Princeton.EDU, doug@CS.Princeton.EDU, wayne@CS.Princeton.EDU
In-Reply-To: <200402221334.i1MDY08o025841@bolle.CS.Princeton.EDU> from "Randolph Wang" at Feb 22, 4 08:34:00 am
Content-Type: text/plain; charset=US-ASCII
X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on 
	memphis.CS.Princeton.EDU
X-Spam-Status: No, hits=-99.5 required=5.0 tests=AWL,USER_IN_WHITELIST 
	autolearn=no version=2.63
X-Spam-Level: 

Thanks, it's very nice. Please encourage Tamara to share more of her
physics with us if she has any more interesting examples. Here's my
attempt at simplifying things. I tried to remove all of the special
cases. Also, with Laplace's equation, it's usually numerically ok
(and sometimes better) to use the most up-to-date version of each
value so I dispensed with the second array. I wasn't too careful
about off-by-ones with the physics...

	-Kevin


import java.awt.Color;

public class Laplace {

    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        int SIZE = 3*N;
        Turtle.create(SIZE+1, SIZE+1);
        double[][] V = new double[SIZE+1][SIZE+1];

        // precompute colors
        Color[] colors = new Color[101];
        for (int i = 0; i <= 100; i++) {
            int red = 255*i/100;
            int green = 128;
            int blue = 255*(100-i)/100;
            colors[i] = new Color(red, green, blue);
        }
        for (int i = 0; i <= 100; i = i + 10)
            colors[i] = Color.white;
       

        // initialize all points with reasonable starting values for the potential
        for (int i = 1; i <= SIZE/2; i++)
            for (int j = 1; j <= SIZE/2; j++)
                V[SIZE-i][j] = V[i][SIZE-j] = V[SIZE-i][SIZE-j] = V[i][j] = 100.0 * (i + j) / (SIZE);

        // outer boundary
        for (int t = 0; t <= SIZE; t++)
            V[0][t] = V[t][0] = V[SIZE][t] = V[t][SIZE] = 0;

        // inner boundary             
        for (int i = N; i < 2*N; i++)
            for (int j = N; j < 2*N; j++)
                V[i][j] = 100;
                


        // numerically solve Laplace's equation 
        while(true) {

            // do 100 updates before drawing to screen
            for (int t = 0; t < 100; t++) {
                for (int i = 1; i < SIZE; i++) {
                    for (int j = 1; j < SIZE; j++) {
                        if (!(i >= N && i < 2*N && j >= N && j < 2*N))
                        V[i][j] = 0.25* (V[i-1][j] + V[i+1][j] + V[i][j-1] + V[i][j+1]);
                    }
                }
            }

            // draw
            for (int i = 0; i <= SIZE; i++) {
                for (int j = 0; j <= SIZE; j++) {
                    Turtle.setColor(colors[(int) Math.round(V[i][j])]);
                    Turtle.fly(i, j);
                    Turtle.spot(1);
                }
            }
            Turtle.render();
        }
    }
}
------- End of forwarded message -------


Re: your Turtle exploits / Randolph Wang