Simplify Spring labeling to respect Label attributes in Mechanics.asy.
authorW. Trevor King <wking@drexel.edu>
Wed, 11 Apr 2012 21:25:18 +0000 (17:25 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 11 Apr 2012 21:25:18 +0000 (17:25 -0400)
asymptote/Mechanics-test.asy
asymptote/Mechanics.asy

index 69ec1b037521cdb25f026fac38012e2e3226dd30..e0017bdf6686aae852a1155403b472b27813cbbe 100644 (file)
@@ -82,9 +82,12 @@ Pendulum p = makePendulum(
     angleL="$\rho$", stringL=Label("r", embed=Shift));
 
 real len = abs(p.pivot.x-b.center().x);
-Spring s = Spring(pFrom=b.center()-(2u,0), pTo=b.center(), L="$k_1$");
+Spring s = Spring(
+    pFrom=b.center(), pTo=b.center()-(2u,0), L=Label("$k_1$", align=N));
 s.draw();
-s = Spring(pFrom=b.center(), pTo=(p.pivot.x, b.center().y), L="$k_2$");
+s = Spring(
+    pFrom=b.center(), pTo=(p.pivot.x, b.center().y+0.5u),
+    L=Label("$k_2$", align=LeftSide, position=Relative(0.7)));
 s.draw();
 
 p.draw(drawVertical=true);
index 3aa7bcf1949b6e60ab02f6df70543de562121ecb..cd7fb4929226f959d5443bf43ae6c138c5cbd4b9 100644 (file)
@@ -489,7 +489,7 @@ struct Spring {
   real unstretchedLength;
   int nLoops;
   pen outline;
-  Label L;
+  Label label;
 
   void operator init(pair pFrom=(0,0), pair pTo=(5mm,0), real k=1, real width=3mm, real dLength=1mm, real deadLength=3mm, real unstretchedLength=12mm, pen outline=currentpen, Label L="") {
     this.pFrom = pFrom;
@@ -499,7 +499,7 @@ struct Spring {
     this.dLength = dLength;
     this.unstretchedLength = unstretchedLength;
     this.outline = outline;
-    this.L = L;
+    this.label = L;
 
     this.nLoops = floor((unstretchedLength-2*deadLength)/dLength);
     if (this.nLoops == 0)
@@ -507,11 +507,7 @@ struct Spring {
     this.deadLength = (unstretchedLength-this.nLoops*dLength)/2;
   }
 
-  void draw(picture pic=currentpicture, bool rotateLabel=true) {
-    picture picF;
-    picture picL;
-    label(picL, L);
-    pair pLabelSize = 1.2 * (max(picL)-min(picL));
+  void draw(picture pic=currentpicture) {
     pair pDiff = pTo - pFrom;
 
     real working_dLength = (length(pDiff) - 2*deadLength) / nLoops;
@@ -527,17 +523,23 @@ struct Spring {
     }
     loopStart = point(p, length(p));
     p = p--(loopStart+(deadLength,0));
-    p = rotate(degrees(pDiff)) * p;
-
-    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) * (width + pLabelSize.y) / 2);
-    add(pic, picF, pFrom);
+    p = shift(this.pFrom)*rotate(degrees(pDiff)) * p;
+    draw(pic, p, outline);
+    align a = this.label.align;
+    embed e = this.label.embed;
+    real m = labelmargin(this.label.p);
+    real scale = (m + this.width/2)/m;
+    if (this.label.align.is3D) {
+      this.label.align.dir3 *= scale;
+    } else {
+      this.label.align.dir *= scale;
+    }
+    if (this.label.embed == Rotate) {
+      this.label.embed = Rotate(this.pTo - this.pFrom);
+    }
+    label(pic=pic, L=this.label, g=this.pFrom -- this.pTo);
+    this.label.align = a;
+    this.label.embed = e;
   }
 }