ランダムな線が描かれており、この場合は 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
}