2
$\begingroup$

I am new to this forum - I wonder if someone can help me improve this code.

  1. I would like the angle $\theta$ to start in the middle of the slider i.e. $\theta = 0$ instead of starting at $\theta = -90$.

  2. How do I keep the circle size constant as I slide the $\theta$?

Mohr[σxx_, σyy_, τxy_] := 
 DynamicModule[{g1, g2, g3}, OC = 1/2*(σxx + σyy); 
  R = Sqrt[((σxx - σyy)/2)^2 + τxy^2] ; 
  g1 = {Thickness[0.01], Circle[{OC, 0}, R]}; σxx1 := 
   1/2*(σxx + σyy) + 1/2*(σxx - σyy)*Cos[2*θ*Degree] + τxy*
     Sin[2*\[Theta]*Degree]; 
  σyy1 := 1/2*(σxx + σyy) + 1/2*(σyy - σxx)*Cos[2*θ*Degree] - τxy*Sin[2*θ*Degree];
  τxy1 := 1/2*(σyy - σxx)*Sin[2*θ*Degree] + τxy*Cos[2*θ*Degree];
  τxy1a := 1/2*(σyy - σxx)*Sin[(2*θ*Degree + π)] + τxy*Cos[(2*θ*Degree + π)]; 
  pts = {{σxx1, -τxy1}, {σyy1, -τxy1a}};
  g2 = {PointSize[0.05], Red, Point[pts] , 
    Inset[Row[{"\!\(\*SubscriptBox[\(σ\), \(xx\)]\) = ", NumberForm[σxx1, {6, 2}], ";", 
       "  \!\(\*SubscriptBox[\(τ\), \(xy\)]\) = ", NumberForm[-τxy1, {6, 2}]}], 
     Dynamic[{pts[[1, 1]], pts[[1, 2]]}]],
    Inset[
     Row[{"\!\(\*SubscriptBox[\(σ\), \(yy\)]\) = ", NumberForm[σyy1, {6, 2}], ";", 
     "  \!\(\*SubscriptBox[\(τ\), \(xy\)]\) = ", NumberForm[-τxy1a, {6, 2}]}], 
     Dynamic[{pts[[2, 1]], pts[[2, 2]]}]]
    }  ;
  g3 = {Thickness[0.01], Blue, Line[pts]};
  {Column[{Graphics[{g1, Dynamic[g2], Dynamic[g3], 
       Inset[Text[Style["Mohr's circle", 18, Purple, Italic]], {49.58, 68.99}]},
         ImageSize -> 300 ], 
     Row[{"θ = " Slider[Dynamic[θ], {-90, 90}], Dynamic@θ " degree"}]}]}]

Mohr[100, 0, 25]

enter image description here

Edit

barebones version

Mohr[x_, R_] :=
 DynamicModule[{g1, g2},
  g1 = Circle[{x, 0}, R];
  pts = Dynamic @{{R Cos@t + x, R Sin@t}, {R Cos[t + Pi] + x, R Sin[t + Pi]}};
  g2 = {PointSize[0.05], Red, Point[Dynamic@pts], 
        Inset["A very long text", Dynamic@(pts[[1, 1]])]};
  {Column[{Graphics[{g1, Dynamic[g2]}, Axes -> True], 
     Row[{"t = " Slider[Dynamic[t], {-Pi/2, Pi/2}], Dynamic@t }]}]}]

Mohr[100, 25]
$\endgroup$
4
  • 2
    $\begingroup$ Did you write this code? $\endgroup$ Commented Jul 14, 2012 at 17:21
  • $\begingroup$ Please reduce your code sample the the minimum where your questions are meaningful and useful for others $\endgroup$ Commented Jul 14, 2012 at 18:56
  • 2
    $\begingroup$ @RChai, welcome to the site! We will do our best to help you out learn this great language! As to your first question, give it a shot by looking up Slider in the documentation. Let us know if you get stuck $\endgroup$
    – Rojo
    Commented Jul 14, 2012 at 23:24
  • $\begingroup$ @RChai let me second Rojo's welcome, and suggest that you register your account. That way, any upvotes from your future questions and answers will be added to the one you have from this question. $\endgroup$
    – Verbeia
    Commented Jul 15, 2012 at 3:19

1 Answer 1

2
$\begingroup$

Adding ImagePadding and ImageSize options and a few changes in the barebones version of your code:

Mohr2[x_, R_] := DynamicModule[{g1, g2, pts}, g1 = Circle[{x, 0}, R];
t = 0; 
pts = Dynamic@{{R Cos@t + x, R Sin@t}, {R Cos[t + Pi] + x, R Sin[t + Pi]}};
g2 = {PointSize[0.05], Red, Point[Dynamic@pts], Line[Dynamic@pts], 
Inset["A very long text", Dynamic@(pts[[1, 1]])]};
Column[{Graphics[{g1, Dynamic@g2}, Axes -> True, ImagePadding -> 30,
  ImageMargins -> 0, ImageSize -> 300], 
Row[{"t = " Slider[Dynamic[t], {-Pi, Pi}], Dynamic@t}]}, 
Alignment -> Center]]

 Mohr2[200, 15]

gives

enter image description here

$\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.