strange rotation values - actionscript-3

I got an object (called tempEnemy) which is flying around and shooting.
The problem is that I can't keep the value tempEnemy.rotateTo positive, i.e.
it shall be between 0 and 359 degrees. Currently rotateTo ranges from:
rotateTo < 0 (bug) && rotateTo > 0 && rotateTo > 359 (bug).
tempEnemy.dX = tempEnemy.destX - tempEnemy.x;
tempEnemy.dY = tempEnemy.destY - tempEnemy.y;
//I added 180 because my tempEnemy object was looking and shooting to the wrong direction
tempEnemy.rotateTo = (toDegrees(getRadians(tempEnemy.dX, tempEnemy.dY))) + 180;
if (tempEnemy.rotateTo > tempEnemy.frame + 180) tempEnemy.rotateTo -= 360;
if (tempEnemy.rotateTo < tempEnemy.frame - 180) tempEnemy.rotateTo += 360;
tempEnemy.incFrame = int((tempEnemy.rotateTo - tempEnemy.frame) / tempEnemy.rotateSpeed);

You can always use the modulo operator (%) to keep a value positive. The module calculates the rest of a division.
E.g. (example works with integers there for a division always has a left over.)
19 % 5 = 4
Because in the number 19 5 only fits 3 times (3 * 5 = 15,, 4 * 5 = 20,, 20 is too high) the left over is 4 (19 - 15). That is the modulo.
Extra examples:
7 % 3 = 1
15 % 4 = 3
21 % 9 = 3
The output of a modulo operation is never higher then the right hand operator - 1 There for it is perfect for your problem.
If your object is rotated 1234 degrees,, then operate it with a modulo 360 to get the respective number between 0 and 360 for it.
1234 % 360 = 154
Other more easier examples:
720 % 360 = 0
360 % 360 = 0
540 % 360 = 180
-180 % 360 = 180
-720 % 360 = 0
-540 % 360 = 180

Sounds like a classic angle averaging issue. Here's a formula that works for averaging angles
private function averageNums($a:Number, $b:Number):Number {
return = (Math.atan2( Math.sin($a) + Math.sin($b) , Math.cos($a) + Math.cos($b) ));
}

Related

How do I calculate the momentum matrix element using the Fourier transform?

I am trying to calculate the momentum matrix element between the ground and first excited vibrational states for a harmonic oscillator using the Fourier transform of the eigenfunctions in position space. I am having trouble calculating the momentum matrix element correctly.
Here is my code
hbar = 1
nPoints = 501
xmin = -5
xmax = 5
dx = (xmax - xmin) / nPoints
x = np.array([(i - nPoints // 2) * dx for i in range(nPoints)])
potential = np.array([0.5 * point**2 for point in x]) # harmonic potential
# get eigenstates, eigenvalues with Fourier grid Hamiltonian approach
energies, psis = getEigenstatesFGH(x, potential, mass=1, hbar=hbar)
# should be 0.5, 1.5, 2.5, 3.5, 4.5 or very close to that
for i in range(5):
print(f"{energies[i]:.2f}")
# identify the necessary wave functions
groundState = psis[:, 0]
firstExcitedState = psis[:, 1]
# fourier transform the wave functions into k-space (momentum space)
dp = (2 * np.pi) / (np.max(x) - np.min(x))
p = np.array([(i - nPoints // 2) * dp for i in range(nPoints)])
groundStateK = (1 / np.sqrt(2 * np.pi * hbar)) * np.fft.fftshift(np.fft.fft(groundState))
firstExcitedStateK = (1 / np.sqrt(2 * np.pi * hbar)) * np.fft.fftshift(np.fft.fft(firstExcitedState))
# calculate matrix elements
xMatrix = np.eye(nPoints) * x
pMatrix = np.eye(nPoints) * p
# <psi0 | x-hat | psi1 >, this works correctly
x01 = np.dot(np.conjugate(groundState), np.dot(xMatrix, firstExcitedState))
# <~psi0 | p-hat | ~psi1 >, this gives wrong answer
p01 = np.dot(np.conjugate(groundStateK), np.dot(pMatrix, firstExcitedStateK))

Big-O notation when the function has negative value

i need to proof that 2n^2 - 2n -7 = O(n^2), when the n is 1 or two i got negative value of f(n). i am not sure does the way i proof Big-O is correct.Your help and advice is highly appreciated.
f(n) = 2n^2 - 2n -7 = O(n^2) if c=2;
n=1->2(1)^2 - 2(1) -7 = -7 <= 2*(1)^2
n=2->2(2)^2 - 2(2) -7 = -3 <= 2*(2)^2
n=3->2(3)^2 - 2(3) -7 = 14 <= 2*(3)^2
The definition of Big-Oh has more to do with asymptotic behavior than local behavior. If your function went negative for increasing values of n, say it oscillated, there might be more of a concern. For this function, though, there is no problem: you are free to consider the function for all values greater than some n0 which you alone are allowed to choose. So, if the function going negative early on bothers you, write your proof such that those numbers aren't used. For example:
Base case: for n = 3, f(n) = 2*3*3 - 2*3 - 7 = 18 - 6 - 7 = 5 <= 9 * c = c * 3 * 3 = c * n^2. This is true provided that c >= 5/9.
Induction hypothesis: assume f(n) <= c * n^2 for all n starting at 3 up through k.
Induction step: we must show that f(k+1) <= c * (k+1)^2. We have f(k+1) = 2(k+1)^2 - 2(k+1) - 7 = 2k^2+4k+2 - 2k - 2 - 7 = 2k^2 + 2k - 7 < 2k^2 + 4k < 2k^2 + 4k + 2 = 2(k^2 + 2k + 1) = 2(k+1)^2, so the choice c = 2 works here.
In hindsight, it should be obvious that 2n^2 - 2n - 7 is always less than 2n^2 for positive increasing n.

-= Operator, confused with output result

I was recently tracing a line of code:
x -= 353 - 350
However, the answer came up to be -3.
To my surprise, I figure the -= operator would follow as:
x = x - 353 - 350
which then would equal to -703
Why is the actual answer -3 and not -703?
I was searching for reference on this website:
http://www.adobe.com/devnet/actionscript/learning/as3-fundamentals/operators.html
The example it gave led me to believe that the operator -= should produce -703.
var x:uint = 5; x -= 5; // x is now 0
Wouldn't the above example represent how x = x - 5 is 0? Or is there an alternative code/logic that I'm missing?
Operator precendece:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
-= has precedence of 3, while + and - have precedence 13 (higher),
so the code executes as the equivalent of
x -= (353 - 350)
x -= (3)
x -= 3;
x = x - 3;
And as per basic mathematics:
x -= (353 - 350)
x += -(353 - 350)
x += (-353 + 350)
x += (-3);
I've tested it in Python. It appears to be just basic arithmetics..
Assuming x is currently zero
x -= 353 - 350 which is equal to saying x = x - (353 - 350).
Get the result of brackets first then equation is simply x = 0 - 3 which gives result : -3
Daniel and Marc are right.
You use the wrong operator :
To get -703 you should use -= like this
var x:int = 0;
x -= 353 + 350;
// x -= (353 + 350);
// So => x -= 703
trace(x); //output -703
Because
x -= 353 - 350;
Means x -= 3;
Same logic.

calculate google maps tile boundary coordinates

I've made a lot of searches and found some useful resources but still having problems about calculation.
I want to calculate NE and SW coordinates for a specific tile.
This is my way :
Zoom = 11
x = 1188
y = 767
number of tile = 2 ^ 11 (equals 2048)
angle1 = 360 / 2048
longitude = (1188 * angle1) - 180
it works correct.
But latitude part is not :
angle2 = 170.1022575596 / (2048/2)
latitude = ((2048 / 2) - 767) * angle2
thanks in advance
The solution you are looking for is this:
z = 11
x = 1188
y = 767
pi = 3.14159
alon1 = (x /2^z)*360.0 - 180.0
alon2 = ((x+1) /2^z)*360.0 - 180.0
an = pi-2*pi*y/2^z
alat1 = 180.0/pi*atan(0.5*(exp(an)-exp(-an)))
an = pi-2*pi*(y+1)/2^z
alat2 = 180.0/pi*atan(0.5*(exp(an)-exp(-an)))
And you get the NW (alon1,alat1) & SE (alon2,alat2) coordinates for this specific tile.

Angles to decimal conversion

I'm trying to understand some flash animation, and am having difficulty working out the following. Can anyone help?
I want to convert a degree range of 0 to 90, to a value between 0 and 1
These is an existing function to convert from the range 0 to 1 to degrees, eg:
function convertToDegrees(Int:Pos)
{
var rot = (45 * pos);
var degrees = (90 - ( rot * 2 ));
return degrees;
}
Now to convert backwards, from degrees to 0 to 1 value, I am trying:
(WHICH IS WRONG)
function convertFromDegrees(Int:currentDegreeValue )
{
var rot = (currentDegreeValue / 2) + 90;
var Pos = rot / 45;
return Pos;
}
Can anyone help me as to where I am going wrong?
The first function could be simplified to 90 * (1 - pos), so the reverse function would be 1 - (degrees / 90).
I want to convert a degree range of 0 to 90, to a value between 0 and 1
How about:
x / 90.0
rot = (90 - degrees) / 2