shannoga
shannoga

Reputation: 19869

CGPoints drawn upside down in GLPaint

We are using GLPaint example for a new app. We wish to demonstrate the user the way to draw few objects. We saw apple GLPaint has an example on how to playback data of points to drawings.

So we have managed to supply our own data and it works great beside the problem that it makes it upside down.

When the points supplied are for that -

alt text

it will draw that

alt text

Is there a simple solution for that?

Thanks, Shani

Upvotes: 1

Views: 415

Answers (2)

shannoga
shannoga

Reputation: 19869

thanks Ginamin

that helped a lot. it sounds that they should make some kind of a build in function to flip the coordinates.

i did that - 1. find the highest and lowest point in the y coordinates. 2. multiply each y point in -1, and add the highest + lowest points to it. so the image is drawn correctly.

this is the code if it will help anyone.

   //points array for example
points = [[NSArray alloc] initWithObjects:
              [NSValue valueWithCGPoint:CGPointMake(0, 0)],
              [NSValue valueWithCGPoint:CGPointMake(0, 90)],
              [NSValue valueWithCGPoint:CGPointMake(100, 90)],
              [NSValue valueWithCGPoint:CGPointMake(100, 0)],
              [NSValue valueWithCGPoint:CGPointMake(0, 0],
              nil];


     CGPoint point;
     NSValue *val;
     NSDictionary * dict;
     NSMutableArray *yPoints =[[NSMutableArray alloc] initWithCapacity:[points count]];

     for (int i=0; i<[points count]; ++i) {
     val = [points objectAtIndex:i];
     point = [val CGPointValue];
     dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:point.y] forKey:@"y"];
     [yPoints addObject:dict];

     }

             //sort the array by the y value
     NSSortDescriptor * yDescriptor =
     [[NSSortDescriptor alloc] initWithKey:@"y"
     ascending:YES];

     NSArray * descriptors =
     [NSArray arrayWithObjects:yDescriptor, nil];
     NSArray * sortedArray =
     [yPoints sortedArrayUsingDescriptors:descriptors];

             //get the first and last points from the sorted array

     yFactorHighest =  [[[sortedArray lastObject] objectForKey:@"y"] intValue];
     yFactorlowst =  [[[sortedArray objectAtIndex:0] objectForKey:@"y"] intValue];

     [yDescriptor release];
     [yPoints release];

and the drawing function -

    - (void) playback:(NSString*)letter 
{

    NSUInteger pointsCount = [points count]-1;


    // Render the current path


    val1 = [points objectAtIndex:p];
    point1 = [val1 CGPointValue];
    point1.y=-(p1.y)+yFactorHighest+yFactorlowst;

    val2 = [points objectAtIndex:p+1];
    point2 = [val2 CGPointValue];
    point2.y=-(p2.y)+yFactorHighest+yFactorlowst;

    [self renderLineFromPoint:point1 toPoint:point2];
    p++;    

    if(p<pointsCount)
        [self performSelector:@selector(playback:) withObject:@"a" afterDelay:0.1];

}

im new to programming, so i hope there are no problems in my code. hope it will help

shani

Upvotes: 0

Beaker
Beaker

Reputation: 1608

The iPhone coordinate system reads up to down, while openGL uses a cartesian system. So, all drawings in openGL will appear upside down on the iPhone. Try and flip your image upside down when rendering.

Upvotes: 2

Related Questions