Compare the value of a variable at intervals - objective-c

Hey I have a problem comparing the value of a CGPoint (struct with two ints: x and y) with another at certain time intervals.
One is called location and has the value of where the cursor is. Every half a second or so, I want to see if it changed. How do I do this? The language is Objective-C (so C++/C stuff should work)

What are you trying to do with this? Would it make more sense to use Key Value Observing to observe when one or both values change?

Take a look at the NSTimer documentation. It does what you want.

As noted in the CGGeometry docs, Apple provides a function, CGPointEqualToPoint(), for doing just this. You would have a CGPoint variable that stores the old value and compare it to the new value you get.

Related

Why are constants used instead of variables?

In what general occasions are constants used instead of variables. I need a few examples.
Thanks in advance.
A variable, as the name implies, varies over time. Variables mostly allocate memory. In your code, when you declare that a value will not change, the compiler can do a series of optimizations (no space is allocated for constants on stack) and this is the foremost advantage of Constants.
Update
You may ask why do we use Constants after all?
It's a good question, actually, we can use literal numbers instead of constants. it does not make any difference for the compiler since it sees both the same. However, in order to have a more readable code (--programming good practice), we'd better use constants.
Using constants, you can also save your time!. To be more specific, take below as an example:
Suppose a rate value for some products in a shopping system (rate value = 8.14). Your system has worked with this constant for several months. But then after some months, you may want to change the rate value, right?. What are you going to do? You have one awful option! Changing all the literals numbers which equal 8.14! But when you declare rate as a constant you just need to change the constant value once and then changes will propagate all over the code. So you see that by using constants you do not need to find 8.14's (literal numbers) and change them one by one.
Constants are used when you want to assign a value that doesn't change. This is helpful because if you try to change this, you will receive an error.
It is also great for readability of the code. A person who reads your code will now know that this particular value will never change.
For example:
$name = 'Danny'; // this could change if I ever changed my name
const SECONDS_IN_MINUTE = 60; // this will never change, so we assign it as a constant
You use a constant, when the value of a variable never changes during the lifetime of your program. Once you defined a constant x, you can't change it's value anymore.
Think of pi. Pi is a constant with value 3.1415. This will never change during your programs lifecyle.
const pi = 3.14159265359
When you use a variable instead, you can change it's value as often as you want to.
int x = 1;
x = 7;

How to check if two given b2Body objects are touching?

I know how to check for collisions in the update loop - but I'm in a position where I need to see if two bodies are touching or overlapping in box2D. I have pointers to these objects and I don't want to check while moving through update.
I am still learning Box2D so apologies if this is a dumb question. I have tried to solve this for an hour or so now but I'm not having any luck.
It looks like you can do this using b2TestOverlap or maybe b2CollideCircles (since the objects are b2CircleShapes). If either of these is the correct strategy can you get the shape from a given body, and what should I use as the transform values? If these aren't the way to go, how can I check if two given b2Body objects are overlapping or touching.
UPDATE: Here's the code that I got thanks to LearnCocos2D:
-(BOOL)isTouchingCentre:(b2Body*)bodyToTest{
//body is a b2Body object
bool overlap = b2TestOverlap(body->GetFixtureList()->GetShape(), bodyToTest->GetFixtureList()->GetShape(), body->GetTransform(), bodyToTest->GetTransform());
return overlap;
}
Not sure about the details of b2TestOverlap but I would give it a try. You can get the necessary info from the bodies, assuming they only have one shape:
body->GetTransform();
body->GetFixtureList()->GetShape();

Finding curCapacity and maxCapacity Value, iPhone

A method which can be used for finding the battery percentage of your device is found here:
http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/comment-page-1/#comment-6085
I have tried it and it works great. If you have a closer look at the code there are two values curCapacity and maxCapacity. I want to be able to use these values in other calculations, yet when I try and do this I always get the error 'undeclared'.
Any ideas as to why?
where are you trying to use this variables? they are declared inside - (double) batteryLevel method so you cannot use them in other methods. if you want to use them in other places, declare them in your .h file. so you will be able to get access to these values not only from this method.

Something really dumb with return values

I'm doing something really dumb, and I don't see it.
I've got an object doc with a method:
-(float) currentOrient
{
return 50.5;
}
In another object, I call:
-(void) showPage
{
float rot2=0;
rot2 = [doc currentOrient] ;
NSLog(#"SP rotation is %.2f", rot2);
}
However, the output is :
SP rotation is 1112145920.000000
No, one question is "Why is the %2f not formatting correctly?" But the more confusing question is "Where is that number coming from?" Yes, I've walked through it with a debugger, the value of rot DOES change from the garbage it starts with. and that number DOES appear to be consistent.
Clearly something really dumb is going on...
It sounds like the showPage method doesn't know right return type for currentOrient, so it's interpreting the value returned as an int and casting that nonsensical int to a float. Are you getting any warnings? Are you sure you're importing the header for currentOrient correctly? Is the currentOrient method declared correctly?
I can answer the first question:
Why is the %2f not formatting correctly?
Because it ought to be %1.2f to round to two decimal places (which I believe is what you're trying to achieve?)
And guess at the second:
Do you have a property named rot in the code? Other than that... shrug... I don't know - I'm assuming you've simplified the example to post on SO, have you taken out other code that may be relevant? Based on the information you've provided everything should be ducky.
On a side note: When I hit bugs like this I go do something physical. Usually when I come back my head is clear and I find the problem immediately. You might want to give that a try too! :D

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .