2014-03-06 3 views
-2

나는 네 점이있다. _pointA_pointB_pointC_pointD. 현재 지점에서 가장 가까운 지점을 찾고 싶습니다. 나는 이와 같은 것을 가졌지 만 때로는 잘못된 결과를줍니다.다른 점에서 가장 가까운 점을 계산하는 가장 좋은 방법은 무엇입니까?

문제 : 내가 근처 pointA에 당신이 준 점으로 pointC

CGPoint neastPoint=CGPointZero; 
for (int i=0; i<3; i++) { 
    CGFloat x; 
    CGFloat y; 
    CGFloat currntDistance; 
    if (i==0){ 
     x=(pointA.x-currentPoint.x) *(pointA.x-currentPoint.x); 
     y=(pointA.y-currentPoint.y)*(pointA.y-currentPoint.y); 
     currntDistance =sqrtf(x+y); 
     distance=currntDistance; 
     neastPoint=pointA; 

    } 
    else if (i==1){ 
     x=(pointB.x-currentPoint.x) *(pointB.x-currentPoint.x); 
     y=(pointB.y-currentPoint.y)*(pointB.y-currentPoint.y); 
     currntDistance =sqrtf(x+y); 
     if (distance>currntDistance) { 
      distance=currntDistance; 
      neastPoint=pointB; 
     } 
    } 
    else if (i==2){ 
     x=(pointC.x-currentPoint.x) *(pointC.x-currentPoint.x); 
     y=(pointC.y-currentPoint.y)*(pointC.y-currentPoint.y); 
     currntDistance =sqrtf(x+y); 
     if (distance>currntDistance) { 
      distance=currntDistance; 
      neastPoint=pointC; 
     } 
    } 
    else { 
     x=(pointD.x-currentPoint.x) *(pointD.x-currentPoint.x); 
     y=(pointD.y-currentPoint.y)*(pointD.y-currentPoint.y); 
     currntDistance =sqrtf(x+y); 
     if (distance>currntDistance) { 
      distance=currntDistance; 
      neastPoint=pointD; 
     } 

    } 



CurrentPoint : {44, 33.140846} 
Point A : {71, 178} 
Point B : {134, 178} 
Point C : {133, 71} 
Point D : {75, 67} 
Nearast Point : {133, 71} 
+0

귀하의 코드는 (당신이'for' 모든'if' 제거 할 수있는 매우 비효율적이다 배열을 사용하면 더 좋을 것입니다.)하지만 작동해야합니다. 정확히 무슨 문제입니까? – Merlevede

+0

if-block body를 공통 함수로 캡슐화하는 것이 좋습니다. 그런 다음 포인트를 목록에 넣고 반복하여 최소 거리의 포인트를 찾습니다. 그것은 아마 비록 작동합니다. –

+2

pointA.y 대신 첫 번째 if 문에서 pointB.y를 사용하고 있습니다. 여기에 오류가 있습니까? 아니면 코드에도 마찬가지입니까? – Logan

답변

3

를 제공 할 때, pointDcurrentPoint에 가장 가까운 것입니다.

for (int i=0; i<3; i++) { 

for (int i=0; i<4; i++) { 

모두 4 점을 확인해야하기 때문에 그것은 당신의 코드에 의해 찾을 수 없습니다. 다른 사람이 눈치 챘을 때, 당신은 예를 들어 , 코드를 단순화 할 수

CGFloat distance = FLT_MAX; // start with some large value 
CGPoint nearestPoint; 

CGPoint points[] = { pointA, pointB, pointC, pointD }; 
for (int i = 0; i < 4; i++) { 
    CGFloat currentDistance = hypotf(points[i].x - currentPoint.x, points[i].y - currentPoint.y); 
    if (currentDistance < distance) { 
     distance = currentDistance; 
     nearestPoint = points[i]; 
    } 
} 
+0

yaaa 맞아요.하지만 C가 돌아 오면 C가 이해할 수없는 코드에 문제가 생길 수 있습니다. –

+0

@Sunnyshah : * * 값을 가진 코드를 테스트했으며, 가장 가까운 pointD를 반환했습니다. currentPoint. –

+0

괜찮습니다. –

1

의사 :

Point[] allPoints = {poinA,pontB,pointC,....,pointN} 

int distance = Int.MAX_VALUE; 
Point nearestPoint = null; 

for(int i = 0 ; i < allPoints.count;i++){ 
    if(pointA != allPoints[i]){ 
     int currentDistance = getDistance(pointA,allPoints[i]); 
     if(currentDistance < distance){ 
      distance = currentDistance; 
      nearestPoint = allPoints[i]; 
     } 
    } 
    } 

print("Nearest point is " + nearestPoint); 
+0

어디 getDistanse() 선언은 무엇입니까? 또한 정수를 사용하는 것은 아마도 포인트가 가까운 경우에 좋은 생각이 아닙니다. – Logan

+0

@Logan, 그냥 가짜입니다. QA alredy는'getDistance()'구현 방법을 알고 있습니다. (몇 번 복제) 질문에서 볼 수 있습니다. –

+0

아, 이제 알겠습니다. 그 문제를 해결해 주셔서 감사합니다! – Logan

관련 문제