2012-04-11 3 views
0

주어진 점으로부터 특정 거리 떨어져있는 직선상의 점을 어떻게 찾을 수 있습니까? 나는이 코드를 C로 작성하고 있지만 올바른 답을 얻지 못한다. 내가 잘못하고있는 것에 대해 나를 안내 해주는 사람이있을 수 있겠는가?C에서 직선 특정 거리를 가리킨다

나는 x1, y1, x2, y2 값을 얻고 거리는 괜찮습니다. 이것들을 사용하여 기울기 m과 y- 절편도 잘 찾을 수 있습니다. 이제 x1, y1 지점에서 10 단위 떨어져있는이 두 지점을 연결하는 직선상의 지점을 찾아야합니다. 나는 여기서 잘못 가고있는 것 같다. 여기 제가 작성한 코드가 있습니다. 여기

int x1 = node[n].currentCoordinates.xCoordinate; 
int y1 = node[n].currentCoordinates.yCoordinate; 
int x2 = node[n].destinationLocationCoordinates.xCoordinate; 
int y2 = node[n].destinationLocationCoordinates.yCoordinate; 

int distanceleft = (y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1); 
distanceleft = sqrt(distanceleft); 
printf("Distance left to cover is %d\n",distanceleft); 
int m = (y2 - y1)/(x2 - x1); // slope. 
int b = y1 - m * x1; //y-intercept 


//find point on the line that is 10 units away from 
//current coordinates on equation y = mx + b. 
if(x2 > x1) 
{ 
    printf("x2 is greater than x1\n"); 
    int tempx = 0; 
    int tempy = 0; 
    for(tempx = x1; tempx <= x2; tempx++) 
    { 
      tempy = y1 + (y2 - y1) * (tempx - x1)/(x2 - x1); 
      printf("tempx = %d, tempy = %d\n",tempx,tempy); 
      int distanceofthispoint = (tempy - y1) * (tempy - y1) + (tempx - x1) * (tempx - x1); 
      distanceofthispoint = sqrt((int)distanceofthispoint); 
      if(distanceofthispoint >= 10) 
      { 
       //found new points. 
       node[n].currentCoordinates.xCoordinate = tempx; 
       node[n].currentCoordinates.yCoordinate = tempy; 
       node[n].TimeAtCurrentCoordinate = clock; 
       printf("Found the point at the matching distance\n"); 
       break; 
      } 
    } 
} 
else 
{ 
    printf("x2 is lesser than x1\n"); 
    int tempx = 0; 
    int tempy = 0; 
    for(tempx = x1; tempx >= x2; tempx--) 
    { 
      tempy = y1 + (y2 - y1) * (tempx - x1)/(x2 - x1); 
      printf("tempx = %d, tempy = %d\n",tempx,tempy); 
      int distanceofthispoint = (tempy - y1) * (tempy - y1) + (tempx - x1) * (tempx - x1); 
      distanceofthispoint = sqrt((int)distanceofthispoint); 
      if(distanceofthispoint >= 10) 
      { 
       //found new points. 
       node[n].currentCoordinates.xCoordinate = tempx; 
       node[n].currentCoordinates.yCoordinate = tempy; 
       node[n].TimeAtCurrentCoordinate = clock; 
       printf("Found the point at the matching distance\n"); 
       break; 
      } 
    } 
} 
printf("at time %f, (%d,%d) are the coordinates of node %d\n",clock,node[n].currentCoordinates.xCoordinate,node[n].currentCoordinates.yCoordinate,n); 

답변

7

은 수학에 어떻게, 내가 당신에게 세그먼트를 제공 연결 할 때, 당신은 점 (x1,y1)하고 다른 (x2,y2)이 C.

뭔가를 쓸 시간이없는 것입니다.

따라서 방향 벡터 v=(xv, yv)xv=x2-x1yv=y2-y1입니다.

지금, 당신은 그것의 규범에 의해이 벡터를 분할해야, 당신은 새로운 벡터를 얻을 : 벡터 = V/SQRT (XV 2 + 2 YV).

이제, 당신은 단지 거리를 곱한 벡터하는 당신이 당신의 점하려는 원점에 추가해야합니다 :

위치를 = (X 원점, Y 원점) + 거리 × 벡터를

이 도움이 되었기를 바랍니다.

+0

동의 거라고있다. 나는 네가 정말로 여기 루프를 필요로하지 않는다고 생각한다. 이것은 알고리즘이 아니라 대수학입니다. – FlavorScape

+0

벡터 방정식의 마지막 v가 sqrt 아래에 있거나 분수 또는 분수 바깥에 있어야합니까? 당신은 괄호를 닫는 것을 잊었습니다 :) –

+0

오, 괜찮습니다. 이해 했어. 그것은 바깥 쪽입니다. v /를하는 대신 v에 1/..을 곱하면됩니다. –

0

또는 간단

기울기 당신은 경사의 분자와 분모를 기반으로 θ의 사분면을 수정 할 수 있습니다

θ = arctan(y2-y1/x2-x1) 

에서 각도를 찾을 수 있습니다. 그런 다음이 경우 (X1, Y1)

x_new = x1 + d×cos(θ) 
y_new = y1 + d×sin(θ) 

로부터 거리 (d)의 라인의 어느 지점을 찾을 수는 10 =

관련 문제