我画了一条随机线,在这种情况下,我想用字母 V 沿着这条线追踪。无论画线的角度或方向如何,我都希望 V 的底点旋转并跟随线的方向。但老实说,我不知道如何计算这个角度。下面是一些简单的代码来演示我的问题。您会看到正在绘制的红线,我希望 V 的底部点引导正在绘制的线。
在此先感谢您的任何建议。
float [ ] lineProgress = { 75, 350, 350, 350, 0 };
int lineSpeed = 25;
float angle = 0;
void setup() {
background (255);
size(400,400);
noFill();
frameRate(5);
}
void draw()
{
background (255);
strokeWeight(1);
stroke (0,255,0);
line(lineProgress[0],lineProgress[1],lineProgress[2],lineProgress[3]);
stroke (255,0,0);
fill(255, 0,0, 125);
float angle;
//How Do I calculate this based on the line being drawn?
angle =radians(270);
line(
lineProgress[0]
, lineProgress[1]
, lerp(lineProgress[0], lineProgress[2],lineProgress[4]/lineSpeed)
, lerp(lineProgress[1], lineProgress[3],lineProgress[4]/lineSpeed)
);
rotLetter(
"V"
, lerp(lineProgress[0]
, lineProgress[2]
, lineProgress[4]/lineSpeed)
, lerp(lineProgress[1]
, lineProgress[3],lineProgress[4]/lineSpeed)
, angle
) ;
rotLetter("V", 200,200,angle) ;
lineProgress[4]++;
if (lineProgress[4]>lineSpeed)
{
lineProgress[4]=0;
lineProgress[0]=random(50,350);
lineProgress[1]=random(50,350);
lineProgress[2]=random(50,350);
lineProgress[3]=random(50,350);
}
}
void rotLetter(String l, float x, float y, float ang) {
pushMatrix(); // save state
textAlign(CENTER); // center letter horiz
translate(x, y); // move to position
rotate(ang); // rotate
// draw char centered on acsender
// this will work for most Caps, and some lc letters
// but it will not allways vert center letters
text(l, 0, textAscent()/2);
popMatrix(); // return to saved coordinate matrix
}