Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-26-2012, 09:31 PM   PM User | #1
pixomanic
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
pixomanic is an unknown quantity at this point
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}
}
pixomanic is offline   Reply With Quote
Old 02-26-2012, 09:59 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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
devnull69 is offline   Reply With Quote
Old 02-26-2012, 11:50 PM   PM User | #3
pixomanic
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
pixomanic is an unknown quantity at this point
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
pixomanic is offline   Reply With Quote
Old 02-27-2012, 07:06 AM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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.
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
pixomanic (02-28-2012)
Old 02-28-2012, 12:03 AM   PM User | #5
pixomanic
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
pixomanic is an unknown quantity at this point
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>
pixomanic is offline   Reply With Quote
Old 02-28-2012, 07:42 AM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This is because PI is not defined. Use Math.PI instead
Code:
A=B*(180/Math.PI)
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
pixomanic (02-28-2012)
Old 02-28-2012, 09:58 PM   PM User | #7
pixomanic
New to the CF scene

 
Join Date: Feb 2012
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
pixomanic is an unknown quantity at this point
Thanx

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

Andy
pixomanic is offline   Reply With Quote
Reply

Bookmarks

Tags
bearing, distance, javascript, math

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:22 AM.


Advertisement
Log in to turn off these ads.