এটি তিনটি পয়েন্ট একই রেখায় (straight line) আছে কিনা চেক করার C++ প্রোগ্রাম। তিনটি পয়েন্ট একই রেখায় থাকবে যদি তাদের দ্বারা গঠিত ত্রিভুজের ক্ষেত্রফল (area) শূন্য হয়।
ত্রিভুজের ক্ষেত্রফল:
0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
এলগরিদম
Begin
পয়েন্টগুলো র্যান্ডমলি জেনারেট করুন।
উপরের ফর্মুলা ব্যবহার করে ক্ষেত্রফল বের করুন।
If area > 0: পয়েন্টগুলো সরল রেখায় নেই।
else if area < 0: পয়েন্টগুলো সরল রেখায় নেই।
else: পয়েন্টগুলো সরল রেখায় আছে।
End
উদাহরণ
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
static int L = 1;
static int U = 20;
int main(int argc, char **argv) {
int x3, y3, x1, x2, y1, y2;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
// rand() ব্যবহার করে র্যান্ডম পয়েন্ট জেনারেট
x1 = rand() % (U - L + 1) + L;
y1 = rand() % (U - L + 1) + L;
x2 = rand() % (U - L + 1) + L;
y2 = rand() % (U - L + 1) + L;
x3 = rand() % (U - L + 1) + L;
y3 = rand() % (U - L + 1) + L;
cout << "The points are: (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << "), & (" << x3 << ", " << y3 << ")\n";
// ক্ষেত্রফল ক্যালকুলেট
float a = 0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
if (a < 0)
cout << "The points don't lie on the straight line";
else if (a > 0)
cout << "The points don't lie on the straight line";
else
cout << "The points lie on the straight line";
}
আউটপুট
The points are: (20, 9), (6, 13), & (13, 11) The points lie on the straight line The points are: (9, 15), (4, 15), & (11, 16) The points don't lie on the straight line