<< Chapter < Page Chapter >> Page >

Screen output

The text shown in Figure 5 should appear in your browser window when you open the html file in your browser.

Figure 5 . Screen output for Listing #3.
Start Script Velocity magnitude = 3.00 miles/hourVelocity angle = 0.00 degrees End Script

Analysis of the code

As before, the script begins by defining the getAngle function, which doesn't require further explanation.

The vectorSum function

As you can see from Listing 3 , this script contains a new function named vectorSum . The purpose of this function is to add two vectors, given the magnitude and angle (in degrees) for each vector asincoming parameters. (The function assumes that both incoming magnitude parameters are expressed in the same units.)

The need to add two vectors occurs so often in physics that I decided to encapsulate the capability into a function that we can copy into future scripts.That will relieve us of the need to rewrite the code each time we need that functionality.

Return an array object

The magnitude and the angle (in degrees) of the resultant vector is returned in a two-element array object, with the magnitude in the element at index 0 and the angle in the element at index 1.I will have a more to say about arrays shortly.

Note that this function calls the getAngle function, which must also be provided in the script.

Subtraction of vectors

The function can also be used to subtract one vector from another. To subtract one vector from another, simply add 180 degrees to the angle for the subtrahend vector before passing the angle to the function.

Adding more than two vectors

Although the function can only be used to add two vectors, it can be called repeatedly to add any number of vectors two at a time.

To use this function to add more than two vectors, add the first two vectors as normal. Then add the vector output from the function to the third vector, etc., until all of the vectors have been included in the sum.

The code in the vectorSum function

Most of the code in the function is the same as code that I have explained in earlier modules. However, there are a couple of lines of code that aredifferent.

The function begins with the following statement:

var vecResult = new Array(2);

The purpose of this statement is to create a two-element array object named vecResult .

What is an array object?

You can think of an array object as a software object containing pigeon holes into which you can store data and from which you can later retrieve that data. This particular arrayobject has two pigeon holes, and they are numbered 0 and 1.

This array object will be used as a container to return the magnitude and angle values for the resultant vector.

Can only return one item

A JavaScript function can only return one item. Up to this point in these modules, that item has consisted of a single numeric value, such as the anglevalue that is returned from the getAngle function.

However, the vectorSum function needs to return two different values. One way to do that is to put those two values in the pigeon holes of an array object andreturn that array object as the one item that can be returned.

Questions & Answers

how do I set up the problem?
Harshika Reply
what is a solution set?
Harshika
find the subring of gaussian integers?
Rofiqul
hello, I am happy to help!
Shirley Reply
please can go further on polynomials quadratic
Abdullahi
I need quadratic equation link to Alpa Beta
Abdullahi Reply
find the value of 2x=32
Felix Reply
divide by 2 on each side of the equal sign to solve for x
corri
X=16
Michael
Want to review on complex number 1.What are complex number 2.How to solve complex number problems.
Beyan
use the y -intercept and slope to sketch the graph of the equation y=6x
Only Reply
how do we prove the quadratic formular
Seidu Reply
hello, if you have a question about Algebra 2. I may be able to help. I am an Algebra 2 Teacher
Shirley Reply
thank you help me with how to prove the quadratic equation
Seidu
may God blessed u for that. Please I want u to help me in sets.
Opoku
what is math number
Tric Reply
4
Trista
x-2y+3z=-3 2x-y+z=7 -x+3y-z=6
Sidiki Reply
Need help solving this problem (2/7)^-2
Simone Reply
x+2y-z=7
Sidiki
what is the coefficient of -4×
Mehri Reply
-1
Shedrak
the operation * is x * y =x + y/ 1+(x × y) show if the operation is commutative if x × y is not equal to -1
Alfred Reply
An investment account was opened with an initial deposit of $9,600 and earns 7.4% interest, compounded continuously. How much will the account be worth after 15 years?
Kala Reply
lim x to infinity e^1-e^-1/log(1+x)
given eccentricity and a point find the equiation
Moses Reply
A soccer field is a rectangle 130 meters wide and 110 meters long. The coach asks players to run from one corner to the other corner diagonally across. What is that distance, to the nearest tenths place.
Kimberly Reply
Jeannette has $5 and $10 bills in her wallet. The number of fives is three more than six times the number of tens. Let t represent the number of tens. Write an expression for the number of fives.
August Reply
What is the expressiin for seven less than four times the number of nickels
Leonardo Reply
How do i figure this problem out.
how do you translate this in Algebraic Expressions
linda Reply
why surface tension is zero at critical temperature
Shanjida
I think if critical temperature denote high temperature then a liquid stats boils that time the water stats to evaporate so some moles of h2o to up and due to high temp the bonding break they have low density so it can be a reason
s.
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask