Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Help needed, please . javascript math calculation

    Hi im construction engineer and trying to build web app for my use. program to calculate distance and bearing between two points. it is thing i use quite frequently. so i ve done distance bit, but cannot work out bearing part of it.
    by the way i have program written in basic for casio calculator. so im trying kinda convert it into js and html. Please i need help. would be very thankful.


    function writeText (form) {

    var E1=form.inputbox1.value; ----this is where i get coordinates for two
    var N1=form.inputbox2.value; points
    var E2=form.inputbox3.value;
    var N2=form.inputbox4.value;
    var E;
    var N;

    E=(E2-E1)*(E2-E1);
    N=(N2-N1)*(N2-N1);

    form.inputbox5.value = Math.sqrt(E+N); ///thats the distance calculation.

    }




    var B;

    if ((N2-N1)<0) {

    B = Math.atan((E2-E1)/(N2-N1)+180);
    }
    else if ((N2-N1)>0) {
    B = Math.atan((E2-E1)/(N2-N1));
    }
    else if (E>0) {
    B=90;
    }
    else {
    B=270;
    return B;
    }

    if (form.inputbox6.value<0){
    form.inputbox6.value+360=Bearing}
    }

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    You must not change the positions of the two operands of the = assignment operator. Left hand side must be a simple variable name whereas the right hand side can be any expression
    Code:
    Bearing = form.inputbox6.value+360

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    hi thanx for ur reply, appreciate that.

    if u had couple minutes would look thru this for me, i get error, i could do with few tips, to direct me the right way.

    thats my html
    <form name="myform">Eastings 1:<input type="text" name="inputbox1" value="">
    <br>Northings 1:<input type="text" name="inputbox2" value="">
    <br>
    <br>Eastings 2:<input type="text" name="inputbox3" value="">
    <br>Northings 2:<input type="text" name="inputbox4" value="">
    <br>
    <br>Distance:<input type="text" name="inputbox5" value="">
    <br>
    <br>Bearing:<input type="text" name="inputbox6" value="">
    <br>
    <input type="button" name="button" value="Calculate" onclick="writeText(this.form)">
    </form>

    and js


    function writeText (form) {

    var E1=form.inputbox1.value;
    var N1=form.inputbox2.value;
    var E2=form.inputbox3.value;
    var N2=form.inputbox4.value;
    var E;
    var N;

    E=(E2-E1)*(E2-E1);
    N=(N2-N1)*(N2-N1);

    form.inputbox5.value = Math.sqrt(E+N);
    }


    var B;

    if ((N2-N1)<0) {

    B = Math.atan((E2-E1)/(N2-N1)+180);
    }
    else if ((N2-N1)>0) {
    B = Math.atan((E2-E1)/(N2-N1));
    }
    else if (E>0) {
    B=90;
    }
    else {
    B=270;

    }

    if (form.inputbox6.value<0){
    Bearing=form.inputbox6.value+360}
    }


    thanx again appreciate u spendin time on it

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Your error might have something to do with your "weird" bracketing. Your function writeText() ends in line 14, the rest is outside of the function in the global scope and will be executed BEFORE the function. All the variables local to the function (like E1, N1, E2, N2, E and N) will not be available there.

    I see no place where you actually call the function.

    The } curly bracket in the end doesn't have a matching opening { curly bracket.

    Btw, please use the [ code ] and [ /code ] (without spaces) to post code here. You can easily achieve that by clicking on the # octathorpe symbol (not the #i symbol) above the text box.

  • Users who have thanked devnull69 for this post:

    pixomanic (02-28-2012)

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanx

    thanx again, after ur fair comment about my 'weird' syntax i decided to go back to the book, anyway i managed to get it working but it returns in radians, cant get conversion formula to work /radians*(180/PI) /


    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    function writeText (form) {
    
        var E1=form.inputbox1.value;
        var N1=form.inputbox2.value;
        var E2=form.inputbox3.value;
        var N2=form.inputbox4.value;
        var E;
        var N;
        
        E=(E2-E1)*(E2-E1);  
        N=(N2-N1)*(N2-N1);
        
        form.inputbox5.value = Math.sqrt(E+N);
        
        var e;
        var n;
        
        e=E2-E1;
        n=N2-N1;
          
        var B;
        var b;
       
        b=e/n;
        
        
    if (n<0) { B = Math.atan(b)+180; }
        else if (n>0) { B = Math.atan(b);  }
        else if (e>0) { B=90; }
            else {B=270; }  
        if (B<0){B=B+360; }
        
     return B;
       // form.inputbox6.value = B;
    
    }
     var A
    A=B*(180/PI)
     form.inputbox6.value = A;
     
    
    
    
    
    </script>
     <form name="myform">Eastings 1:<input type="text" name="inputbox1" value="">
                <br>Northings 1:<input type="text" name="inputbox2" value="">
                <br>
                <br>Eastings 2:<input type="text" name="inputbox3" value="">
                <br>Northings 2:<input type="text" name="inputbox4" value="">
                <br>
                <br>Distance:<input type="text" name="inputbox5" value="">
                <br>
                <br>Bearing:<input type="text" name="inputbox6" value="">
                <br>
                <input type="button" name="button" value="Calculate" onclick="writeText(this.form)">
            </form>
    </body>
    </html>

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    This is because PI is not defined. Use Math.PI instead
    Code:
    A=B*(180/Math.PI)

  • Users who have thanked devnull69 for this post:

    pixomanic (02-28-2012)

  • #7
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Thanx

    Thank you very much for all ur help, excellent stuff.
    much appreciated.

    Andy


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •