8
$\begingroup$

I am trying to rotate a series of lines by the same angle around their centre point. Say each line is defined by a co-ordinate at each end, simplified example below of a typical dataset.

foo = {{{100, 25}, {150, 45}}, {{200, 45}, {240, 85}}};

Then the midpoint can be found by:-

boo = (#[[2]] - #[[1]])/2 + #[[1]] & /@ foo;

This is visualised using the code below:

Show[
Graphics[{Thick, Dashed, Black, Line /@ foo}],
Graphics[{Black, PointSize[Large],
Point[boo],
Table[{Black, Text[ToString[boo[[p]]], boo[[p]] + 2]}, {p, Length[boo]}]}]
]

enter image description here

I was hoping to create a series of transformations using RotationTransform such as below:

rot = RotationTransform[Pi/2, #] & /@ boo

Then mapping this across foo using MapThread, but I'm not sure which function I should be mapping across to make each line rotate by 90 degrees around its own centre point.

MapThread[?, {woo, rot}]

I think I'm close to what I want to achieve (or maybe not), but I can't quite seem to close it out. All suggestions welcome.

$\endgroup$
1
  • $\begingroup$ Midpoint, introduced in 2019, can be used to find the midpoint. It works with a pair of points as well as a line. $\endgroup$
    – Syed
    Commented Oct 20, 2023 at 7:26

3 Answers 3

6
$\begingroup$
grF := Function[{angle},
             Graphics[{Thick, Dashed, Black, Line@#,
             Red, GeometricTransformation[Line@#, 
                                     RotationTransform[angle, Mean@#]],
             Black, PointSize[Large], Point[Mean@#],
             Text[ToString[Mean@#], Mean@# + 2]} & /@ #] &]

grF[Pi/2]@foo

enter image description here

ListAnimate[Table[Show[grF[i]@foo, PlotRange -> {{50, 250}, {0, 100}}],
                 {i, -Pi,  Pi, Pi/32}]]

enter image description here

$\endgroup$
3
$\begingroup$
foo = {{{100, 25}, {150, 45}}, {{200, 45}, {240, 85}}};
Manipulate[
 Graphics[{Line@#, Blue, Rotate[Line@#, t, Mean@#], Transparent, 
          Circle[Mean@#,EuclideanDistance @@ #/2]} & /@ foo], {t,0, 2 Pi}]

The transparent circles are there to pre-calculate the maximum Plot Range.

Mathematica graphics

$\endgroup$
2
$\begingroup$

1:

lines = {{{100, 25}, {150, 45}}, {{200, 45}, {240, 85}}};
f[{p1_, p2_}, theta_] := Module[{mid},
   mid = (p2 - p1)/2 + p1;
   RotationTransform[theta, mid][#] & /@ {p1, p2}
  ];
lines2 = f[#, Pi/6] & /@ lines;
Graphics[{Red, Line[lines], Blue, Line[lines2]}]

enter image description here

2:

lines = {{{100, 25}, {150, 45}}, {{200, 45}, {240, 85}}};
lines2 = RotationTransform[Pi/6, (#2 - #1)/2 + #1][{#1, #2}] & @@@ lines;
Graphics[{Red, Line[lines], Blue, Line[lines2]}]
$\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.