Recursive flood fill on an iPad paint app - objective-c

Im using recursive flood fill algorithm on my ipad painting app and it crashes with stack overflow i think. Can someone help me solve this problem with example code or good advice because im a noob?
-(void)paintingBucket:(int)point point2:(int)point2 width:(int)width colorAtPoint:(UIColor *)color {
int offset = 0;
int x = point;
int y = point2;
offset = 4*((width*round(y))+round(x));
if (((x<1025 && y<705)||(x<1025) ||(y<705)) && (x>0 && y>0) && (offset<2887648)) {
int alpha = data[offset];
int red = data[offset + 1];
int green = data[offset + 2];
int blue = data[offset + 3];
color1 = [UIColor colorWithRed:(green/255.0f) green:(red/255.0f) blue:(alpha/255.0f) alpha:(blue/255.0f)];
if (![color1 isEqual: color] ) {
return;
} else {
color3 = self.currentColor ;
CGFloat r,g,b,a;
[color3 getRed:&r green:&g blue: &b alpha: &a];
int reda = (int)(255.0 * r);
int greena = (int)(255.0 * g);
int bluea = (int)(255.0 * b);
int alphaa = (int)(255.0 * a);
// NSLog(#" red: %u green: %u blue: %u alpha: %u", reda, greena, bluea, alphaa);
data[offset + 3] = alphaa;
data[offset + 2] = reda;
data[offset + 1] = greena;
data[offset] = bluea;
}
}
[self paintingBucket:x+1 point2:y width:width colorAtPoint:color];
[self paintingBucket:x-1 point2:y width:width colorAtPoint:color];
[self paintingBucket:x point2:y+1 width:width colorAtPoint:color];
[self paintingBucket:x point2:y-1 width:width colorAtPoint:color];
}

Here's a naive example fo making this a dynamic algorithm, not recursive.
NSMutableArray *pointsToRender = [NSMutableArray new];
[pointsToRender addObject:startingPoint];
while (pointsToRender.length>0) {
// Get a point from the array and fill it
MyPoint *point = [pointsToRender lastObject];
[pointsToRender removeLastObject];
[self drawColor:color atPoint:point];
// Are any surrounding points needing to be filled?
if (point 1px above) needs to be filled
[pointsToRender addObject : point1Above];
.. repeat for the other three points
}
Yea, this is half objective C and half pseudocode, sorry. But you get the idea. In English it's this :
Make an array of points that you need to fill, containing the start point.
For each point
Fill it
Remove it from the array
Look at it's neighbours. Add each neighbour to the array if it also needs to be filled
repeat until your points to be filled array is empty
This will consume heap, not stack.

Breaking the function into the fillRight and fillLeft methods isn't a long-term solution because if the image gets bigger, overflow may occur again.
I'd recommend using a faster flood-fill algorithm. The 4-Way flood fill described on Wikipedia is easy to implement, but goes through every point four times.
I'd recommend using a scanline fill: see http://lodev.org/cgtutor/floodfill.html - Scanline Floodfill Algorithm With Stack. I replaced my 4-way flood fill in my drawing app and it now is much faster and fills a 1024x768px area in less than a second. Of course, the speed may vary depending on your implementation.
And at last, some notes:
1.You can use CGPoint to store points in an array.
CGPoint point=CGPointMake(100, 50); //Declare a point and put a value in it
Then you can get and set the x and y values using point.x and point.y
2.Use an array to store the points to be checked as deanWombourne suggested.
NSMutableArray * pointsToCheck=[[NSMutableArray alloc]init];//Initialise the array
[pointsToCheck addObject:[NSValue valueWithCGPoint:start]];//Assuming you have a CGPoint start, you need to convert it to a NSValue before you put it into the array like [NSValue valueWithCGPoint:point]
while ([pointsToCheck count]>0){//While there are points to be checked:
NSValue * pointValue=[pointsToCheck objectAtIndex:0];//Get the point: It doesn't matter if you get the first or last item
[pointsToCheck removeObjectAtIndex:0];//Remove the point from the queue so we won't go through it again
CGPoint point=[pointValue CGPointValue];//Get the CGPoint from the NSValue object
//Implement your algorithm here: check for neighbour points, paint the current point or whatever. I'd recommend using scanline fill - see above
//When you need to add a point to the queue (If it was a recursive function, then you'd call the function from itself with different parameters) use:
[pointsToCheck addObject:[NSValue valueWithCGPoint:newPoint];
}

i solved my problem using this two methods. it is a little slow but im working on optimizing this solution.
-(void)fillRight:(int)x point2:(int)y witdh:(int)width {
int x1 = x;
int y1 = y;
int offset = 4*((width*round(y1))+round(x1));
int alpha = data[offset];
int red = data[offset + 1];
int green = data[offset + 2];
int blue = data[offset + 3];
color1 = [UIColor colorWithRed:(green/255.0f) green:(red/255.0f) blue:(alpha/255.0f) alpha:(blue/255.0f)];
// NSLog(#"%d %d %# %#", x,y,color,color1);
if([color1 isEqual: color])
{
color3 = self.currentColor ;
CGFloat r,g,b,a;
[color3 getRed:&r green:&g blue: &b alpha: &a];
int reda = (int)(255.0 * r);
int greena = (int)(255.0 * g);
int bluea = (int)(255.0 * b);
int alphaa = (int)(255.0 * a);
// NSLog(#" red: %u green: %u blue: %u alpha: %u", reda, greena, bluea, alphaa);
data[offset + 3] = alphaa;
data[offset + 2] = reda;
data[offset + 1] = greena;
data[offset] = bluea;
[self fillRight:++x1 point2:y1 witdh:width];
x1 = x1 - 1 ;
[self fillRight:x1 point2:y1-1 witdh:width];
[self fillRight:x1 point2:y1+1 witdh:width];
}
}
-(void)fillLeft:(int)x point2:(int)y width:(int)width {
int x1 = x;
int y1 = y;
int offset = 4*((width*round(y1))+round(x1));
int alpha = data[offset];
int red = data[offset + 1];
int green = data[offset + 2];
int blue = data[offset + 3];
color1 = [UIColor colorWithRed:(green/255.0f) green:(red/255.0f) blue:(alpha/255.0f) alpha:(blue/255.0f)];
// NSLog(#"%d %d %# %#", x,y,color,color1);
if([color1 isEqual: color])
{
color3 = self.currentColor ;
CGFloat r,g,b,a;
[color3 getRed:&r green:&g blue: &b alpha: &a];
int reda = (int)(255.0 * r);
int greena = (int)(255.0 * g);
int bluea = (int)(255.0 * b);
int alphaa = (int)(255.0 * a);
// NSLog(#" red: %u green: %u blue: %u alpha: %u", reda, greena, bluea, alphaa);
data[offset + 3] = alphaa;
data[offset + 2] = reda;
data[offset + 1] = greena;
data[offset] = bluea;
[self fillLeft:--x1 point2:y1 width:width];
x1 = x1 + 1 ;
[self fillLeft:x1 point2:y1-1 width:width];
[self fillLeft:x1 point2:y1+1 width:width];
}
}

Personally I prefer using the A* algorithm for flood fills. Basically, you color the visited nodes. while searching for path to a point that is known to be outside of your fill area. (-1, -1 does the trick)

Related

Having problems seeing polygons in my cocos2d code. Using cocos2d and box2d. Only in debug mode the actual polygons are visible

So I need help figuring out what code I am missing here. I have checked all over the place, but I need specifics on wether its the formulas used or a typo that i haven't noticed yet.
Here is the polygon class. I am trying to create random polygons with 8 vertices and then of course fill with a plain color. But I want them to continue to generate random position but leave them fixed. In a better way the poly's are my terrain.Ok revise: the polygons are there and my character interacts with them, but I cannot see them, and yes they are on the same layer. Oh but they don't keep generating at the bottom, which i am guessing i just need to delete the old ones once they go off the screen and it should make a new poly.
-(void) genBody:(b2World *)world pos:(CGPoint *)pos {
//Here we generate a somewhat random convex polygon by sampling
//the "eccentric anomaly" of an ellipse with randomly generated
//x and y scaling constants (a,b). The algorithm is limited by
//the parameter max_verts, and has a number of tunable minimal
//and scaling values.
// I need to change this to randomly choosing teh number of vertices between 3-8,
// then choosing random offsets from equally distributed t values.
// This will eliminate teh while loop.
screen_pos = ccp(pos->x, pos->y);
float cur_t;
float new_t;
float delta_t;
float min_delta_t = 0.5;
float t_scale = 1.5;
b2Vec2 *verts= new b2Vec2[m_maxVerts]; // this should be replaced by a private verts ... maybe ... hmm that will consume more ram though
float t_vec[m_maxVerts];
// Generate random vertices
int vec_len;
while (true) {
cur_t = 0.0;
for (vec_len=0; vec_len<m_maxVerts; vec_len++) {
//delta_t = t_scale*(float)rand()/(float)RAND_MAX; // wish they just had a randf method :/
delta_t = t_scale*floorf((double)arc4random()/ARC4RANDOM_MAX);
#ifdef POLY_DEBUG
CCLOG(#"delta_t %0.2f", delta_t);
#endif
if (delta_t < min_delta_t) {
delta_t = min_delta_t;
}
new_t = cur_t + delta_t;
if (new_t > 2*PI) {
break;
}
t_vec[vec_len] = new_t;
cur_t = new_t;
}
// We need at least three points for a triangle
if ( vec_len > 3 ) {
break;
}
}
At least where the body is being generated.
then...
float num_verts = vec_len;
b2BodyDef BodyDef;
BodyDef.type = b2_staticBody;
BodyDef.position.Set(pos->x/PTM_RATIO, pos->y/PTM_RATIO);
BodyDef.userData = self; // hope this is correct
m_polyBody = world->CreateBody(&BodyDef);
b2PolygonShape polyShape;
int32 polyVert = num_verts;
polyShape.Set(verts, polyVert);
b2FixtureDef FixtureDef;
FixtureDef.shape = &polyShape;
FixtureDef.userData = self; // hope this is correct
FixtureDef.density = 1.6f;
FixtureDef.friction = 0.4f;
FixtureDef.restitution = 0.5f;
m_polyBody->CreateFixture(&FixtureDef);
for (int i=0; i < num_verts; i++) {
// Convert from b2Vec2 to CCPoint and from physics units to pixels
m_verts[i] = ccp(verts[i].x*PTM_RATIO, verts[i].y*PTM_RATIO);
}
m_numVerts = num_verts;
delete verts;
}
-(void) setColor:(ccColor4F) color {
m_color = color;
}
-(BOOL) dirty {
return true;
}
-(void) draw {
//[super draw];
ccDrawPoly(m_verts, m_numVerts, YES);
CCLOG(#"Drawing?");
}
-(CGAffineTransform) nodeToParentTransform {
b2Vec2 pos = m_polyBody->GetPosition();
float x = pos.x * PTM_RATIO;
float y = pos.y * PTM_RATIO;
/*if ( ignoreAnchorPointForPosition_ ) {
x += anchorPointInPixels_.x;
y += anchorPointInPixels_.y;
}*/
// Make matrix
float radians = m_polyBody->GetAngle();
float c = cosf(radians);
float s = sinf(radians);
if( ! CGPointEqualToPoint(anchorPointInPixels_, CGPointZero) ){
x += c*-anchorPointInPixels_.x + -s*-anchorPointInPixels_.y;
y += s*-anchorPointInPixels_.x + c*-anchorPointInPixels_.y;
}
// Rot, Translate Matrix
transform_ = CGAffineTransformMake( c, s,
-s, c,
x, y );
return transform_;
}
there is some stuff in between but its less important. I can post it if asked.
Then the update function, which is based in my game scene class.
-(void)updateObstacles
{
//CCLOG(#"updating obstacles");
int xpos;
int ypos;
CGPoint pos;
for (int i=0; i<MAX_OBSTACLES; i++ ) {
// If there is no obstacle generate a new one
if ( obstacles[i] == NULL ) {
polyObstacleSprite *sprite = [[polyObstacleSprite alloc] init];
ypos = int(_winSize.width/2*(double)arc4random()/ARC4RANDOM_MAX) - _winSize.width/2;
xpos = int(_winSize.height/2*(double)arc4random()/ARC4RANDOM_MAX) - _winSize.height/2;
//CCLOG(#"generating obstacle at %d,%d", xpos, ypos);
pos = ccp(xpos, ypos);
[sprite genBody:_world pos:&pos];
[self addChild:sprite z:1];
obstacles[i] = sprite;
}
//CCLOG(#"position: %d, %d", obstacles[i]->screen, obstacles[i]->position.y); FINISH
}
}
Sorry if its sort of a mess I set this up quick, but pretty much what I want to do is have randomly generated polygons appear at the bottom of my iphone screen as my character moves down with gravity. I got everything else but the polygons working.
Thanks in advance for spending the time to read this.

Converting HSB color to RGB in Objective C

I'm trying to convert HSB values to RGB values using this algorithm, but I am not getting correct values. I have a fixed s of 29, a fixed b of 100, and am generating random integers between 0-360 for the h value, and feeding them into the function to get RGB back:
float h = (arc4random() % 360);
float s = 29;
float b = 100;
HSL2RGB(h, s, b, &red, &green, &blue);
NSLog(#"r:%f g:%f b:%f", red, green, blue);
output:
r:2971.000000 g:2971.000000 b:2971.000000
I tried this too:
float h = (arc4random() % 360)/1000.0;
float s = 0.29;
float b = 1.0;
HSL2RGB(h, s, b, &red, &green, &blue);
NSLog(#"r:%f g:%f b:%f", red, green, blue);
output:
r:1.000000 g:1.000000 b:1.000000
Am I doing something wrong, or is this algorithm messed up?
You can get the RGB components of a color constructed using HSB directly and easily using UIKit.
UIColor *color = [UIColor colorWithHue: hue saturation: saturation
brightness: brightness alpha: alpha];
if ( [color getRed: &red green: &green blue: &blue alpha: &alpha] ) {
// color converted
}
If all you care about is using the color, you can skip the if and just use it.
Per Apple's UIColor documentation:
If the color is in a compatible color space, the color is converted into RGB format and its components are returned to your application. If the color is not in a compatible color space, the parameters are unchanged.
A compatible color space in this case is RGB or HSB.
I don't know your algorithm but I should you to use this algorigthm:
typedef struct
{
double r; // percent [0 - 1]
double g; // percent [0 - 1]
double b; // percent [0 - 1]
double a; // percent [0 - 1]
} RGBA;
typedef struct
{
double h; // angle in degrees [0 - 360]
double s; // percent [0 - 1]
double v; // percent [0 - 1]
} HSV;
- (RGBA)RGBfromHSV:(HSV)value
{
double hh, p, q, t, ff;
long i;
RGBA out;
out.a = 1;
if (value.s <= 0.0) // < is bogus, just shuts up warnings
{
if (isnan(value.h)) // value.h == NAN
{
out.r = value.v;
out.g = value.v;
out.b = value.v;
return out;
}
// error - should never happen
out.r = 0.0;
out.g = 0.0;
out.b = 0.0;
return out;
}
hh = value.h;
if(hh >= 360.0) hh = 0.0;
hh /= 60.0;
i = (long)hh;
ff = hh - i;
p = value.v * (1.0 - value.s);
q = value.v * (1.0 - (value.s * ff));
t = value.v * (1.0 - (value.s * (1.0 - ff)));
switch(i)
{
case 0:
out.r = value.v;
out.g = t;
out.b = p;
break;
case 1:
out.r = q;
out.g = value.v;
out.b = p;
break;
case 2:
out.r = p;
out.g = value.v;
out.b = t;
break;
case 3:
out.r = p;
out.g = q;
out.b = value.v;
break;
case 4:
out.r = t;
out.g = p;
out.b = value.v;
break;
case 5:
default:
out.r = value.v;
out.g = p;
out.b = q;
break;
}
return out;
}
I used it into own of my project and I had no problem with it. :)

How to get pixel color at location from UIimage scaled within a UIimageView

I'm currently using this technique to get the color of a pixel in a UIimage. (on Ios)
- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;
CGImageRef inImage = self.image.CGImage;
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; /* error */ }
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage);
// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
//offset locates the pixel in the data from x,y.
//4 for 4 bytes of data per pixel, w is width of one row of data.
int offset = 4*((w*round(point.y))+round(point.x));
int alpha = data[offset];
int red = data[offset+1];
int green = data[offset+2];
int blue = data[offset+3];
NSLog(#"offset: %i colors: RGB A %i %i %i %i",offset,red,green,blue,alpha);
color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data) { free(data); }
return color;
}
As illustrated here;
http://www.markj.net/iphone-uiimage-pixel-color/
it works quite well, but when working with images larger than the UIImageView it fails. I tried adding an image and changing the scaling mode to fit the view. How would I modify the code to so that it would still be able to sample the pixel color with a scaled image.
try this for swift3
func getPixelColor(image: UIImage, x: Int, y: Int, width: CGFloat) -> UIColor
{
let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)
let pixelInfo: Int = ((Int(width) * y) + x) * 4
let r = CGFloat(data[pixelInfo]) / CGFloat(255.0)
let g = CGFloat(data[pixelInfo+1]) / CGFloat(255.0)
let b = CGFloat(data[pixelInfo+2]) / CGFloat(255.0)
let a = CGFloat(data[pixelInfo+3]) / CGFloat(255.0)
return UIColor(red: r, green: g, blue: b, alpha: a)
}
Here's a pointer:
0x3A28213A //sorry, I couldn't resist the joke
For real now: after going through the comments on the page at markj.net, a certain James has suggested to make the following changes:
size_t w = CGImageGetWidth(inImage); //Written by Mark
size_t h = CGImageGetHeight(inImage); //Written by Mark
float xscale = w / self.frame.size.width;
float yscale = h / self.frame.size.height;
point.x = point.x * xscale;
point.y = point.y * yscale;
(thanks to http://www.markj.net/iphone-uiimage-pixel-color/comment-page-1/#comment-2159)
This didn't actually work for me... Not that I did much testing, and I'm not the world's greatest programmer (yet)...
My solution was to scale the UIImageView in such a way that each pixel of the image in it was the same size as a standard CGPoint on the screen, then I took my color like normal (using getPixelColorAtLocation:(CGPoint)point) , then I scaled the image back to the size I wanted.
Hope this helps!
Use the UIImageView Layer:
- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
UIColor* color = nil;
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef cgctx = UIGraphicsGetCurrentContext();
if (cgctx == NULL) { return nil; /* error */ }
[self.layer renderInContext:cgctx];
unsigned char* data = CGBitmapContextGetData (cgctx);
/*
...
*/
UIGraphicsEndImageContext();
return color;
}

Bezier curve algorithm in objective-c needs a tweak

I asked a quick question Bezier curve algorithm in objective-c that seemed to solve my issues. I'm asking this new question as I think its different enough rather than repurposing the old one.
I have what seems like a working Bezier Curve algorithm but when compared to built in NSBezierPath version there are some major issues. It looks as though certain types of curves are very much distorted.
You can see from the above image the differences, the red line is my function and the lighter color is the built in version. I am not expecting and exat match, pixel for pixel, but as you can see the red lines go way off course at times.
The first method I'm listing is what calls the 2 Bezier methods, it shows that the inputs are the same to both versions.
- (void)MakeBezier
{
int x1 = [self getMegaNumber:2];
int y1 = self.frame.size.height - [self getMegaNumber:2];
int x2 = [self getMegaNumber:2];
int y2 = self.frame.size.height - [self getMegaNumber:2];
int x3 = [self getMegaNumber:2];
int y3 = self.frame.size.height - [self getMegaNumber:2];
int x4 = [self getMegaNumber:2];
int y4 = self.frame.size.height - [self getMegaNumber:2];
int cnt = [self getMegaNumber:2];
NSBezierPath *bezierPath = [[NSBezierPath alloc] init];
[bezierPath setLineWidth:1.0f];
[bezierPath moveToPoint:NSMakePoint(x1, y1)];
[bezierPath curveToPoint:NSMakePoint(x4, y4) controlPoint1:NSMakePoint(x2, y2) controlPoint2:NSMakePoint(x3, y3)];
// Draw path to image with build in NSBezierPath
[self drawPath:bezierPath fill:NO];
// Draw path with custom algorithm
[self drawBezierFrom:NSMakePoint(x1, y1) to:NSMakePoint(x4, y4) controlA:NSMakePoint(x2, y2) controlB:NSMakePoint(x3, y3) sections:cnt color:4];
}
This next method is the custom algorithm thats used to draw the red lines in the sample image.
- (void)drawBezierFrom:(NSPoint)from to:(NSPoint)to controlA:(NSPoint)a controlB:(NSPoint)b sections:(NSUInteger)cnt color:(NSUInteger)color
{
float qx, qy;
float q1, q2, q3, q4;
int lastx = - 1, lasty;
int plotx, ploty;
float t = 0.0;
while (t <= 1)
{
q1 = t*t*t*-1 + t*t*3 + t*-3 + 1;
q2 = t*t*t*3 + t*t*-6 + t*3;
q3 = t*t*t*-3 + t*t*3;
q4 = t*t*t;
qx = q1*from.x + q2*a.x + q3*to.x + q4*b.x;
qy = q1*from.y + q2*a.y + q3*to.y + q4*b.y;
plotx = round(qx);
ploty = round(qy);
if (lastx != -1)
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(plotx, ploty) color:color];
else
[self drawLineFrom:NSMakePoint(from.x, from.y) to:NSMakePoint(plotx, ploty) color:color];
lastx = plotx;
lasty = ploty;
t = t + (1.0/(cnt + 0.0f));
}
[self drawLineFrom:NSMakePoint(lastx, lasty) to:NSMakePoint(to.x, to.y) color:color];
}
So my question is; is the custom algorithm way off or is it just missing an edge case for specific kinds of lines, or something else? Either way any help in fixing the algorithm would be very appreciated. To reiterate, I am not looking for a pixel perfect match, but I am expecting the curves to lineup together.
Looking at the Wikipedia page here it seems that your coefficients for the q1-q4 are incorrect. Shouldn't they be symmetric?
It also seems that to.x and b.x should be swapped:
qx = q1*from.x + q2*a.x + q3*to.x + q4*b.x;
qy = ...

How to draw circular bars?

I am devleoping a game with cocos2d-iphone.
I want to great a circular health bar. Think of Kingdom Hearts or something.
I am able to draw circles with ccDrawLine, but they are full circles. Basically, I need to be able to draw up to a certain circumference value to represent the health properly. However, I am not really sure about this. Any ideas?
I had a quick look at the code for ccDrawCircle. If I was approaching this, I'd probably start by modifying the way the for loop works (maybe by playing with coef or segs) so that it stops early.
void ccDrawCircle( CGPoint center, float r, float a, NSUInteger segs, BOOL drawLineToCenter)
{
int additionalSegment = 1;
if (drawLineToCenter)
additionalSegment++;
const float coef = 2.0f * (float)M_PI/segs;
GLfloat *vertices = calloc( sizeof(GLfloat)*2*(segs+2), 1);
if( ! vertices )
return;
for(NSUInteger i=0;i<=segs;i++)
{
float rads = i*coef;
GLfloat j = r * cosf(rads + a) + center.x;
GLfloat k = r * sinf(rads + a) + center.y;
vertices[i*2] = j * CC_CONTENT_SCALE_FACTOR();
vertices[i*2+1] =k * CC_CONTENT_SCALE_FACTOR();
}
vertices[(segs+1)*2] = center.x * CC_CONTENT_SCALE_FACTOR();
vertices[(segs+1)*2+1] = center.y * CC_CONTENT_SCALE_FACTOR();
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);
// restore default state
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);
free( vertices );
}
CGContextAddArc() will do the trick. An example explains everything.
CGContextAddArc(CGFloat centerX, CGFloat centerY, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise);
I'm not quite sure about the order of the parameters, you'd better google it or let XCode do the work for you.