How to Draw a Line in Python Using Slope
Program to find slope of a line
Given two coordinates, find the slope of a straight line.
Examples:
Input : x1 = 4, y1 = 2, x2 = 2, y2 = 5 Output : Slope is -1.5
Approach: To calculate the slope of a line you need only two points from that line, (x1, y1) and (x2, y2). The equation used to calculate the slope from two points is:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using
namespace
std;
float
slope(
float
x1,
float
y1,
float
x2,
float
y2)
{
return
(y2 - y1) / (x2 - x1);
}
int
main()
{
float
x1 = 4, y1 = 2;
float
x2 = 2, y2 = 5;
cout <<
"Slope is: "
<< slope(x1, y1, x2, y2);
return
0;
}
Java
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
def
slope(x1, y1, x2, y2):
return
(
float
)(y2
-
y1)
/
(x2
-
x1)
x1
=
4
y1
=
2
x2
=
2
y2
=
5
print
"Slope is :"
, slope(x1, y1, x2, y2)
C#
using
System;
class
GFG
{
static
float
slope(
float
x1,
float
y1,
float
x2,
float
y2)
{
return
(y2 - y1) / (x2 - x1);
}
public
static
void
Main()
{
float
x1 = 4, y1 = 2;
float
x2 = 2, y2 = 5;
Console.WriteLine(
"Slope is "
+
slope(x1, y1, x2, y2));
}
}
PHP
<?php
function
slope(
$x1
,
$y1
,
$x2
,
$y2
)
{
return
(
$y2
-
$y1
) /
(
$x2
-
$x1
);
}
$x1
= 4;
$y1
= 2;
$x2
= 2;
$y2
= 5;
echo
"Slope is: "
, slope(
$x1
,
$y1
,
$x2
,
$y2
);
?>
Javascript
<script>
function
slope(x1, y1,
x2, y2)
{
return
(y2 - y1) / (x2 - x1);
}
let x1 = 4, y1 = 2;
let x2 = 2, y2 = 5;
document.write(
"Slope is "
+
slope(x1, y1, x2, y2));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Special Case: The above code will throw a runtime error when int the function x1 is equal to x2 (x1==x2). In that case, our denominator will become zero(0). To avoid that condition, we will add a condition in the slope function.
C++
#include <bits/stdc++.h>
using
namespace
std;
float
slope(
float
x1,
float
y1,
float
x2,
float
y2)
{
if
(x1 == x2)
return
INT_MAX;
return
(y2 - y1) / (x2 - x1);
}
int
main()
{
float
x1 = 4, y1 = 2;
float
x2 = 2, y2 = 5;
cout <<
"Slope is: "
<< slope(x1, y1, x2, y2);
return
0;
}
Java
import
java.util.*;
class
GFG {
static
float
slope(
float
x1,
float
y1,
float
x2,
float
y2) {
if
(x1 == x2)
return
Integer.MAX_VALUE;
return
(y2 - y1) / (x2 - x1);
}
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));
}
}
Python3
import
sys
def
slope(x1, y1, x2, y2):
if
x1
=
=
x2:
return
(sys.maxsize)
return
((y2
-
y1)
/
(x2
-
x1))
x1
=
4
y1
=
2
x2
=
2
y2
=
5
print
(
"Slope is :"
, slope(
4
,
2
,
2
,
5
))
C#
using
System;
class
GFG {
static
float
slope(
float
x1,
float
y1,
float
x2,
float
y2) {
if
(x1 == x2)
return
1000000000;
return
(y2 - y1) / (x2 - x1);
}
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));
}
}
Javascript
<script>
function
slope(x1 , y1 , x2 , y2) {
if
(x1 == x2)
return
Number.MAX_VALUE;
return
(y2 - y1) / (x2 - x1);
}
var
x1 = 4, y1 = 2;
var
x2 = 2, y2 = 5;
document.write(
"Slope is: "
+ slope(x1, y1, x2, y2));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
How to Draw a Line in Python Using Slope
Source: https://www.geeksforgeeks.org/program-find-slope-line/
0 Response to "How to Draw a Line in Python Using Slope"
Post a Comment