开发者

how to get buffer zone around a uibezierpath

开发者 https://www.devze.com 2023-03-16 08:11 出处:网络
i have some uibezierpaths. as paths, they don\'t really have thickness. but, I am hoping to find a way to define an area around a path like the grayish areas around the lines in this picture

i have some uibezierpaths. as paths, they don't really have thickness. but, I am hoping to find a way to define an area around a path like the grayish areas around the lines in this picture

how to get buffer zone around a uibezierpath

basically, i want to test whether drawn lines fall within the buffer zone around the lines. i thought this would be simple, but it's turning out to be much more complex than i thought. I can use the CGPathApply function to examine the points along my path, and then getting a range +or- each point, but it'开发者_JS百科s more complicated than that with angles and curves. any ideas?


Expanding the width of a path is actually quite difficult. However, you could just stroke it with a thicker width and get pretty much the same effect. Something like...

CGContextSetRGBStrokeColor(context, 0.4, 0.4, 0.4, 1.0);
[path setLineWidth:15];
[path stroke];
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
[path setLineWidth:3];
[path stroke];

...would produce a picture like the one in your question. But I doubt that's news to you.

The real trick is the test of "whether drawn lines fall within the buffer zone." That problem is very similar to one which I just answered for myself in another question. Take a look at the LineSample.zip code I shared there. This implements a bitmap/bitwise data comparison to detect hits on lines much like you need. You could just draw the thicker "buffer" paths into the bitmap for testing and show the thinner lines in your view.


Basically, you want to check if any point falls inside a region of specified size around your path.

It is actually very simple to do. First, you need a value which will define the amount of space around path you want to test. Let's say 20 points. So what you need to do is start a FOR loop, starting from -20 to 20, and at each iteration, create a copy of your path, translate the path's x and y co-odrinates, check each of them.

All of this is more clear in this code sample.

CGPoint touchPoint = /*get the point*/;

NSInteger space = 20;

for (NSInteger i = -space; i < space; i++) {

    UIBezierPath *pathX = [UIBezierPath bezierPathWithCGPath:originalPath.CGPath];
    [pathX applyTransform:CGAffineTransformMakeTranslation(i, 0)];

    if ([pathX containsPoint:touchPoint]) {
        /*YEAH!*/
    }

    else {
        UIBezierPath *pathY = [UIBezierPath bezierPathWithCGPath:originalPath.CGPath];
        [pathY applyTransform:CGAffineTransformMakeTranslation(0, i)];

        if ([pathY containsPoint:touchPoint]) {
            /*YEAH!*/
        }
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号