Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
You mean you want to "see" the coordinates of the vertexes from the polygon myGraphics? If so, try InputForm@myGraphics. –  xzczd Nov 2 '12 at 5:51
    
Sorry, I need to nitpick this! It's vertices, not vertexes. –  Joren Nov 2 '12 at 11:04
    
please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` –  chris Nov 2 '12 at 13:42
    
@Joren I thought so too but FWIW Wikipedia says vertexes is acceptable... –  Yves Klett Nov 2 '12 at 20:18
    
@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. –  J. M. Nov 3 '12 at 1:38

3 Answers 3

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]
share|improve this answer
1  
Just a note: this would extract 2D polygons from Epilog (if present) too. –  István Zachar Nov 2 '12 at 8:14
1  
You might go as far as Polygon[pts:{{_,_,_}..},___] to take into account any options such as VertexColors if present. –  Yves Klett Nov 5 '12 at 12:40

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

share|improve this answer

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.

share|improve this answer
    
@Ajasja that wuz quick - thx :-) –  Yves Klett Nov 2 '12 at 7:46
    
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:)" –  Ajasja Nov 2 '12 at 7:52
    
@Ajasja no worries, coffeedit away! –  Yves Klett Nov 2 '12 at 7:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.