Given the co-ordinates of a 2-dimensional point p(x0, y0). Find the points at a distance L away from it, such that the line formed by joining these points has a slope of M.
Examples:
Input : p = (2, 1)
L = sqrt(2)
M = 1
Output :3, 2
1, 0
Explanation:
The two points are sqrt(2) distance away
from the source and have the required slope
m = 1.
Input : p = (1, 0)
L = 5
M = 0
Output : 6, 0
-4, 0
We need to find two points that are L distance from given point, on a line with slope M.
The idea has been introduced in below post.
Find Corners of Rectangle using mid points
Based on the input slope, the problem can be classified into 3 categories.
- If slope is zero, we just need to adjust the x coordinate of the source point
- If slope is infinite, the we need to adjust the y coordinate
- For other values of slope, we can use the following equations to find the points

Now using the above formula we can find the required points.
C++
#include <bits/stdc++.h>
using namespace std;
struct Point {
float x, y;
Point() { x = y = 0; }
Point( float a, float b) { x = a, y = b; }
};
void printPoints(Point source, float l, int m)
{
Point a, b;
if (m == 0) {
a.x = source.x + l;
a.y = source.y;
b.x = source.x - l;
b.y = source.y;
}
else if (m == std::numeric_limits< float >::max()) {
a.x = source.x;
a.y = source.y + l;
b.x = source.x;
b.y = source.y - l;
}
else {
float dx = (l / sqrt (1 + (m * m)));
float dy = m * dx;
a.x = source.x + dx;
a.y = source.y + dy;
b.x = source.x - dx;
b.y = source.y - dy;
}
cout << a.x << ", " << a.y << endl;
cout << b.x << ", " << b.y << endl;
}
int main()
{
Point p(2, 1), q(1, 0);
printPoints(p, sqrt (2), 1);
cout << endl;
printPoints(q, 5, 0);
return 0;
}
|
Java
class GFG {
static class Point {
float x, y;
Point() { x = y = 0 ; }
Point( float a, float b)
{
x = a;
y = b;
}
};
static void printPoints(Point source, float l, int m)
{
Point a = new Point();
Point b = new Point();
if (m == 0 ) {
a.x = source.x + l;
a.y = source.y;
b.x = source.x - l;
b.y = source.y;
}
else if (Double.isInfinite(m)) {
a.x = source.x;
a.y = source.y + l;
b.x = source.x;
b.y = source.y - l;
}
else {
float dx = ( float )(l / Math.sqrt( 1 + (m * m)));
float dy = m * dx;
a.x = source.x + dx;
a.y = source.y + dy;
b.x = source.x - dx;
b.y = source.y - dy;
}
System.out.println(a.x + ", " + a.y);
System.out.println(b.x + ", " + b.y);
}
public static void main(String[] args)
{
Point p = new Point( 2 , 1 ), q = new Point( 1 , 0 );
printPoints(p, ( float )Math.sqrt( 2 ), 1 );
System.out.println();
printPoints(q, 5 , 0 );
}
}
|
C#
using System;
class GFG {
public class Point {
public float x, y;
public Point() { x = y = 0; }
public Point( float a, float b)
{
x = a;
y = b;
}
};
static void printPoints(Point source, float l, int m)
{
Point a = new Point();
Point b = new Point();
if (m == 0) {
a.x = source.x + l;
a.y = source.y;
b.x = source.x - l;
b.y = source.y;
}
else if (Double.IsInfinity(m)) {
a.x = source.x;
a.y = source.y + l;
b.x = source.x;
b.y = source.y - l;
}
else {
float dx = ( float )(l / Math.Sqrt(1 + (m * m)));
float dy = m * dx;
a.x = source.x + dx;
a.y = source.y + dy;
b.x = source.x - dx;
b.y = source.y - dy;
}
Console.WriteLine(a.x + ", " + a.y);
Console.WriteLine(b.x + ", " + b.y);
}
public static void Main(String[] args)
{
Point p = new Point(2, 1), q = new Point(1, 0);
printPoints(p, ( float )Math.Sqrt(2), 1);
Console.WriteLine();
printPoints(q, 5, 0);
}
}
|
Python3
import math
class Point:
def __init__( self , x, y):
self .x = x
self .y = y
def printPoints(source, l, m):
a = Point( 0 , 0 )
b = Point( 0 , 0 )
if m = = 0 :
a.x = source.x + l
a.y = source.y
b.x = source.x - l
b.y = source.y
elif math.isfinite(m) is False :
a.x = source.x
a.y = source.y + l
b.x = source.x
b.y = source.y - l
else :
dx = (l / math.sqrt( 1 + (m * m)))
dy = m * dx
a.x = source.x + dx
a.y = source.y + dy
b.x = source.x - dx
b.y = source.y - dy
print (f "{a.x}, {a.y}" )
print (f "{b.x}, {b.y}" )
p = Point( 2 , 1 )
q = Point( 1 , 0 )
printPoints(p, math.sqrt( 2 ), 1 )
print ( "\n" )
printPoints(q, 5 , 0 )
|
Javascript
<script>
class Point
{
constructor(x, y)
{
this .x = x;
this .y = y;
}
}
function printPoints(source, l, m)
{
let a = new Point();
let b = new Point();
if (m == 0)
{
a.x = source.x + l;
a.y = source.y;
b.x = source.x - l;
b.y = source.y;
}
else if (!isFinite(m))
{
a.x = source.x;
a.y = source.y + l;
b.x = source.x;
b.y = source.y - l;
}
else
{
var dx = (l / Math.sqrt(1 + (m * m)));
var dy = m * dx;
a.x = source.x + dx;
a.y = source.y + dy;
b.x = source.x - dx;
b.y = source.y - dy;
}
document.write(a.x + ", " + a.y+ "\n" );
document.write(b.x + ", " + b.y+ "\n" );
}
let p = new Point(2, 1);
let q = new Point(1, 0);
printPoints(p, Math.sqrt(2), 1);
document.write( "\n" );
printPoints(q, 5, 0);
</script>
|
Output3, 2
1, 0
6, 0
-4, 0
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Ashutosh Kumar 😀 If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.