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<as.length; i+=1)
as[i].draw();
Surface s = Surface(pFrom=(0,-7u), pTo=(3.5u, -7u), L="Table");
s.draw();
+Vector v1 = Vector((-2u, -7u), Label("$v_1$", align=E, position=EndPoint));
+v1.draw();
+Vector v2 = Vector(v1.center, mag=10mm, dir=90, Label("$v_2$", align=W));
+v2.draw();
+Vector v3 = v1 + v2;
+v3.label = Label("$v_3 = v_1 + v_2$", align=E, position=EndPoint);
+v3.draw();
+Vector v4 = v1 - v2;
+v4.label = Label("$v_4 = v_1 - v_2$", align=S, position=EndPoint);
+v4.draw();
Vector v = Velocity();
pair vfc = (5u,0); // vector field center
real dir; // angle in the plane of the drawing.
real phi; // angle with the plane of the drawing, 90 is out of the page.
pen outline;
- Label L;
+ Label label;
real out_of_plane_radius;
real out_of_plane_tolerance;
this.dir = dir;
this.phi = phi;
this.outline = outline;
- this.L = L;
+ this.label = L;
this.out_of_plane_radius = 1mm;
this.out_of_plane_tolerance = 0.01;
}
+ Vector copy() {
+ Vector v = Vector(center=this.center, mag=this.mag, dir=this.dir,
+ phi=this.phi, outline=this.outline, L=this.label);
+ v.out_of_plane_radius = this.out_of_plane_radius;
+ v.out_of_plane_tolerance = this.out_of_plane_tolerance;
+ return v;
+ }
+
+ pair dTip() { // offset from center to tip
+ pair p = (0,0);
+ real phi_e = this.phi % 360; // effective phi
+ if (Tan(phi_e) == 0 || abs(1.0/Tan(phi_e)) > 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) {