How to create random closed smooth CGPath?

I am trying to find a way to create a random closed smooth path ( CGPathor UIBezierPath). I read about De Castellau 's algorithm and tons of other articles about Bezier paths, but it doesn't seem to be suitable for what I'm trying to achieve.

I was thinking of creating a CGPath circle. Then I would multiply the path by a function that would distort the positions of the points, for example, sine or cosine. However, I do not know if this direction is right, because the path would not have a random shape.

CGMutablePathRef circle = CGPathCreateMutable();
CGPathAddArc(circle, nil, 0.0f, 0.0f, 100.0f, 2 * M_PI, 0.0f, true);
...
CGPathRelease(circle);

It would be great if someone could tell me in the right direction how to start its implementation. An example of the path I'm trying to generate:

enter image description here

+4
source share
2 answers

What you painted looks like a distorted circle.

Assuming you are after this, here is what I will do:

Enter a code that sets the angle from 0 to 2pi for a fixed number of steps. (Try 8). The angle depends on a small random amount of less than ± pi / steps.

Choose a radius of the base that is slightly less than 1/2 the length of the side of the closing square, so there is a place for your points to fall in or out of the radius of the base without going beyond the bounding square. Try 3/8 of the length of your frame.

For each slightly randomized value of the angle along the circle, calculate the radius value, the radius of the base - a random value from 0 to the base radius / 2.

x y .

. , 8- , .

Catmull-Rom, .

EDIT: github RandomBlobs, , , :

3x3 . . 8 . / ( ). UIBezierPath, 8 . Catmull-Rom, .

, , :

, , . . , Catmull-Rom, , . , , . , , "" ( , .)


RandomBlobs. , , . , , .

-, blob ( , ): Circle-based blob - :

-, blob , :

Square-based blob

. ( ).

+6

, ... , ;-D Hop .

//
//  DrawView.h
//  test
//
//  Created by Armand DOHM on 03/03/2014.
//
//

#import <UIKit/UIKit.h>

@interface DrawView : UIView

@end


//
//  DrawView.m
//  test
//
//  Created by Armand DOHM on 03/03/2014.
//
//

#import "DrawView.h"
#import <math.h>

@implementation DrawView

- (void)drawRect:(CGRect)rect
{
    float r; //radius
    float rmax = MIN(rect.size.height,rect.size.width) * .5; //max radius
    float rmin = rmax * .1; //min radius

    NSMutableArray *points = [[NSMutableArray alloc] init];

    /*cut a circle into x pies. for each of this pie take a point at a random radius
     link all of this point with quadcurve*/

    for (double a=0;a < 2 * M_PI;a += M_PI / 10) {
        r = rmin +  ((arc4random_uniform((int)(rmax - rmin) * 100)) / 100.0f);
        CGPoint p = CGPointMake((rect.size.width / 2) + (r * cos (a)) , (rect.size.height / 2) + (r * sin (a)));
        [points addObject:[NSValue valueWithCGPoint:p]];
    }


    UIBezierPath *myPath=[[UIBezierPath alloc]init];
    myPath.lineWidth=2;
    [myPath strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];

    r = rmin +  ((arc4random_uniform((int)(rmax - rmin) * 100)) / 100.0f);
    [myPath moveToPoint:CGPointMake((rect.size.width / 2) + (r * cos (0)) , (rect.size.height / 2) + (r * sin (0)))];


    for (int i = 0; i < points.count; i+=2) {
        NSValue *value = [points objectAtIndex:i];
        CGPoint p1 = [value CGPointValue];
        value = [points objectAtIndex:(i+1)];
        CGPoint p2 = [value CGPointValue];
        [myPath addQuadCurveToPoint:p2 controlPoint:p1];
    }


    [myPath closePath];

    [myPath stroke];
}

@end
+1

Source: https://habr.com/ru/post/1529874/


All Articles