Ulam Spiral in Processing

/* So I found the YouTube Channel of ViHart. She's got these quirky, awesome math videos involving lots of doodles. Her most recent video is on the Ulam Spiral, so I thought I would try to recreate it in processing. Enjoy. Here's the video of her drawing. http://www.youtube.com/watch?v=Yhlv5Aeuo_k */ void setup() { size(400, 400); background(255); } void draw() { int cy = height / 2; int cx = width / 2; int points = 10000000; int[][] direction = { {0,1}, // Down {1,0}, // Right {0,-1}, // Up {-1,0} // Left }; int stretch = 0; int repeater = 2; int current_direction = 0; for (int i = 1; i <= points; i++) { if (isPrime(i)) { point(cx, cy); } cx += direction[current_direction][0]; cy += direction[current_direction][1]; stretch += 1; if (stretch * 2 == repeater) { current_direction = (current_direction + 1) % 4; } if (stretch == repeater) { current_direction = (current_direction + 1) % 4; repeater += 2; stretch = 0; } } } boolean isPrime(int n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; int m = (int)sqrt(n); for (int i = 3; i <= m; i += 2) { if (m % i == 0) { return false; } } return true; }
-
igoumeninja likes this
-
jcchurch posted this