2016-06-01 1 views
0

저는 C++에서 매우 새롭습니다.inf 출력 컴퓨팅 라인 경사

두 줄에 교차점이 있는지 알려주는이 코드를 작성하여 y = Mx + B 방정식에서 "M"이 같은 두 줄을 교차 시켰으며 다른 모든 줄은 생각할 수 없습니다.

프로그램은 이것을 이해하고있는 것처럼 보이지만 입력 된 선분의 기울기가 0이 아니면 inf 또는 -inf를 출력합니다.

왜 이런 일이 발생합니까?

#include <iostream> 
using namespace std; 
int main() 
{ 
typedef double vector2d[2]; 
vector2d pointA, pointB, pointC, pointD; 
double LineSeg1, LineSeg2; 
double yes, no; 


cout << "Enter x for point A: "; 
cin >> pointA[0]; 
cout << "Enter y for point A: "; 
cin >> pointA[1]; 
cout << "Point A = (" << pointA[0] << "," << pointA[1] << ")" << endl; 

cout << "Enter x for point B: "; 
cin >> pointB[0]; 
cout << "Enter y for point B: "; 
cin >> pointB[1]; 
cout << "Point B = (" << pointB[0] << "," << pointB[1] << ")" << endl; 

cout << "Enter x for point C: "; 
cin >> pointC[0]; 
cout << "Enter y for point C: "; 
cin >> pointC[1]; 
cout << "Point C = (" << pointC[0] << "," << pointC[1] << ")" << endl; 

cout << "Enter x for point D: "; 
cin >> pointD[0]; 
cout << "Enter y for point D: "; 
cin >> pointD[1]; 
cout << "Point D = (" << pointD[0] << "," << pointD[1] << ")" << endl; 

LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); 
cout << "slope segment 1 = (" << LineSeg1 << endl; 

LineSeg2 = ((pointD[1]-pointC[1])/(pointD[0]-pointC[0])); 
cout << "slope segment 2 = (" << LineSeg2 << endl; 


if (LineSeg1 == LineSeg2) { 
    cout << "no\n"; 
} 

else (LineSeg1 != LineSeg2) ;{ 
    cout << "yes\n"; 
} 

return 0; 

} 
+0

pointB '[0] -pointB [0]'이어야 pointB '[0] -pointA [0]'. 하지만 여전히 0으로 나눗셈이 필요한지 확인해야합니다. –

+0

두 벡터 v0 = A ~ B 및 v1 = C ~ D를가집니다. 그러면 벡터 곱 (교차 곱) v0 x v1이 0이면 선이 교차하지 않습니다. –

답변

1

이 라인 :

LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointB[0])); 

0으로 나누기 오류가 있습니다.

내가 식이어야 믿는다

LineSeg1 = ((pointB[1]-pointA[1])/(pointB[0]-pointA[0])); 
+0

@TStro이 방법으로 문제가 해결되면 질문에 답변하는 것을 잊지 마십시오. – Stephen

+0

'pointB [0] == pointA [0]'및'pointD [0] == pointC [0]'경우를 수정하지 않습니다. –