Skip to content

Commit 7313ae6

Browse files
committed
Added Objects implementation, read README for more information
1 parent 62013d1 commit 7313ae6

File tree

11 files changed

+556
-3
lines changed

11 files changed

+556
-3
lines changed

IntegerSequencesObjects/Fibonacci.pde

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
class fibonacci extends Sequence{
2+
String author(){
3+
return "Leonardo Bonacci";
4+
}
5+
6+
String description(){
7+
return "Fibonacci sequence returns 1 for first two and the sum of last two numbers.";
8+
}
9+
10+
int compute(int i){
11+
if(i==1 || i==2)
12+
return 1; //a(1)=1, a(2)=2
13+
if(i>1)
14+
return compute(i-1)+compute(i-2); //a(n)=a(n-1)+a(n-2)
15+
return -1; //Casos indefinidos
16+
}
17+
18+
19+
void display(int i){
20+
float nH=remapH(compute(i)*2), nW=remapW(compute(i)*2);
21+
switch(counter){
22+
case 0: //Primera coordenada tomada como referencia
23+
if(i==1){
24+
x=remapW(detWid()-sum(i+1));
25+
y=remapH(detHei()-sum(i));
26+
arc(x,y,nW,nH,HALF_PI,PI);
27+
}
28+
if(i==2){
29+
arc(x,y,nW,nH,0,HALF_PI);
30+
counter=3;
31+
}
32+
break;
33+
case 1: //Condicion para todos los arcos que son desde PI/2 hasta PI
34+
x=x+remapW(compute(i-2));
35+
arc(x,y,nW,nH,HALF_PI,PI);
36+
counter++;
37+
break;
38+
case 2: //Condicion para todos los arcos que son desde 0 hasta PI/2
39+
y=y-remapH(compute(i-2));
40+
arc(x,y,nW,nH,0,HALF_PI);
41+
counter++;
42+
break;
43+
case 3: //Condicion para todos los arcos que son desde 3PI/2 hasta 2PI
44+
x=x-remapW(compute(i-2));
45+
arc(x,y,nW,nH,PI+HALF_PI,TWO_PI);
46+
counter++;
47+
break;
48+
case 4: //Condicion para todos los arcos que son desde PI hasta 3PI/2
49+
y=y+remapH(compute(i-2));
50+
arc(x,y,nW,nH,PI,PI+HALF_PI);
51+
counter=1;
52+
break;
53+
}
54+
dibujoRect(i, nW, nH);
55+
stroke(255);
56+
57+
}
58+
59+
int sum(int i){ //Sumatoria modificada para hallar la primera coordenada x, y de Fibonacci
60+
int resultado=0;
61+
for(int j=0;(i+j*4)<=n;j++)
62+
resultado+=compute(i+j*4);
63+
return resultado;
64+
}
65+
66+
void dibujoRect(int i, float nW, float nH){
67+
stroke(0,100,100);
68+
int a=compute(i);
69+
float textX, textY;
70+
textSize(map(a,0,compute(n),5,50));
71+
switch(i%4){
72+
case 1:
73+
rect(x-nW/2,y,nW/2,nH/2);
74+
fill(255);
75+
text(a,x-nW/2+nW/4,y+nH/4);
76+
break;
77+
case 2:
78+
rect(x,y,nW/2,nH/2);
79+
fill(255);
80+
text(a,x+nW/5,y+nH/4);
81+
break;
82+
case 3:
83+
rect(x,y-nH/2,nW/2,nH/2);
84+
fill(255);
85+
text(a,x+nW/5,y-nH/2+nH/4);
86+
break;
87+
case 0:
88+
rect(x-nW/2,y-nH/2,nW/2,nH/2);
89+
fill(255);
90+
text(a,x-nW/2+nW/4,y-nH/2+nH/4);
91+
break;
92+
}
93+
noFill();
94+
stroke(255);
95+
}
96+
97+
}

IntegerSequencesObjects/Golomb.pde

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class golomb extends Sequence{
2+
3+
String author(){
4+
return "Solomon Wolf Golomb";
5+
}
6+
7+
String description(){
8+
return "a(n) is the number of times that n occurs in the sequence, starting with a(1)=1";
9+
}
10+
11+
int compute(int i){ //Implementación función Golomb
12+
if(i==1) //Definición de wikipedia: a(1)=1
13+
return 1;
14+
if(i>1) //a(n+1)=1+a(n+1-a(a(n)))
15+
return 1+compute(i-compute(compute(i-1))); //a(n)=1+a(n-a(a(n-1)))
16+
return 0; //Casos indefinidos
17+
}
18+
19+
void display(int n){
20+
int nAct=1;
21+
float div;
22+
float sizeWidth;
23+
for(int i=0; i<=compute(n); i++){
24+
x=remapW(i);
25+
sizeWidth=width/compute(n);
26+
rect(x, 0, sizeWidth, height-1);
27+
28+
while(compute(nAct+1)==i && nAct+1<=n)
29+
nAct++;
30+
div=contarDivisiones(nAct);
31+
for(int j=0; j<div; j++){
32+
rect(remapW(i-1), j*height/div, width/compute(n), height/div);
33+
text(compute(nAct),remapW(i-1)+width/(8*compute(n)),j*height/div+height/(2*div));
34+
}
35+
while(compute(nAct)==i)
36+
nAct++;
37+
}
38+
}
39+
40+
float contarDivisiones(int i){ //Divisiones verticales
41+
float divisiones=1;
42+
while(compute(i)==compute(i-1) && i<=n){
43+
divisiones++;
44+
i--;
45+
}
46+
return divisiones;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Objects declaration
2+
fibonacci fiboSeq;
3+
golomb goloSeq;
4+
juggler juggSeq;
5+
6+
int n=2, serie, mode, counter;
7+
8+
void setup() {
9+
size(840,540);
10+
background(0);
11+
colorMode(HSB, 360, 100, 100);
12+
noFill();
13+
smooth();
14+
// Object init
15+
fiboSeq = new fibonacci();
16+
goloSeq = new golomb();
17+
juggSeq = new juggler();
18+
}
19+
20+
void draw() {
21+
background(0);
22+
dibujoSerie();
23+
}
24+
25+
void dibujoSerie() {
26+
switch(serie) {
27+
case 0: //Implementación gráfico de serie Fibonacci
28+
switch(mode){
29+
case 0:
30+
for(int i=1; i<=n; i++)
31+
fiboSeq.display(i);
32+
counter=0;
33+
break;
34+
case 1:
35+
fiboSeq.barChart(n);
36+
break;
37+
case 2:
38+
fiboSeq.lineChart(n);
39+
break;
40+
case 3:
41+
fiboSeq.curveFitting(n);
42+
break;
43+
}
44+
break;
45+
case 1: //Implementación gráfico de serie Golomb
46+
switch(mode){
47+
case 0:
48+
goloSeq.display(n);
49+
break;
50+
case 1:
51+
goloSeq.barChart(n);
52+
break;
53+
case 2:
54+
goloSeq.lineChart(n);
55+
break;
56+
case 3:
57+
goloSeq.curveFitting(n);
58+
break;
59+
}
60+
break;
61+
case 2: //Implementación gráfico de serie Juggler
62+
switch(mode){
63+
case 0:
64+
juggSeq.display(n);
65+
break;
66+
case 1:
67+
juggSeq.barChart(n);
68+
break;
69+
case 2:
70+
juggSeq.lineChart(n);
71+
break;
72+
case 3:
73+
juggSeq.curveFitting(n);
74+
break;
75+
}
76+
break;
77+
}
78+
}
79+
80+
void keyPressed() {
81+
// Object use:
82+
switch(key){
83+
case '>':
84+
serie=(serie+1) % 3;
85+
break;
86+
case '<':
87+
if(serie==0){
88+
serie=2;
89+
}else{
90+
serie--;
91+
}
92+
break;
93+
case '+':
94+
n++;
95+
println(n + " term Fibonacci value is: " + fiboSeq.compute(n));
96+
println(n + " term Golomb value is: " + goloSeq.compute(n));
97+
println(n + " term Juggler value is: " + Arrays.toString(juggSeq.juggler(n)));
98+
break;
99+
case '-':
100+
if(n>1){
101+
n--;
102+
println(n + " term Fibonacci value is: " + fiboSeq.compute(n));
103+
println(n + " term Golomb value is: " + goloSeq.compute(n));
104+
println(n + " term Juggler value is: " + Arrays.toString(juggSeq.juggler(n)));
105+
}
106+
break;
107+
case 'p':
108+
fiboSeq.printFirstN(n, "Fibonacci");
109+
goloSeq.printFirstN(n, "Golomb");
110+
juggSeq.printFirstN(n, "Juggler (max value of array)");
111+
break;
112+
case '.':
113+
mode= (mode+1) % 4;
114+
break;
115+
case ',':
116+
if(mode==0){
117+
mode=3;
118+
}else{
119+
mode--;
120+
}
121+
break;
122+
}
123+
}

IntegerSequencesObjects/Juggler.pde

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class juggler extends Sequence{
2+
3+
String author(){
4+
return "Clifford Alan Pickover";
5+
}
6+
7+
String description(){
8+
return "Starts with a positive integer a(0) and each subsequent term is defined by the relation: a(k+1)=floor(a(k)^(1/2)) if a(k) is even, else a(k+1)=floor(a(k)^(3/2))";
9+
}
10+
11+
boolean isEven(int i) {
12+
if(i%2==0)
13+
return true;
14+
return false;
15+
}
16+
17+
int proc(int i) {
18+
if(isEven(i))
19+
return floor(sqrt(i));
20+
return floor(sqrt(i)*sqrt(i)*sqrt(i));
21+
}
22+
23+
int tamArr(int i) {
24+
int a;
25+
for(a=0; i>1; a++)
26+
i=proc(i);
27+
return a+1;
28+
}
29+
30+
int compute(int i){
31+
int tam=tamArr(i), number=0;
32+
int a[]= new int[tam];
33+
a=juggler(i);
34+
for(int j=0; j<tam; j++){
35+
if(a[j]>number)
36+
number=a[j];
37+
}
38+
return number;
39+
}
40+
41+
int[] juggler(int i){
42+
int tam=tamArr(i);
43+
int jugg[]= new int[tam];
44+
jugg[0]=i;
45+
for(int j=0; j+1<tam; j++) {
46+
jugg[j+1]=proc(i);
47+
i=jugg[j+1];
48+
}
49+
return jugg;
50+
}
51+
52+
float diam(){
53+
if(width>height)
54+
return height;
55+
return width;
56+
}
57+
58+
void display(int n){
59+
int tam=tamArr(n);
60+
int arr[]=new int[tam];
61+
arr=juggler(n);
62+
int sum=0;
63+
float start=0, stop=0, textX=0, textY=0;
64+
for(int i=0;i<tam;i++){
65+
sum+=arr[i];
66+
}
67+
float rad=diam()/2;
68+
for(int i=0;i<tam;i++){
69+
start=stop;
70+
stop+=float(arr[i])*TWO_PI/sum;
71+
arc(width/2,height/2,rad*2, rad*2, start, stop, PIE);
72+
textX=3*rad/4*(cos((stop-start)/2+start));
73+
textY=3*rad/4*(sin((stop-start)/2+start));
74+
text(arr[i], width/2+textX , height/2+textY);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)