0

When trying to pass a CGPoint to a method the point is passed, but once inside the called method the CGPoint values show (inf, inf)? So passing the location CGPoint to the createShapeAt method ends in code not working. Maybe I am not implementing something correctly or do I need to COPY the CGPoint when passed over?

- (void)handleTap:(UITapGestureRecognizer *)recognizer {
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]])
{
    NSLog(@"User just tapped!");
    CGPoint location = [recognizer locationInView:recognizer.view];
    float scale = [(BasicCanvasUIView *)recognizer.view scale];
    location = CGPointMake(location.x / scale, location.y / scale);
    [self createShapeAt:location];
}
}

- (void)createShapeAt:(CGPoint)point {
//Create a managed object to store the shape
----------------------------------------------------------------------------
<-- WHEN I GET HERE ON BREAKPOINT THE (point) VARIABLE SHOWS "(inf,inf)"  -->
----------------------------------------------------------------------------
NSManagedObject *shape = nil;

//Randomly choose a Circle or a Polygon
int type = arc4random() % 2;
if (type == 0) { // Circle
    //Create the circle
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Circle" inManagedObjectContext:self.managedObjectContext];
    NSManagedObject *circle = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];
    shape = circle;

    //Randomly create a radius and set the attributes of the circle
    float radius = 10 + (arc4random() % 90);
    [circle setValue:[NSNumber numberWithFloat:point.x] forKey:@"x"];
    [circle setValue:[NSNumber numberWithFloat:point.y] forKey:@"y"];
    [circle setValue:[NSNumber numberWithFloat:radius] forKey:@"radius"];
} else { // Polygon
    //Create the Polygon
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Polygon" inManagedObjectContext:self.managedObjectContext];
    NSManagedObject *polygon = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:self.managedObjectContext];
    shape = polygon;

    //Get the vertices. At this point, no Vertex objects for this shape exist.
    //Anything you add to the set, however, will be added to the Vertex entity;
    NSMutableSet *vertices =[polygon mutableSetValueForKey:@"vertices"];

    //Create a random number of vertices
    int nVertices = 3 + (arc4random() % 20);
    float angleIncrement = (2 * M_PI) / nVertices;
    int index = 0;
    for (float i = 0; i < nVertices; i++) {
        // Generate random values of each vertex
        float a = i * angleIncrement;
        float radius = 10 + (arc4random() % 90);
        float x = point.x + (radius * cos(a));
        float y = point.y + (radius * sin(a));

        // Create the Vertex managed object
        NSEntityDescription *vertexEntity = [NSEntityDescription entityForName:@"Vertex" inManagedObjectContext:self.managedObjectContext];
        NSManagedObject *vertex = [NSEntityDescription insertNewObjectForEntityForName:[vertexEntity name] inManagedObjectContext:self.managedObjectContext];

        //Set teh values for the vertex
        [vertex setValue:[NSNumber numberWithFloat:x] forKey:@"x"];
        [vertex setValue:[NSNumber numberWithFloat:y] forKey:@"y"];
        [vertex setValue:[NSNumber numberWithFloat:index++] forKey:@"index"];

        //Add the Vertex object to the relationship
        [vertices addObject:vertex];

    }// ----------------- END IF/ELSE (circle and polygon done) -------------------------------------------------------------------------------------

    //Set the shapes color
    [shape setValue:[self makeRandomColor] forKey:@"color"];

    //Add  the same shape to both canvases
    [[topView.canvas mutableSetValueForKey:@"shapes"] addObject:shape];
    [[bottomView.canvas mutableSetValueForKey:@"shapes"] addObject:shape];

    //Save the context
    NSError *error = nil;
    if (![self.managedObjectContext save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    // Tell the views to repaint themselves
    [topView setNeedsDisplay];
    [bottomView setNeedsDisplay];

 }
}
jdog
  • 10,351
  • 29
  • 90
  • 165

1 Answers1

5

One obvious possibility is that recognizer.view.scale is zero. Have you tested that?

Put this statement in handleTap:, before you call createShapeAt::

NSLog(@"recognizer.view.scale = %f", scale);

What is the output of that statement? If it's zero, that's your problem. You need to use a scale that is not zero.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Yes, I break at the self.createShapeAt call in the taphandler and the CGPoint is there. and I break when the createShapesAt is called, but when i reach the shape = nil line its (inf,inf). What is inf anyway? – jdog Apr 09 '12 at 03:55
  • `inf` means infinity. It is a special value that you usually get when you divide a `float` or `double` by zero. Hence my suggestion that `scale` is zero. – rob mayoff Apr 09 '12 at 03:57
  • You are correct scale is "0". It looks as though I cannot access recognizer.view scale. – jdog Apr 09 '12 at 04:11
  • Sure you can access it. Otherwise you'd get an “unrecognized selector” exception. The problem is that it's set to zero. (Or `recognizer.view` is nil. But that seems unlikely.) – rob mayoff Apr 09 '12 at 04:18
  • Ok, its something with Core Data. If I run the app the first time all is ok, but reopening or rerunning in simulator causes the app to not work (aka nothing gets drawn on screen and scale is 0). Core Data is suppose to be storing the drawn objects and their transform objects. Will have to figure it out from here. Thanks for your help! – jdog Apr 09 '12 at 17:42