将(0,0)设置为左下方而不切换Processing(.org)中的X轴和Y轴

时间:2012-05-19 04:58:11

标签: processing

我想在2d处理草图中更改坐标系,将(0,0)放在左下角而不是左上角。以下代码将(0,0)移动到左下角:

transform(0, height);
rotate(radians(-90));

但是,它也使X轴成为垂直轴。是否有一种简单的方法可以将(0,0)移动到左下角保持X轴水平?

我考虑过的一个选项是将P3D与rotate和rotateY结合使用,但更喜欢2d案例的解决方案。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

你可以简单地翻译而不旋转:

transform(0, height);

处理你的坐标翻转: boolean useVFlip = true;

void setup(){
  size(400,400);
}
void draw(){
  background(255);

  if(useVFlip) translate(0,height);
  drawAxes(100);

  translate(width * .5, useVFlip ? (height-mouseY)*-1 : mouseY);
  triangle(0,0,100,0,100,100);
}
void keyPressed(){   useVFlip = !useVFlip; }
void drawAxes(int size){
  pushStyle();
  strokeWeight(10);
  stroke(192,0,0);
  line(0,0,size,0);
  stroke(0,192,0);
  line(0,0,0,size);
  popStyle();
}

如果它更容易获得翻转坐标,您可以使用scale()方法翻转整个坐标系: boolean useVFlip = true;

void setup(){
  size(400,400);
}
void draw(){
  background(255);

  if(useVFlip){
    scale(1,-1);
    translate(0,-height);
  }
  drawAxes(100);

  translate(width * .5, useVFlip ? height-mouseY : mouseY);
  triangle(0,0,100,0,100,100);
}
void keyPressed(){   useVFlip = !useVFlip; }
void drawAxes(int size){
  pushStyle();
  strokeWeight(10);
  stroke(192,0,0);
  line(0,0,size,0);
  stroke(0,192,0);
  line(0,0,0,size);
  popStyle();
}

您可以使用PMatrix2D执行相同操作,但不确定您对它们的熟悉程度如何: boolean useCustomCS = true; PMatrix2D customCS;

void setup(){
  size(400,400);
  customCS = new PMatrix2D(  1,  0,  0,
                             0, -1,height);
  fill(0);
}
void draw(){
  background(255);

  if(useCustomCS) applyMatrix(customCS);
  drawAxes(100);

  translate(width * .5, useCustomCS ? height-mouseY : mouseY);
  text("real Y:" + mouseY + " flipped Y: " + (height-mouseY),0,0);
  triangle(0,0,100,0,100,100);
}
void keyPressed(){   useCustomCS = !useCustomCS; }
void drawAxes(int size){
  pushStyle();
  strokeWeight(10);
  stroke(192,0,0);
  line(0,0,size,0);
  stroke(0,192,0);
  line(0,0,0,size);
  popStyle();
}

答案 1 :(得分:2)

尝试以下方法:

translate(0,height);

scale(1,-1);