# How to draw a vector in a surface with asymptote?

This is my code so far:

import three;
import math;

texpreamble("\usepackage{bm}");
texpreamble("\newcommand{\ex}{\bm{e}_x}");
texpreamble("\newcommand{\ey}{\bm{e}_y}");
texpreamble("\newcommand{\ez}{\bm{e}_z}");
texpreamble("\newcommand{\av}{\bm{a}}");
texpreamble("\newcommand{\Ri}[1]{\bm{\vec{R}_{#1}}}");

size(300,0);

pen thickp=linewidth(2mm);
real rho=1, phi=60, z=0.8;

currentprojection=perspective((4,1,2));

real r=1.1;
pen p=black;
draw(Label("$\ex$",1.1),O--r*X,p,Arrow3); // x unit vector
draw(Label("$\ey$",1.1),O--r*Y,p,Arrow3); // y unit vector
draw(Label("$\ez$",1.1),O--r*Z,p,Arrow3); // z unit vector

triple R0=(-1,-3,-4); // vector R0 to cable
triple a=(0,1,0); // direction
draw(Label("$\av$",0.65),(R0-a/2)--(R0+a/2),red,Arrow3);
draw("$\Ri{0}$",R0--O,align=2*dir(90,phi),Arrow3,DotMargin3);


I want to achieve something similar to the red arrow on the blue surface: . The surface should be in the plane created by $a$ and $\vec{R_0}$. The new arrow should start in the origin of the coordinate axis, and the vector components of that arrow should be projected on $e_x, e_y$ and $e_z$.

I can not find an example for that, and I am an asymptote newbie.

• In your title, plane, not surface. Nov 4, 2021 at 7:59
• surface is the asymptote word for plane, that is why i called is surface, both for search results and for asymptote people who know it, @NguyenVanChi1998 Nov 4, 2021 at 8:04

This is what I ended up with (primarily thanks to the help of Alan Bromborsky):

import three;
import math;

texpreamble("\usepackage{bm}");
texpreamble("\newcommand{\ex}{\bm{e}_x}");
texpreamble("\newcommand{\ey}{\bm{e}_y}");
texpreamble("\newcommand{\ez}{\bm{e}_z}");
texpreamble("\newcommand{\av}{\bm\vec{{a}}}");
texpreamble("\newcommand{\Bv}{\bm\vec{{B}}}");
texpreamble("\newcommand{\nv}{\hat{\bm{n}}}");
texpreamble("\newcommand{\Ri}[1]{\bm\vec{{R}_{#1}}}");
texpreamble("\newcommand{\Bi}[1]{\bm\vec{{B}_{#1}}}");

void draw_right_angle(triple a, triple b, triple O, real l=0.25){
a = l*a/abs(a);
b = l*b/abs(b);
draw("",(O+a)--(O+a+b)--(O+b),linewidth(0.25mm)+black);
return;}

void draw_two_vector_surface(triple a, triple b, triple O,real l=1.4, material m=opacity(0.5)+yellow){
real lc = l-1;
a = l*a;
b = l*b;
triple ac = lc*a;
triple bc = lc*b;
path3 P = (O+a+b)--(O-ac+b)--(O-ac-bc)--(O-bc+a)--cycle;
draw(surface(P),m,nolight);
return;}

triple draw_two_vector_vector(triple a, triple b, triple O, real l=0.6){
a = a/abs(a);
b = -b/abs(b);
triple c = l * (a+b);
draw((O)--(O+c),green,Arrow3);
return c;}

size(300,0);

pen thickp=linewidth(2mm);
real rho=1, phi=60, z=0.8;

// currentprojection=orthographic((4,1,2));
currentprojection=perspective((4,-2,2));

real r=1.1;
pen p=black;
draw(Label("$\ex$",1.1),O--r*X,p,Arrow3); // x unit vector
draw(Label("$\ey$",1.1),O--r*Y,p,Arrow3); // y unit vector
draw(Label("$\ez$",1.1),O--r*Z,p,Arrow3); // z unit vector

triple R0=(-2,-1,-2); // vector R0 to cable
triple a=(1,0.2,-0.1); // direction
triple n=cross(a,R0);
triple O=(0,0,0);

n = n/abs(n);
draw(Label("$\av$",0.65),(R0-a/2)--(R0+a/2),align=W, red,Arrow3);
draw_right_angle(a,n,R0,0.25);
draw_right_angle(-R0,n,R0,0.25);

draw("$\Ri{0}$",R0--O,align=2*dir(90,phi),Arrow3,DotMargin3);

draw_two_vector_surface(a, R0, O);
triple Bv = draw_two_vector_vector(a, R0, O );
label("$\Bv$", O+Bv, SE, green);

draw("",(O--O+(Bv.x,0,0)), green, Arrow3);
draw("",(O+Bv--O+(Bv.x,0,0)), dashed+green);
label("$\Bi{x}$", O+(Bv.x,0,0),W, green);

draw("",(O--O+(0,Bv.y,0)), green, Arrow3);
draw("",(O+Bv--O+(0,Bv.y,0)), dashed+green);
label("$\Bi{y}$", O+(0,Bv.y,0),NW*0.4, green);

draw("",(O--O+(0,0,Bv.z)), green, Arrow3);
draw("",(O+Bv--O+(0,0,Bv.z)), dashed+green);
label("$\Bi{z}$", O+(0,0,Bv.z),W, green);