2016-09-02 13 views
-1

私は、CGポイントの配列を持っています。CGPoint interpolation by jnfisherを使用してCatmull-Romベジエパスを描きました。ユーザーがパス上の任意の点を囲む範囲のどこかにタッチすると、タッチポイントのパス上の最も近いポイントを選択します。この範囲は、X軸とY軸に沿ったパス上のすべての点の前後に許容値を持つパス上の点を含む虚数四角で指定します。私の戦略は、1.タッチの位置が正方形の内側にあるかどうかを確認するパス上の各点の許容値をX Axis上で設定します。(discoveryPoint.x - discoveryPoint.x + toleranceに対する許容値)軸:(discoveryPoint.y - discoveryPoint.y +公差に対する許容差)2.タッチされた点と、その配列内の四角形の中にタッチされた点を含むすべての点との間の距離を計算する3.距離から最小量を選択する。 4.タッチ点から最低の距離を持っているポイントを見つける。任意のより簡単かつ効率的な方法はありますか?私は非常に事前に任意のhelp.Thanksに感謝。範囲の中の接触点までの点の集合から最も近い点を効率的に見つける方法はありますか?

-(void)drawRect:(CGRect)rect{ 
      self.modifiablePath = [UIBezierPath interpolateCGPointsWithCatmullRom:self.pointsArray closed:YES alpha:0.9]; 
      self.modifiablePath.lineWidth = 20.0; 
      [[UIColor yellowColor] setStroke]; 
      [self.modifiablePath stroke]; 
    } 
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
      { 

     CGPoint touchedPoint = [[touches anyObject]locationInView:self]; 
     CGPoint discoveryPoint; 
     CGFloat tolerance = 20; 
     int numOfPointsWithMinDistance=0; 
     NSDictionary* pointsWithMinDistanceWithKeys = [NSDictionary new]; 
     NSDictionary* minDistancesWithKeys = [NSDictionary new]; 
     // Had to Create these two arrays because there's no such thing as [NSDictionary objectAtIndex:] 
     NSArray *pointsWithMinDistancesArray = [NSArray new]; 
     NSArray *minDistanceArray = [NSArray new]; 

     for (NSValue * cgpointVal in self.pointsArray){ 
      discoveryPoint = cgpointVal.CGPointValue; 

      if (fabs(touchedPoint.x - discoveryPoint.x)<tolerance && fabs(touchedPoint.y - discoveryPoint.y)<tolerance) { 
       numOfPointsWithMinDistance++; 
       //Adding the points which touchedPoint is inside their range(Square). 
       [pointsWithMinDistanceWithKeys setValue:[NSValue valueWithCGPoint:discoveryPoint] forKey:[NSString stringWithFormat:@"%d",numOfPointsWithMinDistance]]; 

       //Calculating the distance between points with touchedPoint in their range(Square) and adding them to an array. 
       CGFloat distance = hypotf(touchedPoint.x - discoveryPoint.x, touchedPoint.y - discoveryPoint.y); 
       [minDistancesWithKeys setValue:[NSNumber numberWithFloat:distance] forKey:[NSString stringWithFormat:@"%d",numOfPointsWithMinDistance]]; 
      } 
     } 
     //Finding the key for minimum distance between all distances to the touchedPoint. 
     minDistanceArray = [minDistancesWithKeys allValues]; 
     pointsWithMinDistancesArray = [pointsWithMinDistanceWithKeys allValues]; 

     CGFloat k = [[minDistanceArray objectAtIndex:0] floatValue]; 
     int keyOfPointWithMinDistance =0; 
     for (unsigned i = 1; i <[minDistanceArray count]; i++){ 

      if([[minDistanceArray objectAtIndex:i] floatValue] < k){ 
       k = [[minDistanceArray objectAtIndex:i] floatValue]; 
       keyOfPointWithMinDistance=i; 
      }else{ 
       keyOfPointWithMinDistance=0; 
      } 
     } 
     //Getting the nearest CGPoint value with the smallest distance to the touchedPoint. 
     NSValue * valueOfNearestPoint =[pointsWithMinDistancesArray objectAtIndex:keyOfPointWithMinDistance]; 
     CGPoint nearestPointToTouchedPoint =valueOfNearestPoint.CGPointValue; 
} 

答えて

0
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    CGPoint touchedPoint = [[touches anyObject]locationInView:self]; 
    CGPoint discoveryPoint; 
    CGFloat tolerance = 20; 
    // Had to Create these two arrays because there's no such thing as [NSDictionary objectAtIndex:] 
    NSArray *pointsArray; 

    CGFloat keyOfPointWithMinDistance = -1; 
    CGPoint nearestPointToTouchedPoint = CGPointZero; 
    Int index = 0; 
    Int keyIndex = NSNotFound; 

    for (NSValue * cgpointVal in self.pointsArray){ 

     discoveryPoint = cgpointVal.CGPointValue; 

     if (fabs(touchedPoint.x - discoveryPoint.x)<tolerance && fabs(touchedPoint.y - discoveryPoint.y)<tolerance) { 
      //Calculating the distance between points with touchedPoint in their range(Square) and adding them to an array. 
      CGFloat distance = hypotf(touchedPoint.x - discoveryPoint.x, touchedPoint.y - discoveryPoint.y); 

      if (keyOfPointWithMinDistance == -1 || keyOfPointWithMinDistance < distance) { 
       keyOfPointWithMinDistance = distance; 
       nearestPointToTouchedPoint = discoveryPoint; 
       keyIndex = index; 
      } 

      index++; 
     } 
    } 

    //Update self.pointsArray using keyIndex 

    if keyIndex != NSNotFound { 

    } 

} 
+1

それは働いた!ありがとうございました! –

+0

実際、私にはNSDictionaryを使用せずにコードを使用して達成するにはどうしたらよいですか? –

+1

私の答えを更新しました。 –