在Mathematica中绘制DelaunayTriangulation

时间:2011-06-24 14:41:42

标签: graphics geometry wolfram-mathematica

考虑以下示例(from Sjoerd Solution on plotting a ConvexHull

Needs["ComputationalGeometry`"]
pts = RandomReal[{0, 10}, {60, 2}];
dtpts=DelaunayTriangulation[pts]

我现在想把DelaunayTriangulation绘制成一组点,但是不能用图形来计算Plot语法。

想法?

3 个答案:

答案 0 :(得分:4)

方法一,使用像Sjoerd这样的多边形,但没有凸壳上的点引起的问题:

Graphics[{FaceForm[], EdgeForm[Black], 
  Polygon[pts[[#]] & /@ 
    DeleteCases[dtpts, {i_, _} /; MemberQ[ConvexHull[pts], i]][[All, 
      2]]], Red, Point[pts]}]

方法二,使用连接相邻点的线:

edges[pts_, {a_, l_List}] := {pts[[a]], #} & /@ pts[[l]]
Graphics[{Line[edges[pts, #]] & /@ dtpts, Red, Point[pts]}]

这两种方法都会产生重复的基元(三个多边形或两条线,从使用每个点作为起点。)

我们可以稍微修改数据并使用内置的可视化功能:

Graphics[{FaceForm[], EdgeForm[Black], 
  Cases[Normal[
    ListDensityPlot[{##, 0.} & @@@ pts, Mesh -> All]], _Polygon, 
   Infinity], Red, Point[pts]}, ImageSize -> 175]

enter image description here

答案 1 :(得分:3)

Graphics[
  GraphicsComplex[
    pts, 
    {
      Function[{startPt, finishPts},Line[{startPt, #}] & /@ finishPts] @@@ dtpts, 
      Red, Point@Range[Length@pts]
    }
   ]
  ]

enter image description here

如果你需要真正的多边形:

Graphics[
 GraphicsComplex[
  pts, 
  {EdgeForm[Black], 
   Function[{startPt, finishPts}, 
      {FaceForm[RGBColor[RandomReal[], RandomReal[], RandomReal[]]], 
        Polygon[{startPt, ##}]} & @@@ 
          Transpose[{Drop[finishPts, 1], 
                     Drop[RotateRight@finishPts, 1]
                    }
          ]
         ] @@@ dtpts, 
   Red, Point@Range[Length@pts]
  }
 ]
]

enter image description here

答案 2 :(得分:1)

我喜欢Sjoerd使用GraphicsComplex,但我不认为中间需要巴洛克式代码。

这似乎工作正常:

Needs["ComputationalGeometry`"]
pts = RandomReal[{0, 10}, {60, 2}];
dtpts = DelaunayTriangulation[pts];

Graphics[GraphicsComplex[
  pts,
  {Line /@ Thread /@ dtpts, Red, Point@Range@Length@pts}
]]

enter image description here


多边形

Graphics[GraphicsComplex[
  pts,
  {
    EdgeForm[Black],
    ( {FaceForm[RGBColor @@ RandomReal[1, 3]], Polygon@#} & /@ 
      Append @@@ Thread@{Partition[#2, 2, 1], #} & ) @@@ dtpts,
    Red,
    Point@Range[Length@pts]
  }
]]

enter image description here