Java 计算处理中跟随直线的旋转角度

Java 计算处理中跟随直线的旋转角度,java,processing,Java,Processing,我画了一条随机线,我想沿着这条线画一个字母,在这里是V。我想让V的底点旋转,并跟随线的方向,无论线的角度或方向是什么。但我真的不知道如何计算这个角度。下面是一些简单的代码来演示我的问题。你会看到红线被画出来,我希望V的底点引导这条线被画出来 提前感谢您的建议 float [ ] lineProgress = { 75, 350, 350, 350, 0 }; int lineSpeed = 25; float angle = 0; void setup() { background (25

我画了一条随机线,我想沿着这条线画一个字母,在这里是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
}

你计算旋转角度的朋友是。您可以使用其中任何一个,但切线不涉及计算斜边的长度:

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)一致


干杯,

你的朋友在计算旋转角度时是正确的。您可以使用其中任何一个,但切线不涉及计算斜边的长度:

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)一致

干杯

  • 求直线的斜率
    y=mx+c
    m
    为斜率)。角度=
    arctan(m)
  • 旋转角度(顺时针)=
    2*Pi-角度
  • 求直线的斜率
    y=mx+c
    m
    为斜率)。角度=
    arctan(m)
  • 旋转角度(顺时针)=
    2*Pi-角度
  • 给定一条从
    (x0,y0)
    (x1,y1)
    的直线,其中X偏移
    dx=x1-x0
    和Y偏移
    dy=y1-y0
    ,角度为:

    atan2(dy, dx)
    
    以弧度为单位

    使用而不是atan(y/x)可确保返回的角度位于右象限
    atan
    只返回从
    -π/2
    +π/2
    的结果,而不是从

    (x0,y0)
    (x1,y1)
    的完整
    ,角度为:

    atan2(dy, dx)
    
    以弧度为单位

    使用而不是atan(y/x)
    可确保返回的角度位于右象限
    atan
    仅将结果从
    -π/2
    返回到
    +π/2
    ,而不是将完整的
    返回到