Tutorialspoint.dev

Find points at a given distance on a line of given slope

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
  Given  that  the  point (x, y)  is  at  distance  I  away  from  (x_0, y_0) 
ewline 
ewline (y-y_0)^{2} + (x-x_0)^{2}= l^{2} 
ewline 
ewline Also  as  the  line   that  passes  through  (x, y)  and  (x0, y0)  satisfies 
ewline 
ewline frac{y-y_0}{x-x_0}= m 
ewline 
ewline Rearranging  we  get 
ewline y=y_0+m*(x-x_0) 
ewline 
ewline  Putting  the  values  in  first  equation 
ewline 
ewline  m^2.(x-x_0)^2+(x-x_0)^2=l^2 
ewline 
ewline Hence,  we  have 
ewline 
ewline x=x_0pm l.sqrt{frac{1}{1+m^2}} 
ewline 
ewline y=y_0 pm m.l.sqrt{frac{1}{1+m^2}}

Now using the above formula we can find the required points.

// C++ program to find the points on a line of
// slope M at distance L
#include <bits/stdc++.h>
using namespace std;
  
// structure to represent a co-ordinate
// point
struct 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 source
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, 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 function
int main()
{
    Point p(2, 1), q(1, 0);
    printPoints(p, sqrt(2), 1);
    cout << endl;
    printPoints(q, 5, 0);
    return 0;
}

Output:

3, 2
1, 0

6, 0
-4, 0

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter