2
$\begingroup$

I have a simple manipulation with points & lines:

Manipulate[Graphics[{Rotate[{
 Rotate[{Line[{{0, 0}, {(a + b)/2, 0}}], Red, PointSize[Large], 
   Point[{(a + b)/2, 0}]}, r, {0, 0}],
 Rotate[{Line[{{0, 0}, {(a + b)/2, 0}}], Red, PointSize[Large], 
   Point[{(a + b)/2, 0}]}, -r, {0, 0}],
 Rotate[{Line[{{0, 0}, {-a, 0}}], Red, PointSize[Large], 
   Point[{-a, 0}]}, r, {0, 0}],
 Rotate[{Line[{{0, 0}, {-b, 0}}], Red, PointSize[Large], 
   Point[{-b, 0}]}, -r, {0, 0}]
 }, q, {0, 0}]}, Axes -> True, PlotRange -> {{-2, 2}, {-2, 2}}], 
{{r, Pi/8}, 0, Pi/2}, {{q, 0}, -Pi/2, Pi/2}, {{a, 1}, 0, 1.5}, {{b, 1}, 0, 1.5}]

Mathematica graphics

Is there any way of getting and using the coordinates of the end-points while they are being manipulated (other than work out their trig relationships ... I anticipate there will be quite a few)?

$\endgroup$
6
  • $\begingroup$ rewrite this with RotationTransform. $\endgroup$
    – Kuba
    Oct 7 '14 at 20:03
  • $\begingroup$ @Kuba will this approach work if I have lots of rotations within rotations? $\endgroup$
    – martin
    Oct 7 '14 at 20:14
  • $\begingroup$ @martin: Your rotations are simple rotations about the origin, so a composition of two rotations is just a rotation by the sum of the angles of the individual rotations. $\endgroup$ Oct 7 '14 at 20:22
  • $\begingroup$ @DumpsterDoofus the intention is to have lines rotating about the endpoints too ... things could get a little complicated $\endgroup$
    – martin
    Oct 7 '14 at 20:28
  • 1
    $\begingroup$ @martin Manipulate[ r1 = RotationTransform[q]; r2 = RotationTransform[r]; r3 = RotationTransform[-r]; pkt2 = r1 /@ MapThread[Compose, {{r2, r3, r2, r3}, pkt}]; Grid[{{ Graphics[{ Thick, AbsolutePointSize@7, {Point[#], Line[{{0, 0}, #}]} & /@ pkt2 }, Axes -> True, PlotRange -> {{-2, 2}, {-2, 2}}], Column@pkt2 }}], {{r, Pi/8}, 0, Pi/2}, {{q, 0}, -Pi/2, Pi/2}, {{a, 1}, 0, 1.5}, {{b, 1}, 0, 1.5}, Initialization :> ( pkt := {{(a + b)/2, 0}, {(a + b)/2, 0}, {-a, 0}, {-b, 0}} ), TrackedSymbols :> {a, b, r, q}] $\endgroup$
    – Kuba
    Oct 7 '14 at 20:39
5
$\begingroup$

As Kuba mentioned, you can quite easily work out the trig relations to give the endpoints directly, using RotationTransform:

RotationTransform[q] /@ {RotationTransform[r]@{(a + b)/2, 0}, 
  RotationTransform[-r]@{(a + b)/2, 0}, RotationTransform[r]@{-a, 0}, 
  RotationTransform[-r]@{-b, 0}}

which produces the coordinates of the red dots:

{{1/2 (a + b) Cos[q] Cos[r] - 1/2 (a + b) Sin[q] Sin[r], 
  1/2 (a + b) Cos[r] Sin[q] + 
   1/2 (a + b) Cos[q] Sin[r]}, {1/2 (a + b) Cos[q] Cos[r] + 
   1/2 (a + b) Sin[q] Sin[r], 
  1/2 (a + b) Cos[r] Sin[q] - 
   1/2 (a + b) Cos[q] Sin[r]}, {-a Cos[q] Cos[r] + 
   a Sin[q] Sin[r], -a Cos[r] Sin[q] - 
   a Cos[q] Sin[r]}, {-b Cos[q] Cos[r] - 
   b Sin[q] Sin[r], -b Cos[r] Sin[q] + b Cos[q] Sin[r]}}

Similarly, you can add the numerical coordinates to your Manipulate object:

Manipulate[{MatrixForm[
   RotationTransform[q] /@ {RotationTransform[r]@{(a + b)/2, 0}, 
     RotationTransform[-r]@{(a + b)/2, 0}, 
     RotationTransform[r]@{-a, 0}, RotationTransform[-r]@{-b, 0}}], 
  Graphics[{Rotate[{Rotate[{Line[{{0, 0}, {(a + b)/2, 0}}], Red, 
        PointSize[Large], Point[{(a + b)/2, 0}]}, r, {0, 0}], 
      Rotate[{Line[{{0, 0}, {(a + b)/2, 0}}], Red, PointSize[Large], 
        Point[{(a + b)/2, 0}]}, -r, {0, 0}], 
      Rotate[{Line[{{0, 0}, {-a, 0}}], Red, PointSize[Large], 
        Point[{-a, 0}]}, r, {0, 0}], 
      Rotate[{Line[{{0, 0}, {-b, 0}}], Red, PointSize[Large], 
        Point[{-b, 0}]}, -r, {0, 0}]}, q, {0, 0}]}, Axes -> True, 
   PlotRange -> {{-2, 2}, {-2, 2}}, ImageSize -> 500]}, {{r, Pi/8}, 0,
   Pi/2}, {{q, 0}, -Pi/2, Pi/2}, {{a, 1}, 0, 1.5}, {{b, 1}, 0, 1.5}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you so much! I was just about to go about it the old fashioned way with trig .. $\endgroup$
    – martin
    Oct 7 '14 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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