arduino - 缩放图形处理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 的值在此处映射到处理:
inByte = map(inByte, 0, 1023, 0, height);
yNew = inByte;
您应该尝试将更改映射height
到您认为合适的不同值。然而,这只会缩放 Y 值。x 值在此处递增:
xPos++;
您可能希望将此增量更改为与您尝试在 x 和 y 之间保持的比例一起使用的不同值。
推荐阅读
- scripting - 试图只下载今天上传的文件
- c++ - 复制构造函数的初始化列表中的 make_unique 是否是不使用 noexcept 说明符的好目的?
- java - 如何从另一个 xml 文件访问我的 id?
- php - PHP 使用 preg_match_all 从字符串中提取属性名称
- karate - 空手道 - 有没有办法在场景大纲的示例部分从数据库中获取数据?
- time - 在 Ubuntu Vm 上重新启动 chrony 服务时出现“506 无法与守护进程对话”错误
- android - 为什么单击后退按钮时 MainActivity 会重新打开?
- php - Laravel 测试。测试时重置控制器 DI
- python - 平均列中每个唯一值的最后几个条目以生成新的 df
- javascript - 从 JS 数组创建字符串