// Line.C // (c) Burt Rosenberg 1996 // A program to generate the line equation from two // points on the line. #include void main() { float x1, y1 ; float x2, y2 ; float dx, dy ; float A, B, C ; // get two points cout << "Coordinates of first point on the line." << endl ; cout << " x: " ; cin >> x1 ; cout << " y: " ; cin >> y1 ; cout << "Coordinates of second point on the line." << endl ; cout << " x: " ; cin >> x2 ; cout << " y: " ; cin >> y2 ; // A xi + B yi + C = 0 , for i =1 , 2 // subtract the equations gives // A ( x1 - x2 ) = B ( y2 - y1 ) dx = x1 - x2 ; dy = y2 - y1 ; if ( dx == 0.0 ) { // the line is vertical if ( dy == 0.0 ) { cout << "The two points are identical," << endl ; cout << "No line can be derived." << endl ; return ; } // dx is 0, but dy is not. Hence B = 0. A = 1.0 ; B = 0.0 ; C = - x1 ; } else if ( dy == 0.0 ) { A = 0.0 ; B = 1.0 ; C = - y1 ; } else // the line is neither horizontal nor vertical. { A = 1.0 ; B = dx / dy ; C = - ( A*x1 + B*y1 ) ; } // output cout << A << " x + " << B << " y + " << C << " = 0 " << endl ; }