![如何在 metapost 中将一个点初始化为另外两个点的线性组合?](https://linux22.com/image/407977/%E5%A6%82%E4%BD%95%E5%9C%A8%20metapost%20%E4%B8%AD%E5%B0%86%E4%B8%80%E4%B8%AA%E7%82%B9%E5%88%9D%E5%A7%8B%E5%8C%96%E4%B8%BA%E5%8F%A6%E5%A4%96%E4%B8%A4%E4%B8%AA%E7%82%B9%E7%9A%84%E7%BA%BF%E6%80%A7%E7%BB%84%E5%90%88%EF%BC%9F.png)
我正在尝试将一个新点初始化为另外两个点的线性组合,作为绘制一条花式线的第一步。
目前我正在尝试这个:
vardef line(expr pfirst, psecond)=
tangent := psecond-pfirst;
draw u*pfirst--u*psecond withpen pencircle scaled 1 withcolor black;
enddef;
我也尝试过不使用冒号,而是将表达式放在括号中。
我得到的错误是:
>> tangent
(luamplib) >> (-1,-1)
(luamplib) ! Equation cannot be performed (numeric=pair).
(luamplib) <to be read again>
答案1
中未声明的变量metapost
被假定为numeric
。因此,类似
beginfig(0);
% myvar undeclared-> assumed to be numeric
myvar:=0;
endfig;
不会产生错误。但是,
beginfig(0);
% myvar undeclared-> assumed to be numeric
% error because assigning the pair (0,0) to the numeric variable myvar.
myvar:=(0,0);
endfig;
将产生与上述相同的错误。因此,可以通过告知metapost
这tangent
是一个pair
变量来解决您的问题:
vardef line(expr pfirst, psecond)=
save tangent; % make tangent local to the vardef (if this is desired)
pair tangent; % tell metapost that tangent will hold a pair
tangent := psecond-pfirst;
draw u*pfirst--u*psecond withpen pencircle scaled 1 withcolor black;
enddef;