javascript

This is a discussion on javascript within the Tech Board forums, part of the Community Boards category; I'm learning javascript right now and i'm having a bit of trouble. Code: function components() { var magnitude = document.getElementById("magnitude").innerHTML; ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    javascript

    I'm learning javascript right now and i'm having a bit of trouble.

    Code:
    function components()	{
    			var magnitude = document.getElementById("magnitude").innerHTML;
    			var angle = document.getElementById("angle").innerHTML;
    			
    			var xComponent, yComponent;
    			
    			xComponent = magnitude * Math.cos(angle);
    			yComponent = magnitude * Math.sin(angle);
    						
    			var myString = "The X componenent is: " + xComponent + " The y Component is: " + yComponent;
    			
    			alert(myString);
    		}
    It keeps giving me the wrong vector components. Am I using the cos and sine functions correctly?

    -Redseal

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You may have to convert from degrees to radians (rad = deg*pi/180)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Are you sure its parsing the other numbers as floats? It should do automatically, but you could enforce it by putting 'magnitude' and the components inside the brackets of parseFloat(). Don't know if that would help....
    Last edited by mike_g; 06-15-2007 at 03:05 PM.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2
    Weird, it works when I convert to radians, any idea why you have to? I never would have thought about the conversion without your help, i was beginning to bang my head against the wall. Thanks!

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In most languages, in particular in those derived from C, the math library trigonometry functions deal in radians. That's pretty much just the way it is.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Redseal View Post
    Weird, it works when I convert to radians, any idea why you have to? I never would have thought about the conversion without your help, i was beginning to bang my head against the wall. Thanks!
    Degrees are a very unnatural unit for measuring angles, whereas radians are the "natural" unit of angle (if you encountered an alien, chances are he would understand what a radian is). So in mathematics, we pretty much always use radians.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I've always found the use/requirement of radians confusing when it comes to angles. A simple macro or function would be all you need to make it easier:

    Code:
    #define degrees 0.017453292519943296
    double ConvertDegrees(number)
    {
    	return(number*0.017453292519943296);
    }
    
    ...
    
    value = cos(60*degrees); // both should give exactly 0.5
    value = cos(ConvertDegrees(60));
    This is how it'd be set up and used in C. I don't know about javascript though. The first method is the easiest to use and understand.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  8. #8
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    That doesn't keep radians from being the natural way to measure angles. Sure, you learn how to do it in degrees, first, but once you know radians it's a very backwards way to do things.

    For instance, one radian is exactly the angle for there to be exactly an arc length of 1 in the unit circle. As such, it gives you a direct mapping from angles onto arc lengths. There is no such property with degrees.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    A macro is more efficient ulillillia, although it's not entirely type-safe
    Code:
    #define DEGTORAD(x) (x * 0.017453292519943296)
    value = cos(DEGTORAD(60));


    Let's go half way and use gradians

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Professional JavaScript for Web Developers, 2nd ed.
    By neandrake in forum Programming Book and Product Reviews
    Replies: 2
    Last Post: 05-16-2009, 09:44 AM
  2. JavaScript book recommendations
    By neandrake in forum Tech Board
    Replies: 2
    Last Post: 04-05-2009, 12:27 PM
  3. Replies: 2
    Last Post: 03-10-2009, 08:36 PM
  4. Javascript: Error in FTP access
    By alphaoide in forum Tech Board
    Replies: 2
    Last Post: 05-10-2005, 07:13 AM
  5. JavaScript grafic
    By Zahl in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 07:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21