Marking polygon vertices in Mathematica

Given the set of points in the plane T={a1,a2,...,an} , then Graphics[Polygon[T]] constructs the polygon generated by the points. How to add labels to the vertices of a polygon? Just an index, like a shortcut, will be better than nothing. Any ideas?

+6
source share
3 answers
 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; Graphics[ {{LightGray, Polygon[pts]}, {pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}]}} ] 

enter image description here

To add a point also

 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; Graphics[ {{LightGray, Polygon[pts]}, {pts /. {x_, y_} :> Text[Style[{x, y}, Red], {x, y}, {0, -1}]}, {pts /. {x_, y_} :> {Blue, PointSize[0.02], Point[{x, y}]}} } ] 

enter image description here

update:

Use index:

 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; Graphics[ {{LightGray, Polygon[pts]}, {pts /. {x_, y_} :> Text[Style[Position[pts, {x, y}], Red], {x, y}, {0, -1}]} } ] 

enter image description here

+9
source

The Nasser version (update) uses pattern matching . It uses functional programming . MapIndexed gives you both the coordinates and their index without the need for Position to find it.

 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; Graphics[ { {LightGray, Polygon[pts]}, MapIndexed[Text[Style[#2[[1]], Red], #1, {0, -1}] &, pts] } ] 

enter image description here

or, if you do not like MapIndexed , here is the version with Apply (at level 1, notation infix @@@ ).

 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; idx = Range[Length[pts]]; Graphics[ { {LightGray, Polygon[pts]}, Text[Style[#2, Red], #1, {0, -1}] & @@@ ({pts, idx}\[Transpose]) } ] 

This can be decomposed into arbitrary labels as follows:

 pts = {{1, 0}, {0, Sqrt[3]}, {-1, 0}}; idx = {"One", "Two", "Three"}; Graphics[ { {LightGray, Polygon[pts]}, Text[Style[#2, Red], #1, {0, -1}] & @@@ ({pts, idx}\[Transpose]) } ] 

enter image description here

+7
source

You can use the GraphPlot options for GraphPlot . Example:

 c = RandomReal[1, {3, 2}] g = GraphPlot[c, VertexLabeling -> True, VertexCoordinateRules -> c]; Graphics[{ Polygon@c , g[[1]]}] 

That way you can also use VertexLabeling -> Tooltip or VertexRenderingFunction if you want. If you do not want the overlays to be added, you can add EdgeRenderingFunction -> None to the GraphPlot function. Example:

 c = RandomReal[1, {3, 2}] g = GraphPlot[c, VertexLabeling -> All, VertexCoordinateRules -> c, EdgeRenderingFunction -> None, VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .02], Black, Text[#2, #1]} &)]; Graphics[{Brown, Polygon@c , g[[1]]}] 

enter image description here

0
source

Source: https://habr.com/ru/post/894167/


All Articles