Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 7 of 7

02262012, 09:31 PM #1
 Join Date
 Feb 2012
 Posts
 4
 Thanks
 2
 Thanked 0 Times in 0 Posts
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=(E2E1)*(E2E1);
N=(N2N1)*(N2N1);
form.inputbox5.value = Math.sqrt(E+N); ///thats the distance calculation.
}
var B;
if ((N2N1)<0) {
B = Math.atan((E2E1)/(N2N1)+180);
}
else if ((N2N1)>0) {
B = Math.atan((E2E1)/(N2N1));
}
else if (E>0) {
B=90;
}
else {
B=270;
return B;
}
if (form.inputbox6.value<0){
form.inputbox6.value+360=Bearing}
}

02262012, 09:59 PM #2
 Join Date
 Dec 2010
 Posts
 2,398
 Thanks
 12
 Thanked 570 Times in 563 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

02262012, 11:50 PM #3
 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=(E2E1)*(E2E1);
N=(N2N1)*(N2N1);
form.inputbox5.value = Math.sqrt(E+N);
}
var B;
if ((N2N1)<0) {
B = Math.atan((E2E1)/(N2N1)+180);
}
else if ((N2N1)>0) {
B = Math.atan((E2E1)/(N2N1));
}
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

02272012, 07:06 AM #4
 Join Date
 Dec 2010
 Posts
 2,398
 Thanks
 12
 Thanked 570 Times in 563 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 (02282012)

02282012, 12:03 AM #5
 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=(E2E1)*(E2E1); N=(N2N1)*(N2N1); form.inputbox5.value = Math.sqrt(E+N); var e; var n; e=E2E1; n=N2N1; 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>

02282012, 07:42 AM #6
 Join Date
 Dec 2010
 Posts
 2,398
 Thanks
 12
 Thanked 570 Times in 563 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 (02282012)

02282012, 09:58 PM #7
 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