|
- def barycentric_triangle(a,b,c, n=1, max_color=(0,0,0.8), min_color=(0,0,0.8)):
- """
- Returns a plot of the nth barycentric subdivision of a triangle
- with vertices a, b, c
- INPUT:
- - a, b, c - pairs of numbers, viewed as 2-dimensional coordinates:
- the vertices of the triangle
- - n - the number of times to subdivide (default 1)
- EXAMPLES::
- sage: P = barycentric_subdivision((0,0), (1,0), (0,1), n=1)
- sage: P # this displays the plot
- By default, plots include the axes, and also the x- and y-scaling
- are probably not the same. You can adjust this::
- sage: P.show(axes=False, aspect_ratio=1)
- You can also change the size of the picture::
-
- sage: P.show(axes=False, aspect_ratio=1, figsize=[12,12])
- For some interesting pictures:
- sage: P4 = barycentric_subdivision((0,0), (1,0), (0,1), n=4)
- sage: P5 = barycentric_subdivision((0,0), (1,0), (0,1), n=5)
- sage: P4.show(axes=False, aspect_ratio=1, figsize=[12,12])
- sage: P5.show(axes=False, aspect_ratio=1, figsize=[12,12])
- Equilateral triangles::
- sage: a = n(1/sqrt(2))
- sage: Q4 = barycentric_subdivision((0,0), (1,0), (1/2,a), n=4)
- sage: Q5 = barycentric_subdivision((0,0), (1,0), (1/2,a), n=5)
- """
- pic = line([a,b], color=max_color)
- pic += line([b,c], color=max_color)
- pic += line([c,a], color=max_color)
- old_triangles = [(vector(a),vector(b),vector(c))]
- for i in range(n):
- new_triangles = []
- for t in old_triangles:
- # subdivide t, store result in new_triangles
- # and store plot in pic
- a = t[0]
- b = t[1]
- c = t[2]
- abc = (a + b + c)/3
- ab = (a + b)/2
- ac = (a + c)/2
- bc = (b + c)/2
- new_triangles.append((a, ab, abc))
- new_triangles.append((a, ac, abc))
- new_triangles.append((b, ab, abc))
- new_triangles.append((b, bc, abc))
- new_triangles.append((c, ac, abc))
- new_triangles.append((c, bc, abc))
-
- new_color = tuple([(n-i-1)*x/n + (i+1)*y/n for (x,y) in zip(max_color, min_color)])
-
- pic += line([a,bc], color=new_color)
- pic += line([b,ac], color=new_color)
- pic += line([c,ab], color=new_color)
- old_triangles=new_triangles
- return pic
- def barycentric_simplex(a,b,c,d, n=1, max_color=(0,0,0.8), min_color=(0,0,0.8)):
- """
- Returns a plot of the nth barycentric subdivision of a 3-simplex
- with vertices a, b, c, d
- INPUT:
- - a, b, c, d - triples of numbers, viewed as 3-dimensional coordinates:
- the vertices of the 3-simplex
- - n - the number of times to subdivide (default 1)
- """
- pic = line([a,b], color=max_color)
- pic += line([b,c], color=max_color)
- pic += line([c,a], color=max_color)
- pic += line([a,d], color=max_color)
- pic += line([b,d], color=max_color)
- pic += line([c,d], color=max_color)
- old_simplices = [(vector(a),vector(b),vector(c),vector(d))]
- for i in range(n):
- new_simplices = []
- for t in old_simplices:
- # subdivide t, store result in new_simplices
- # and store plot in pic
- a = t[0]
- b = t[1]
- c = t[2]
- d = t[3]
- abcd = (a + b + c + d)/4
- abc = (a + b + c)/3
- abd = (a + b + d)/3
- acd = (a + c + d)/3
- bcd = (b + c + d)/3
- ab = (a + b)/2
- ac = (a + c)/2
- bc = (b + c)/2
- ad = (a + d)/2
- bd = (b + d)/2
- cd = (c + d)/2
- new_simplices.append((a, ab, abc, abcd))
- new_simplices.append((a, ab, abd, abcd))
- new_simplices.append((a, ac, abc, abcd))
- new_simplices.append((a, ac, acd, abcd))
- new_simplices.append((a, ad, abd, abcd))
- new_simplices.append((a, ad, acd, abcd))
- new_simplices.append((b, ab, abc, abcd))
- new_simplices.append((b, ab, abd, abcd))
- new_simplices.append((b, bc, abc, abcd))
- new_simplices.append((b, bc, bcd, abcd))
- new_simplices.append((b, bd, abd, abcd))
- new_simplices.append((b, bd, bcd, abcd))
- new_simplices.append((c, ac, abc, abcd))
- new_simplices.append((c, ac, acd, abcd))
- new_simplices.append((c, bc, abc, abcd))
- new_simplices.append((c, bc, bcd, abcd))
- new_simplices.append((c, cd, acd, abcd))
- new_simplices.append((c, cd, bcd, abcd))
- new_simplices.append((d, ad, abd, abcd))
- new_simplices.append((d, bd, abd, abcd))
- new_simplices.append((d, ad, acd, abcd))
- new_simplices.append((d, cd, acd, abcd))
- new_simplices.append((d, bd, bcd, abcd))
- new_simplices.append((d, cd, bcd, abcd))
-
- new_color = tuple([(n-i-1)*x/n + (i+1)*y/n for (x,y) in zip(max_color, min_color)])
-
- pic += line([a,bc], color=new_color)
- pic += line([a,bd], color=new_color)
- pic += line([a,cd], color=new_color)
- pic += line([b,ac], color=new_color)
- pic += line([b,ad], color=new_color)
- pic += line([b,cd], color=new_color)
- pic += line([c,ab], color=new_color)
- pic += line([c,ad], color=new_color)
- pic += line([c,bd], color=new_color)
- pic += line([d,ab], color=new_color)
- pic += line([d,ac], color=new_color)
- pic += line([d,bc], color=new_color)
- pic += line([a,bcd], color=new_color)
- pic += line([b,acd], color=new_color)
- pic += line([c,abd], color=new_color)
- pic += line([d,abc], color=new_color)
- old_simplices=new_simplices
- return pic
复制代码
|
|