I am writing an iPhone code that fuzzily recognizes whether the line with the wire is straight. I get a bearing of two end points and compare it with 0, 90, 180 and 270 degrees with a tolerance of 10 degrees plus or minus. Right now I am doing this with a bunch of if blocks that seem super awkward.
How to write a function, which, given the bearing 0..360, the percentage tolerance (say, 20% = (from -10 ° to + 10 °)), and a right angle , for example, 90 degrees, does the bearing return within the tolerance?
Update: I may be too specific. I think a good, general function that determines if a number is a percentage of another number is useful in many areas.
For example: Is the number of swipeLength inside 10% of maxSwipe ? That would be helpful.
BOOL isNumberWithinPercentOfNumber(float firstN, float percent, float secondN) { // dunno how to calculate } BOOL result; float swipeLength1 = 303; float swipeLength2 = 310; float tolerance = 10.0; // from -5% to 5% float maxSwipe = 320.0; result = isNumberWithinPercentOfNumber(swipeLength1, tolerance, maxSwipe); // result = NO result = isNumberWithinPercentOfNumber(swipeLength2, tolerance, maxSwipe); // result = YES
Do you see what I get?
source share