I used HTML and PH PHP to calculate the normal unit
<html>
<head>
<title> Comprehensive FORM CGI program </title>
</head>
<body bgcolor=#DDDDDD>
<p>
<!-- The lines above are the standard html header -->
<?php
// First get the input variables and their values
$A = $_POST['f_A'];
$B = $_POST['f_B'];
$Acomponent = explode(",", $A);
$Bcomponent = explode(",", $B);
$AX = (int)$Acomponent[0];
$AY = (int)$Acomponent[1];
$AZ = (int)$Acomponent[2];
$BX = (int)$Bcomponent[0];
$BY = (int)$Bcomponent[1];
$BZ = (int)$Bcomponent[2];
function dot_product($AX, $AY, $AZ, $BX, $BY, $BZ){
$dotproduct = ($AX*$BX $AY*$BY $AZ*$BZ);
return($dotproduct);
}
function cross_product($AX, $AY, $AZ, $BX, $BY, $BZ){
$i = ($AY*$BZ-$AZ*$BY);
$j = (-1)*($AX*$BZ-$AZ*$BX);
$k = ($AX*$BY-$AY*$BX);
return($i);
return($j);
return($k);
}
function norm($AX, $AY, $AZ, $BX, $BY, $BZ) {
$Anorm=sqrt(pow($AX,2) pow($AY,2) pow($AZ,2));
$Bnorm=sqrt(pow($BX,2) pow($BY,2) pow($BZ,2));
return($Anorm);
return($Bnorm);
}
function anglebetween($dotproduct,$Anorm, $Bnorm) {
$angle = acos($dotproduct/($Anorm*$Bnorm));
return($angle);
}
function normalunitvector($i,$j,$k) {
$crossnorm=sqrt(pow($i,2) pow($j,2) pow($k,2));
$normaluniti = $i/$crossnorm;
$normalunitj = $j/$crossnorm;
$normalunitk = $k/$crossnorm;
$normalunit = [];
$normalunit[0] = $normaluniti;
$normalunit[1] = $normalunitj;
$normalunit[2] = $normalunitk;
return($normalunit);
}
dot_product($AX, $AY, $AZ, $BX, $BY, $BZ);
cross_product($AX, $AY, $AZ, $BX, $BY, $BZ);
norm($AX, $AY, $AZ, $BX, $BY, $BZ);
anglebetween($dotproduct,$Anorm, $Bnorm);
normalunitvector($i,$j,$k);
echo($angle);
echo($normaluniti);
echo($normalunitj);
echo($normalunitk);
?>
<p>
<hr>
<p>
</body>
</html>
AX, AY, AZ, BX, BY, BZ values were printed very well, but the value of angle, normaluniti, normalunij, normalunitk were not printed.
I don't know why, but I guess there is an error in my function?
echo($angle);
echo($normaluniti);
echo($normalunitj);
echo($normalunitk);
did not show anything. How can fix this issue?
CodePudding user response:
You're using them as local variables inside your functions. Their life begins and ends between the brackets of a function.
Try echo anglebetween($dotproduct,$Anorm, $Bnorm);
tell me if that works.
If so, you can either store anglebetween($dotproduct,$Anorm, $Bnorm); in a variable or just call the functions.
CodePudding user response:
- After execution of the functions , please assign the returned value(s) to local variable(s)
- For return of multiple values from a single function, please use an array
Hence, please find below a working example :
(For testing, I have added two lines to assign the values of $A and $B, please remove them if you want to apply the values of $_POST)
<html>
<head>
<title> Comprehensive FORM CGI program </title>
</head>
<body bgcolor=#DDDDDD>
<p>
<!-- The lines above are the standard html header -->
<?php
// First get the input variables and their values
//$A = $_POST['f_A'];
//$B = $_POST['f_B'];
$A = "10,20,30";
$B = "25,100,1000";
$Acomponent = explode(",", $A);
$Bcomponent = explode(",", $B);
$AX = (int)$Acomponent[0];
$AY = (int)$Acomponent[1];
$AZ = (int)$Acomponent[2];
$BX = (int)$Bcomponent[0];
$BY = (int)$Bcomponent[1];
$BZ = (int)$Bcomponent[2];
function dot_product($AX, $AY, $AZ, $BX, $BY, $BZ){
//$dotproduct = ($AX*$BX $AY*$BY $AZ*$BZ);
//return($dotproduct);
return ($AX*$BX $AY*$BY $AZ*$BZ);
}
function cross_product($AX, $AY, $AZ, $BX, $BY, $BZ){
$i = ($AY*$BZ-$AZ*$BY);
$j = (-1)*($AX*$BZ-$AZ*$BX);
$k = ($AX*$BY-$AY*$BX);
// return($i);
// return($j);
// return($k);
return array($i, $j, $k);
}
function norm($AX, $AY, $AZ, $BX, $BY, $BZ) {
$Anorm=sqrt(pow($AX,2) pow($AY,2) pow($AZ,2));
$Bnorm=sqrt(pow($BX,2) pow($BY,2) pow($BZ,2));
// return($Anorm);
// return($Bnorm);
return array($Anorm,$Bnorm);
}
function anglebetween($dotproduct,$Anorm, $Bnorm) {
// $angle = acos($dotproduct/($Anorm*$Bnorm));
// return($angle);
return acos($dotproduct/($Anorm*$Bnorm));
}
function normalunitvector($i,$j,$k) {
$crossnorm=sqrt(pow($i,2) pow($j,2) pow($k,2));
$normaluniti = $i/$crossnorm;
$normalunitj = $j/$crossnorm;
$normalunitk = $k/$crossnorm;
// $normalunit = [];
// $normalunit[0] = $normaluniti;
// $normalunit[1] = $normalunitj;
// $normalunit[2] = $normalunitk;
// return($normalunit);
return array($normaluniti, $normalunitj, $normalunitk);
}
$dotproduct=dot_product($AX, $AY, $AZ, $BX, $BY, $BZ);
$array1 = cross_product($AX, $AY, $AZ, $BX, $BY, $BZ);
$i=$array1[0];
$j=$array1[1];
$k=$array1[2];
$array2=norm($AX, $AY, $AZ, $BX, $BY, $BZ);
$Anorm=$array2[0];
$Bnorm=$array2[1];
$angle=anglebetween($dotproduct,$Anorm, $Bnorm);
$array3 =normalunitvector($i,$j,$k);
$normaluniti=$array3[0];
$normalunitj=$array3[1];
$normalunitk=$array3[2];
echo " Angle is :". ($angle);
echo "<br>";
echo " Normaluniti is :".($normaluniti);
echo "<br>";
echo " Normalunitj is :".($normalunitj);
echo "<br>";
echo " Normalunitk is :".($normalunitk);
echo "<br>";
?>
<p>
<hr>
<p>
</body>
</html>