From 06f04ddae080572a159bc0a39acf0a3227a96907 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 11 Apr 2012 17:25:18 -0400 Subject: [PATCH] Simplify Spring labeling to respect Label attributes in Mechanics.asy. --- asymptote/Mechanics-test.asy | 7 +++++-- asymptote/Mechanics.asy | 38 +++++++++++++++++++----------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/asymptote/Mechanics-test.asy b/asymptote/Mechanics-test.asy index 69ec1b0..e0017bd 100644 --- a/asymptote/Mechanics-test.asy +++ b/asymptote/Mechanics-test.asy @@ -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); diff --git a/asymptote/Mechanics.asy b/asymptote/Mechanics.asy index 3aa7bcf..cd7fb49 100644 --- a/asymptote/Mechanics.asy +++ b/asymptote/Mechanics.asy @@ -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; } } -- 2.26.2