我已经知道这个程序的输出应该是什么 - 我的问题是我无法让程序给出正确的输出,或者任何输出。我的问题是:识别和显示给定游览的所有跨过任何其他腿的腿。数据如下:旅游(“城市”)是 {0, 4, 1, 3, 2} 并且这些“城市”的点是 {2,0} , {4,1} , {0,1 } , {3,2} , {1,2}。这是我的程序:
#include <iostream>
using namespace std;
const int MAX = 100;
bool doCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
int main()
{
int numPts = 5;
int tourAry[MAX] = {0, 4, 1, 3, 2};
int pointsAry[MAX][2] = {{2, 0},{4, 1},{0, 1},{3, 2},{1, 2}};
for(int start = 0; start <= numPts - 3; start++)
{
int startPt = tourAry[0];
int endPt = tourAry[1];
int testSegmentX1 = pointsAry[startPt][0];
int testSegmentY1 = pointsAry[startPt][1];
int testSegmentX2 = pointsAry[endPt][0];
int testSegmentY2 = pointsAry[endPt][1];
for(int nextSeg = start + 2; nextSeg <= numPts - 2; nextSeg++)
{
startPt = tourAry[2];
endPt = tourAry[3];
int startX = pointsAry[startPt][0];
int startY = pointsAry[startPt][1];
int endX = pointsAry[endPt][0];
int endY = pointsAry[endPt][1];
if(doCross(testSegmentX1, testSegmentY1, testSegmentX2, testSegmentY2, startX, startY, endX, endY))
{
cout << tourAry[start] << " - " << tourAry[start+1] << " crosses " << tourAry[nextSeg] << " - " << tourAry[nextSeg+1] << endl;
}
}//for
}//for
return 0;
}//main
bool doCross(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
bool cross = true;
double denom, numerA, numerB, uA, uB;
denom = ((y4 - y3) * (x2 - x1)) - ((x4 - x3) * (y2 - y1));
numerA = ((x4 - x3) * (y1 - y3)) - ((y4 - y3) * (x1 - x3));
numerB = ((x2 - x1) * (y1 - y3)) - ((y2 - y1) * (x1 - x3));
if(denom == 0.0)
{
if(numerA == 0.0 && numerB == 0.0)
{
cross = false;
}//if
}//if
else
{
float uA = numerA / denom;
float uB = numerB / denom;
if (uA > 0.0 && uA < 1.0 && uB > 0.0 && uB < 1.0)
{
cross = true;
}//if
else
{
cross = false;
}//else
}//else
return cross;
}//doCross
我知道输出应该是: 0-4 交叉 3-2 4-1 交叉 3-2
任何帮助将不胜感激。