Related Articles
Find points at a given distance on a line of given slope
• Difficulty Level : Easy
• Last Updated : 29 Oct, 2020

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.

1. If slope is zero, we just need to adjust the x coordinate of the source point
2. If slope is infinite, the we need to adjust the y coordinate
3. 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++

 // C++ program to find the points on a line of// slope M at distance L#include using namespace std; // structure to represent a co-ordinate// pointstruct Point {     float x, y;    Point()    {        x = y = 0;    }    Point(float a, float b)    {        x = a, y = b;    }}; // Function to print pair of points at// distance 'l' and having a slope 'm'// from the sourcevoid printPoints(Point source, float l,                                 int m){    // m is the slope of line, and the    // required Point lies distance l    // away from the source Point    Point a, b;     // slope is 0    if (m == 0) {        a.x = source.x + l;        a.y = source.y;         b.x = source.x - l;        b.y = source.y;    }     // if slope is infinte    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;    }     // print the first Point    cout << a.x << ", " << a.y << endl;     // print the second Point    cout << b.x << ", " << b.y << endl;} // driver functionint main(){    Point p(2, 1), q(1, 0);    printPoints(p, sqrt(2), 1);    cout << endl;    printPoints(q, 5, 0);    return 0;}

## Java

 // Java program to find the points on // a line of slope M at distance Lclass GFG{ // Class to represent a co-ordinate// pointstatic class Point{    float x, y;    Point()    {        x = y = 0;    }    Point(float a, float b)    {        x = a;        y = b;    }}; // Function to print pair of points at// distance 'l' and having a slope 'm'// from the sourcestatic void printPoints(Point source,                        float l, int m){         // m is the slope of line, and the    // required Point lies distance l    // away from the source Point    Point a = new Point();    Point b = new Point();         // Slope is 0    if (m == 0)    {        a.x = source.x + l;        a.y = source.y;         b.x = source.x - l;        b.y = source.y;    }     // If slope is infinte    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;    }     // Print the first Point    System.out.println(a.x + ", " + a.y);     // Print the second Point    System.out.println(b.x + ", " + b.y);} // Driver codepublic 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);}} // This code is contributed by Rajnis09

## C#

 // C# program to find the points on // a line of slope M at distance Lusing System; class GFG{ // Class to represent a co-ordinate// pointpublic class Point{    public float x, y;         public Point()    {        x = y = 0;    }         public Point(float a, float b)    {        x = a;        y = b;    }}; // Function to print pair of points at// distance 'l' and having a slope 'm'// from the sourcestatic void printPoints(Point source,                        float l, int m){         // m is the slope of line, and the    // required Point lies distance l    // away from the source Point    Point a = new Point();    Point b = new Point();         // Slope is 0    if (m == 0)    {        a.x = source.x + l;        a.y = source.y;         b.x = source.x - l;        b.y = source.y;    }     // If slope is infinte    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;    }     // Print the first Point    Console.WriteLine(a.x + ", " + a.y);     // Print the second Point    Console.WriteLine(b.x + ", " + b.y);} // Driver codepublic 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);}} // This code is contributed by Amit Katiyar

Output:

3, 2
1, 0

6, 0
-4, 0



This article is contributed by Ashutosh Kumar 😀 If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
{"ft_lang_label":"__label__en","ft_lang_prob":0.6999063,"math_prob":0.9972808,"size":5373,"snap":"2021-04-2021-17","text_gpt3_token_len":1702,"char_repetition_ratio":0.17526542,"word_repetition_ratio":0.47892377,"special_character_ratio":0.3588312,"punctuation_ratio":0.21383648,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997768,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-19T16:56:13Z\",\"WARC-Record-ID\":\"<urn:uuid:10ec0a94-f70c-4d84-9f45-d6308a4715a5>\",\"Content-Length\":\"123837\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:54cd7846-7a11-463c-9c2f-ef3373d61675>\",\"WARC-Concurrent-To\":\"<urn:uuid:a2a6e2b7-ee2b-4658-8f5b-28933e837d3a>\",\"WARC-IP-Address\":\"23.12.144.230\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/find-points-at-a-given-distance-on-a-line-of-given-slope/\",\"WARC-Payload-Digest\":\"sha1:VZLWRKL77HKARXMNT5L36F44EYG33HNP\",\"WARC-Block-Digest\":\"sha1:55OVADN72DSW6ZIJS7TOF3VHUY24A4LV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038887646.69_warc_CC-MAIN-20210419142428-20210419172428-00520.warc.gz\"}"}