我需要一個公式來計算第三個點的位置,通過我們知道Point1和Point2的位置以及線的長度(Point1,Point2)和線的長度(Point1,Point3)。
# We represent it as follows:
Point1 as P1, Point2 as P2, Point3 as P3
P1 = (2, 16) or P1x,P1y = (2, 16)
P2 = (8, 10) or P2x,P2y = (8, 10)
length of the line (P1, P2) as L1, Length of the line (P1, P3) as L2
# I want to make length of the L2 longer than L1 ( so I plus 5 to length of the line L1 )
L1 = 8.5
L2 = L1 5 = 13.5
Find : Point 3 => P3 = (P3x = ?, P3y =?)
這是我的代碼:我如何找到線的長度。
import math
def calculateDistance(x1,y1,x2,y2):
dist = math.sqrt((x2 - x1)**2 (y2 - y1)**2)
return dist
那么我們如何找到P3的位置呢?
Find : P3 = (P3x = ?, P3y =?)
uj5u.com熱心網友回復:
矢量P1->P3和P1->P2共線,即P1->P3的坐標與P1->P2的坐標成正比;并且比例因子必須正好是長度的比值length(P1->P3) / length(P1->P2)
。
這為您提供了 P3x 和 P3y 的等式:
P3x - P1x == (L2 / L1) * (P2x - P1x)
P3y - P1y == (L2 / L1) * (P2y - P1y)
將其轉化為 P3x 和 P3y 的定義:
ratio = L2 / L2
P3x = P1x ratio * (P2x - P1x)
P3y = P1y ratio * (P2y - P1y)
請注意,您不需要定義自己的distance
函式;標準庫math
模塊中已經有一個:https :
//docs.python.org/3/library/math.html#math.dist
from math import dist
print( dist((2,16), (8,10)) )
# 8.485281374238571
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/402043.html