// // MyPolygon.m // B-spline // // Created by Alexander Powell on Mon Aug 26 2002. // Copyright (c) 2002 __MyCompanyName__. All rights reserved. // #import "MyPolygon.h" @implementation MyPolygon - (id)init { if (self = [super init]) { points = [[NSMutableArray alloc] init]; return self; } return nil; } - (void)dealloc { [points release]; [super dealloc]; } - (void)loadFromFile:(NSString *)filename { int ii, numPoints; NSString *file, *trimmed; NSArray *pointsAsStrings; // Reset the points array [points release]; points = [[NSMutableArray alloc] init]; file = [NSString stringWithContentsOfFile: filename]; //trimmed = [file stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]]; trimmed = file; pointsAsStrings = [trimmed componentsSeparatedByString: @" : "]; numPoints = [pointsAsStrings count]; for (ii = 0; ii < numPoints; ii++) { MyPoint *newPoint; float currentZ = 0.0f; NSString *currentPointString = [pointsAsStrings objectAtIndex: ii]; NSArray *currentPointAsStrings = [currentPointString componentsSeparatedByString: @","]; // Read the first 2 components as x and y, respectively, and the third (if it exists) // as z.... if ([currentPointAsStrings count] > 2) currentZ = [[currentPointAsStrings objectAtIndex: 2] floatValue]; newPoint = [[MyPoint alloc] initWithX: [[currentPointAsStrings objectAtIndex: 0] floatValue] y: [[currentPointAsStrings objectAtIndex: 1] floatValue] z: currentZ]; [points addObject: newPoint]; [newPoint release]; // release now, since the point was retained when added to the array } } - (MyPolygon*)subdivideWith:(int)subdivisionType { MyPolygon *newPolygon = [[MyPolygon alloc] init]; switch (subdivisionType) { case SPLIT_AND_TWEAK: [newPolygon setPoints: splitAndTweak(points)]; break; case FOUR_POINT: [newPolygon setPoints: fourPoint(points)]; break; } return newPolygon; // CAUTION: must be released by caller... this is kinda sloppy } - (void)setPoints:(NSMutableArray *)newPoints { [newPoints retain]; [points release]; points = newPoints; } - (void)drawSelf { int ii; glBegin(GL_LINE_LOOP); for (ii = 0; ii < [points count]; ii++) { MyPoint *currentPoint = [points objectAtIndex: ii]; glVertex3f([currentPoint x], [currentPoint y], [currentPoint z]); } glEnd(); } @end