My Objective-C is pretty rusty or non-existent. I’m trying to figure out how this code does what it does in order to modify it and having some trouble. This code basically draws lines at a pixel position on an ipad UIView (along the width of the iPad, like a density or column plot) based on the data it has. From the data, it calculates what values to assign and then another method draws the information.
In the .h file, this ivars are declared
ushort *mapBins;
int mapBinSize;
int mapBinMin;
int mapBinMax;
Then in the .m (commented out code is what I’m trying to do)
mapBins = 1024;
mapBins = malloc(mapBinSize * sizeof(ushort));
memset(mapBins, 0, mapBinSize * sizeof(ushort));
int pixelPos = 0;
// populate bins
for (int l=0; l<[locs count]; l++) // locs is a member variable that has positions to draw where there is valid data, everything else is blank
{
int sLoc = [[locs objectAtIndex:l] intValue];
pixelPos = (sLoc - self.startPos) / [self basesPerPixel];
int index = pixelPos;
if (index > mapBinMax) {
continue;
}
if (index < 0 )
{
continue;
}
// if ([m.type rangeOfString:@"A"].location == NSNotFound) {
// mapBins[index] = 0;
// }
// else {
// mapBins[index] = 1;
// NSLog(@"1");
// }
// if ([m.type rangeOfString:@"T"].location == NSNotFound) {
// mapBins[index] = 0;
// }
// else {
// mapBins[index] = 2;
// NSLog(@"2");
// }
mapBins[index] += 1;
}
return YES;
}
So with their code, in the drawing code, it basically checks
if (mapBins[posX] == 0) {
[blank setStroke];
}
else {
[YELLOW_COLOR setStroke];
}
for each pixel. So now I want to change the colors based on the information I have from the data. Like in the commented out code, it shows a part of what I’m trying to do.
What I don’t understand is, I set the mapBin[index] to something other than 0. I see that by the NSLog(@”1″) statement on the console. However, if I log the output at the end of the loop by doing: NSLog(@"%i", mapBins[posX]); I get 0 as the output. And when I try drawing it, I get a blankStroke instead of a colored stroke since the value is 0.
Is something going on that I do not understand in C? What I gather is they are doing this (could be wrong though):
- set the mapBinSize to the size of one screen length or 1024 pixels
- create memory for that array?
- then based on the data, if there is a value at that bin, add a number so it can draw a value at that region later on.
Your commented-out logic will always result in either 0 or 2. Whatever you set from the comparison to @”A” is written over unconditionally by the following if/else.