11
$\begingroup$

I made a polygon with the following vertexes :

p = {{0, 0, 0}, {0, 0, 300}, {0, 300, 300}, {0, 300, 0}};(*Vertices of the polygon*) myGraphics=Graphics3D[{Polygon[p]}, Boxed -> False, Lighting -> {Gray}]; 

How can I find the vertexes of myGraphics?

$\endgroup$
6
  • $\begingroup$ You mean you want to "see" the coordinates of the vertexes from the polygon myGraphics? If so, try InputForm@myGraphics. $\endgroup$ Commented Nov 2, 2012 at 5:51
  • $\begingroup$ Sorry, I need to nitpick this! It's vertices, not vertexes. $\endgroup$ Commented Nov 2, 2012 at 11:04
  • $\begingroup$ please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` $\endgroup$ Commented Nov 2, 2012 at 13:42
  • $\begingroup$ @Joren I thought so too but FWIW Wikipedia says vertexes is acceptable... $\endgroup$ Commented Nov 2, 2012 at 20:18
  • $\begingroup$ @Joren, both "vertices" and "vertexes" are acceptable (as with "indexes" and "indices"); "vertexes" discomfits me personally, but that's more of a stylistic than a grammatical complaint. $\endgroup$ Commented Nov 3, 2012 at 1:38

3 Answers 3

14
$\begingroup$

Try the following:

In[2]:= Cases[myGraphics, Polygon[pts_] -> pts, Infinity] Out[2]= {{{0, 0, 0}, {0, 0, 300}, {0, 300, 300}, {0, 300, 0}}} 

If your Graphics3D object had many polygons you would get a list comprised of lists of the vertexes of all of them.

Update

In response to István Zachar's commentL: to confine the pattern to match only 3D polygons, use

Cases[myGraphics, Polygon[pts:{{_,_,_}..}] -> pts, Infinity] 
$\endgroup$
2
  • 1
    $\begingroup$ Just a note: this would extract 2D polygons from Epilog (if present) too. $\endgroup$ Commented Nov 2, 2012 at 8:14
  • 1
    $\begingroup$ You might go as far as Polygon[pts:{{_,_,_}..},___] to take into account any options such as VertexColors if present. $\endgroup$ Commented Nov 5, 2012 at 12:40
11
$\begingroup$

A function to extract graphics primitives:

ClearAll[getVrtxCoords]; getVrtxCoords[plot_, prims : {___}] := Module[{pts, pts2, prms = Alternatives @@ prims, vcoords = Cases[plot, GraphicsComplex[{coords___}, __] :> {coords}, {0, Infinity}]}, pts = Cases[plot, prms[points : {({_, _} | {_, _, _}) ..}, ___] :> points, {0, Infinity}]; If[vcoords === {}, If[pts === {}, {}, First@pts], If[(pts2 = Cases[plot, prms[{points___}, ___] :> ((First[vcoords][[#]]) & /@ {points}), {0, Infinity}]) == {}, {}, First@pts2]]] 

Examples:

(* OP's example : *) p = {{0, 0, 0}, {0, 0, 300}, {0, 300, 300}, {0, 300, 0}}; myGraphics = Graphics3D[{Polygon[p]}, Boxed -> False, Lighting -> {Gray}]; Grid[{{"image", "Points", "Lines", "Polygons"}, Prepend[Column[N[getVrtxCoords[myGraphics, {#}]]] & /@ {Point, Line, Polygon}, myGraphics]}, Spacings -> {1, 1}, Dividers -> All] 

enter image description here

A 2D example with GraphicsComplex:

 v = Table[15 {Cos[t], Sin[t]}, {t, 0, 4 Pi, 4 Pi/5}]; grphcs2 = Graphics[GraphicsComplex[v, {Red, Thick, Polygon[{1, 2, 3, 4, 5, 6}], Blue, Opacity[.7], Polygon[{{1, 2, 3}, {3, 4, 5}}, VertexColors -> {{Yellow, Yellow, Yellow}, {Blue, Blue, Blue}}], Opacity[1], Dashed, Thickness[.02], Brown, Line[{1, 4, 2}], PointSize[Large], Red, Point[{1, 2, 3, 4, 5}]}]]; Grid[{{"image", "Points", "Lines", "Polygons"}, Prepend[Column[N[getVrtxCoords[grphcs2, {#}]]] & /@ {Point, Line, Polygon}, grphcs2]}, Spacings -> {1, 1}, Dividers -> All] 

enter image description here

A 3D example:

v3 = PolyhedronData["Dodecahedron", "VertexCoordinates"]; Short[i = PolyhedronData["Dodecahedron", "FaceIndices"]]; dodec = Graphics3D[{Yellow, GraphicsComplex[v3, Polygon[i]]}, ImageSize -> 300]; modifieddodec = Graphics3D[{Opacity[.8], EdgeForm[Opacity[.3]], Polygon[#, VertexColors -> Table[Hue[RandomReal[]], {25}]] & /@ getVrtxCoords[dodec, {Polygon}]}, Lighting -> "Neutral", ImageSize -> 300]; Grid[{{"image", "reconstructed", "Points", "Lines", "Polygons"}, {dodec, modifieddodec, N@getVrtxCoords[dodec, {Point}], N@getVrtxCoords[dodec, {Line}], Short[N@getVrtxCoords[dodec, {Polygon}], 10]}}, Spacings -> {1, 1}, Dividers -> All, Alignment -> {Center, Top}] 

enter image description here

$\endgroup$
9
$\begingroup$

Another version using pattern matching which can be quite useful for other purposes as well (other than in @m_goldberg's patent solution, all points will be returned in one list, the primitive structure will not be preserved):

Cases[myGraphics, {x_?NumericQ, y_?NumericQ, z_?NumericQ}, Infinity] 
{{0, 0, 0}, {0, 0, 300}, {0, 300, 300}, {0, 300, 0}} 

Why is that approach (esp. the pattern) useful? You can easily work on the coordinates and it works on several important Graphics3D primitives (Point, Line, Polygon,Text) out of the box regardless of the structure of your expression. Apart from the {x,y,z} tuples your expression is not changed.

myGraphics /. {x_?NumericQ, y_?NumericQ, z_?NumericQ} :> {x, 2 y-z, 3 z} 

For multi-primitive syntax, GraphicsComplex etc. this approach works as well if you get a bit more fancy on the pattern side.

$\endgroup$
3
  • $\begingroup$ @Ajasja that wuz quick - thx :-) $\endgroup$ Commented Nov 2, 2012 at 7:46
  • $\begingroup$ Hehe, just drinking my morning coffee, and browsing the site. I did consider that it would be polite to wait, but in the end I said "meeh, the post won't go into community wiki because of one edit:)" $\endgroup$ Commented Nov 2, 2012 at 7:52
  • $\begingroup$ @Ajasja no worries, coffeedit away! $\endgroup$ Commented Nov 2, 2012 at 7:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.