给定二维点 p(x 0 , y 0 )的坐标。找到距离该点 L 的点,使得连接这些点所形成的线的斜率为M。
例子:
输入: p = (2, 1)
L = sqrt(2)
M = 1
输出:3, 2
1, 0
解释:
与源的距离为 sqrt(2) ,并具有所需的斜率m = 1。
输入: p = (1, 0)
L = 5
M = 0
输出: 6, 0
-4, 0
我们需要找到与给定点距离为 L 的两个点,它们位于斜率为 M 的直线上。
这个想法已在下面的帖子中介绍:
C++ https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141320964
Java https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321133
Python https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321178
C# https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321206
Javascript https://blog.youkuaiyun.com/hefeng_aspnet/article/details/141321238
根据输入的斜率,该问题可以分为 3 类。
1、如果斜率为零,我们只需要调整源点的 x 坐标
2、如果斜率无限大,则需要调整 y 坐标
3、对于其他斜率值,我们可以使用以下方程来找到点

现在利用上述公式我们可以找到所需的点。
# Python program to find the points on a line of
# slope M at distance L
import math
# structure to represent a co-ordinate
# point
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
# Function to print pair of points at
# distance 'l' and having a slope 'm'
# from the source
def printPoints(source, l, m):
# m is the slope of line, and the
# required Point lies distance l
# away from the source Point
a = Point(0, 0)
b = Point(0, 0)
# 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
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 the first Point
print(f"{a.x}, {a.y}")
# print the second Point
print(f"{b.x}, {b.y}")
# driver function
p = Point(2, 1)
q = Point(1, 0)
printPoints(p, math.sqrt(2), 1)
print("\n")
printPoints(q, 5, 0)
# The code is contributed by Gautam goel(gautamgoel962)
输出:
3, 2
1, 0
6, 0
-4, 0
时间复杂度: O(1)
辅助空间: O(1)
667

被折叠的 条评论
为什么被折叠?



