patternphpMinor
Remapping Heading Angle to Coordinates to Move By
Viewed 0 times
headingcoordinatesmoveangleremapping
Problem
I wrote the following code to take the heading of an object, and determine which direction
```
/*
* map($orgLow, $orgHigh, $newLow, $newHigh, $value)
* Take existing range of values and remap to new range of values
* Requires original high range value, original low range value
* new high range value, new low range value, value to be mapped
* Returns newly mapped integer value
*/
function map($orgLow, $orgHigh, $newLow, $newHigh, $value) {
return $newLow + ($value - $orgLow) * ($newHigh - $newLow) / ($orgHigh - $orgLow);
}
/*
* coordsToMoveFromDeg($deg)
* Take heading and return movement on a (-1 to 1, -1 to 1) plane
* Requires heading to be scaled
* Optional precision to be given
* Returns array consisting of x and y values to change by
*/
function coordsToMoveFromDeg($deg, $precision = false) {
//Rotate plane 90º CCW by remapping the degree value
$xdeg = map(0,360,-90,270,$deg);
if ($xdeg 180) {
//Remap values larger than 180 to their mirrored
//value prior to getting final movement values
$xdeg = map(180,360,180,0,$xdeg);
}
$ydeg = $deg;
if ($ydeg > 180) {
//Remap values larger than 180 to their mirrored
//value prior to getting final movement values
$ydeg = map(180,360,180,0,$deg);
}
//Remap x and y to how much to move by, -1 to 1
$x = map(180,0,-1,1,$xdeg);
$y = map(180,0,-1,1,$ydeg);
//Show equal amount of decimal points always for
//easier readability if precision is set, recommended
//to only be set when in debug environment
if ($precision) {
$x = number_format($x,$precision);
$y = number_format($y,$precision);
}
return array('x'=>$x,'y'=>$y);
}
//Show the mapped values f
(x,y) it needs to move to be heading forward in a 2D plane. I have debugged the code and know that it functions as expected, but as far as readability/maintainability is concerned, I feel like it could be better. Thoughts/opinions?```
/*
* map($orgLow, $orgHigh, $newLow, $newHigh, $value)
* Take existing range of values and remap to new range of values
* Requires original high range value, original low range value
* new high range value, new low range value, value to be mapped
* Returns newly mapped integer value
*/
function map($orgLow, $orgHigh, $newLow, $newHigh, $value) {
return $newLow + ($value - $orgLow) * ($newHigh - $newLow) / ($orgHigh - $orgLow);
}
/*
* coordsToMoveFromDeg($deg)
* Take heading and return movement on a (-1 to 1, -1 to 1) plane
* Requires heading to be scaled
* Optional precision to be given
* Returns array consisting of x and y values to change by
*/
function coordsToMoveFromDeg($deg, $precision = false) {
//Rotate plane 90º CCW by remapping the degree value
$xdeg = map(0,360,-90,270,$deg);
if ($xdeg 180) {
//Remap values larger than 180 to their mirrored
//value prior to getting final movement values
$xdeg = map(180,360,180,0,$xdeg);
}
$ydeg = $deg;
if ($ydeg > 180) {
//Remap values larger than 180 to their mirrored
//value prior to getting final movement values
$ydeg = map(180,360,180,0,$deg);
}
//Remap x and y to how much to move by, -1 to 1
$x = map(180,0,-1,1,$xdeg);
$y = map(180,0,-1,1,$ydeg);
//Show equal amount of decimal points always for
//easier readability if precision is set, recommended
//to only be set when in debug environment
if ($precision) {
$x = number_format($x,$precision);
$y = number_format($y,$precision);
}
return array('x'=>$x,'y'=>$y);
}
//Show the mapped values f
Solution
I'll separate this into two sections: readability and maintainability. I'm not exactly sure of the usage, so I don't feel I can comment on that.
Readability
Personally, I think the largest burden on the cleanliness of this code, is the comments. I think they're too large and there's too many.
One way to improve this, is PHPDocs. Just like JavaDocs, they help organize the functionality of code properties.
Another way to reduce them is to make your variables as meaningful as possible. What exactly is
Breaking apart one large method into several smaller functions can also improve the cleanliness, not to mention the benefits of modularized code and improved organization.
One last note: I see a lot of numbers. What's the decision behind these numbers. Making some of the numbers in
Maintainability
You code is very simple (as in it takes care of one thing), and I think this is the greatest tool to avoiding confusion in the future.
Having well documented code is also great, just make sure your wording is clear and concise. Sometimes referencing to other code in the comments can help. Explain why, not how.
I don't think there's too much else you can do to future-proof this!
Readability
Personally, I think the largest burden on the cleanliness of this code, is the comments. I think they're too large and there's too many.
One way to improve this, is PHPDocs. Just like JavaDocs, they help organize the functionality of code properties.
Another way to reduce them is to make your variables as meaningful as possible. What exactly is
$deg? What kind of degree is it? Same principle for $xdeg and $x.Breaking apart one large method into several smaller functions can also improve the cleanliness, not to mention the benefits of modularized code and improved organization.
One last note: I see a lot of numbers. What's the decision behind these numbers. Making some of the numbers in
map(180,360,180,0 constants or variables at the top can help readers understand the meaning.Maintainability
You code is very simple (as in it takes care of one thing), and I think this is the greatest tool to avoiding confusion in the future.
Having well documented code is also great, just make sure your wording is clear and concise. Sometimes referencing to other code in the comments can help. Explain why, not how.
I don't think there's too much else you can do to future-proof this!
Context
StackExchange Code Review Q#27600, answer score: 3
Revisions (0)
No revisions yet.