From: W. Trevor King Date: Wed, 11 Apr 2012 20:27:49 +0000 (-0400) Subject: Simplify Angle labeling to respect Label attributes in Mechanics.asy. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=7eb6f5a8e176aee8fb04de17cd9c6ec8b81a301e;p=course.git Simplify Angle labeling to respect Label attributes in Mechanics.asy. --- diff --git a/asymptote/Mechanics-test.asy b/asymptote/Mechanics-test.asy index 9dc92ec..42897d7 100644 --- a/asymptote/Mechanics-test.asy +++ b/asymptote/Mechanics-test.asy @@ -46,14 +46,18 @@ Mass a = Mass(center=(0,0)); Mass b = Mass(center=(2u,1u), Label("$m_b$", align=N)); Mass c = Mass(center=(1u,-2u), Label("$m_c$", align=E)); Mass ms[] = {a,b, c}; -Distance dab = Distance(a.center(), b.center(), scale=u, L="$r_{ab}$"); +Distance dab = Distance(b.center(), a.center(), scale=u, + L=Label("$r_{ab}$", embed=Shift)); Distance dac = Distance(a.center(), c.center(), scale=u, L="$r_{ac}$"); Distance ds[] = {dab, dac}; Angle bac1 = Angle( b.center(), a.center(), c.center(), radius=.7u, fill=red, L="$\theta_T'$"); Angle bac2 = Angle( b.center(), a.center(), c.center(), radius=-.5u, L="$\theta_T$"); -Angle as[] = {bac1, bac2}; +Angle bac3 = Angle( + b.center(), a.center(), c.center(), radius=1.5u, + L=Label("$\theta_T''$", position=Relative(0.3))); +Angle as[] = {bac1, bac2, bac3}; Vector vs[]; diff --git a/asymptote/Mechanics.asy b/asymptote/Mechanics.asy index 4c925fa..96e1a9b 100644 --- a/asymptote/Mechanics.asy +++ b/asymptote/Mechanics.asy @@ -38,7 +38,6 @@ struct LabeledCircle { } void draw_label(picture pic=currentpicture, Label L=null) { - picture picL; align a; if (L == null) { L = this.label; @@ -322,7 +321,7 @@ struct Distance { real offset; real scale; pen outline; - Label L; + Label label; void operator init(pair pFrom=(0,0), pair pTo=(5mm,0), real offset=0, real scale=5mm, pen outline=currentpen, Label L="") { this.pFrom = pFrom; @@ -330,30 +329,19 @@ struct Distance { this.offset = offset; this.scale = scale; this.outline = outline; - this.L = L; + this.label = L; } - - void draw(picture pic=currentpicture, bool rotateLabel=true, - real labelangle=90, real labeloffset=-1) { - picture picF; - pair pDiff = pTo - pFrom; - if (labeloffset == -1) { - picture picL; - label(picL, L); - pair pLabelSize = 1.2 * (max(picL)-min(picL)); - labeloffset = pLabelSize.y/2; + + void draw(picture pic=currentpicture) { + pair o = this.offset*unit(rotate(-90)*(this.pTo - this.pFrom)); + path p = (this.pFrom+o) -- (this.pTo+o); + draw(pic, p, outline, Arrows); + embed e = this.label.embed; + if (this.label.embed == Rotate) { + this.label.embed = Rotate(this.pFrom - this.pTo); } - path p = (0,0)--pDiff; - pair label_rotate=pDiff; - if (rotateLabel == false) - label_rotate=(1,0); - draw(picF, p, outline, Arrows); - label(pic = picF, - L = rotate(degrees(label_rotate)) * L, - position = pDiff/2 - + unit(rotate(labelangle)*pDiff) * labeloffset); - //label(pic=picF, L = rotate(degrees(label_rotate)) format("%g", pDiff/scale), position = TODO); - add(pic, picF, pFrom+offset*unit(rotate(-90)*pDiff)); + label(pic=pic, L=this.label, g=p); + this.label.embed = e; } } @@ -364,7 +352,7 @@ struct Angle { real radius; // radius < 0 for exterior angles. pen outline; pen fill; - Label L; + Label label; void operator init(pair B, pair A, pair C, real radius=5mm, pen outline=currentpen, pen fill=invisible, Label L="") { this.B = B; @@ -373,17 +361,13 @@ struct Angle { this.radius = radius; this.outline = outline; this.fill = fill; - this.L = L; + this.label = L; } - void draw(picture pic=currentpicture, bool rotateLabel=false, - real labelOffsetAdjustment=0) { + void draw(picture pic=currentpicture) { picture picF; - picture picL; bool direction; - label(picL, L); - pair pLabelSize = 1.2 * (max(picL)-min(picL)); real ccw_angle = degrees(C-A)-degrees(B-A); bool direction = CCW; if (ccw_angle < 0) ccw_angle += 360.0; @@ -392,23 +376,15 @@ struct Angle { if (radius < 0) direction = !direction; path p = arc((0,0), fabs(radius), degrees(B-A), degrees(C-A), direction); - real t = reltime(p, 0.5); - pair P = midpoint(p); - pair tangent = dir(p, t); - if (direction == CW) tangent *= -1.0; - - pair label_rotate = tangent; - if (rotateLabel == false) - label_rotate = (1,0); - if (this.fill != invisible) { path pcycle = (0,0) -- p -- cycle; fill(picF, pcycle, this.fill); } draw(picF, p, this.outline); - label(pic = picF, - L = rotate(degrees(label_rotate)) * L, - position = P + unit(P) * (pLabelSize.y / 2 + labelOffsetAdjustment)); + if (direction == CW) { + p = reverse(p); + } + label(pic=picF, L=this.label, g=p); add(pic, picF, A); } } @@ -601,7 +577,7 @@ struct Pendulum { Pendulum makePendulum(pair pivot=(0,0), Mass mass=Mass(), real length=15mm, real angleDeg=0, Label angleL="", Label stringL="") { mass.set_center(pivot + length*dir(angleDeg-90)); Pendulum p = Pendulum(pivot=pivot, mass=mass); - p.angle.L = angleL; + p.angle.label = angleL; p.str.L = stringL; return p; }