mirror of
				https://github.com/osmarks/random-stuff
				synced 2025-10-29 21:13:00 +00:00 
			
		
		
		
	Mandelbrot implementations for some reason
This commit is contained in:
		
							
								
								
									
										42
									
								
								mandelbrot.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								mandelbrot.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| #define STB_IMAGE_WRITE_IMPLEMENTATION | ||||
| #include "stb_image_write.h" | ||||
| #include <complex.h> | ||||
| #include <stdint.h> | ||||
|  | ||||
| const int WIDTH = 768; | ||||
| const int WIDTH_THIRD = 768 / 3; | ||||
| const int HEIGHT = 512; | ||||
| const int HEIGHT_HALF = HEIGHT / 2; | ||||
| const int MAX_ITERS = 32; | ||||
| typedef struct { | ||||
|     uint8_t r; | ||||
|     uint8_t g; | ||||
|     uint8_t b; | ||||
| } pixel; | ||||
|  | ||||
| int main(int argc, char **argv) { | ||||
|     char *outfile = argc >= 2 ? argv[1] : "/tmp/mandelbrot.png"; | ||||
|     stbi_write_png_compression_level = 9; | ||||
|     pixel *buf = malloc(WIDTH * HEIGHT * sizeof(pixel)); | ||||
|     memset(buf, 0, WIDTH * HEIGHT * sizeof(pixel)); | ||||
|     for (int px = 0; px < WIDTH; px++) { | ||||
|         for (int py = 0; py < HEIGHT; py++) { | ||||
|             double x = (double)px / WIDTH_THIRD - 2; | ||||
|             double y = (double)py / HEIGHT_HALF - 1; | ||||
|             int i = 0; | ||||
|             double complex z = 0; | ||||
|             double complex c = x + y * I; | ||||
|             for (; i < MAX_ITERS && cabs(z) < 5.0; i++) { | ||||
|                 z = z*z + c; | ||||
|             } | ||||
|             if (i != MAX_ITERS) { | ||||
|                 pixel pix = { 0, 0, (uint8_t)((double)i / MAX_ITERS * 255) }; | ||||
|                 buf[px + py * WIDTH] = pix; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     if (stbi_write_png(outfile, WIDTH, HEIGHT, sizeof(pixel), buf, WIDTH * sizeof(pixel)) == 0) { | ||||
|         printf("oh no it broke\n"); | ||||
|     } | ||||
|     free(buf); | ||||
| } | ||||
							
								
								
									
										22
									
								
								mandelbrot.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								mandelbrot.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,22 @@ | ||||
| from PIL import Image | ||||
| import math | ||||
|  | ||||
| H = 512 | ||||
| W = math.floor(H * 1.5) | ||||
| ITERATIONS = 32 | ||||
| Wdiv3, Hdiv2 = W / 3, H / 2 | ||||
| im = Image.new("RGB", (W, H)) | ||||
| data = im.load() | ||||
| for px in range(W): | ||||
|     for py in range(H): | ||||
|         x, y = px / Wdiv3 - 2, py / Hdiv2 - 1 | ||||
|         c = x + (y*1j) | ||||
|         v = 0j | ||||
|         for it in range(ITERATIONS + 1): | ||||
|             if abs(v) > 5: | ||||
|                 break | ||||
|             nondiverging_iterations = it | ||||
|             v = v*v + c | ||||
|         if ITERATIONS != nondiverging_iterations: | ||||
|             data[px, py] = (0, 0, math.floor(255 * min(nondiverging_iterations / ITERATIONS, 1))) | ||||
| im.save("/tmp/mandelbrot.png") | ||||
							
								
								
									
										1690
									
								
								stb_image_write.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1690
									
								
								stb_image_write.h
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user