为什么当我使用变量时我的向量的大小更准确?
Why is my vector's magnitude more accurate when I use a variable?
我正在 Processing 中编写一个程序,我将一个向量规范化为一个单位向量。
当我这样做时,我希望我的矢量的大小为 1。但是,我的矢量的大小是这样的(见下文)
幅度接近 1,但 不完全 1. 我发现如果我声明一个等于向量幅度的变量并且将矢量的分量除以矢量大小的变量 而不是 ,如下所示。
知道为什么当我使用变量时我的矢量幅度更准确吗?
mag
等于 mag()
,mag
是一个浮点数,而 mag()
returns 是一个浮点数,所以我不明白为什么我的幅度会有所不同。
我的全部代码如下。
PVector center, mouse;
void setup() {
size(1000,300);
background(0);
stroke(255);
center = new PVector(width/2,height/2);
}
void draw() {
background(0);
mouse = new PVector(mouseX, mouseY);
mouse.sub(center); //Mouse is now the vector between mouse and center.
mouse.normalize();
println("magnitude: " + mouse.mag() +
", Vector coordinates: (" + mouse.xPos + "," + mouse.yPos + ")" );
/* These values are getting normalized already, so the magnitude of the
vectors should be equal to 1.
*/
translate(center.xPos, center.yPos);
line(0,0,mouse.xPos,mouse.yPos);
}
class PVector {
float xPos; // xPos and yPos are the vector's components.
float yPos;
PVector (float xTemp, float yTemp) {
xPos = xTemp;
yPos = yTemp;
}
void sub(PVector vectorTemp) {
xPos = xPos - vectorTemp.xPos;
yPos = yPos - vectorTemp.yPos;
}
float mag() {
float magnitude = sqrt(xPos * xPos + yPos * yPos);
return magnitude;
}
void normalize() {
float mag = mag(); // My vector's magnitude is 1 when I use a variable.
xPos = xPos/mag;
yPos = yPos/mag;
}
}
第一个(不精确的)版本有一个缺陷:
它更新 xPos
和 之后 使用更新后的 xPos
计算 mag()
。这意味着 yPos
是相对于一个完全不同的顶点进行缩放的。
例如向量 (3, 4)
的该方法的示例流程如下所示:
xPos = 3
yPos = 4
mag() = sqrt(3^2 + 4^2) = 5
xPos = 3 / 5
mag() = sqrt(3^2/5^2 + 4^2) ~= 4.045
yPos = 4 / ~4.045
导致上例中的总震级约为 1,2605。
另一方面,另一个版本正确地首先计算星等,然后更新 position-values。
我正在 Processing 中编写一个程序,我将一个向量规范化为一个单位向量。 当我这样做时,我希望我的矢量的大小为 1。但是,我的矢量的大小是这样的(见下文)
幅度接近 1,但 不完全 1. 我发现如果我声明一个等于向量幅度的变量并且将矢量的分量除以矢量大小的变量 而不是 ,如下所示。
知道为什么当我使用变量时我的矢量幅度更准确吗?
mag
等于 mag()
,mag
是一个浮点数,而 mag()
returns 是一个浮点数,所以我不明白为什么我的幅度会有所不同。
我的全部代码如下。
PVector center, mouse;
void setup() {
size(1000,300);
background(0);
stroke(255);
center = new PVector(width/2,height/2);
}
void draw() {
background(0);
mouse = new PVector(mouseX, mouseY);
mouse.sub(center); //Mouse is now the vector between mouse and center.
mouse.normalize();
println("magnitude: " + mouse.mag() +
", Vector coordinates: (" + mouse.xPos + "," + mouse.yPos + ")" );
/* These values are getting normalized already, so the magnitude of the
vectors should be equal to 1.
*/
translate(center.xPos, center.yPos);
line(0,0,mouse.xPos,mouse.yPos);
}
class PVector {
float xPos; // xPos and yPos are the vector's components.
float yPos;
PVector (float xTemp, float yTemp) {
xPos = xTemp;
yPos = yTemp;
}
void sub(PVector vectorTemp) {
xPos = xPos - vectorTemp.xPos;
yPos = yPos - vectorTemp.yPos;
}
float mag() {
float magnitude = sqrt(xPos * xPos + yPos * yPos);
return magnitude;
}
void normalize() {
float mag = mag(); // My vector's magnitude is 1 when I use a variable.
xPos = xPos/mag;
yPos = yPos/mag;
}
}
第一个(不精确的)版本有一个缺陷:
它更新 xPos
和 之后 使用更新后的 xPos
计算 mag()
。这意味着 yPos
是相对于一个完全不同的顶点进行缩放的。
例如向量 (3, 4)
的该方法的示例流程如下所示:
xPos = 3
yPos = 4
mag() = sqrt(3^2 + 4^2) = 5
xPos = 3 / 5
mag() = sqrt(3^2/5^2 + 4^2) ~= 4.045
yPos = 4 / ~4.045
导致上例中的总震级约为 1,2605。
另一方面,另一个版本正确地首先计算星等,然后更新 position-values。