如何在 Processing 中旋转圆圈(类似雷达)的线同时绘制点?
How to rotate a line in a circle (radar like) in Processing while also plotting points?
我试图在代表传感器朝向的圆圈中旋转一条线,同时还绘制距离测量值。所以我不能在 draw 函数中使用 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();
}
我是 Processing 和一般编码的新手,但谁能为我指明正确的方向,告诉我如何做到这一点?谢谢
这是它的输出:http://imgur.com/I825mjE
您可以使用 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);
}
}
我试图在代表传感器朝向的圆圈中旋转一条线,同时还绘制距离测量值。所以我不能在 draw 函数中使用 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();
}
我是 Processing 和一般编码的新手,但谁能为我指明正确的方向,告诉我如何做到这一点?谢谢
这是它的输出:http://imgur.com/I825mjE
您可以使用 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);
}
}