首页 > 解决方案 > 缩放图形处理2.2.1

问题描述

请帮助,在 Arduino Uno,我收到来自传感器的信号并使用处理 2.2.1 构建图形,但您需要在不丢失比例的情况下扩大规模。我的尝试失败了,比例崩溃了(我试图将值相乘)代码:

    Serial myPort; 
int xPos = 1;  
int yPos = 100;
float yOld = 0;
float yNew = 0;
float inByte = 0;
int lastS = 0;
PFont f;
void setup () {
  size(1200, 500);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
  background(0xff);
}
void draw () {
  int s = second();
  PFont f = createFont("Arial",9,false);
  textFont(f,9);
  fill(0);
  if (s != lastS){
    stroke(0xcc, 0xcc, 0xcc);
    line(xPos, yPos+10, xPos, yPos+30);
    text(s + " Sec.", xPos+5, yPos+30);
    lastS = s;
  }
}
void mousePressed(){
  save(lastS + "-heart.jpg");
}
void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    if (inString.equals("!")) {
      stroke(0, 0, 0xff); // blue
      inByte = 1023; 
    } else {
      stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
      inByte = float(inString);
    }
    inByte = map(inByte, 0, 1023, 0, height);
    yNew = inByte;
    line(xPos-1, yPos-yOld, xPos, yPos-yNew);
    yOld = yNew;
    if (xPos >= width) {
      xPos = 1;
      yPos+=200;
      if (yPos > height-200){
        xPos = 1;
        yPos=100;
        background(0xff);
      }
    } else {
      xPos++;
    }
  }
}

标签: arduinoprocessing

解决方案


有多种方法可以缩放图形。

一个简单的尝试方法是简单scale()的渲染(绘图坐标系)。

请记住,当前缓冲区仅在xPos到达屏幕右侧时才被清除。

来自 Arduino 的值在此处映射到处理:

inByte = map(inByte, 0, 1023, 0, height);
    yNew = inByte;

您应该尝试将更改映射height到您认为合适的不同值。然而,这只会缩放 Y 值。x 值在此处递增:

xPos++;

您可能希望将此增量更改为与您尝试在 x 和 y 之间保持的比例一起使用的不同值。


推荐阅读