Migrated from private repo
This commit is contained in:
BIN
sierpinski/SierpinskiTriangle.mp4
Normal file
BIN
sierpinski/SierpinskiTriangle.mp4
Normal file
Binary file not shown.
1
sierpinski/no_code.md
Normal file
1
sierpinski/no_code.md
Normal file
@ -0,0 +1 @@
|
||||
The code is lost unfortunately, but we didn't have this in the slides anyway for obvious reasons though.
|
42
sierpinski/triangle.c
Normal file
42
sierpinski/triangle.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define SIZE 32 // Size of the grid
|
||||
|
||||
void sierpinski(int x, int y, int size, char grid[SIZE][SIZE]) {
|
||||
if (size == 1) {
|
||||
grid[y][x] = '*';
|
||||
return;
|
||||
}
|
||||
|
||||
int subSize = size / 2;
|
||||
|
||||
// Top triangle
|
||||
sierpinski(x, y, subSize, grid);
|
||||
|
||||
// Bottom left triangle
|
||||
sierpinski(x - subSize, y + subSize, subSize, grid);
|
||||
|
||||
// Bottom right triangle
|
||||
sierpinski(x + subSize, y + subSize, subSize, grid);
|
||||
}
|
||||
|
||||
int main() {
|
||||
char grid[SIZE][SIZE];
|
||||
memset(grid, ' ', sizeof(grid));
|
||||
|
||||
sierpinski(SIZE / 2, 0, SIZE / 2, grid);
|
||||
|
||||
// Calculate the height of the triangle and print only the necessary rows
|
||||
int height = (int)(0.866 * SIZE / 2);
|
||||
|
||||
for (int i = 0; i <= height; i++) {
|
||||
for (int j = 0; j < SIZE; j++) {
|
||||
printf("%c", grid[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user