我有一个充满i元素的数组。我想检查两个人之间发生的事情。到目前为止,我只能检查这个数组中某个特定元素会发生什么,我怎么能在2之间做呢?
我的数组填写如下:
int iSegment = pDatagram->header.start - 1;
pdX[0] = (-(pDatagram->distances[0]) * ROD4::dCos_table[0]);
pdY[0] = ( (pDatagram->distances[0]) * ROD4::dSin_table[0]);
iSegment += 1; //correct start of interval #1
//calculate cartesian values
for(int i = 1 ; i < pDatagram->distanceCount; i++)
{
pdX[i] = (-(pDatagram->distances[i]) * ROD4::dCos_table[iSegment]);
pdY[i] = ( (pDatagram->distances[i]) * ROD4::dSin_table[iSegment]);
iSegment += pDatagram->header.resolution;
}
我正在检查第70个元素中发生的事情,包括以下几行:
pdX[70] = (-(pDatagram->distances[70]) * ROD4::dCos_table[70]);
if( pdX[70] > 0 && pdX[70] < 45 ) // these are to test the distances of the 70th element
{
cout << "My line is broken in the X axis" << endl;
}
我如何检查第40至第70元素之间的情况?
答案 0 :(得分:0)
尝试以下内容,但要根据您的需要进行调整
for(int i = 1; i < pDatagram->distanceCount; i++) {
pdX[i] = (-(pDatagram->distances[i]) * ROD4::dCos_table[iSegment]);
pdY[i] = ( (pDatagram->distances[i]) * ROD4::dSin_table[iSegment]);
iSegment += pDatagram->header.resolution;
if (i <= 70 && i >= 40) {
if( pdX[i] > 0 && pdX[i] < 45 ) {
cout << "My line is broken in the X axis" << endl;
}
}
}
答案 1 :(得分:0)
如果您使用的是C风格的数组,我只需要从头到尾使用while循环遍历所需的元素。虽然,我仍然不确定pdX是什么,它似乎是关于sin / cos(而不是矩形笛卡尔形式)的坐标,因为你有条件需要0和45让我假设你在谈论角度虽然我这里可能错了。请澄清所以我可以编辑这个答案
size_t x_start = 40;
size_t x_end = 70;
size_t counter = x_start;
bool line_broken_x = false;
while (line_broken_x != false && counter != x_end+1)
{
if( pdX[counter] < 0 || pdX[counter] > 45 )
line_broken_x = false;
counter++;
}
if (line_broken_x == true)
cout << "My line is broken in the X axis" << endl;
else
cout << "My line is not broken in the X axis" << endl;