找回密码
 快速注册
搜索
查看: 7|回复: 0

重心 三角剖分 barycentric subdivision

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2024-12-6 09:24 |阅读模式
  1. def barycentric_triangle(a,b,c, n=1, max_color=(0,0,0.8), min_color=(0,0,0.8)):
  2.     """
  3.     Returns a plot of the nth barycentric subdivision of a triangle
  4.     with vertices a, b, c
  5.     INPUT:
  6.     - a, b, c - pairs of numbers, viewed as 2-dimensional coordinates:
  7.       the vertices of the triangle
  8.     - n - the number of times to subdivide (default 1)
  9.     EXAMPLES::
  10.         sage: P = barycentric_subdivision((0,0), (1,0), (0,1), n=1)
  11.         sage: P  # this displays the plot
  12.     By default, plots include the axes, and also the x- and y-scaling
  13.     are probably not the same.  You can adjust this::
  14.         sage: P.show(axes=False, aspect_ratio=1)
  15.     You can also change the size of the picture::
  16.         
  17.         sage: P.show(axes=False, aspect_ratio=1, figsize=[12,12])
  18.     For some interesting pictures:
  19.         sage: P4 = barycentric_subdivision((0,0), (1,0), (0,1), n=4)
  20.         sage: P5 = barycentric_subdivision((0,0), (1,0), (0,1), n=5)
  21.         sage: P4.show(axes=False, aspect_ratio=1, figsize=[12,12])
  22.         sage: P5.show(axes=False, aspect_ratio=1, figsize=[12,12])
  23.     Equilateral triangles::
  24.         sage: a = n(1/sqrt(2))
  25.         sage: Q4 = barycentric_subdivision((0,0), (1,0), (1/2,a), n=4)
  26.         sage: Q5 = barycentric_subdivision((0,0), (1,0), (1/2,a), n=5)
  27.     """
  28.     pic = line([a,b], color=max_color)
  29.     pic += line([b,c], color=max_color)
  30.     pic += line([c,a], color=max_color)
  31.     old_triangles = [(vector(a),vector(b),vector(c))]
  32.     for i in range(n):
  33.         new_triangles = []
  34.         for t in old_triangles:
  35.             # subdivide t, store result in new_triangles
  36.             # and store plot in pic
  37.             a = t[0]
  38.             b = t[1]
  39.             c = t[2]
  40.             abc = (a + b + c)/3
  41.             ab = (a + b)/2
  42.             ac = (a + c)/2
  43.             bc = (b + c)/2
  44.             new_triangles.append((a, ab, abc))
  45.             new_triangles.append((a, ac, abc))
  46.             new_triangles.append((b, ab, abc))
  47.             new_triangles.append((b, bc, abc))
  48.             new_triangles.append((c, ac, abc))
  49.             new_triangles.append((c, bc, abc))
  50.             
  51.             new_color = tuple([(n-i-1)*x/n + (i+1)*y/n for (x,y) in zip(max_color, min_color)])
  52.             
  53.             pic += line([a,bc], color=new_color)
  54.             pic += line([b,ac], color=new_color)
  55.             pic += line([c,ab], color=new_color)
  56.         old_triangles=new_triangles
  57.     return pic
  58. def barycentric_simplex(a,b,c,d, n=1, max_color=(0,0,0.8), min_color=(0,0,0.8)):
  59.     """
  60.     Returns a plot of the nth barycentric subdivision of a 3-simplex
  61.     with vertices a, b, c, d
  62.     INPUT:
  63.     - a, b, c, d - triples of numbers, viewed as 3-dimensional coordinates:
  64.       the vertices of the 3-simplex
  65.     - n - the number of times to subdivide (default 1)
  66.     """
  67.     pic = line([a,b], color=max_color)
  68.     pic += line([b,c], color=max_color)
  69.     pic += line([c,a], color=max_color)
  70.     pic += line([a,d], color=max_color)
  71.     pic += line([b,d], color=max_color)
  72.     pic += line([c,d], color=max_color)
  73.     old_simplices = [(vector(a),vector(b),vector(c),vector(d))]
  74.     for i in range(n):
  75.         new_simplices = []
  76.         for t in old_simplices:
  77.             # subdivide t, store result in new_simplices
  78.             # and store plot in pic
  79.             a = t[0]
  80.             b = t[1]
  81.             c = t[2]
  82.             d = t[3]
  83.             abcd = (a + b + c + d)/4
  84.             abc = (a + b + c)/3
  85.             abd = (a + b + d)/3
  86.             acd = (a + c + d)/3
  87.             bcd = (b + c + d)/3
  88.             ab = (a + b)/2
  89.             ac = (a + c)/2
  90.             bc = (b + c)/2
  91.             ad = (a + d)/2
  92.             bd = (b + d)/2
  93.             cd = (c + d)/2
  94.             new_simplices.append((a, ab, abc, abcd))
  95.             new_simplices.append((a, ab, abd, abcd))
  96.             new_simplices.append((a, ac, abc, abcd))
  97.             new_simplices.append((a, ac, acd, abcd))
  98.             new_simplices.append((a, ad, abd, abcd))
  99.             new_simplices.append((a, ad, acd, abcd))
  100.             new_simplices.append((b, ab, abc, abcd))
  101.             new_simplices.append((b, ab, abd, abcd))
  102.             new_simplices.append((b, bc, abc, abcd))
  103.             new_simplices.append((b, bc, bcd, abcd))
  104.             new_simplices.append((b, bd, abd, abcd))
  105.             new_simplices.append((b, bd, bcd, abcd))
  106.             new_simplices.append((c, ac, abc, abcd))
  107.             new_simplices.append((c, ac, acd, abcd))
  108.             new_simplices.append((c, bc, abc, abcd))
  109.             new_simplices.append((c, bc, bcd, abcd))
  110.             new_simplices.append((c, cd, acd, abcd))
  111.             new_simplices.append((c, cd, bcd, abcd))
  112.             new_simplices.append((d, ad, abd, abcd))
  113.             new_simplices.append((d, bd, abd, abcd))
  114.             new_simplices.append((d, ad, acd, abcd))
  115.             new_simplices.append((d, cd, acd, abcd))
  116.             new_simplices.append((d, bd, bcd, abcd))
  117.             new_simplices.append((d, cd, bcd, abcd))
  118.             
  119.             new_color = tuple([(n-i-1)*x/n + (i+1)*y/n for (x,y) in zip(max_color, min_color)])
  120.             
  121.             pic += line([a,bc], color=new_color)
  122.             pic += line([a,bd], color=new_color)
  123.             pic += line([a,cd], color=new_color)
  124.             pic += line([b,ac], color=new_color)
  125.             pic += line([b,ad], color=new_color)
  126.             pic += line([b,cd], color=new_color)
  127.             pic += line([c,ab], color=new_color)
  128.             pic += line([c,ad], color=new_color)
  129.             pic += line([c,bd], color=new_color)
  130.             pic += line([d,ab], color=new_color)
  131.             pic += line([d,ac], color=new_color)
  132.             pic += line([d,bc], color=new_color)
  133.             pic += line([a,bcd], color=new_color)
  134.             pic += line([b,acd], color=new_color)
  135.             pic += line([c,abd], color=new_color)
  136.             pic += line([d,abc], color=new_color)
  137.         old_simplices=new_simplices
  138.     return pic
复制代码
bary[1].gif

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 12:05

Powered by Discuz!

× 快速回复 返回顶部 返回列表