Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. SeedRandom[1];
  2. pts = RandomReal[{0, 12}, {100, 2}];
  3. Needs["ComputationalGeometry`"];
  4. dt = DelaunayTriangulation[pts];
  5. dt // Column
  6. toPairs[{m_, ns_List}] := Map[{m, #} &, ns];
  7. edges = Flatten[Map[toPairs, dt], 1];
  8. Graphics[GraphicsComplex[pts, {Line[edges],
  9. Red, PointSize[Large], Point[pts]}]]
  10.  
  11. g = GraphData[{"Heptahedral", 5}]
  12.  
  13. n = VertexCount[g];
  14. pts = Table[PropertyValue[{g, i}, VertexCoordinates], {i, n}];
  15. rads = Array[r, n];
  16. xs = Array[x, n];
  17. ys = Array[y, n];
  18.  
  19. f1 = Total[(r[#1] + r[#2] - EuclideanDistance[{x[#1], y[#1]}, {x[#2], y[#2]}])^2 & @@@ EdgeList[g]];
  20. f2 = Total[MapThread[({#1, #2} - #3)^2 &, {xs, ys, pts}], -1];
  21.  
  22. rules = Last@NMinimize[f1 + 0.1 f2, Join[rads, xs, ys]];
  23.  
  24. circles = MapThread[Circle[{#1, #2}, #3] &, {xs, ys, rads} /. rules];
  25. g2 = Fold[SetProperty[{##}, VertexCoordinates -> {x[#2], y[#2]} /. rules] &, g, Range[n]];
  26.  
  27. Show[Graphics[circles], g2]
  28.  
  29. Graphics`Region`RegionInit[];
  30. dist[{a_, b_}] := Sqrt[(a - b).(a - b)];
  31. n = 30;
  32.  
  33. (* get a random mesh *)
  34. pts = RandomReal[1, {n, 2}];
  35. m = DelaunayMesh[pts]["MeshObject"];
  36.  
  37. (* get edges and non-edges *)
  38. edges = Sort /@ m["Edges"];
  39. nonedges = Complement[Subsets[Range[n], {2}], edges];
  40.  
  41. (* variable lists *)
  42. rads = Array[r, n];
  43. pos = Array[{x[#], y[#]} &, n];
  44.  
  45. (* set up constraints for edges *)
  46. cons = (Total@rads[[#]] == dist@pos[[#]]) & /@ edges;
  47. cons = Join[cons, Thread[rads > 0]];
  48.  
  49. (* define function to maximize *)
  50. f = Total[dist@pos[[#]] & /@ nonedges];
  51.  
  52. (* variable initializations *)
  53. vars = Join[rads, Flatten[{pos, pts}, {2, 3}]];
  54.  
  55. (* do the maximization *)
  56. {posv, radsv} = {pos, rads} /. Last@FindMaximum[{f, cons}, vars];
  57.  
  58. (* result *)
  59. Graphics[{MapThread[Circle, {posv, radsv}],
  60. Opacity[0.5, Green], Line[posv[[#]] & /@ edges]}]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement