给定坐标平面上的两点P和Q,求出通过这两点的直线的方程。 这种转换在许多几何算法中非常有用,比如线的相交,求 环中心 一个三角形,找到 因森特 一个三角形和更多…
null
例如:
Input : P(3, 2) Q(2, 6)Output : 4x + 1y = 14Input : P(0, 1) Q(2, 4)Output : 3x + -2y = -2
设给定两点为P(x) 1. Y 1. )和Q(x) 2. Y 2. )现在,我们找到由这些点构成的直线方程。 任何线条都可以表示为, ax+by=c 让两点满足给定的直线。所以我们有, 斧头 1. +由 1. =c 斧头 2. +由 2. =c
我们可以设置以下值,使所有方程都成立,
a = y2 - y1b = x1 - x2c = ax1 + by1
首先直接求出斜率,然后求出直线的截距,就可以得到它们。或者,也可以通过以下简单观察巧妙得出:
推导:
ax1 + by1 = c ...(i)ax2 + by2 = c ...(ii)Equating (i) and (ii),ax1 + by1 = ax2 + by2=> a(x1 - x2) = b(y2 - y1)Thus, for equating LHS and RHS, we can simply have,a = (y2 - y1)ANDb = (x1 - x2)so that we have,(y2 - y1)(x1 - x2) = (x1 - x2)(y2 - y1)ANDPutting these values in (i), we get,c = ax1 + by1
因此,我们现在有a,b和c的值,这意味着我们有坐标平面上的线。
C++
// C++ Implementation to find the line passing // through two points #include <iostream> using namespace std; // This pair is used to store the X and Y // coordinate of a point respectively #define pdd pair<double, double> // Function to find the line given two points 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; } } // Driver code int main() { pdd P = make_pair(3, 2); pdd Q = make_pair(2, 6); lineFromPoints(P, Q); return 0; } |
JAVA
// Java Implementation to find the line passing // through two points class GFG { // This pair is used to store the X and Y // coordinate of a point respectively static class Pair { int first, second; public Pair( int first, int second) { this .first = first; this .second = second; } } // Function to find the line given two points static void lineFromPoints(Pair P, Pair Q) { int a = Q.second - P.second; int b = P.first - Q.first; int c = a * (P.first) + b * (P.second); if (b < 0 ) { System.out.println( "The line passing through points P and Q is: " + a + "x - " + b + "y = " + c); } else { System.out.println( "The line passing through points P and Q is: " + a + "x + " + b + "y = " + c); } } // Driver code public static void main(String[] args) { Pair P = new Pair( 3 , 2 ); Pair Q = new Pair( 2 , 6 ); lineFromPoints(P, Q); } } // This code is contributed by Princi Singh |
Python3
# Python3 Implementation to find the line passing # through two points # This pair is used to store the X and Y # coordinate of a point respectively # define pdd pair<double, double> # Function to find the line given two points def lineFromPoints(P, Q): a = Q[ 1 ] - P[ 1 ] b = P[ 0 ] - Q[ 0 ] c = a * (P[ 0 ]) + b * (P[ 1 ]) if (b < 0 ): print ( "The line passing through points P and Q is:" , a, "x - " , b, "y = " , c, "" ) else : print ( "The line passing through points P and Q is: " , a, "x + " , b, "y = " , c, "" ) # Driver code if __name__ = = '__main__' : P = [ 3 , 2 ] Q = [ 2 , 6 ] lineFromPoints(P, Q) # This code is contributed by ash264 |
C#
// C# Implementation to find the line passing // through two points using System; class GFG { // This pair is used to store the X and Y // coordinate of a point respectively public class Pair { public int first, second; public Pair( int first, int second) { this .first = first; this .second = second; } } // Function to find the line given two points static void lineFromPoints(Pair P, Pair Q) { int a = Q.second - P.second; int b = P.first - Q.first; int c = a * (P.first) + b * (P.second); if (b < 0) { Console.WriteLine( "The line passing through points P and Q is: " + a + "x - " + b + "y = " + c); } else { Console.WriteLine( "The line passing through points P and Q is: " + a + "x + " + b + "y = " + c); } } // Driver code public static void Main(String[] args) { Pair P = new Pair(3, 2); Pair Q = new Pair(2, 6); lineFromPoints(P, Q); } } // This code has been contributed by 29AjayKumar |
Javascript
<script> // Javascript implementation to find the // line passing through two points // Function to find the line given two points function lineFromPoints(P, Q) { var a = Q[1] - P[1] var b = P[0] - Q[0] var c = a*(P[0]) + b*(P[1]) if (b < 0) document.write( "The line passing through " + "points P and Q is: " + a + "x - " + b + "y = " + c + "<br>" ) else document.write( "The line passing through " + "points P and Q is: " + a + "x + " + b + "y = " + c + "<br>" ) } // Driver code var P = [ 3, 2 ] var Q = [ 2, 6 ] lineFromPoints(P, Q) // This code is contributed by akshitsaxenaa09 </script> |
输出:
The line passing through points P and Q is: 4x + 1y = 14
本文由 安雅·金达尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END