2

私はいくつかの uibezierpaths を持っています。パスとして、実際には厚みがありません。しかし、この写真の線の周りの灰色がかった領域のように、パスの周りの領域を定義する方法を見つけたいと思っていますここに画像の説明を入力

基本的に、描画された線が線の周りのバッファー ゾーン内に収まるかどうかをテストしたいと考えています。これは簡単だと思っていましたが、思ったよりもはるかに複雑であることがわかりました。CGPathApply 関数を使用してパスに沿ったポイントを調べ、各ポイントの範囲 + または - を取得できますが、角度や曲線の場合よりも複雑です。何か案は?

4

2 に答える 2

1

パスの幅を広げることは、実際には非常に困難です。ただし、太い幅でストロークするだけで、ほぼ同じ効果が得られます。何かのようなもの...

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];

...質問のような画像が生成されます。しかし、それがあなたにとってのニュースだとは思えません。

本当のトリックは、「描画された線がバッファー ゾーン内にあるかどうか」のテストです。その問題は、私が別の質問で自分自身のために答えた問題と非常によく似ています。そこで共有したLineSample.zipコードを見てください。これは、ビットマップ/ビットごとのデータ比較を実装して、必要に応じて行のヒットを検出します。テスト用に太い「バッファ」パスをビットマップに描画し、ビューに細い線を表示するだけです。

于 2011-06-29T22:13:16.923 に答える
0

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!*/
        }
    }

}
于 2011-11-01T17:36:03.627 に答える