Skip to content

Commit c305725

Browse files
Benjamin HorowitzBenjamin Horowitz
Benjamin Horowitz
authored and
Benjamin Horowitz
committed
Code added
0 parents  commit c305725

File tree

4 files changed

+386
-0
lines changed

4 files changed

+386
-0
lines changed

heathoundary.pde

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//Heat Differential Equations Visualization
2+
//Currently shows thermalization of a block with originally randomly generated internal temperatures. Can be later altered to follow a given distribution.
3+
4+
// Ben Horowitz ([email protected])
5+
6+
int bh = 300;
7+
int bw = 300;
8+
int[][] temp = new int[bh][bw];
9+
int[][] temp1 = new int[bh][bw];
10+
int mint=0;
11+
int maxt=100;
12+
float k = 0.5;
13+
14+
15+
16+
void setup()
17+
{
18+
size(600,600);
19+
fill(255);
20+
frameRate(80);
21+
noStroke();
22+
colorMode(HSB, 255);
23+
intcond();
24+
}
25+
26+
int t;
27+
28+
29+
30+
31+
void intcond()
32+
{
33+
34+
35+
float h;
36+
h= height/bh;
37+
float w;
38+
w= width/bw;
39+
40+
for(int i=0; i < bw; i+=1)
41+
{
42+
for(int j=0; j < bh; j+=1)
43+
{
44+
45+
46+
if(i==0 || i==bw-1 || j==0 || j==bh-1)
47+
{
48+
temp[i][j]=10;
49+
temp1[i][j]=10;
50+
}
51+
else
52+
{
53+
temp[i][j] = int(random(256));
54+
temp1[i][j]= temp[i][j];
55+
}
56+
}
57+
}
58+
}
59+
60+
void draw()
61+
{
62+
int[][] adjust;
63+
float h;
64+
h= height/bh;
65+
float w;
66+
w= width/bw;
67+
t=t+1;
68+
69+
for(int i=0; i*w < width; i+=1)
70+
{
71+
for(int j=0; j*h < height; j+=1)
72+
{
73+
int x;
74+
int a;
75+
76+
if(i==0 || i==bw-1 || j==0 || j==bh-1)
77+
{
78+
a=temp[i][j];
79+
}
80+
else
81+
{
82+
a= (temp1[i+1][j]+temp1[i-1][j]+temp1[i][j+1]+temp1[i][j-1])/4;
83+
}
84+
85+
x = temp[i][j] - a;
86+
temp[i][j] = int(temp[i][j] - k*x);
87+
float value = temp[i][j];
88+
fill(value, 255, 255);
89+
rect(i*w,j*h,w,h);
90+
91+
}
92+
93+
}
94+
95+
for(int i=0; i*w < width; i+=1)
96+
{
97+
for(int j=0; j*h < height; j+=1)
98+
{
99+
temp1[i][j]=temp[i][j];
100+
}
101+
}
102+
}
103+

nbody.pde

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// N-Body Simulation including gravity and "sticky" collisions
2+
3+
// Ben Horowitz ([email protected])
4+
5+
int bh = 100;
6+
int bw = 100;
7+
int n = 60;
8+
float a = 0.0000000002*10;
9+
float[] pos_x = new float[n];
10+
float[] pos_y = new float[n];
11+
float[] vel_x = new float[n];
12+
float[] vel_y = new float[n];
13+
float[] mass = new float[n];
14+
float k = 0.5;
15+
16+
17+
18+
void setup()
19+
{
20+
size(700,700);
21+
fill(255);
22+
noStroke();
23+
frameRate(40);
24+
colorMode(HSB, 255);
25+
intcond();
26+
}
27+
28+
int t=0;
29+
30+
void intcond()
31+
{
32+
\\ initial conditions of the system
33+
fill(255);
34+
35+
float h;
36+
h= height/bh;
37+
float w;
38+
w= width/bw;
39+
40+
for(int i=0; i < n; i+=1)
41+
{
42+
pos_x[i]=int(random(width/2))+width/4;
43+
pos_y[i]=int(random(height/2))+height/4;
44+
vel_x[i]=0.01*(int(random(4))-2)+200*(width/2-pos_y[i]+1)/(pos_x[i]*pos_x[i]+pos_y[i]*pos_y[i]);
45+
vel_y[i]=0.01*(int(random(4))-2)-200*(height/2-pos_x[i]+1)/(pos_x[i]*pos_x[i]+pos_y[i]*pos_y[i]);
46+
mass[i]=int(random(4));
47+
}
48+
}
49+
50+
void draw()
51+
{
52+
fill(255);
53+
rect(0,0,width,height);
54+
55+
int[][] adjust;
56+
float h;
57+
h= height/bh;
58+
float w;
59+
w= width/bw;
60+
61+
for(int i=0; i < n; i+=1)
62+
{
63+
float dv_x = 0;
64+
float dv_y = 0;
65+
for(int j=0; j < n; j+=1){
66+
\\equation of motion
67+
dv_x = dv_x - a*(pos_x[i] - pos_x[j])*mass[i]*mass[j];
68+
dv_y = dv_y - a*(pos_y[i] - pos_y[j])*mass[j]*mass[i];
69+
70+
pos_x[i] = pos_x[i] + vel_x[i]/20;
71+
pos_y[i] = pos_y[i] + vel_y[i]/20;
72+
vel_x[i] = vel_x[i] + dv_x;
73+
vel_y[i] = vel_y[i] + dv_y;
74+
if(pos_x[i]-pos_x[j] < mass[i]/2 & pos_x[i]-pos_x[j]>0){
75+
if(pos_y[i]-pos_y[j] < mass[i]/2 & pos_y[i]-pos_y[j]>0){
76+
mass[i]=mass[i]+mass[j];
77+
mass[j]=0;
78+
vel_x[i]= (mass[i]*vel_x[i]+mass[j]*vel_x[j])/(mass[i]+mass[j]);
79+
vel_y[i]= (mass[i]*vel_y[i]+mass[j]*vel_y[j])/(mass[i]+mass[j]);
80+
}
81+
}
82+
}
83+
}
84+
float x_a = 0;
85+
float y_a = 0;
86+
int U = 0;
87+
for(int i=0; i<n; i+=1){
88+
if(mass[i]>0){
89+
U+=1;
90+
x_a = x_a + pos_x[i];
91+
y_a = y_a + pos_y[i];
92+
}
93+
}
94+
float x_av = x_a/U;
95+
float y_av = y_a/U;
96+
\\draw the boxes
97+
for(int i=0; i<n; i+=1){
98+
if(pos_x[i]<width){
99+
if(pos_y[i]<height){
100+
if(mass[i]>0){
101+
int x = int(pos_x[i] - (width/2 - x_av));
102+
int y = int(pos_y[i] - (height/2 - y_av));
103+
fill(5*i, 255, 255);
104+
rect(x,y,2*mass[i],2*mass[i]);
105+
}
106+
}
107+
}
108+
}
109+
}
110+
111+

poincarebendixson.pde

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//Demonstration of the Poincare Bendixson Theorem
2+
//Eventually might be expanded to identify such limit cycles in a given set of equations.
3+
4+
// Ben Horowitz ([email protected])
5+
6+
7+
//block resolution
8+
int bh = 34;
9+
int bw = 34;
10+
11+
void setup()
12+
{
13+
size(600,600);
14+
fill(255);
15+
//noStroke();
16+
colorMode(HSB, 255);
17+
}
18+
19+
float x;
20+
float[][] xp= new float[bw][bh];
21+
float y;
22+
float[][] yp= new float[bw][bh];
23+
24+
void arrow(int x1, int y1, int x2, int y2) {
25+
line(x1, y1, x2, y2);
26+
float d = min(dist(x1, y1, x2, y2), 40);
27+
pushMatrix();
28+
translate(x2, y2);
29+
float a = atan2(x1-x2, y2-y1);
30+
rotate(a);
31+
line(0, 0, -d/4, -d/4);
32+
line(0, 0, d/4, -d/4);
33+
popMatrix();
34+
}
35+
36+
void draw()
37+
{
38+
stroke(0);
39+
background(120);
40+
for(int i=0; i < bw; i+=1)
41+
{
42+
for(int j=0; j < bh; j+=1)
43+
{
44+
x=(i-bw*0.5)/8;
45+
y=(j-bh*0.5)/8;
46+
float xp =(-1*y+x*(1-x*x-y*y));
47+
float yp = (1*x+y*(1-x*x-y*y));
48+
int xt=i*width/bw;
49+
int yt=j*height/bh;
50+
//float nor=(sqrt((xtp)*(xtp)+(ytp)*(ytp)));
51+
52+
int l;
53+
l =int( width/(sqrt(bw*bw+bh*bh)))+2;
54+
55+
arrow(xt,height-yt,xt+int(10*xp),height-(yt+int(10*yp)));
56+
}
57+
}
58+
noFill();
59+
stroke(180);
60+
ellipse(width/2,height/2, 4*bw,4*bh);
61+
ellipse(width/2,height/2, 16*bw,16*bh);
62+
}
63+

wave.pde

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Wave equation with a fixed boundary condition
2+
// Ben Horowitz ([email protected])
3+
4+
int bh = 100;
5+
int bw = 100;
6+
float[][] temp = new float[bh][bw];
7+
float[][] tm1 = new float[bh][bw];
8+
float[][] tm2 = new float[bh][bw];
9+
float k = 0.5;
10+
11+
12+
13+
void setup()
14+
{
15+
size(600,600);
16+
fill(255);
17+
noStroke();
18+
//frameRate(1);
19+
colorMode(HSB, 255);
20+
intcond();
21+
}
22+
23+
int t;
24+
25+
boolean incircle(float i, float j)
26+
{
27+
28+
float h;
29+
h= height/bh;
30+
float w;
31+
w= width/bw;
32+
33+
float d= min(height,width);
34+
35+
return( ((width/2-j*w)*(width/2-j*w) + (height/2-i*h)*(height/2-i*h))<((d/2-10)*(d/2-10)));
36+
37+
}
38+
39+
40+
void intcond()
41+
{
42+
43+
for(int i=0; i < bw; i+=1)
44+
{
45+
for(int j=0; j < bh; j+=1)
46+
{
47+
48+
if(!incircle(i,j))
49+
{
50+
tm2[i][j]=tm1[i][j]=128;
51+
}
52+
/*
53+
else if(i!=0 && i!=bw-1 && j==0 || j==bh-1)
54+
{
55+
tm2[i][j]=tm1[i][j]=128;
56+
}
57+
*/
58+
else
59+
{
60+
61+
tm2[i][j]=tm1[i][j]=10;
62+
}
63+
}
64+
}
65+
}
66+
67+
void draw()
68+
{
69+
int[][] adjust;
70+
float h;
71+
h= height/bh;
72+
float w;
73+
w= width/bw;
74+
t=t+1;
75+
76+
for(int i=0; i < bw; i+=1)
77+
{
78+
for(int j=0; j < bh; j+=1)
79+
{
80+
float x;
81+
float a;
82+
83+
84+
if(!incircle(i,j))
85+
{
86+
temp[i][j] = tm1[i][j];
87+
}
88+
else
89+
{
90+
//a= (tm1[i+1][j]+tm1[i-1][j]+tm1[i][j+1]+tm1[i][j-1])/4;
91+
temp[i][j] = 2*tm1[i][j] - tm2[i][j] + (tm1[i+1][j]+tm2[i-1][j]+tm1[i][j+1]+tm2[i][j-1]-4*tm1[i][j]) /4;
92+
}
93+
float value = temp[i][j];
94+
fill(100, 0, value);
95+
//fill(value, 255, 255);
96+
rect(i*w,j*h,w,h);
97+
tm2[i][j]=tm1[i][j];
98+
tm1[i][j]=temp[i][j];
99+
}
100+
/*
101+
float[][] swap = tm2;
102+
tm2 = tm1;
103+
tm1 = temp;
104+
temp = swap;
105+
*/
106+
}
107+
108+
}
109+

0 commit comments

Comments
 (0)