Last active
November 12, 2019 11:50
-
-
Save kazucmpt/1e913d1b9bf1ad5859ebc0759befd923 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import time | |
import matplotlib.pyplot as plt | |
max_depth = 8 | |
# Internal dividing point | |
m = 1 | |
n = 1 | |
# Initial Conditions | |
a0 = +0.0 + 0J | |
a1 = +1.0 + 0J | |
a2 = +0.5 + 1J*math.sqrt(3.0)/2.0 | |
epsilon = 0.001 | |
save_img_fname = "SierpinskiGasket_depth{}.png".format(max_depth) | |
def get_coordinate(triangle): | |
x0 = triangle[0].real | |
y0 = triangle[0].imag | |
x1 = triangle[1].real | |
y1 = triangle[1].imag | |
x2 = triangle[2].real | |
y2 = triangle[2].imag | |
return (x0, y0), (x1, y1), (x2, y2) | |
def draw(triangles): | |
plt.figure(figsize=(7, 7), dpi=150) | |
print("drawing...") | |
for triangle in triangles: | |
(x0, y0), (x1, y1), (x2, y2) = get_coordinate(triangle) | |
poly = plt.Polygon(((x0, y0), (x1, y1), (x2, y2))) | |
plt.gca().add_patch(poly) | |
plt.axis("off") | |
plt.savefig(save_img_fname) | |
print("{} has been saved".format(save_img_fname, max_depth)) | |
def check_initial_conditions(a0, a1, a2): | |
d0 = abs(a0-a1) | |
d1 = abs(a1-a2) | |
d2 = abs(a2-a0) | |
if abs(d0-d1) > epsilon or abs(d1-d2) > epsilon or abs(d2-d0) > epsilon: | |
print("initial condition is inccorect") | |
def make_3triangles_from_triangle(triangle): | |
p0 = triangle[0] | |
p1 = triangle[1] | |
p2 = triangle[2] | |
new_p2 = (m*p0 + n*p1) / (m+n) | |
new_p0 = (m*p1 + n*p2) / (m+n) | |
new_p1 = (m*p2 + n*p0) / (m+n) | |
triangle0 = [p0, new_p1, new_p2] | |
triangle1 = [new_p1, p2, new_p0] | |
triangle2 = [new_p2, new_p0, p1] | |
return triangle0, triangle1, triangle2 | |
def get_new_triangles(triangles): | |
new_triangles = [] | |
for i, triangle in enumerate(triangles): | |
triangle0, triangle1, triangle2 = make_3triangles_from_triangle(triangles[i]) | |
new_triangles.append(triangle0) | |
new_triangles.append(triangle1) | |
new_triangles.append(triangle2) | |
return new_triangles | |
def get_circumference_and_area(triangles): | |
distance_between_two_point = abs(triangles[-1][0] - triangles[-1][1]) | |
circumference = len(triangles) * distance_between_two_point | |
area = math.sqrt(3)/4.0 * distance_between_two_point ** 2 | |
return circumference, area | |
def main(): | |
check_initial_conditions(a0,a1,a2) | |
triangles = [] | |
triangles.append([a0, a1, a2]) | |
start = time.time() | |
for depth in range(max_depth): | |
triangles = get_new_triangles(triangles) | |
circumference, area = get_circumference_and_area(triangles) | |
elapased_time = time.time() - start | |
print("depth:{} num_triangles:{} circumference:{:.5f} area:{:.5f} time:{:.6f} [sec]".format(depth, len(triangles), circumference, area, elapased_time)) | |
start = time.time() | |
draw(triangles) | |
elapased_time = time.time() - start | |
print("drowing time: {:.6f} [sec]".format(elapased_time)) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import time | |
import matplotlib.pyplot as plt | |
max_depth = 4 | |
# Initial Conditions | |
a0 = +0.0 + 0J | |
a1 = +1.0 + 0J | |
a2 = +0.5 + 1J*math.sqrt(3.0)/2.0 | |
epsilon = 0.001 | |
save_img_fname = "SierpinskiGasket_depth{}.png".format(max_depth) | |
def get_coordinate(triangle): | |
x0 = triangle[0].real | |
y0 = triangle[0].imag | |
x1 = triangle[1].real | |
y1 = triangle[1].imag | |
x2 = triangle[2].real | |
y2 = triangle[2].imag | |
return (x0, y0), (x1, y1), (x2, y2) | |
def draw(triangles): | |
plt.figure(figsize=(7, 7), dpi=150) | |
print("drawing...") | |
coordx = [] | |
coordy = [] | |
for triangle in triangles: | |
(x0, y0), (x1, y1), (x2, y2) = get_coordinate(triangle) | |
poly = plt.Polygon(((x0, y0), (x1, y1), (x2, y2))) | |
plt.gca().add_patch(poly) | |
plt.axis("off") | |
plt.savefig(save_img_fname) | |
plt.show() | |
print("{} has been saved".format(save_img_fname, max_depth)) | |
def check_initial_conditions(a0, a1, a2): | |
d0 = abs(a0-a1) | |
d1 = abs(a1-a2) | |
d2 = abs(a2-a0) | |
if abs(d0-d1) > epsilon or abs(d1-d2) > epsilon or abs(d2-d0) > epsilon: | |
print("initial condition is inccorect") | |
def midpoint(p1, p2): | |
return 2.0/3.0 * p1 + 1.0/3.0 * p2 | |
def make_6triangles_from_triangle(triangle): | |
p0 = triangle[0] | |
p1 = triangle[1] | |
p2 = triangle[2] | |
new_p1 = midpoint(p0, p2) | |
new_p2 = midpoint(p2, p0) | |
new_p3 = midpoint(p2, p1) | |
new_p4 = midpoint(p1, p2) | |
new_p5 = midpoint(p1, p0) | |
new_p6 = midpoint(p0, p1) | |
new_p0 = (new_p1 + new_p2 + new_p3 + new_p4 + new_p5 + new_p6) / 6.0 | |
triangle0 = [p0, new_p6, new_p1] | |
triangle1 = [new_p5, p1, new_p4] | |
triangle2 = [new_p2, new_p3, p2] | |
triangle3 = [new_p6, new_p5, new_p0] | |
triangle4 = [new_p0, new_p4, new_p3] | |
triangle5 = [new_p1, new_p0, new_p2] | |
return triangle0, triangle1, triangle2, triangle3, triangle4, triangle5 | |
def get_new_triangles(triangles): | |
new_triangles = [] | |
for i, triangle in enumerate(triangles): | |
triangle0, triangle1, triangle2, triangle3, triangle4, triangle5 = make_6triangles_from_triangle(triangles[i]) | |
new_triangles.append(triangle0) | |
new_triangles.append(triangle1) | |
new_triangles.append(triangle2) | |
new_triangles.append(triangle3) | |
new_triangles.append(triangle4) | |
new_triangles.append(triangle5) | |
return new_triangles | |
def get_circumference_and_area(triangles): | |
distance_between_two_point = abs(triangles[-1][0] - triangles[-1][1]) | |
circumference = len(triangles) * distance_between_two_point | |
area = math.sqrt(3)/4.0 * distance_between_two_point ** 2 | |
return circumference, area | |
def main(): | |
check_initial_conditions(a0,a1,a2) | |
triangles = [] | |
triangles.append([a0, a1, a2]) | |
start = time.time() | |
for depth in range(max_depth): | |
triangles = get_new_triangles(triangles) | |
circumference, area = get_circumference_and_area(triangles) | |
elapased_time = time.time() - start | |
print("depth:{} num_triangles:{} circumference:{:.5f} area:{:.5f} time:{:.6f} [sec]".format(depth, len(triangles), circumference, area, elapased_time)) | |
start = time.time() | |
draw(triangles) | |
elapased_time = time.time() - start | |
print("drowing time: {:.6f} [sec]".format(elapased_time)) | |
if __name__ == "__main__": | |
m |
Author
kazucmpt
commented
Nov 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment