Processing: conditional to test for variable change - variables

My question pertains to the environment of Processing 2.0.
I need to write a conditional (or set of conditionals) in void draw() that tests if the variable x has increased by 1 or decreased by 1 and adjust the variable y depending on the increase/decrease in x. For example if x decreases 1, y should increase by 10, and if x increases by 1 y should decrease by 10. How would I accomplish this?

The most obvious answer is to try to think of y as a multiple of x plus maybe an offset? So if you have your x going up and down every time you enter the draw() method you should do y = x * 10; or y = 400 + x * 10 if you have an offset (int this case 400) of some sorts...
If you absolutely have to do it like that, then the way is to store the previous value of x and check each at each draw() call. So create a new variable int prevX and in your draw() method do:
y = y + (x-prevX) * 10;
or
int diff = x - prevX;
if(diff == -1) y = y - 10;
else if (diff == 1) y = y + 10;

Related

Z3 ArithRef type: is there a way to show value once model evaluated?

Using Z3Py, once a model has been checked for an optimization problem, is there a way to convert ArithRef expressions into values?
Such as
y = If(x > 5, 0, 0.5 * x)
Once values have been found for x, can I get the evaluated value for y, without having to calculate again based on the given values for x?
Many thanks.
You need to evaluate, but it can be done by the model for you automatically:
from z3 import *
x = Real('x')
y = If(x > 5, 0, 0.5 * x)
s = Solver()
r = s.check()
if r == sat:
m = s.model();
print("x =", m.eval(x, model_completion=True))
print("y =", m.eval(y, model_completion=True))
else:
print("Solver said:", r)
This prints:
x = 0
y = 0
Note that we used the parameter model_completion=True since there are no constraints to force x (and consequently y) to any value in this model. If you have sufficient constraints added, you wouldn't need that parameter. (Of course, having it does not hurt.)

How to explain the strange results for while loop with floating point in swift

I have tested while loop below and don’t understand the result.
var x: Float = 0.0
var counter = 0
while x < 1.41
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x) // 1.5
How is it possible to have the result x = 1.5 for the used while condition where x < 14.1 ? How to explain this result?
Update:
and one more. Why the results are different for Double and Float ?
var x: Double = -0.5
var counter = 0
while x < 1.0
{
x += 0.1
counter += 1
}
print (counter) // 16
print (x)//1.1
var x: Float = -0.5
var counter = 0
while x < 1.0
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x)//1.0
Update 2
and another one. Why there is no difference for < and <= conditions. Does it mean that usage of <= has no sense for floating point ?
var x: Double = 0.0
var counter = 0
while x < 1.5
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x) //1.5
var x: Double = 0.0
var counter = 0
while x <= 1.5
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x) //1.5
What else would you expect? The loop is executed 15 times. On the 14th time, x is 1.4 and so you add another 0.1, making it 1.5.
If you expect the loop to terminate at 1.4, you should increment x before checking the while condition, not after that.
If you expect the loop to terminate on 1.41, your increment is wrong and you should do
x += 0.01
instead, making it 141 iterations.
As for the second question, I am aware that Float should not be used for monetary calculations and such due to its lack of precision. However, I trusted Double so far, and the while loop in run 15 actually claims the Double value to be less than 1.0 while it is reported to be 1.0. We have got a precision problem here, as we can see if we substract x from 1.0:
print(1.0-x)
which returns: 1.11022302462516e-16
At the same time, Float seems to be unprecise in the other direction. In the last run, it is a little bigger than 0.9 (0.9 + 5.96046e-08), making it bigger than 10 in the following run.
The reason why Double and Float are wrong in different directions is just a matter of how the values are stored, and the result will be different depending on the number. For example, with 2.0 both actual values are bigger: Double by 4.440892..e-16 and Float by 2.38419e-07. For 3.0 Double is bigger by 1.33226e-15 and Float smaller by 7.1525e-07.
The same problems occur using x.isLess(than: 1.0), but this method is the basis for the < operator as of https://developer.apple.com/reference/swift/floatingpoint/1849403-isless
isLessThanOrEqualTo(1.0), on the other hand, seems to work reliably as expected.
This answer is pretty much a question itself by now, so I'm curious if anyone has an in-depth explanation of this...
Update
The more I think about it, the less of a Swift problem it is. Basically, you have that problem in all floating point calculations, because they are never precise. Both Float and Double are not precise, Double is just twice as accurate. However, this means that comparisons like == are useless with floating point values unless they are both rounded. Therefore, good advice in loops like those of yours with a known precision (in your case one decimal) would be to round to that precision before doing any kind of comparison. For example, this would fix the loop:
var x: Double = -0.5
var counter = 0
while (round(x * 1000) / 1000) < 1.0
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x)//1.0
var x: Float = -0.5
var counter = 0
while (round(x * 1000) / 1000) < 1.0
{
x += 0.1
counter += 1
}
print (counter) // 15
print (x)//1.0

C/Obj-C noise generators always return 0 after the first run?

Unfortunately the simplex/perlin noise generator I've always used is very bloated and java-based, and would be a pain to transfer to c/obj-c. I'm looking for better classes to use in an iOS version of a game, but i have an odd problem.
I have code that loops through each "tile" of a 2d background - it should calculate a noise value for each tile. In my java implementations it works fine.
However, each time I run the code, it appears to print a proper value the first time the breakpoint is hit, but from then on only ever returns zero:
for (double x = 0; x < 2; x++){
for (double y = 0; y < 2; y++){
double tileNoise = PerlinNoise2D(x,y,2,2,1);
}
}
I've tried two different implementations, the current being this c perlin library.
The breakpoint shows a value like 1.88858049852505e-308 the first time, but when I continue execution all subsequent breaks show "0".
What am I missing?
Perlin noise is defined to be zero for integer locations. Try rotating, scaling or translating your space and see what happens example:
double u = 0.1;
double v = 0.1;
for (double x = 0; x < 2; x++){
for (double y = 0; y < 2; y++){
double tileNoise = PerlinNoise2D(x+u,y+v,2,2,1);
}
}

Working with circle radials. Subtraction and addition

I'm working with magnetic headings and I am struggling a little bit with the math around it.
Let's say I have a heading of 270 degrees, and I turn clockwise 110 degrees. I want the new heading, 020 degrees, as an output and not 380 degrees. Is the best way to do something like this:
if (x > 360) { x = x - 360; }
or can I use calculations with M_PI to make it more correct?
Thanks for any replies!
An angle of x degree is equal to any angle X which satisfies X = x [360] that's modular arithmetic note that x can be less then -360 or greater then 720, so in that case, adding or subtracting 360 won't give you the result you're expecting. If you want a pure calculation method by adding or subtracting, you can use this piece of code:
if (x >= 360){
while( x >= 360)
x -= 360;
}
}else if (x < 0){
while( x < 0)
x += 360;
}
}
Or, more easily, in Objective-C, the operator that gives the value of X satisfying the equation below and being in the interval of [0;360[,is the % operator. You can use it that way:
X = x % 360
Although, your angles may not be int types. Than that operator won't work because it takes int as arguments. In this case, you can use the fmod or fmodf functions. The syntax you would use would then be :
X = fmodf(x,360)
this could be best way..
x = x % 360;

If statements - certain value or more

I have an app where i want it to only do some thing if the int value is a certain number or above and i cant seem to figure out how to do it, i can do it if it is exactly the amount but not if its more.
For example i have an int value of 0 and its goes up every time you push a button, when it becomes 20 or higher i want it to do something when another button is pressed.
Thanks for he help!
- (IBAction)storeTroll:(id)sender {
if (count == 20) {
trollButton.hidden = NO;
}
}
Operator Description
x == y Returns true if x is equal to y
x > y Returns true if x is greater than y
x >= y Returns true if x is greater than or equal to y
x < y Returns true if x is less than y
x <= y Returns true if x is less than or equal to y
x != y Returns true if x is not equal to y
So, use >= :
- (IBAction)storeTroll:(id)sender {
if (count >= 20) {
trollButton.hidden = NO;
}
}