Quantcast
Channel: Unfocused Brain
Viewing all articles
Browse latest Browse all 10

Polar to Cartesian and Back Again Snippets

$
0
0

PHP:

	$p2c=polartoCartesian(36.8699,5);
	echo "p2c: angle=36.8699 distance=5 [$p2c[0]] [$p2c[1]]\n";

	$c2p=cartesiantoPolar(4,3);
	echo "c2p: x=4 y=3 [$c2p[0]] [$c2p[1]]\n";

function degreesToRadians($degrees)
{
	$radians=(pi()/180)*$degrees;
	return $radians;
}
function radiansToDegrees($radians)
{
	$degrees=$radians*(180/pi());
	return $degrees;
}
function polartoCartesian($deg,$radius)
{

	$radians=degreesToRadians($deg);

	$X= cos($radians) * $radius;
	$Y= sin($radians) * $radius;

	$retVal[0]=$X;
	$retVal[1]=$Y;


	return $retVal;
}
function cartesiantoPolar($x,$y)
{

	$C=sqrt( ($x*$x) + ($y*$y) );
	$angle=atan($x/$y);

	$angle=radiansToDegrees($angle);
	$angle=90-$angle; // to set 0deg at north;

	$retV[0]=$C;
	$retV[1]=$angle;

	return $retV;

}


function map($value, $fromLow, $fromHigh, $toLow, $toHigh) 
{
    $fromRange = $fromHigh - $fromLow;
    $toRange = $toHigh - $toLow;
    $scaleFactor = $toRange / $fromRange;
    $tmpValue = $value - $fromLow;
    $tmpValue *= $scaleFactor;
    return $tmpValue + $toLow;
}


Viewing all articles
Browse latest Browse all 10

Trending Articles