imbear

draw something

Jan 4th, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         if (point1.x > -1) {
  2.             double x0 = (point0.x > -1) ? point0.x : point1.x; //after 4 touches we should have a back anchor point, if not, use the current anchor point
  3.             double y0 = (point0.y > -1) ? point0.y : point1.y; //after 4 touches we should have a back anchor point, if not, use the current anchor point
  4.             double x1 = point1.x;
  5.             double y1 = point1.y;
  6.             double x2 = point2.x;
  7.             double y2 = point2.y;
  8.             double x3 = point3.x;
  9.             double y3 = point3.y;
  10.             // Assume we need to calculate the control
  11.             // points between (x1,y1) and (x2,y2).
  12.             // Then x0,y0 - the previous vertex,
  13.             //      x3,y3 - the next one.
  14.            
  15.             double xc1 = (x0 + x1) / 2.0;
  16.             double yc1 = (y0 + y1) / 2.0;
  17.             double xc2 = (x1 + x2) / 2.0;
  18.             double yc2 = (y1 + y2) / 2.0;
  19.             double xc3 = (x2 + x3) / 2.0;
  20.             double yc3 = (y2 + y3) / 2.0;
  21.            
  22.             double len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
  23.             double len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
  24.             double len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
  25.            
  26.             double k1 = len1 / (len1 + len2);
  27.             double k2 = len2 / (len2 + len3);
  28.            
  29.             double xm1 = xc1 + (xc2 - xc1) * k1;
  30.             double ym1 = yc1 + (yc2 - yc1) * k1;
  31.            
  32.             double xm2 = xc2 + (xc3 - xc2) * k2;
  33.             double ym2 = yc2 + (yc3 - yc2) * k2;
  34.             double smooth_value = 0.9;
  35.             // Resulting control points. Here smooth_value is mentioned
  36.             // above coefficient K whose value should be in range [0...1].
  37.             float ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
  38.             float ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
  39.            
  40.             float ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
  41.             float ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
  42.            
  43.            
  44.            
  45.            
  46.             UIGraphicsBeginImageContext(drawImage.frame.size);
  47.             //Albert Renshaw - Apps4Life
  48.             [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
  49.             UIColor *color = [UIColor colorWithHue:.5 saturation:0.7 brightness:1.0 alpha:.5];
  50.             CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
  51.             CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
  52.             //CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, .5); //values for R, G, B, and Alpha
  53.             CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [color CGColor]);
  54.             CGContextBeginPath(UIGraphicsGetCurrentContext());
  55.             //CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
  56.             CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1.x, point1.y);
  57.             //CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
  58.             CGContextAddCurveToPoint(UIGraphicsGetCurrentContext(), ctrl1_x, ctrl1_y, ctrl2_x, ctrl2_y, point2.x, point2.y);
  59.             CGContextStrokePath(UIGraphicsGetCurrentContext());
  60.             drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
  61.             UIGraphicsEndImageContext();
  62.             int offa = 100;
  63.             int offb = 200;
  64.             CGRect dirtyPoint1 = CGRectMake(point1.x-offa, point1.y-offa, offb, offb);
  65.             CGRect dirtyPoint2 = CGRectMake(point2.x-offa, point2.y-offa, offb, offb);
  66.             [self setNeedsDisplayInRect:CGRectUnion(dirtyPoint1, dirtyPoint2)];
  67.         }
Advertisement
Add Comment
Please, Sign In to add comment