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

Standard Unit Conversion Tables
rounding to the nearest zero, bitwise
additive persistance ocaml [closed]
Moving a object based on its rotation in three.js
Boolean formula encoding
Getting cells in a 2D grid between two angles
how to find the shortest distance in geomatry route?
how to reduce dimensionality of vector
Direction from a normalized vector
matrix norm that is invariant through a change of basis
Convert Cartesian point to point on rotated plane (pic)
Spot light cone direction rotation and orientation inversion
Adding two params of a predicate that aren't static
undefined reference to `tan' but math.h has been included
Computing which side of a line a point is [closed]
Solving the recurrence relation for number of nodes in an AVL tree?

Categories

HOME
apache-spark
dynamic-programming
logstash
opencl
elf
garbage-collection
ionic3
whitespace
r-caret
luci
nanotime
android-dialogfragment
aerospike
similarity
netlify
semantics
google-distancematrix-api
azure-ad-graph-api
netcdf
dsc
pvs-studio
code-signing
physics
pdfa
android-arrayadapter
codeceptjs
python-behave
json-c
angular2-databinding
pdfstamper
bcel
p4-lang
macvim
ecdsa
uitabbarcontroller
es6-promise
hawkular
mamp-pro
projection
textangular
free-diameter
psycopg2
smartbanner
domdocument
kony
g-wan
master-data-management
cmsmadesimple
graph-traversal
tastypie
qtcpserver
masspay
dr-memory
android-sdk-tools
mit-scheme
amazon-dynamodb-streams
xendesktop
xmllint
mongolab
id3
hl7-v2
relaxng
greenfoot
oracle-bi
materialdrawer
dbfit
kindlegen
netbsd
stringr
windows-universal
cordova-cli
adserver
bit.ly
jasmin
physx
publish-actions
clearcase-remote-client
syndication-feed
globus-toolkit
team-explorer-everywhere
dojo.gridx
array-address
libtorrent-rasterbar
buildout
qdebug
mms-gateway
progressdialog
sizzle
hamlc
12factor
jcycle
doophp
51degrees
harvest
working-directory
jsr223
ribbon-control
mknetworkkit
letter-spacing
ed
cluetip
android-searchmanager
program-transformation
excel-web-query
scramble
time-estimation
non-relational-database
development-machine
shareware

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