2012-04-11 8 views
0

特定の点から特定の距離だけ離れた直線上の点を見つける方法を教えてください。私はこのコードをC言語で書いていますが、正しい答えは得られません。私が間違っていることについて誰かが私を導くことができますか?直線上の特定の距離をCで表します

私はx1、y1、x2、y2の値を得て、距離はきれいに残っています。これらを使って、傾きmとy切片もうまく見つけることができます。 ここでは、ポイントx1、y1から10単位離れたこれら2つのポイントを結ぶ直線上のポイントを見つける必要があります。私はここで間違っているようです。ここに私が書いたコードがあります。ここで

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)と別の1 (x2,y2)を持っているC.

で何かを書くための時間を持っていないです。

したがって、方向ベクトルv=(xv, yv)は、xv=x2-x1yv=y2-y1です。

今、あなたはその規範により、このベクトルを分割する必要がある、あなたは新しいベクトルを取得:ベクトル= V/SQRT(XV + YV)

さて、あなたは自分の原点にあなたがあなたのポイントを先に距離を乗じたベクトル追加する必要があります。

位置=(X原点、yの原点)+距離×ベクトル

私はこれが助けてくれることを願っています!

+0

が同意= Dきました。私は本当にここにループが必要とは思わない。これはアルゴリズムではなく、代数です。 – 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

関連する問題