यह एक C++ प्रोग्राम है जो एक लाइन के सापेक्ष पॉइंट की स्थिति जांचने के लिए Above-Below-on टेस्ट लागू करता है। प्लेन पर किसी भी पॉइंट 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 पॉइंट्स को इनपुट के रूप में लें। लाइन का समीकरण उत्पन्न करने के लिए, कंपाइलेशन के हर बार rand फंक्शन का उपयोग करके x और y के गुणांक (x1,x2,y1,y2) के लिए रैंडम नंबर उत्पन्न करें। s की गणना (y2 - y1) * x + (x1 - x2) * y + (x2 * y1 - x1 * y2) के रूप में करें। if (s < 0) "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