带有表示力的标记的向量

带有表示力的标记的向量

在第一种教学方法中,向学生展示具有各种力的图表,并要求他们根据力的方向、方向和大小对力进行分组。我经常使用此代码,但想简化它,纠正标签的方向并调整不是参考倍数的矢量的长度(我的变量名:escala)。

\starttext

\startMPcode
path p;
numeric t, escala; 
pair OLD, NEW;
OLD:=(1.23433,3.46578634); 
escala:=10; %% scale
def vector(expr A, B,ve)= 
p := A..B;  
 for i = 0 upto 50: 
 t := escala*i/arclength(p);  
NEW := point t of p; 
 if NEW <>OLD: 
 draw ((-2,0)--(2,0)) 
 rotated (angle direction t of p-90)  
 shifted (point t of p) 
 withpen pencircle scaled .5pt withcolor red;  
fi; 
OLD:=point t of p; 
 endfor;  
 drawarrow p  withpen pencircle scaled 1pt withcolor .625red;  
label.lft(textext ( ve ), point 0.5 of p); 
enddef; 

def reference(expr A, B,ve)= 
  p := A..B;  
  for i = 0 upto 50: 
    t := escala*i/arclength(p);  
    NEW := point t of p; 
    if NEW <>OLD: 
        draw ((-2,0)--(2,0)) rotated (angle direction t of p-90) shifted (point t of p)  withpen pencircle scaled .5pt withcolor red;  
    fi; 
    OLD:=point t of p; 
  endfor;  
  draw p  withpen pencircle scaled 1pt withcolor .625red;  
  label.top(ve, point 0.5 of p); 
enddef; 

vector((2cm,5cm),(4cm,5cm),"$\vec{F}_1$"); 
vector(origin,(0cm,5cm),"$\vec{F}_2$");
vector((2cm,2cm),(4cm,4cm),"$\vec{F}_3$");
vector((2cm,1cm),(7cm,1cm),"$\vec{F}_4$");
reference((5cm,3cm),(6cm,3cm), "3 N");
\stopMPcode

\stoptext

在此处输入图片描述

答案1

在我看来,你的代码现在看起来已经足够简单了,尽管我不确定你希望调整长度的原因。至于标签,我建议使用freelabelMetafun 的宏,也就是说,用以下行替换

label.lft(textext ( ve ), point 0.5 of p);

按照你的vector定义是这样的:

pair lpoint ; lpoint = point 0.5 of p; 
freelabel(ve, lpoint, lpoint + (A-B) rotated 90);

查看MetaFun 手册(第 274 页)了解有关此最有用的宏的更多详细信息。结果如下:

在此处输入图片描述

编辑对于长度调整,我最终通过引入给出细分数量的vector数字变量来改变定义,并替换了指令N = floor(arclength p / escala)

drawarrow p  withpen pencircle scaled 1pt withcolor .625red; 

经过

drawarrow A -- (point N*escala on p)  withpen pencircle scaled 1pt withcolor .625red; 

vector定义:

def vector(expr A, B,ve)= 
  p := A..B; 
  numeric N ; N = floor(arclength(p)/escala) ; 
  for i = 0 upto N : 
    t := escala*i/arclength(p);    
    draw ((-2,0)--(2,0)) rotated (angle direction t of p-90)  
      shifted (point t of p) 
      withpen pencircle scaled .5pt withcolor red;   
  endfor;  
  drawarrow A -- (point N*escala on p)  withpen pencircle scaled 1pt withcolor .625red;  
  pair lpoint ; lpoint = point 0.5 of p; 
  freelabel(ve, lpoint, lpoint + (A-B) rotated 90);
enddef; 

新结果:

在此处输入图片描述

请注意,箭头会略微超出最后的标记,这是因为所选的笔粗细决定了绘制线条的粗细。如果要避免这种情况,请减小笔粗细或增加标记的粗细。

相关内容