我正在尝试围绕一个表示传感器所面向方向的圆圈旋转一条线,同时还绘制距离测量值。所以我不能在绘图功能中使用background()来清除屏幕,因为它会删除距离读数的绘图。我尝试过pggraphics和其他一些方法,但似乎无法找到方法。
这就是我现在所拥有的:
void setup() {
background(255,255,255);
size(540, 540);
}
void draw() {
translate(width/2, height/2);
ellipse(0,0,100,100);
newX = x*cos(theta)- y*sin(theta);
newY = x*sin(theta)+ y*cos(theta);
theta = theta + PI/100;
//pushMatrix();
fill(255, 255);
line(0, 0, newX, newY);
rotate(theta);
//popMatrix();
}
我是处理和编码的新手,但是任何人都可以指出我正确的方向如何做到这一点?感谢
这是它输出的内容:http://imgur.com/I825mjE
答案 0 :(得分:0)
您可以使用background()。您只需要重绘每帧的读数。您可以将读数存储在ArrayList中,这样您就可以添加新的读数,更改它们并将其删除。
一个例子:
ArrayList<PVector> readings;
int readingsCount = 15;
void setup() {
size(540, 540);
// create collection of random readings
readings = new ArrayList<PVector>();
for(float angle = 0; angle < TWO_PI; angle += TWO_PI/ readingsCount) {
float distance = random(100, 200);
// the new reading has an angle...
PVector newReading = PVector.fromAngle(angle);
// ... and a distance
newReading.mult(distance);
// Add the new reading to the collection
readings.add(newReading);
}
}
void draw() {
background(255);
// Put (0, 0) in the middle of the screen
translate(width/2, height/2);
float radius = 250;
noFill();
ellipse(0, 0, 2*radius, 2*radius);
// draw spinning line
float angle = frameCount * 0.1;
line(0, 0, radius * cos(angle), radius * sin(angle));
// draw readings
for(PVector p : readings) {
ellipse(p.x, p.y, 20, 20);
}
}