放大鼠标指向的位置
Zoom into where mouse in pointing
我正在编写太阳系模拟,我需要能够缩放 in/out 我的鼠标指向的位置。到目前为止,我得到的是移动行星并缩放 in/out 点 (0, 0) 所在的位置。
这是我的代码:
主要class
Planeta sun = new Planeta(color(235, 225, 52), 0, 696000, 0, 0);
Planeta mercury = new Planeta(color(166, 142, 88), 57909170, 2439, 0, 87.989);
Planeta venus = new Planeta(color(250, 193, 50), 108208926, 6052, 0, 224.701);
color stroke = color(70, 70, 70, 70);
int sx, sy;
int centerX, centerY;
static float m1 = 1000;
public static float magn = 1/m1;
void setup() {
fullScreen();
background(0);
sx = width/2;
sy = height/2;
ellipseMode(RADIUS);
stroke(stroke);
strokeWeight(3);
centerX = width/2;
centerY = height/2;
}
void draw() {
magn = 1/m1;
background(0);
translate(sx, sy);
sun.drawOrbit();
sun.drawPlanet();
mercury.drawOrbit();
mercury.drawPlanet();
venus.drawOrbit();
venus.drawPlanet();
if(mousePressed) {
float wx = mouseX - pmouseX;
float wy = mouseY - pmouseY;
sx += wx;
sy += wy;
}
}
void mouseWheel(MouseEvent event) {
float e = float(event.getCount())*m1/3;
if(m1+e > 0.2) {
m1 += e;
}
}
第二个class
class Planeta {
color c;
float distance;
float radius;
float angle;
float orbit_time;
Planeta(color kolor, float dystans, float promien, float kat, float czas) {
this.c = kolor;
this.distance = dystans/100;
this.radius = promien/100;
this.angle = kat;
this.orbit_time = czas;
}
public PVector getPos() {
float x = this.distance*sin(this.angle);
float y = this.distance*cos(this.angle);
return new PVector(x, y);
}
public void drawOrbit() {
noFill();
circle(0, 0, this.distance*magn);
}
public void drawPlanet() {
fill(this.c);
PVector pos = getPos();
circle(pos.x*magn, pos.y*magn, this.radius*magn);
}
}
sx 和 sy 用于平移,centerX 和 centerY 不变,magn 和 m1 用于缩放。
所以移动行星非常有效,但我不知道如何将 in/out 放大到鼠标所在的位置。我搜索了 google 代码,但对我没有用。
感谢每一个帮助。
我建议使用浮点变量进行翻译,以提高准确性:
float sx, sy;
您必须根据比例因子的变化更改翻译。
计算比例因子的相对变化:
float fromM = m1;
float toM = m1 + e;
float scaleRel = fromM / toM;
计算从鼠标光标到当前翻译的距离:
float dx = mouseX - sx;
float dy = mouseY - sy;
根据鼠标光标到当前翻译的距离 (dx
、dy
) 和相对比例的增量 (1 - scaleRel
) 更改翻译:
sx += dx * (1.0-scaleRel);
sy += dy * (1.0-scaleRel);
例如:
void mouseWheel(MouseEvent event) {
float e = float(event.getCount())*m1/3;
if(m1+e > 0.2) {
float fromM = m1;
float toM = m1 + e;
float scaleRel = fromM / toM;
float dx = mouseX - sx;
float dy = mouseY - sy;
sx += dx * (1.0-scaleRel);
sy += dy * (1.0-scaleRel);
m1 = toM;
}
}
我正在编写太阳系模拟,我需要能够缩放 in/out 我的鼠标指向的位置。到目前为止,我得到的是移动行星并缩放 in/out 点 (0, 0) 所在的位置。
这是我的代码:
主要class
Planeta sun = new Planeta(color(235, 225, 52), 0, 696000, 0, 0);
Planeta mercury = new Planeta(color(166, 142, 88), 57909170, 2439, 0, 87.989);
Planeta venus = new Planeta(color(250, 193, 50), 108208926, 6052, 0, 224.701);
color stroke = color(70, 70, 70, 70);
int sx, sy;
int centerX, centerY;
static float m1 = 1000;
public static float magn = 1/m1;
void setup() {
fullScreen();
background(0);
sx = width/2;
sy = height/2;
ellipseMode(RADIUS);
stroke(stroke);
strokeWeight(3);
centerX = width/2;
centerY = height/2;
}
void draw() {
magn = 1/m1;
background(0);
translate(sx, sy);
sun.drawOrbit();
sun.drawPlanet();
mercury.drawOrbit();
mercury.drawPlanet();
venus.drawOrbit();
venus.drawPlanet();
if(mousePressed) {
float wx = mouseX - pmouseX;
float wy = mouseY - pmouseY;
sx += wx;
sy += wy;
}
}
void mouseWheel(MouseEvent event) {
float e = float(event.getCount())*m1/3;
if(m1+e > 0.2) {
m1 += e;
}
}
第二个class
class Planeta {
color c;
float distance;
float radius;
float angle;
float orbit_time;
Planeta(color kolor, float dystans, float promien, float kat, float czas) {
this.c = kolor;
this.distance = dystans/100;
this.radius = promien/100;
this.angle = kat;
this.orbit_time = czas;
}
public PVector getPos() {
float x = this.distance*sin(this.angle);
float y = this.distance*cos(this.angle);
return new PVector(x, y);
}
public void drawOrbit() {
noFill();
circle(0, 0, this.distance*magn);
}
public void drawPlanet() {
fill(this.c);
PVector pos = getPos();
circle(pos.x*magn, pos.y*magn, this.radius*magn);
}
}
sx 和 sy 用于平移,centerX 和 centerY 不变,magn 和 m1 用于缩放。
所以移动行星非常有效,但我不知道如何将 in/out 放大到鼠标所在的位置。我搜索了 google 代码,但对我没有用。
感谢每一个帮助。
我建议使用浮点变量进行翻译,以提高准确性:
float sx, sy;
您必须根据比例因子的变化更改翻译。
计算比例因子的相对变化:
float fromM = m1;
float toM = m1 + e;
float scaleRel = fromM / toM;
计算从鼠标光标到当前翻译的距离:
float dx = mouseX - sx;
float dy = mouseY - sy;
根据鼠标光标到当前翻译的距离 (dx
、dy
) 和相对比例的增量 (1 - scaleRel
) 更改翻译:
sx += dx * (1.0-scaleRel);
sy += dy * (1.0-scaleRel);
例如:
void mouseWheel(MouseEvent event) {
float e = float(event.getCount())*m1/3;
if(m1+e > 0.2) {
float fromM = m1;
float toM = m1 + e;
float scaleRel = fromM / toM;
float dx = mouseX - sx;
float dy = mouseY - sy;
sx += dx * (1.0-scaleRel);
sy += dy * (1.0-scaleRel);
m1 = toM;
}
}