在本教程中,我们将讨论一个程序来查找穿过2点的线。
为此,我们将提供两点。我们的任务是使用这些值并找到通过这些点的直线方程。
#include <iostream> using namespace std; //存储x,y对 #define pdd pair<double, double> //从给定点找到线 void lineFromPoints(pdd P, pdd Q){ double a = Q.second - P.second; double b = P.first - Q.first; double c = a*(P.first) + b*(P.second); if(b<0){ cout << "The line passing through points P and Q is: " << a << "x " << b << "y = " << c << endl; } else { cout << "The line passing through points P and Q is: " << a << "x + " << b << "y = " << c << endl; } } int main(){ pdd P = make_pair(3, 2); pdd Q = make_pair(2, 6); lineFromPoints(P, Q); return 0; }
输出结果
The line passing through points P and Q is: 4x + 1y = 14