![调整均匀偏差值](https://linux22.com/image/284365/%E8%B0%83%E6%95%B4%E5%9D%87%E5%8C%80%E5%81%8F%E5%B7%AE%E5%80%BC.png)
我需要以随机方式(角度和 x、y 位置)将所有 10 个分子插入矩形中。我不知道我的代码有什么问题。
\startMPinclusions
vardef spring (expr a, b, w, h, n) =
pair vec ; path pat ; numeric len ; numeric ang ;
vec := (b-a) ;
pat := for i=1 upto n-1: (if odd(i):-fi w/2,i)--endfor (0,n) ;
pat := (0,0)--(0,h)-- pat shifted (0,h)--(0,n+h)--(0,n+2h) ;
len := (xpart vec ++ ypart vec)/(n+2h) ;
ang := -90+angle(vec) ;
( pat yscaled len rotatedaround(origin,ang) shifted a )
enddef ;
\stopMPinclusions
\enabletrackers
[metapost.showlog]
\starttext
\starttext
\startMPcode
tracingall;
picture molecule;
path p ; p :=(.1cm,0)--spring((.2cm,0),(2.5cm,0),.5cm,0,10)--(2.6cm,0) ;
draw p;
draw fullcircle scaled 2cm shifted(-0.9cm,0);
draw fullcircle scaled 2cm shifted(3.6cm,0);
molecule := currentpicture scaled .1;currentpicture:=nullpicture;
numeric angulo; numeric xx; numeric yy; pair par;
for i=1 step 1 until 10:
angulo:=uniformdeviate 180;
xx:=uniformdeviate 80;
yy:=uniformdeviate 50;
par:=(xx,yy);
draw molecule shifted par rotated angulo ;
endfor;
%clip currentpicture to (0,0)--(0,2cm)--(3cm,2cm)--(3cm,0)--cycle;
draw (0,0)--(0,2cm)--(3cm,2cm)--(3cm,0)--cycle withcolor 0.625red;
\stopMPcode
\stoptext
答案1
替换此
draw molecule shifted par rotated angulo ;
通过这个
draw molecule rotated angulo shifted par;
给出了更好的(但仍然不完美)结果:
(旋转rotated
总是以原点为中心,因此是前一种行为。你也可以使用线
draw molecule shifted par rotatedaround(par, angulo);
反而)。
编辑
我刚刚替换了定义红色边界框的线,
draw (((0,0)--(0,2cm)--(3cm,2cm)--(3cm,0)--cycle) withcolor 0.625red;
经过
draw (((0, 0)--(0, 50)--(80, 50)--(80, 0)--cycle) enlarged .46cm) withcolor 0.625red;
更精确地适合以下参数: