微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Processing(java)中使用鼠标坐标绘制箭头

如何解决在Processing(java)中使用鼠标坐标绘制箭头

我正在开发一个用鼠标绘制箭头的程序,因此使用 PVector 使箭头指向正确的方向(一个用于底部一个用于箭头尖端)。

我让它工作,所以你可以画一个箭头,它留在屏幕上,但第二个和第三个箭头等都画在了一个完全错误的地方。 看起来坐标没有正确传递给 drawArrow() 函数。有谁知道我解决这个问题的方法有什么问题?

PVector arrowTip = new PVector(); //for drawing arrows
PVector lineBase = new PVector(); //for drawing arrows

//save drawn arrow value for drawing saved arrows
PVector [] base = new PVector[100];
PVector [] vec = new PVector[100];
PVector vector = new PVector();
boolean drawing;
float xx,yy;


int flag=0;


void settings() {
  float hg=displayHeight/1.1;
  int wd=(int)hg*10/8;
  size(wd,(int)hg);
  drawing=false;
}

void draw() {
  background(255);

  //draw arrows
   if (flag == 1) {
   
   arrowTip.x = mouseX;
   arrowTip.y = mouseY;
   drawArrow(lineBase,arrowTip.sub(lineBase));
   }
   
  
  if (flag==1) {
    arrowTip.x = mouseX;
    arrowTip.y = mouseY;
  }
  
  //new
  drawSavedArrows();
}

//new
void drawSavedArrows() {
  for (int i =0; i<base.length; i++) {
    if (vec[i]!=null && base[i]!=null) {
      //draw saved arrow
      xx=vec[i].x;
      yy=vec[i].y;
      vector.x=xx;
      vector.y=yy;
      drawArrow(base[i],vector.sub(base[i]));//this is not working,the relative o,o coor seem to be stuck on the first one saved
    }
  }
}


void drawArrow(PVector base,PVector vector) {

  strokeWeight(height / 50);
  fill(20,85,30,180);
  stroke(20,180);
  translate(base.x,base.y); //set relative 0,0 coor
  line(0,vector.x,vector.y); //draw line from relative 0,0 to mouse
  rotate(vector.heading());
  int arrowSize = height / 20;
  translate(vector.mag() - arrowSize,0); //length of vec minus arrowsize relative
  triangle(0,arrowSize / 1.5,-arrowSize / 1.5,arrowSize,0);
}


void mousepressed() {
  if (mouseButton == RIGHT) //right click for drawing arrows
  {
    lineBase.x = pmouseX; //get the coor for the staring point of the arrow
    lineBase.y = pmouseY;


    //save base vector to first null
    for (int i =0; i<base.length; i++) {
      if (base[i]==null && vec[i]==null) {

        base[i] = new PVector();
        base[i].x=pmouseX;
        base[i].y=pmouseY;
        break;
      }
    }
    drawing=true;
  } else if(mouseButton == LEFT){
    for (int i=0; i<base.length; i++) {     //delete all vectors
      if (base[i]==null) {
        break;
      }
      base[i]=null; 
      vec[i]=null;
    }
  }
} 


void mouseDragged() {
  if (mouseButton == RIGHT) {
    flag = 1;
  }
}
void mouseReleased() {
  flag = 0;
  if (drawing) {
    for (int i =0; i<vec.length; i++) {
      if (vec[i]==null) {

        vec[i] = new PVector();//save coor for arrow tip
        vec[i].x=arrowTip.x;
        vec[i].y=arrowTip.y;
        break;
      }
    }
  }
  drawing=false;
}


解决方法

我发现翻译是累积性的。 所以这个:

translate(100,300);

与此相同:

translate(50,150);
translate(50,150):

我必须做的是使用 push() 和 pop() 隔离转换的效果。你所要做的就是像这样更新你的 drawArrow() 函数:

 void drawArrow(PVector base,PVector vector) {
  push();
  strokeWeight(height / 50);
  fill(20,85,30,180);
  stroke(20,180);
  translate(base.x,base.y); //set relative 0,0 coor
  line(0,vector.x,vector.y); //draw line from relative 0,0 to mouse
  rotate(vector.heading());
  int arrowSize = height / 20;
  translate(vector.mag() - arrowSize,0); //length of vec minus arrowsize relative
  triangle(0,arrowSize / 1.5,-arrowSize / 1.5,arrowSize,0);
  pop();
}
,

我建议使用 pushMatrix()popMatrix()。现在发生的事情是您正在对第一个向量进行平移和旋转,然后您正在对它上面的第二个向量进行另一个平移和旋转。 pushMatrix()popMatrix() 将重置每个向量的转换:

void drawArrow(PVector base,PVector vector) {
  strokeWeight(height / 50);
  fill(20,180);
  pushMatrix();
  translate(base.x,0);
  popMatrix();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。