math


Getting the shortest angle path


I need to get the shortest direction between two angles. Like if Ang1 is 5 and Ang2 is 355 then I want it to return that I need to subtract from Ang1 to get to Ang2 .
I have some code that will tell me the shortest distance, but not the shortest direction.
function getShortAngle(a1, a2)
{
var angle = (Math.abs(a1 - a2))%360;
if(angle > 180)
angle = 360 - angle;
return angle;
};
trace(getShortAngle(360, 720));
Or in Smallbasic:
Sub GetShortestpath
angle = Math.Remainder((Math.abs(a1 - a2)),360)
if angle > 180 Then
angle = 360 - angle
EndIf
Return = angle
EndSub
Thanks for any help!
Surely that's just setting the direction based on which angle you choose.
If you're working out the angle/direction from a1 to a2, the following pseudo-code should give you what you need:
# precondition: [a1,a2] >= 0
angle = ((a2 % 360) - (a1 % 360) + 360) % 360
direction = CLOCKWISE
if angle > 180:
angle = 360 - angle
direction = ANTICLOCKWISE
In cases where the difference is exactly 180°, it prefers the clockwise direction.
Forgive the complex expression getting the angle, it just to ensure you get a value 0 through 359 regardless of the relative positions and workings of your modulo operator with negative numbers.
You can greately simplify it if you make the precondition more restrictive, ensuring a1 and a2 are limited to the range 0..359:
# precondition: 0 <= [a1,a2] <= 359
angle = (a2 - a1 + 360) % 360

Related Links

Collision detection between two objects
Explonential function with a lower growing rate [closed]
Calculating sqrt and arcTan in javacard without float type
Move point in cartesian coordinate through distance in the given direction
LU-Decomposition factorization error
How to find polygons in a given set of points and edges?
Calculate how much a point is worth based on played games
LibGDX: How to keep an Object at same visual screen position while rotating the camera
Setting the magnitude of a 2D Vector
Convert Lat/Long to X,Y position within a Bounding Box
d3js Cluster Force Layout IV block by Mike
Maximum number of points that lie on the same straight straight line in a 2D plane
How to draw a gps marker on a rotated custom image map
Conversion coordinates from a bound rectangle, to the coordinates of rotated rectangle contained by it
Why isn't this Math.random method working?
Calculate radius of curve/arc formed by line segments (approximate)

Categories

HOME
math
actions-on-google
angular2-forms
servlets
tcl
watson
react-navigation
libreoffice
cql3
ionic3
pyusb
build.gradle
square-connect
calayer
w3.css
sparkpost
vulkan
plots.jl
vast
ssrs-2008
contains
jet
csom
broker
ssms-2014
google-oauth2
intersystems
android-arrayadapter
ocean
debugview
matterjs
gecko
pvlib
spring-websocket
blockui
google-crawlers
sieve-of-eratosthenes
microsoft-translator
runge-kutta
amazon-elasticsearch
linear
freerdp
gulp-babel
simplesamlphp
xbrl
sshd
column-oriented
bosh-deployer
assembly-references
findall
vs-2015-preview
codeigniter-upload
struts1
telecommunication
android-location
pager
nuitka
bash-completion
android-popupwindow
ctakes
training-data
cucumber-java
domo
kinto
coldfusion-10
heroku-toolbelt
kindlegen
structured-programming
smarty2
alt
lossless-compression
simpleadapter
bitrock
sbrk
culture
publish-actions
pkcs#10
email-spam
xcode6.1.1
qt4.7
mongo-shell
sqlprofiler
sshj
deap
llblgenpro
semantic-markup
nsmanagedobjectmodel
google-chrome-frame
vote
ad-hoc-distribution
mknetworkkit
httpcontext.cache
jtemplates
staging
symstore
regexbuddy
wmd
coords
posterous
drwatson
help-authoring

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App