How to Check for Intersecting Rects in An Array - ios

How would I be able to detect a intersection of rects within an array? I am not entirely sure of the most efficient way to do this.
Thanks

Just use nested for loops:
for (int i = 0; i<[array length]; i++) {
for (int j = 0; j<[array length] j++) {
if (i!=j) {
j = i+1;
//Check for intersection between array[i] and array[j]
}
}
}

Related

SIGABRT on sprintf

My code keep crashing (it's an old iOS app, without ARC). This is the code in question:
- (NSString *) dataToHex:(NSData *)data {
NSUInteger len2 = [data length] * 2;
unsigned char* chars = (unsigned char*) [data bytes];
char finalChar[len2 + 1];
for(int i = 0; i < len2; i++) {
sprintf(finalChar + (i * 2), "%02x", chars[i]);
}
finalChar[len2] = '\0';
NSString *hexString = [NSString stringWithFormat:#"%s", finalChar];
return hexString;
}
The crash is in the line of sprintf(). Any ideas? I've tried using bigger buffer sizes, but doesn't work.
It looks like this loop:
for(int i = 0; i < len2; i++) {
should be:
for(int i = 0; i < len; i++) {
(otherwise you're reading beyond the end of your input data and writing beyond the end of your output data).
In your case, the size of the finalChar array is len2 + 1. later, in the for loop, by saying
for(int i = 0; i < len2; i++) {
sprintf(finalChar + (i * 2), "%02x", chars[i]);
}
finalChar + (i * 2) will point to out of bound memory at some point (when i will be greater than len2/2) because, i is not limited to data length. Out of bound memory access cause undefined behavior.
I believe, it should be something like
for(int i = 0; i < len2/2 ; i++) {
sprintf(finalChar + (i * 2), "%02x", chars[i]);
}

How can I compare an NSMutableArray with a c array?

I have an NSMutableArray and I want to test it against multiple "Win" cases.
Meaning, I want to test each 3 integer C array, against the NSMutableArray and see if the ints found in the C array are found in the NSMutablearray
These are my C arrays:
// horizontal
int winCaseHorizontal1[3] = {9, 1, 2};
int winCaseHorizontal2[3] = {3, 4, 5};
int winCaseHorizontal3[3] = {6, 7, 8};
// verticle
int winCaseVerticle1[3] = {9, 3, 6};
int winCaseVerticle2[3] = {1, 4, 7};
int winCaseVerticle3[3] = {2, 5, 8};
// diaganol
int winCaseDiagonal1[3] = {9, 4, 8};
int winCaseDiagonal2[3] = {2, 4, 6};
I want to test the numbers say in the array winCaseHorizontal1, and see if those numbers are found in my NSMutableArray.
I've read about testing if two NSMutableArrays are equal, but this is quite different.
One; this is comparing a c array with an NSMutable,
Two; I don't care if they are equal, I just want to see if the c array ints are found in the NSMutable.
Thanks in advance for the help!
You can loop through your arrays and can find if element exists in NSMutableArray.
-(BOOL)elementFound{
for(NSNumber *number in mutableArray)
{
for(int i = 0; i < sizeof(winCaseHorizontal1) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal1[i]){
return YES;
}
}
for(int i = 0; i < sizeof(winCaseHorizontal2) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal2[i]){
return YES;
}
}
for(int i = 0; i < sizeof(winCaseHorizontal3) / sizeof(int); i++)
{
if([number integerValue] == winCaseHorizontal3[i]){
return YES;
}
}
//other arrays
}
return NO;
}
If you want index or elements which found in your c arrays than you can make another array and add the index or elements in that array and return it at end of function.
EDIT: As BryanChen suggested a faster way to do that is make and NSSet from c array and check if mutableArray element exist of not.
NSMutableSet *winCaseHorizontalSet1 = [[NSMutableSet alloc] init];
for(int i = 0; i < sizeof(winCaseHorizontal1) / sizeof(int); i++)
{
[winCaseHorizontalSet1 addObject:#(winCaseHorizontal1[i])];
}
for(NSNumber *number in mutableArray)
{
BOOL isContain = [winCaseHorizontalSet1 containsObject:number];
if(isContain) return YES;
//other set of c arrays
}

how to iterate an array in reverse order after it encounter last element?

How to put condition for an array to display the images. if it encounters last element of array it should go to previous order i.e., consider array have some 5 elements. 0 1 2 3 4 i need to traverse from 0 1 2 3 4 after encounter 5th object now to it should traverse to 4 3 2 1 0. How to put this logic?
I just put my logic
for(int i = 0 ; i < myArray.count; i ++)
{
NSLog(#"%d", i);
if(i == myArray.count -1)
{
for(int j = myArray.count-1 ; j >= 0; j --)
{
NSLog(#"%d", j);
}
}
}
Try with it, its working well. :)
The easiest way is doing 2 loops.
for (NSInteger i = 0;i < [array count]; i++)
{
NSLog(#"%#", [array objectAtIndex:i]);
}
for (NSInteger i = [array count] - 1;i >= 0; i--)
{
NSLog(#"%#", [array objectAtIndex:i]);
}

Filling NSMutableArray from Json Response

I am trying to fill an array of points for graphing. Here is an example of the data returned:
(
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123660;
},
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123720;
},
{
precipIntensity = 0;
precipProbability = 0;
time = 1398123780;
})
I can access the individual values by using:
responseArray[i][#"precipProbability"]
where i is equal to the index.
I am trying to fill an array with these points but I can't seem to get it to work. Can someone help?
I have tried:
if (!responseArray || !responseArray.count){
} else {
for (int i=0; i > [responseArray count]; i++) {
ArrayOfValues[i] = responseArray[i][#"precipProbability"];
}
and
if (!responseArray || !responseArray.count){
} else {
for (int i=0; i > [responseArray count]; i++) {
[ArrayOfValues insertObject:responseArray[i][#"precipProbability"] atIndex:i];
}
and the ArrayOfValues is always empty (null).
Your for loop condition is backwards. You want:
if (!responseArray || !responseArray.count){
} else {
for (NSUInteger i = 0; i < [responseArray count]; i++) {
[ArrayOfValues addObject:responseArray[i][#"precipProbability"]];
}
Also note the proper data type for i.
it looks like your data is already in an array. if you'd like to copy them to a mutable array you can call
NSMutableArray *mut = [responseArray mutableCopy];
if your real goal is to parse the JSON objects into an Obj-C model object, you probably want to read up on <NSFastEnumeration>
How about this:
NSArray *array = [responseArray valueForKey:#"precipProbability"];

Non repeating random numbers in iOS? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Non repeating random numbers in Objective-C
How to generate non repeating random numbers?
I saw this on many sites but they give in main.c file code.
When I use the main.c file the code working is fine, but when I try to convert in to my.m file it is not working.
example:
I need to get all the numbers between 0-10 randomly.and the numbers should not repeat again.
srand(time(NULL));
int s[10];
BOOL fl = 1;
for (int i = 0; i<10; i++) {
while (fl) {
s[i] = rand()%10;
fl = 0;
for (int j = 0; j < i; j++) {
if (s[j] == s[i]) {fl = 1; j = i+1;}
}
}
}
int n = 10;
NSMutableArray *numbers = [NSMutableArray array];
for (int i = 0; i <= n; i++) {
[numbers addObject:[NSNumber numberWithInt:i]];
}
NSMutableArray *result = [NSMutableArray array];
while ([numbers count] > 0) {
int r = arc4random() % [numbers count];
NSNumber *randomElement = [numbers objectAtIndex:r];
[result addObject:randomElement];
[numbers removeObjectAtIndex:r];
}
NSLog(#"%#", result);
Hope the below lines of code will help you
srand(time(NULL));
int randomIndex = rand() % limit;
[reportIDTextField setText:[NSString stringWithFormat:#"%i", randomIndex]];
Benefit of this code is it won't allow repetitive random numbers. Here limit for random numbers.
Hope this is what you are looking for.
Enjoy Coding :)
NSMutableArray *storeArray = [[NSMutableArray alloc] init];
BOOL record = NO;
int x;
for (int i=0; [storeArray count] < 10; i++) //Loop for generate different random values
{
x = arc4random() % 10;//generating random number
if(i==0)//for first time
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
else
{
for (int j=0; j<= [storeArray count]-1; j++)
{
if (x ==[[storeArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
{
record = NO;
}
else
{
[storeArray addObject:[NSNumber numberWithInt:x]];
}
}
}
NSLog(#" Non Repeated Random Numbers : %#",storeArray);
can u try this code may be it's use full to you
Try this:-
This will produce numbers between 0 to 10 randomly.
srand(time(NULL));
int n = 11;
int card[n];
for (int y=0; y<n; y++) {
card[y] = y;
}
for(int q= 0 ;q<3;q++) {
for (int y=0; y<(n-1); y++) {
int r = y + (arc4random() % (n-y));
int temp = card[y]; card[y] = card[r]; card[r] = temp;
}
}
for (int g=0;g<n ; g++) {
int w = card[g];
NSLog(#"%i",w);
}
You can do something like this
number = (arc4random()%10)+1; //Generates Number from 1 to 10.
save it to an Array
Then you can put a condition,That will check the number whether it is present or not in the array.
if not then add it to the array ,if it is already present then simply discard it.
A NOTE: "Never, ever add or multiply random numbers in an attempt to get 'better' randomness
:)

Resources