在给定坡度的直线上找到给定距离的点

给定二维点p(x)的坐标 0 Y 0 ).找出距离L的点,使这些点连接形成的线的斜率为M。

null

例如:

Input : p = (2, 1)        L = sqrt(2)        M = 1Output :3, 2        1, 0Explanation:The two points are sqrt(2) distance away from the source and have the required slopem = 1.Input : p = (1, 0)        L = 5        M = 0Output : 6, 0        -4, 0

我们需要在一条斜率为M的直线上找到两个距离给定点L的点。 下面的帖子介绍了这个想法。 使用中点查找矩形的角点

根据输入斜率,问题可分为三类。

  1. 如果斜率为零,我们只需要调整源点的x坐标
  2. 如果斜率为无穷大,则需要调整y坐标
  3. 对于坡度的其他值,我们可以使用以下方程式来找到这些点

  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}}

现在使用上述公式,我们可以找到所需的点。

C++

// 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 infinite
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;
}


JAVA

// Java program to find the points on
// a line of slope M at distance L
class GFG{
// Class to represent a co-ordinate
// point
static 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 source
static 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 infinite
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 code
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 );
}
}
// This code is contributed by Rajnis09


C#

// C# program to find the points on
// a line of slope M at distance L
using System;
class GFG{
// Class to represent a co-ordinate
// point
public 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 source
static 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 infinite
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 code
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);
}
}
// This code is contributed by Amit Katiyar


输出:

3, 21, 06, 0-4, 0

本文由 阿什图什·库马尔 如果你喜欢Geeksforgek,并且想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享