Simplify Wire and Pendulum labeling to respect Label attributes in Mechanics.asy.
[course.git] / asymptote / Mechanics.asy
index a3246547593cb654cdf1258a48ed82d603182c43..8cdfbee09ab8b2267ee5cc73be68f18e86d77af3 100644 (file)
@@ -420,31 +420,24 @@ struct Wire {
   pair pFrom;
   pair pTo;
   pen outline;
-  Label L;
-  
+  Label label;
+
   void operator init(pair pFrom=(0,0), pair pTo=(5mm,0), pen outline=currentpen, Label L="") {
     this.pFrom = pFrom;
     this.pTo = pTo;
     this.outline = outline;
-    this.L = L;
+    this.label = L;
   }
-  
-  void draw(picture pic=currentpicture, bool rotateLabel=true) {
-    picture picF;
-    picture picL;
-    label(picL, L);
-    pair pLabelSize = 1.2 * (max(picL)-min(picL));
-    pair pDiff = pTo - pFrom;
-    path p = (0,0)--pDiff;
-    pair label_rotate=pDiff;
-    if (rotateLabel == false)
-      label_rotate=(1,0);
-    draw(picF, p, outline);
-    label(pic = picF,
-          L = rotate(degrees(label_rotate)) * L,
-          position =  pDiff/2
-            + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
-    add(pic, picF, pFrom);
+
+  void draw(picture pic=currentpicture) {
+    path p = this.pFrom--this.pTo;
+    draw(pic, p, outline);
+    embed e = this.label.embed;
+    if (this.label.embed == Rotate) {
+      this.label.embed = Rotate(this.pFrom - this.pTo);
+    }
+    label(pic=pic, L=this.label, g=p);
+    this.label.embed = e;
   }
 }
 
@@ -555,16 +548,16 @@ struct Pendulum {
   Mass mass;
   Angle angle;
   Wire str;
-  
+
   void operator init(pair pivot=(0,0), Mass mass=Mass()) {
     this.pivot = pivot;
     this.mass = mass;
     this.angle = Angle(mass.center(), pivot, pivot-(0,1));
     this.str = Wire(pivot, mass.center());
   }
-  
+
   void draw(picture pic=currentpicture, bool drawVertical=false) {
-    str.draw(pic=pic, rotateLabel=false);
+    str.draw(pic=pic);
     if (drawVertical == true) {
       pair final = pivot + realmult((0,0.5),(mass.center()-pivot));
       draw(pic=pic, pivot--final, p=currentpen+dashed);
@@ -580,7 +573,7 @@ Pendulum makePendulum(pair pivot=(0,0), Mass mass=Mass(), real length=15mm, real
   mass.set_center(pivot + length*dir(angleDeg-90));
   Pendulum p = Pendulum(pivot=pivot, mass=mass);
   p.angle.label = angleL;
-  p.str.L = stringL;
+  p.str.label = stringL;
   return p;
 }