5
$\begingroup$

I have been trying to obtain list of {x,y} coordinates from this code

Graphics[
  Line[
    Table[
      {r*Cos[θ*Degree] + x, r*Sin[θ*Degree] + y}, 
      {θ, 0, 360, 360/4}] /. {r -> 1, x -> 1, y -> 1}]]

Its output is

enter image description here

I want the output to be a list of all {x, y} coordinates like what Mathematica sees. For example,

enter image description here

I could perhaps control the point density. Could this be possible?

$\endgroup$
4
  • 7
    $\begingroup$ Graphics[...][[1, 1]]? $\endgroup$
    – Kuba
    Commented Jan 4, 2021 at 20:46
  • $\begingroup$ @Kuba no.basically i wanted to avoid writing equations of these polygon lines and somehow get the coordinates.I hope i am a bit more clear. $\endgroup$
    – Irtiza
    Commented Jan 4, 2021 at 21:00
  • $\begingroup$ `InputForm[%] gives you what Mathematica sees. It doesn't see as many points as you do. $\endgroup$
    – Bill Watts
    Commented Jan 4, 2021 at 21:36
  • 1
    $\begingroup$ Do you want the problem solved for all Graphics[] or just the problem given above? (I doubt it can be done for all graphics or rather that someone would do it. What would you want for a Rectangle[], the two corners specified or all four corners? Etc.) $\endgroup$
    – Michael E2
    Commented Jan 4, 2021 at 22:49

3 Answers 3

4
$\begingroup$
g0 = Graphics[Line[Table[{r*Cos[θ*Degree] + x, r*Sin[θ*Degree] + y}, 
  {θ, 0, 360, 360/4}] /.  {r -> 1, x -> 1, y -> 1}]]; 

We can also construct a BSplineFunction from extracted line coordinates

 bSF = BSplineFunction[g0[[1, 1]], SplineDegree -> 1];

and map it on an equally spaced list of desired length:

Graphics[{PointSize[Small], Point[bSF /@ Subdivide[100]]}, ImageSize -> 300]

enter image description here

Row[Graphics[{PointSize[Small], Point[BSF /@ Subdivide[#]]}, 
    ImageSize -> 2 (# /. 4 | 8 -> 20)] & /@ {4, 8, 40, 100, 300}, Spacer[20]]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is very good.Thankyou $\endgroup$
    – Irtiza
    Commented Jan 5, 2021 at 16:00
8
$\begingroup$

You can interpolate between the corners to get coordinates along the lines. n controls the number of points for each line.

square = Graphics[Line[Table[{r*Cos[\[Theta]*Degree] + x, r*Sin[\[Theta]*Degree] + y}, 
    {\[Theta], 0, 360, 360/4}] /. {r -> 1, x -> 1, y -> 1}]];

n=10
points = Flatten[Table[t*#1 + (1 - t) #2, {t, 0, 1, 1/n}] & @@@ 
    Partition[square[[1, 1]], 2, 1], {1, 2}]
{{1., 2.}, {1.1, 1.9}, {1.2, 1.8}, {1.3, 1.7}, {1.4, 1.6}, {1.5, 1.5}, 
 {1.6, 1.4}, {1.7, 1.3}, {1.8, 1.2}, {1.9, 1.1}, {2., 1.}, {0., 1.}, 
 {0.1, 1.1}, {0.2, 1.2}, {0.3, 1.3}, {0.4, 1.4}, {0.5, 1.5}, {0.6, 1.6}, 
 {0.7, 1.7}, {0.8, 1.8}, {0.9, 1.9}, {1., 2.}, {1.,0.}, {0.9, 0.1}, 
 {0.8, 0.2}, {0.7, 0.3}, {0.6, 0.4}, {0.5, 0.5}, {0.4, 0.6}, {0.3, 0.7},
 {0.2, 0.8}, {0.1, 0.9}, {0., 1.}, {2., 1.}, {1.9, 0.9}, {1.8, 0.8}, 
 {1.7, 0.7}, {1.6, 0.6}, {1.5, 0.5}, {1.4, 0.4}, {1.3, 0.3}, {1.2, 0.2},
 {1.1, 0.1}, {1., 0.}}
ListPlot[points, AspectRatio -> 1]

enter image description here

$\endgroup$
7
$\begingroup$

Using Cases, you can extract the information you need from any graphics. From the following graphics:

gr = Graphics[
      Line[
        Table[{r*Cos[θ*Degree] + x, r*Sin[θ*Degree] + y}, {θ, 0, 360, 360/4}] 
         /. {r -> 1, x -> 1, y -> 1}
      ]
     ];

We want to extract the Lines: this can easily be done by

Cases[gr, Line[__]]

(* Out: {Line[{{2, 1}, {1, 2}, {0, 1}, {1, 0}, {2, 1}}]} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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