缩放图形处理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++;
}
}
}

缩放图形有多种方法。

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

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

Arduino的值被映射到这里的Processing:

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

您应该尝试将更改height映射到您认为合适的其他值。但是,这将仅缩放Y值。x值在此处递增:

xPos++;

您可能想要将这个增量更改为一个不同的值,该值与您试图在x和y之间保持的比例一致。

最新更新