2

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

3 に答える 3

3

XオフセットとYオフセットのあるから(x0, y0)への線が与えられた場合、角度は次のようになります。(x1, y1)dx = x1 - x0dy = y1 - y0

atan2(dy, dx)

これはラジアンで測定されます。

atan2(y, x)の代わりに使用atan(y / x)すると、返される角度が正しい象限にあることが保証されます。 完全なからではなく、atanからの結果のみを返します。-π/2+π/2

于 2012-10-19T08:09:53.127 に答える
1
  1. y = mx + c直線の傾き(傾き)を見つけますm。角度= arctan(m)
  2. 回転する角度(時計回り)=2 * Pi - Angle
于 2012-10-19T08:09:23.950 に答える
0

回転角度を計算する際の友は、サイン/コサインの関係です。それらのいずれかを使用できますが、接線のものは斜辺の長さを計算する必要はありません。

tan A = a / b

だからあなたの角度は

A = arctan( a / b )

またはJava用語で:

double angle = Math.atan( ( lineprogress[ 3 ] - lineprogress[ 1 ] ) / 
                          ( lineprogress[ 2 ] - lineprogress[ 0 ] ) );

または@Alnitakも書いているように、atan2を使用して右象限の結果を取得します。

double angle =  Math.atan2( lineprogress[ 2 ] - lineprogress[ 0 ] ,
                            lineprogress[ 3 ] - lineprogress[ 1 ] );

(x1,y1) == ( lineprogress[ 0 ] , lineprogress[ 1 ] ) と仮定し、(x2,y2) に対して合同

乾杯、

于 2012-10-19T08:03:33.937 に答える