Plot DelaunayTriangulation in Mathematica

女生的网名这么多〃 提交于 2019-12-11 06:52:03

问题


Considering the following example (from Sjoerd Solution on plotting a ConvexHull)

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

I would now like to plot the DelaunayTriangulation for a set of point but can`t figure out the Plot syntax using Graphics.

Thoughts ?


回答1:


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

And if you need real polygons:

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]
  }
 ]
]




回答2:


Method one, using polygons like Sjoerd, but without the problem caused by points on the convex hull:

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

Method two, using lines connecting the adjacent points:

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

Both of these methods result in duplicated primitives (three polygons or two lines, from using each point as a starting point.)

We can modify the data slightly and use built in visualization functions:

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




回答3:


I like Sjoerd's use of GraphicsComplex, but I don't see the need for the baroque code in the middle.

This appears to work just fine:

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

Lines

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


Polygons

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



来源:https://stackoverflow.com/questions/6469411/plot-delaunaytriangulation-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!