Loading

Thursday, July 15, 2010

Smooth Line Eraser From View

UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [touch locationInView:self.view];
CGPoint currentPoint = [touch locationInView:self.view];
CGContextRef Mycontext=UIGraphicsGetCurrentContext();

UIGraphicsBeginImageContext(Image_Cookie.frame.size);
[Image_Cookie.image drawInRect:CGRectMake(0, 0, Image_Cookie.frame.size.width, Image_Cookie.frame.size.height)];
CGContextSetLineCap(Mycontext,kCGImageAlphaPremultipliedLast); //kCGImageAlphaPremultipliedLast);
CGContextSetLineWidth(Mycontext, 10);
CGContextSetRGBStrokeColor(Mycontext, 1, 0, 0, 20);
CGContextBeginPath(Mycontext);
CGContextMoveToPoint(Mycontext, lastPoint.x, lastPoint.y);

int x, cx, deltax, xstep,y, cy, deltay, ystep,error, st, dupe;
int x0, y0, x1, y1;

x0 = currentPoint.x;
y0 = currentPoint.y;
x1 = lastPoint.x;
y1 = lastPoint.y;

// find largest delta for pixel steps
st = (abs(y1 - y0) > abs(x1 - x0));

// if deltay > deltax then swap x,y
if (st) {
(x0 ^= y0); (y0 ^= x0); (x0 ^= y0); // swap(x0, y0);
(x1 ^= y1); (y1 ^= x1); (x1 ^= y1); // swap(x1, y1);
}

deltax = abs(x1 - x0);
deltay = abs(y1 - y0);
error = (deltax / 2);
y = y0;

if (x0 > x1) { xstep = -1; }
else { xstep = 1; }

if (y0 > y1) { ystep = -1; }
else { ystep = 1; }

for ((x = x0); (x != (x1 + xstep)); (x += xstep))
{
(cx = x); (cy = y); // copy of x, copy of y

// if x,y swapped above, swap them back now
if (st) { (cx ^= cy); (cy ^= cx); (cx ^= cy); }

(dupe = 0); // initialize no dupe

if(!dupe) {
CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(cx, cy,40, 40));
}

(error -= deltay); // converge toward end of line

if (error < 0) { // not done yet
(y += ystep);
(error += deltax);
}
}

CGContextStrokePath(Mycontext);
Image_Cookie.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;

No comments:

Post a Comment