📜  求直线斜率的程序

📅  最后修改于: 2021-10-23 09:08:08             🧑  作者: Mango

给定两个坐标,求一条直线的斜率。

例子:

Input  : x1 = 4, y1 = 2, 
         x2 = 2, y2 = 5 
Output : Slope is -1.5

方法:要计算一条直线的斜率,您只需要该直线上的两个点,(x1, y1) 和 (x2, y2)。用于计算两点斜率的公式为:

下面是上述方法的实现:

C++
// C++ program for slope of line
#include 
using namespace std;
 
// function to find the slope of a straight line
float slope(float x1, float y1, float x2, float y2)
{
    return (y2 - y1) / (x2 - x1);
}
 
// driver code to check the above function
int main()
{
    float x1 = 4, y1 = 2;
    float x2 = 2, y2 = 5;
    cout << "Slope is: "
         << slope(x1, y1, x2, y2);
    return 0;
}


Java
// Java program for slope of line
import java.io.*;
 
class GFG {
    static float slope(float x1, float y1,
                       float x2, float y2)
    {
        return (y2 - y1) / (x2 - x1);
    }
    public static void main(String[] args)
    {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        System.out.println("Slope is " +
                    slope(x1, y1, x2, y2));
    }
}


Python
# Python program for slope of line
def slope(x1, y1, x2, y2):
    return (float)(y2-y1)/(x2-x1)
 
# driver code   
x1 = 4
y1 = 2
x2 = 2
y2 = 5
print "Slope is :", slope(x1, y1, x2, y2)


C#
// C# program for slope of line
using System;
 
class GFG
{
    static float slope(float x1, float y1,
                       float x2, float y2)
    {
        return (y2 - y1) / (x2 - x1);
    }
     
    // Driver code
    public static void Main()
    {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        Console.WriteLine("Slope is " +
                    slope(x1, y1, x2, y2));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


C++
// C++ program for slope of line
#include 
using namespace std;
 
// function to find the slope of a straight line
float slope(float x1, float y1, float x2, float y2)
{
    if(x1 == x2)
        return INT_MAX;
    return (y2 - y1) / (x2 - x1);
}
 
// driver code to check the above function
int main()
{
    float x1 = 4, y1 = 2;
    float x2 = 2, y2 = 5;
    cout << "Slope is: "
         << slope(x1, y1, x2, y2);
    return 0;
}


Java
// Java program for slope of line
 
import java.util.*;
 
class GFG {
 
    // function to find the slope of a straight line
    static float slope(float x1, float y1, float x2, float y2) {
        if (x1 == x2)
            return Integer.MAX_VALUE;
        return (y2 - y1) / (x2 - x1);
    }
 
    // driver code to check the above function
    public static void main(String[] args) {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        System.out.print("Slope is: " + slope(x1, y1, x2, y2));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 program to find slope
import sys
 
# Function to find the slope of a straight line
def slope(x1, y1, x2, y2):
     
    if x1 == x2:
        retrun (sys.maxsize)
         
    return ((y2 - y1) / (x2 - x1))
 
# Driver code
x1 = 4
y1 = 2
x2 = 2
y2 = 5
 
print("Slope is :", slope(4, 2, 2, 5))
 
# This code is contributed by vaishaligoyal878


C#
// C# program for slope of line
using System;
class GFG {
 
    // function to find the slope of a straight line
    static float slope(float x1, float y1, float x2, float y2) {
        if (x1 == x2)
            return 1000000000;
        return (y2 - y1) / (x2 - x1);
    }
 
    // driver code to check the above function
    public static void Main(string[] args) {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        Console.Write("Slope is: " + slope(x1, y1, x2, y2));
    }
}
 
// This code is contributed by famously.


Javascript


输出
Slope is: -1.5

时间复杂度: O(1)

辅助空间: O(1)

特殊情况:当 int函数x1 等于 x2 (x1==x2) 时,上述代码将抛出运行时错误。在这种情况下,我们的分母将变为零(0)。为了避免这种情况,我们将在斜率函数添加一个条件。

C++

// C++ program for slope of line
#include 
using namespace std;
 
// function to find the slope of a straight line
float slope(float x1, float y1, float x2, float y2)
{
    if(x1 == x2)
        return INT_MAX;
    return (y2 - y1) / (x2 - x1);
}
 
// driver code to check the above function
int main()
{
    float x1 = 4, y1 = 2;
    float x2 = 2, y2 = 5;
    cout << "Slope is: "
         << slope(x1, y1, x2, y2);
    return 0;
}

Java

// Java program for slope of line
 
import java.util.*;
 
class GFG {
 
    // function to find the slope of a straight line
    static float slope(float x1, float y1, float x2, float y2) {
        if (x1 == x2)
            return Integer.MAX_VALUE;
        return (y2 - y1) / (x2 - x1);
    }
 
    // driver code to check the above function
    public static void main(String[] args) {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        System.out.print("Slope is: " + slope(x1, y1, x2, y2));
    }
}
 
// This code contributed by Rajput-Ji

蟒蛇3

# Python3 program to find slope
import sys
 
# Function to find the slope of a straight line
def slope(x1, y1, x2, y2):
     
    if x1 == x2:
        retrun (sys.maxsize)
         
    return ((y2 - y1) / (x2 - x1))
 
# Driver code
x1 = 4
y1 = 2
x2 = 2
y2 = 5
 
print("Slope is :", slope(4, 2, 2, 5))
 
# This code is contributed by vaishaligoyal878

C#

// C# program for slope of line
using System;
class GFG {
 
    // function to find the slope of a straight line
    static float slope(float x1, float y1, float x2, float y2) {
        if (x1 == x2)
            return 1000000000;
        return (y2 - y1) / (x2 - x1);
    }
 
    // driver code to check the above function
    public static void Main(string[] args) {
        float x1 = 4, y1 = 2;
        float x2 = 2, y2 = 5;
        Console.Write("Slope is: " + slope(x1, y1, x2, y2));
    }
}
 
// This code is contributed by famously.

Javascript


输出
Slope is: -1.5

时间复杂度: O(1)

辅助空间: O(1)