Reputation: 211
I'm trying to use one variable (diameter) to act in concert with another variable (baseCurve), such that for every 0.5 increase to diameter, baseCurve is adjusted down by 0.25, and for every 0.5 decrease to diameter, baseCurve is adjusted up by 0.25.
The diameter is adjusted in 0.1 increments, by clicking an "increase" or "decrease" button. Seems simple enough, but I can't get around the problem that arises when the user adds less than the 0.5, and then reverses course.
Example (code works as intended): starting diameter = 9.0; starting baseCurve = 42.50
User clicks "Increase" 5x, now diameter = 9.5; baseCurve = 42.25
Example of problem: starting diameter = 9.0; starting baseCurve = 42.50
User clicks "Increase" 1x, now diameter = 9.1; baseCurve = 42.50
User then clicks "Decrease" 1x, now diameter = 9.0; baseCurve = 42.75.
The problem is, no adjustment should have been made, but my code is set up to make adjustments based on a modulus result, yielding the problem.
Can anyone point me to an approach that will basically allow the user to adjust baseCurve by 0.25 for every net change of 0.5 in diameter? The approach that I've taken, using a counter and modulus check isn't working, at least as I've set it up. I've also tried a switch, but can't avoid the same problem. Is there some way to use a couple of booleans to make this work? Many thanks in advance.
var adjustmentSum = 0
var baseCurve = 45.00
var diameter = 9.0
let diameterAdjustmentValue : Float = 0.1
let baseCurveAdjustmentValue : Float = 0.25
@IBAction func increaseDiameter(sender: AnyObject) {
adjustmentSum++
diameter = diameter + diameterAdjustmentValue
if adjustmentSum % 5 == 0 {
baseCurve = baseCurve - baseCurveAdjustmentValue
}
}
@IBAction func decreaseDiameter(sender: AnyObject) {
adjustmentSum--
diameter = diameter - diameterAdjustmentValue
if adjustmentSum % 5 == 0 {
baseCurve = baseCurve + baseCurveAdjustmentValue
}
}
Upvotes: 0
Views: 33
Reputation: 76
If I understand your problem correctly, you don't even need to use the modulus operator. You can just keep a regular counter of the increments/decrements and reset it to zero each time you make a change to baseCurve
var adjustmentSum = 0
var baseCurve = 45.00
var diameter = 9.0
let diameterAdjustmentValue : Float = 0.1
let baseCurveAdjustmentValue : Float = 0.25
@IBAction func increaseDiameter(sender: AnyObject) {
adjustmentSum++
diameter = diameter + diameterAdjustmentValue
if adjustmentSum == 5 {
baseCurve = baseCurve - baseCurveAdjustmentValue
adjustmentSum = 0
}
}
@IBAction func decreaseDiameter(sender: AnyObject) {
adjustmentSum--
diameter = diameter - diameterAdjustmentValue
if adjustmentSum == 5 {
baseCurve = baseCurve + baseCurveAdjustmentValue
adjustmentSum = 0
}
}
Upvotes: 1