위-아래-온 테스트를 적용하여 선에 대한 점의 위치를 찾는 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<stdlib.h>
#include<iostream>
#include<math.h>
#include<time.h>
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