
移動して、1点の座標を原点(0,0)に

map()で引き算してます。もどりのlistは書き換えてくれないので次のようにしないとだめみたいです。
l_x=list(map(lambda i:i-x1,l_x))
l_y=list(map(lambda i:i-y1,l_y))
2点間の距離を求める (webでの拾い物)
def get_distance(x1, y1, x2, y2):
d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return d
2点の座標を与えます。
3点間の角度を求める
import numpy as np
import math
def calc_angle(a, b, c):
a[0]=a[0]-c[0]
b[0]=b[0]-c[0]
a[1]=a[1]-c[1]
b[1]=b[1]-c[1]
cosine_val = (a[0] * b[0] + (a[1]) * b[1]) / (math.sqrt(a[0]**2 + a[1]**2) * math.sqrt(b[0]**2 + b[1]**2))
ans = math.degrees(math.acos(cosine_val))
return ans
a = [27.0168575063613, -35.8441475826972]
b = [-81.050572519084, -29.9592875318066]
c = [-46.0089058524173, 45.4739185750636]
calc_angle(a, b, c)
こちらのサイトを参考にしました。
python コード (環境:Anaconda)
import sys
import math
#-----------------------
def get_distance(x1, y1, x2, y2):
d = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
return d
#-----------------------
def calc_angle(a, b, c):
a[0]=a[0]-c[0]
b[0]=b[0]-c[0]
a[1]=a[1]-c[1]
b[1]=b[1]-c[1]
cosine_val = (a[0] * b[0] + (a[1]) * b[1]) / (math.sqrt(a[0]**2 + a[1]**2) * math.sqrt(b[0]**2 + b[1]**2))
ans = math.degrees(math.acos(cosine_val))
return ans
#-----------------------
#-----------------------
if __name__ == '__main__':
args = sys.argv
s=args[1]
div_num=int(s[2:])
#print(s[2:])
with open('TEMP.TXT') as f:
list_jwc = f.readlines() #すべて読み込む
#------------------
#最終行取得 len-1
end_num = len(list_jwc)
list_sel_ln = [float(x.strip()) for x in list_jwc[end_num-1].split()]
#print(list_sel_ln)
x1=list_sel_ln[0]
y1=list_sel_ln[1]
x2=list_sel_ln[2]
y2=list_sel_ln[3]
if y1 < y2: #始点yが上でなければ
x1,y1,x2,y2 = x2, y2, x1, y1
#print([x1,y1,x2,y2])
#-------------------
for line in list_jwc:
#文字列の数え方 endは-1まで
if line.find("hp2",0,3) >= 0:
list_hp2 = line.split()
list_hp2.pop(0) #先頭削除
list_hp2 = [float(x.strip()) for x in list_hp2]
xp=list_hp2[0]
yp=list_hp2[1]
#print(list_hp2)
# #-----------------------
# 線の長さ
lnA = get_distance(x1,y1,xp,yp)
#print(lnA)
# 角度を求める
p1=[x2,y2]
p2=[xp,yp]
pp=[x1,y1]
angleA = calc_angle(p1,p2,pp)
#print(angleA)
# 垂直線の長さ
lnAA = lnA*math.sin(math.radians(angleA))
#print(lnAA)
# ピッチ
ct=div_num
pitchA=lnA/ct
# xy 変化分
# 収束点 Line1 角度
dgrA = math.degrees(math.atan2(x1-xp,y1-yp))
#print(dgrA)
# #傾き px[0]-x1
kx1=pitchA*math.sin(math.radians(dgrA))
ky1=pitchA*math.cos(math.radians(dgrA))
# #-----------------------
# #-----------------------
# 線の長さ
lnB = get_distance(x2,y2,xp,yp)
print(lnB)
# ピッチ
ct=div_num
pitchB=lnB/ct
# xy 変化分
# 収束点 Line1 角度
dgrB = math.degrees(math.atan2(x2-xp,y2-yp))-90
print(dgrB)
# #傾き px[0]-x1
kx2=pitchB*math.cos(math.radians(dgrB))
ky2=pitchB*math.sin(math.radians(dgrB))
# #print(str(ct))
for i in range(1,ct+1):
x1=x1-kx1
x2=x2-kx2
y1=y1-ky1
y2=y2+ky2
print("{0} {1} {2} {3}".format(x1, y1,x2, y2))
#
コメント