From a9ecd537906dd5233b9f5bc445d072624f15bc67 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 11 Apr 2012 16:02:25 -0400 Subject: [PATCH] Add Vector.copy(), +, and - to Mechanics.asy. Also simplify Vector labeling to respect Label attributes (along the lines of the earlier changes with LabeledCircle). --- asymptote/Mechanics-test.asy | 13 ++++++- asymptote/Mechanics.asy | 67 +++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/asymptote/Mechanics-test.asy b/asymptote/Mechanics-test.asy index a99f088..9dc92ec 100644 --- a/asymptote/Mechanics-test.asy +++ b/asymptote/Mechanics-test.asy @@ -62,7 +62,8 @@ vs.push(jhat(center=(0,u))); vs.push(Vector(center=(-1u,-4u), phi=90, "Out")); vs.push(Vector(center=(-1u,-5.5u), phi=-90, "In")); -vs.push(Vector(center=(0, -4u), mag=1.5u, dir=-90, phi=60, "60dg OOP")); +vs.push(Vector(center=(0, -4u), mag=1.5u, dir=-90, phi=60, + Label("60dg OOP", position=EndPoint))); for (int i=0; i this.out_of_plane_tolerance) { + return this.mag*Cos(this.phi)*dir(this.dir); + } + return (0, 0); + } + pair pTip() { - return this.mag*dir(this.dir)+this.center; + return this.dTip() + this.center; } - - void draw(picture pic=currentpicture, bool rotateLabel=false, pair labelOffset=(0,0)) { + + void draw(picture pic=currentpicture) { picture picF; - pair p = (0,0); - pair label_rotate = (1,0); - pair label_align = (0,1); + pair p = this.dTip(); + path P; real phi_e = this.phi % 360; // effective phi if (this.mag < 0) (phi_e + 180) % 360; if (Tan(phi_e) == 0 || abs(1.0/Tan(phi_e)) > this.out_of_plane_tolerance) { // draw arrow in the plane of the drawing // TODO: thickening for phi? - p = this.mag*Cos(this.phi)*dir(this.dir); - path P = (0,0)--p; + P = (0,0)--p; draw(picF, P, outline, Arrow); - if (rotateLabel == true) - label_rotate = p; - label_align = unit(rotate(90)*p); } else if (phi_e > 0 && phi_e < 180) { // draw a circled dot for out-of-the-page - p = (0, this.out_of_plane_radius); - draw(picF, scale(this.out_of_plane_radius)*unitcircle, outline); + P = scale(this.out_of_plane_radius)*unitcircle; + draw(picF, P, outline); dot(picF, (0,0), outline); } else { // draw a circled cross for into-the-page real a = 0.8*sqrt(2.0)/2.0; - p = (0, this.out_of_plane_radius); - draw(picF, scale(this.out_of_plane_radius)*unitcircle, outline); + P = scale(this.out_of_plane_radius)*unitcircle; + draw(picF, P, outline); draw(picF, scale(this.out_of_plane_radius)*((-a,-a)--(a,a)), outline); draw(picF, scale(this.out_of_plane_radius)*((-a,a)--(a,-a)), outline); } - label(pic = picF, - L = rotate(degrees(label_rotate)) * L, - position = p+labelOffset, - align = label_align); + label(pic=picF, L=this.label, g=P); add(pic, picF, center); } } +Vector operator +(Vector a, Vector b) { + Vector c = a.copy(); + pair p = a.mag*dir(a.dir) + b.mag*dir(b.dir); + c.mag = length(p); + c.dir = degrees(p); + return c; +} + +Vector operator -(Vector a, Vector b) { + Vector c = a.copy(); + pair p = a.mag*dir(a.dir) - b.mag*dir(b.dir); + c.mag = length(p); + c.dir = degrees(p); + return c; +} + void vector_field(pair center=(0,0), real width=2cm, real height=2cm, real dv=0.5cm, real buf=2pt, Vector v=null, pen outline=invisible) { -- 2.26.2