2

I am trying to calculate the rotation from one cross to another. The correspondences between lines in the crosses are known.

The rotation needs to be calculated within 180 degrees either clockwise or anti-clockwise, currently I can calculate within 90 degrees but the algorithms fails with anything larger. The problem seems to be when the matching bearings pass around 360 degrees, such that A = 350 and A' = 80. Repeating this for each line of the cross, causes an incorrect total rotation to be calculated.

The algorithm at present, works as follows for comparing the rotation between two lines, from two crosses is; where crossB and crossA are the corresponding bearings for each cross.

 if ((crossB < 360 && crossB >= 270) && (crossA >= 0 && crossA < 90)) { angle = -((360) - crossB) - crossA; } else if ((crossA < 360 && crossA >= 270) && (crossB >= 0 && crossB < 90) { angle = crossB + (360 - crossA); } else { angle = crossB - crossA; } 

Any thoughts on how to improve or change the algorithm so that it will allow any amount of rotation to be determined?

2
  • If you want your input to be within 0 and 360, then use the modulo operator (%) to ensure it's in that range: int deg = val % 360;' - that will take a value in val` and ensure that it's within 360 by dividing val by 360, and assigning the remainder to deg. Commented Mar 5, 2011 at 16:59
  • As its a bearing it will always be within 360 degrees, however as the above algorithm checks it for the angle between each corresponding line to calculate the average, there are certain changes in bearings that dont provide the correct rotation Commented Mar 5, 2011 at 17:04

1 Answer 1

2

If I understand correctly, you want to find the smallest angle between two vectors, where the vectors are expressed in a bearing angle in degrees. If that's the case you should be able to use the following code, from NASA's open source WorldWind project

/** * Computes the shortest distance between this and angle, as an angle. * * @param angle the angle to measure angular distance to. * * @return the angular distance between this and <code>value</code>. */ public Angle angularDistanceTo(Angle angle) { if (angle == null) { String message = Logging.getMessage("nullValue.AngleIsNull"); Logging.logger().severe(message); throw new IllegalArgumentException(message); } double differenceDegrees = angle.subtract(this).degrees; if (differenceDegrees < -180) differenceDegrees += 360; else if (differenceDegrees > 180) differenceDegrees -= 360; double absAngle = Math.abs(differenceDegrees); return Angle.fromDegrees(absAngle); } 

Where subtract works the way you would think it does. The algorithm should be easy to adapt to a non-object oriented approach.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.