Skip to content

Commit

Permalink
archimedean / dual spiral sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mayakraft committed Jul 28, 2016
1 parent f5857b5 commit b42b6c8
Show file tree
Hide file tree
Showing 30 changed files with 196 additions and 50 deletions.
Expand Up @@ -15,8 +15,9 @@ int width = 800;
int height = 800;

// shape
int REVOLUTIONS = 50;
float SPACING = 7.0f;
int REVOLUTIONS = 24;
int START_RADIUS = 3;
float SPACING = 15.0f;

int main(int argc, char **argv){
time_t t;
Expand All @@ -35,7 +36,7 @@ int main(int argc, char **argv){

float x, y;
for(int i = START_RADIUS; i < REVOLUTIONS; i++){
float circleResolution = i * 10; // how many points in one full circle
float circleResolution = i * 20; // how many points in one full circle
for(float a = 0; a < TWOPI; a += TWOPI/circleResolution){

float revolution = a/TWOPI; // 0-1, progress of one revolution
Expand Down
File renamed without changes.
55 changes: 55 additions & 0 deletions 01-spiral-archimedean/002-polygons.c
@@ -0,0 +1,55 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define TWOPI 6.28318530718

// output
char filename[128] = "002-polygons.svg\0";
char path[128] = "out/\0";

// document
int width = 800;
int height = 800;

// shape
int POLYGONS = 72;
int START_POLYGON = 3;
int radius = 360;

int main(int argc, char **argv){
time_t t;
srand((unsigned) time(&t));

strcat(path, filename);
FILE *file = fopen(path, "w");
// HEADER
fprintf(file, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fprintf(file, "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" ");
fprintf(file, "x=\"0px\" y=\"0px\" width=\"%dpx\" height=\"%dpx\" ", width, height);
fprintf(file, "viewBox=\"0 0 %d %d\">\n", width, height);
// BODY
fprintf(file, "<g>\n");
fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open polyline

float x, y;
for(int i = START_POLYGON; i < POLYGONS+START_POLYGON; i++){
float circleResolution = TWOPI/i; // angle of increment inside each circle
for(float a = 0; a < TWOPI; a += circleResolution){

float revolution = a/TWOPI; // 0-1, progress of one revolution

x = width*.5 + sinf(a) * radius;
y = height*.5 - cosf(a) * radius;
fprintf(file, "%.2f,%.2f ", x, y);
}
}
fprintf(file, "\"/>\n"); // closing polyline

fprintf(file, "</g>\n");
fprintf(file, "</svg>");
fclose(file);
return 0;
}
6 changes: 6 additions & 0 deletions 01-spiral-archimedean/out/002-polygons.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -7,7 +7,7 @@
#define TWOPI 6.28318530718

// output
char filename[128] = "000-in-out.svg\0";
char filename[128] = "000-base.svg\0";
char path[128] = "out/\0";

// document
Expand All @@ -18,6 +18,7 @@ int height = 800;
int REVOLUTIONS = 4;
float SPACING = 40.0f;
float ROTATE_2ND = .5;//.3; // angle of the second spiral against the first (percent of 2pi)
float circleResolution = 5 * 60;

int main(int argc, char **argv){
time_t t;
Expand All @@ -34,11 +35,9 @@ int main(int argc, char **argv){
fprintf(file, "<g>\n");
fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open polyline

float divider = 5 * 60;

// spiral in toward the center
for(int i = REVOLUTIONS-1; i >= 0; i--){
for(float a = TWOPI; a >= 0 ; a -= TWOPI/divider){
for(float a = TWOPI; a >= 0 ; a -= TWOPI/circleResolution){
float radius = 2 * (i + a/TWOPI);
float x = width*.5 + cos(a) * radius * SPACING;
float y = height*.5 + sin(a) * radius * SPACING;
Expand All @@ -52,7 +51,7 @@ int main(int argc, char **argv){

// spiral out from the center
for(int i = 0; i < REVOLUTIONS; i++){
for(float a = 0; a <= TWOPI; a += TWOPI/divider){
for(float a = 0; a <= TWOPI; a += TWOPI/circleResolution){
float radius = 2 * (i + a/TWOPI);
float x = width*.5 + cos(a-TWOPI*ROTATE_2ND) * radius * SPACING;
float y = height*.5 + sin(a-TWOPI*ROTATE_2ND) * radius * SPACING;
Expand Down
Expand Up @@ -7,7 +7,7 @@
#define TWOPI 6.28318530718

// output
char filename[128] = "002-in-out-sine-wave.svg\0";
char filename[128] = "001-dual-sine-wave.svg\0";
char path[128] = "out/\0";

// document
Expand Down Expand Up @@ -42,10 +42,8 @@ int main(int argc, char **argv){
// spiral in toward the center
for(int i = REVOLUTIONS-1; i >= 0; i--){
for(float a = TWOPI; a >= 0; a -= TWOPI/divider){

float revolution = a/TWOPI;
float radius = 2*(i+revolution) + sin(a*wobbleFreq) * wobbleMag * 2*(i+revolution);

float x = width*.5 + cos(a) * radius * SPACING;
float y = height*.5 + sin(a) * radius * SPACING;
fprintf(file, "%.2f,%.2f ", x, y);
Expand All @@ -59,10 +57,8 @@ int main(int argc, char **argv){
// spiral out from the center
for(int i = 0; i < REVOLUTIONS; i++){
for(float a = 0; a <= TWOPI; a += TWOPI/divider){

float revolution = a/TWOPI;
float radius = 2*(i+revolution) + sin(a*wobbleFreq) * wobbleMag * 2*(i+revolution);

float x = width*.5 + cos(a-TWOPI*ROTATE_2ND) * radius * SPACING;
float y = height*.5 + sin(a-TWOPI*ROTATE_2ND) * radius * SPACING;
fprintf(file, "%.2f,%.2f ", x, y);
Expand Down
Expand Up @@ -9,13 +9,17 @@
#define TWOPI 6.28318530718

// output
char filename[128] = "003-logarithmic.svg\0";
char filename[128] = "002-dual-logarithmic.svg\0";
char path[128] = "out/\0";

// document
int width = 800;
int height = 800;

float SCALE = 6;
float REVOLUTIONS = 4;//15.0;//18.0;
float circleResolution = 5 * 60;

int main(int argc, char **argv){
time_t t;
srand((unsigned) time(&t));
Expand All @@ -31,17 +35,10 @@ int main(int argc, char **argv){
fprintf(file, "<g>\n");
fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open polyline

float SCALE = 6;
float REVOLUTIONS = 4;//15.0;//18.0;

float divider = 5 * 60;

// spiral in toward the center
for(int i = REVOLUTIONS-1; i >= 0; i--){
for(float a = TWOPI; a >= 0; a -= TWOPI/divider){

for(float a = TWOPI; a >= 0; a -= TWOPI/circleResolution){
float radius = SCALE * powf(i+a/TWOPI, 3);

float x = width*.5 + cos(a) * radius;
float y = height*.5 + sin(a) * radius;
fprintf(file, "%.2f,%.2f ", x, y);
Expand All @@ -54,10 +51,8 @@ int main(int argc, char **argv){

// spiral out from the center
for(int i = 0; i < REVOLUTIONS; i++){
for(float a = 0; a <= TWOPI; a += TWOPI/divider){

for(float a = 0; a <= TWOPI; a += TWOPI/circleResolution){
float radius = SCALE * powf(i+a/TWOPI, 3);

float x = width*.5 + cos(a-TWOPI*.5) * radius;
float y = height*.5 + sin(a-TWOPI*.5) * radius;
fprintf(file, "%.2f,%.2f ", x, y);
Expand Down
Expand Up @@ -9,7 +9,7 @@
#define TWOPI 6.28318530718

// output
char filename[128] = "004-phyllotaxic-join.svg\0";
char filename[128] = "003-dual-phyllotaxic-2.svg\0";
char path[128] = "out/\0";

// document
Expand All @@ -19,6 +19,11 @@ int height = 800;
// shape
int REVOLUTIONS = 8;
float SPACING = 7.0f;
float wobbleFreq = 13.0;
float wobbleMag = 1;
// wobbleFreq = 7.0;
// wobbleMag = 0.33;
float circleResolution = 10 * 60;

int main(int argc, char **argv){
time_t t;
Expand All @@ -36,14 +41,8 @@ int main(int argc, char **argv){
fprintf(file, "<g>\n");
fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open polyline

float divider = 10 * 60;
float wobbleFreq = 13.0;
float wobbleMag = 1;
// wobbleFreq = 7.0;
// wobbleMag = 0.33;

for(int i = REVOLUTIONS-1; i >= 0; i--){
for(float a = TWOPI; a >= 0; a -= TWOPI/divider){
for(float a = TWOPI; a >= 0; a -= TWOPI/circleResolution){
float revolution = 2*(i+a/TWOPI);
float SCALE = 1.5 * revolution;
float x = width*.5 + cos(a) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag );
Expand All @@ -57,7 +56,7 @@ int main(int argc, char **argv){
// fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" stroke-miterlimit=\"10\" points=\""); // hanging open polyline

for(int i = 0; i < REVOLUTIONS; i++){
for(float a = 0; a < TWOPI; a += TWOPI/divider){
for(float a = 0; a < TWOPI; a += TWOPI/circleResolution){
float revolution = 2*(i+a/TWOPI);
float SCALE = 1.5 * revolution;
float x = width*.5 + cos(a-TWOPI*.5) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag );
Expand Down
Expand Up @@ -9,7 +9,7 @@
#define TWOPI 6.28318530718

// output
char filename[128] = "004-phyllotaxic.svg\0";
char filename[128] = "003-dual-phyllotaxic.svg\0";
char path[128] = "out/\0";

// document
Expand All @@ -19,6 +19,14 @@ int height = 800;
// shape
int REVOLUTIONS = 6;
float SPACING = 7.0f;
float SCALE = 26;
float wobbleFreq = 19.0;
float wobbleMag = 0.4;
// float SCALE = 45;
// float REVOLUTIONS = 7;
// float wobbleFreq = 7.0;
// float wobbleMag = 0.33;
float circleResolution = 5 * 60;

int main(int argc, char **argv){
time_t t;
Expand All @@ -36,17 +44,8 @@ int main(int argc, char **argv){
fprintf(file, "<g>\n");
fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open polyline

float divider = 5 * 60;
float SCALE = 26;
float wobbleFreq = 19.0;
float wobbleMag = 0.4;
// float SCALE = 45;
// float REVOLUTIONS = 7;
// float wobbleFreq = 7.0;
// float wobbleMag = 0.33;

for(int i = REVOLUTIONS-1; i >= 0; i--){
for(float a = TWOPI; a >= 0; a -= TWOPI/divider){
for(float a = TWOPI; a >= 0; a -= TWOPI/circleResolution){
float revolution = 2*(i+a/TWOPI);
float x = width*.5 + cos(a) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag);
float y = height*.5 + sin(a) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag);
Expand All @@ -59,7 +58,7 @@ int main(int argc, char **argv){
// fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" stroke-miterlimit=\"10\" points=\""); // hanging open quote

for(int i = 0; i < REVOLUTIONS; i++){
for(float a = 0; a < TWOPI; a += TWOPI/divider){
for(float a = 0; a < TWOPI; a += TWOPI/circleResolution){
float revolution = 2*(i+a/TWOPI);
float x = width*.5 + cos(a-TWOPI*.5) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag);
float y = height*.5 + sin(a-TWOPI*.5) * SCALE * (revolution + sin(a*wobbleFreq)*wobbleMag);
Expand All @@ -71,7 +70,7 @@ int main(int argc, char **argv){
// fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" stroke-miterlimit=\"10\" points=\""); // hanging open quote

// int i = 14;
// for(float a = 0; a <= TWOPI+.0001; a += TWOPI/divider){
// for(float a = 0; a <= TWOPI+.0001; a += TWOPI/circleResolution){
// float wobbleIncr = 5*sqrt(i + 2*a/TWOPI);
// wobbleIncr = 1.0;
// x = width*.5 + SCALE * cos(a) * (i + sin(a*wobbleFreq)*wobbleMag*wobbleIncr );
Expand Down
6 changes: 6 additions & 0 deletions 02-spiral-dual/out/003-dual-phyllotaxic-2.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
86 changes: 86 additions & 0 deletions 05-sinewaves/002-wrapping-sines.c
@@ -0,0 +1,86 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>

#define TWOPI 6.28318530718

// output
char filename[128] = "002-wrapping-sines.svg\0";
char path[128] = "out/\0";
// path[0] = '\0';

// document
int width = 1200;
int height = 900;

// groups
float NUM_GROUPS = 6;
float GROUP_SPACING = 200.0;
float GROUP_NUM_VERTICALS = 4.0;

// sine curve
float FREQUENCY = .028;
float AMPLITUDE = 10;


int mod2PosNeg(int iterator){
if(iterator%2 == 0){ return -1; }
return 1;
}

int main(int argc, char **argv){
time_t t;
srand((unsigned) time(&t));
float seed = rand()%1000/100.0;

strcat(path, filename);
FILE *file = fopen(path, "w");
fprintf(file, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fprintf(file, "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" ");
fprintf(file, "x=\"0px\" y=\"0px\" width=\"%dpx\" height=\"%d", width, height);
fprintf(file, "px\" viewBox=\"0 0 %d %d", width, height);
fprintf(file, "\" xml:space=\"preserve\">\n<g>\n");

for(int i = 0; i < NUM_GROUPS; i++){
float xGroup = GROUP_SPACING * i; // the major X axis of each group

// 4 sine waves in every group
for(int j = 0; j < GROUP_NUM_VERTICALS; j++){
// pattern: L L R R L L ...
int direction = mod2PosNeg(j*.5); // -1 or +1

// spacing
float percentOfGroup = (j / GROUP_NUM_VERTICALS);
if(j == 0) percentOfGroup = .175;
if(j == 1) percentOfGroup = .3;
if(j == 2) percentOfGroup = .625;
if(j == 3) percentOfGroup = .75;
float xInternalSpacing = percentOfGroup * GROUP_SPACING;

// affine scale
float affine = 1.0; // multiply to all
if(j == 0 || j == GROUP_NUM_VERTICALS-1){ // multiply to outer curves
// affine = 1.0 + 1.0 * (i/(NUM_GROUPS-1));
affine = 1.75;
}

fprintf(file, "<polyline fill=\"none\" stroke=\"#000000\" points=\""); // hanging open quote
for(float h = 0; h < height; h++){

float sine = sinf(h*FREQUENCY) * AMPLITUDE;
float x = xGroup + xInternalSpacing + sine * direction * affine;
float y = h;

fprintf(file, "%.2f,%.2f ", x, y);
}
fprintf(file, "\"/>\n"); // closing quote
}
}

fprintf(file, "</g>\n");
fprintf(file, "</svg>");
fclose(file);
return 0;
}
File renamed without changes.
File renamed without changes.

0 comments on commit b42b6c8

Please sign in to comment.