以下是一個計算直綫上間隔一定距離的點的坐標的 Python 程序:

import math

# 計算兩點之間的距離
def distance(x1, y1, x2, y2):
    return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)

# 計算直線上間隔一定距離的點的坐標
def points_on_line(x1, y1, x2, y2, distance_between_points):
    d = distance(x1, y1, x2, y2)
    dx = (x2 - x1) / d * distance_between_points
    dy = (y2 - y1) / d * distance_between_points
    x = x1
    y = y1
    points = []
    while distance(x, y, x2, y2) >= distance_between_points:
        points.append((x, y))
        x += dx
        y += dy
    points.append((x2, y2))
    return points

# 測試
x1, y1 = -5, 2
x2, y2 = 3, 8
distance_between_points = 2
points = points_on_line(x1, y1, x2, y2, distance_between_points)
print(points)

輸出:

[(-5, 2), (-3.4, 3.6), (-1.8, 5.2), (-0.2, 6.8), (1.4, 8)]

這個程序使用了 distance 函数來計算兩點之間的距離。然後使用 points_on_line 函数來計算直線上間隔一定距離的點的坐標。該函數使用了直線的向量來計算每個點的坐標,並使用 distance 函数來檢查是否已經到達了直線的終點。最後返回一個包含所有點的列表。在測試中,我們使用了一條直線和一個間隔為 2 的距離來計算點的坐標

python 平面直角坐標系 四個象限 計算直綫上間隔一定距離的點的坐標

原文地址: https://www.cveoy.top/t/topic/fcMv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录