C ++中的距离公式 [英] Distance Formula in C++
问题描述
您好,
在我的代码中,我试图找到两点之间的距离
(X1,Y1)& (X2,Y2)
这些数字来自代码的第一部分
我正确地获得了第一个距离,但它不断重复,我不知道为什么
任何建议都表示赞赏
Hello,
In my code I’m trying to find the distance between two points
(X1,Y1) & (X2,Y2)
These numbers are generating from the first part of the code
I got the first distance correctly but its keep repeating itself , I don’t know why
Any suggestion is appreciated
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
/// generating 100 point, find x,y for each point///
int Radio_Range=80 ,No_Max=100 ;
int x, y ;
int Total_Degree=0;
int GridF[100][2];
ofstream myfile;
myfile.open ("example.txt");
int row=0;
int miny=0,max_y=99;
for(int i=0;i<5;i++)
{
int minx=0,max_x=99;
for(int j=0;j<5;j++)
{
for(int k=0;k<4;k++)
{
x= minx+rand()%(max_x - minx+1);
y= miny+rand()%(max_y - miny+1);
GridF[row][0]= x;
GridF[row][1]= y;
cout<<GridF[row][0]<<" "<<GridF[row][1]<<endl;
myfile<<GridF[row][0]<<" "<<GridF[row][1]<<endl;
}
minx=max_x+1;
max_x=max_x+100;
}
miny=max_y+1;
max_y=max_y+100;
}
/////////////////// Find the distance btw 2 points///////////
double result ,xGrid,yGrid;
int DistanceCounter=0;
cout<<"Distance : "<<endl;
cout<<endl;
for(int i=0;i<50;i++)
{
for(int j=0;j<2;j++)
{
//xresult
xGrid=(GridF[j+1][0] - GridF[j][j]);?????????????
xGrid=abs(xGrid); xGrid=xGrid*xGrid;
cout<<"xGrid="<<xGrid<<endl;
//yresult
yGrid=(GridF[j+1][j+1] - GridF[j][j+1]);?????????
yGrid=abs(yGrid); yGrid=yGrid*yGrid;
cout<<"yGrid="<<yGrid<<endl;
////result
result = xGrid + yGrid;
result = sqrt (result);
cout<<"Equation : "<<result<<endl;
}
}
}
我想问题发生在GridF [] [] // 2D数组中,是不是正确的方法?
I thik the problem occur in GridF[][]// 2D array, is it the right way ??
推荐答案
有趣的代码。我无法找到您更改行
变量的位置。它看起来总是保持为0.在这种情况下,您首先将tripple-nested循环总是将值分配给GridF [0] [0]和GridF [0] [1]元素,如连续5000次,保留所有其他元素未分配。我可以很容易地修复你的代码,但请先尝试自己修复它并让我们知道结果
Interesting code. I can''t find where you change your row
variable. It looks like it always stays as 0. In this case you first tripple-nested loop always assigns values to GridF[0][0] and GridF[0][1] elements like 5000 times in a row, leaving all other elements unassigned. I can easily fix your code, but please try to fix it yourself first and let us know about the results
第一个问题,已经解释过的是缺少的行变量。
然而,第二部分是类似的,关于代码的第二部分...
你的第二个循环使用我
从0到49,j
从0到1。您只能使用j + 1
访问GridF
。因此,xGrid
和yGrid
始终访问相同的索引(1) - 导致相同的值。
作为概念证明,你应该有一个0-99的单循环,并使用单个变量访问数组索引。如果代码是生产代码的精简版本,则必须使用i
GridF 数组>和j
。
我看到第三个问题,你的数组有100个值,但循环为我
只能达到50.
概念证明解决方案如下:
The first issue, as already explained is the missing row variable.
However, the second is of a similar ilk, regarding the second part of the code...
Your second loop usesi
from 0-49 andj
from 0-1. You are only accessingGridF
withj + 1
. As a resultxGrid
andyGrid
are always accessing the same index (1) - resulting in identical values.
As a "proof of concept", you should really have a single loop from 0-99 and access the array index using a single variable. If the code is a stripped down version of production code, then you must access theGridF
array using a operation of bothi
andj
.
I see a third problem in that your array has 100 values, but the loop fori
only reaches 50.
A "proof of concept" solution would be the following:
for (int i = 1; i < 100; ++i)
{
xGrid = abs(GridF[i][0] - GridF[i - 1][0]);
xGrid *= xGrid;
cout << "xGrid=" << xGrid << endl;
yGrid = abs(GridF[i][1] - GridF[i - 1][1]);
yGrid *= yGrid;
cout << "yGrid=" << yGrid << endl;
result = sqrt(xGrid + yGrid);
cout << "Equation : " << result << endl;
}
另请注意显而易见的事实:
1)您将获得99个输出,不是100,
2)没有验证(如果min-max值需要的话),
3)双int转换的考虑。
Also note the obvious facts:
1) You will get 99 outputs, not 100,
2) There is no validation (if required for min-max values),
3) Consideration for the double-int casting.
这篇关于C ++中的距离公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!