Processing 处理-毕达哥拉斯定理计算鼠标指针和x,y之间的距离
我试图用毕达哥拉斯定理使所有接近指针的球体变暗。我不知道为什么我的mouseMoved不起作用。如果有人能检查一下我的代码,看看我的错误在哪里Processing 处理-毕达哥拉斯定理计算鼠标指针和x,y之间的距离,processing,Processing,我试图用毕达哥拉斯定理使所有接近指针的球体变暗。我不知道为什么我的mouseMoved不起作用。如果有人能检查一下我的代码,看看我的错误在哪里 int numBalls = 100; float spring = 0.17; float gravity = 0; float friction = -1; float value = 255; Ball[] balls = new Ball[numBalls]; void setup() { size(500, 500); for (i
int numBalls = 100;
float spring = 0.17;
float gravity = 0;
float friction = -1;
float value = 255;
Ball[] balls = new Ball[numBalls];
void setup() {
size(500, 500);
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(5, 60), i, balls);
}
noStroke();
fill(value);
}
void draw() {
background(0);
for (Ball ball : balls) {
ball.collide();
ball.move();
ball.display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity;
x += vx;
y += vy;
if (x + diameter/15 > width) {
x = width - diameter/15;
vx *= friction;
}
else if (x - diameter/15 < 0) {
x = diameter/15;
vx *= friction;
}
if (y + diameter/15 > height) {
y = height - diameter/15;
vy *= friction;
}
else if (y - diameter/15 < 0) {
y = diameter/15;
vy *= friction;
}
int numBalls=100;
浮簧=0.17;
浮球重力=0;
浮动摩擦力=-1;
浮点值=255;
Ball[]balls=新球[numBalls];
无效设置(){
大小(500500);
for(int i=0;i宽度){
x=宽度-直径/15;
vx*=摩擦;
}
否则如果(x-直径/15<0){
x=直径/15;
vx*=摩擦;
}
如果(y+直径/15>高度){
y=高度-直径/15;
vy*=摩擦力;
}
否则如果(y-直径/15<0){
y=直径/15;
vy*=摩擦力;
}
}
void display(){
椭圆(x,y,直径,直径);
}
float d=sqrt((x-y)*(x-y)+(mouseX-mouseY)*(mouseX-mouseY));
void mouseMoved(){
值=值-85;
如果(d<75){
填充(值);
}
}
}
您犯了两个主要错误。第一个是关于定理,应该这样计算:
sqrt((ball.x - mouseX) * (ball.x - mouseX) + (ball.y - mouseY) * (ball.y - mouseY))
第二个:如果你想使用鼠标事件,它们不能像那样在你的类中被重写(你只是创建了你自己的名为mouseMoved
的函数,它与此无关)。要简单地覆盖它们,请将其放置在草图末尾的任何类之外。另外,当您更改fill
值时,由于处理语言作为状态机的性质,它将影响所有的球,而不仅仅是接近的球(有关fill()
的更多信息)
现在您只需删除mouseMoved()
,为了更好地理解,可以添加以下行,而不是ball.display()代码>内部绘图
功能:
void draw() {
background(0);
for (Ball ball : balls) {
ball.collide();
ball.move();
float d = sqrt((ball.x - mouseX) * (ball.x - mouseX) + (ball.y - mouseY) * (ball.y - mouseY) );
if(d<75) ball.display();
}
}
void draw(){
背景(0);
用于(球:球){
ball.collide();
ball.move();
float d=sqrt((ball.x-mouseX)*(ball.x-mouseX)+(ball.y-mouseY)*(ball.y-mouseY));
如果(d@GaneshSittampalam这是在处理程序中。不确定我看到的是什么语言,这实际上是一种语言,我以前从未听说过。我正在删除函数编程标记,因为它在这里不相关。这并不能真正回答问题,但值得指出……sqrt计算非常昂贵,任何时候都可以避免。你应该不要把<代码>浮点D <代码> 75,你应该考虑使用<代码> dScord <代码> 75×75 。其中dcDalk是代码>(球.xMoux)*(球.xMoux)+(球.y - MouSe)*(球.Y -穆西)< /代码>
void draw() {
background(0);
for (Ball ball : balls) {
ball.collide();
ball.move();
float d = sqrt((ball.x - mouseX) * (ball.x - mouseX) + (ball.y - mouseY) * (ball.y - mouseY) );
if(d<75) ball.display();
}
}