Skip to content

Instantly share code, notes, and snippets.

@blueintegral
Created January 15, 2013 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blueintegral/4535909 to your computer and use it in GitHub Desktop.
Save blueintegral/4535909 to your computer and use it in GitHub Desktop.
Simple recursive Sierpinski triangle generator I wrote for MATLAB
function sierpinski(pair1,pair2,pair3)
X = (((pair1(1)-pair2(1))^2)+((pair1(2)-pair2(2))^2))^(1/2);
Y = (((pair3(1)-pair2(1))^2)+((pair3(2)-pair2(2))^2))^(1/2);
Z = (((pair3(1)-pair1(1))^2)+((pair3(2)-pair1(2))^2))^(1/2);
if X >= 0.1 && Y >= 0.1 && Z >= 0.1
x = [pair1(1) pair2(1) pair3(1) pair1(1)];
y = [pair1(2) pair2(2) pair3(2) pair1(2)];
for index = 1:length(pair1)
A(index) = (pair2(index)+pair3(index))/2;
B(index) = (pair1(index)+pair3(index))/2;
C(index) = (pair1(index)+pair2(index))/2;
end
hold on
plot(x,y);
sierpinski([pair1(1) pair1(2)],[B(1) B(2)],[C(1) C(2)])
sierpinski([A(1) A(2)],[pair2(1) pair2(2)],[C(1) C(2)])
sierpinski([A(1) A(2)],[B(1) B(2)],[pair3(1) pair3(2)])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment