应用上下测试来查找点相对于直线的位置的 C++ 程序
这是一个C++程序,用于应用上下测试来查找点相对于线的位置。对于平面上的任何点t(xt,yt),它相对于连接m和n的线L的位置是通过计算标量s找到的-
Y = A xt + B yt + C
如果Y<0,t位于L的顺时针半平面内;如果Y>0,则t位于逆时针半平面上;如果Y=0,则t位于L上。
算法
Begin
Take the points as input.
For generating equation of the line, generate random numbers for coefficient of x and y (x1,x2,y1,y2) by using rand function at every time of compilation.
Compute s as (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2).
if (s < 0)
Print "The point lies below the line or left side of the line".
else if (s >0)
print "The point lies above the line or right side of the line";
else
print "The point lies on the line"
End示例代码
#include输出结果#include #include #include using namespace std; const int L = 0; const int H= 20; int main(int argc, char **argv) { time_t seconds; time(&seconds); srand((unsigned int) seconds); int x1, x2, y1, y2; x1 = rand() % (H - L + 1) + L; x2 = rand() % (H - L + 1) + L; y1 = rand() % (H - L + 1) + L; y2 = rand() % (H - L + 1) + L; cout << "The Equation of the 1st line is : (" << (y2 - y1) << ")x+(" << (x1 - x2) << ")y+(" << (x2 * y1 - x1 * y2) << ") = 0\n"; int x, y; cout << "\nEnter the point:"; cin >>x; cin >>y; int s = (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2); if (s < 0) cout << "The point lies below the line or left side of the line"; else if (s >0) cout << "The point lies above the line or right side of the line"; else cout << "The point lies on the line"; return 0; }
The Equation of the 1st line is : (7)x+(0)y+(-105) = 0 Enter the point:7 6 The point lies below the line or left side of the line
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短