如何计算一组 x、y 坐标和位置变量之间的距离?
Posted
技术标签:
【中文标题】如何计算一组 x、y 坐标和位置变量之间的距离?【英文标题】:How do I calculate the distance between a set x, y coordinate and location variables? 【发布时间】:2021-07-07 02:44:19 【问题描述】:对于上下文,我试图在 python 中转换此代码,该代码在地球的我的世界地图上获取一个位置,从地图周围获取多个经线,并向用户发送一个与最近的经线不一致的图像,其中一条线在输入位置和经线位置
import discord
import os
from discord.ext import commands
import math
from PIL import Image, ImageDraw
from io import BytesIO
import time
eco = []
NearestLand = ''
NearestCoords = []
sa = 0
side = ''
spawnPoints =
"Oceania Spawn": [16801, 2761],
"Antartica Spawn": [8178, 8661],
"Europe Spawn": [-386, -4782],
"Asia Spawn": [12808, -3192],
"Africa Spawn": [2420, 3738],
"North America Spawn": [-10288, -4852],
"South America Spawn": [-6487, 1360],
"Soviet Nexus": [16507,-6595],
"Ryvendor Warp-Pad":[9640,-2390],
"Gulag Warp-Pad":[11741,-4596],
"Soviet-Serbia Base Warp-Pad":[-10137,-5374],
"KGB HQ Warp-Pad":[12958,-5627],
client = commands.Bot(command_prefix = '!')
@client.event
async def on_ready():
print('Bot Started.')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="For Intruders"))
@client.command()
async def test(ctx):
await ctx.send('All systems functioninal comrade!')
@client.command()
async def envoy(ctx, x, y):
await ctx.send("Calculating Comrade!")
time.sleep(2)
lowest = 1000000000
eco = [x, y]
for i in range(0, 2):
eco[i] = int(eco[i])
print("Crate's Coordinates at X = x, Y = y".format(x=eco[0], y=eco[1]))
# print(eco)
# print(spawnPoints.get('Oceania'))
for i in spawnPoints:
z = math.dist(spawnPoints[i], eco)
# print("From spawn, ".format(spawn = str(i)) + str(z) + " blocks away.")
if lowest >= z:
lowest = z
# print("The lowest distance is at: " + str(lowest) + ", at "+ i)
NearestLand = i
NearestCoords = spawnPoints[i]
print("newNearDist: " + str(math.dist(spawnPoints[i], eco)))
await ctx.send("The nearest warp point is on nl, and the distance is z.".format
(nl=NearestLand, z=round(lowest, 2)))
print(NearestCoords)
xmap = Image.open("map.jpg")
draw = ImageDraw.Draw(xmap)
draw.line(
((int(NearestCoords[0]) + 21472) / 10,
(int(NearestCoords[1]) + 10735) / 10,
(int(x) + 21472) / 10,
(int(y) + 10735) / 10),
fill = (255, 0, 0),
width = 10)
draw.ellipse(
((((int(x) + 21472) / 10) - 25),
(((int(y) + 10735) / 10) - 25),
(((int(x) + 21472) / 10) + 25),
(((int(y) + 10735) / 10) + 25)),
fill = (255, 0, 0),
width = 25)
#draw.line(((42975 - x)/10), ((21471 - y)/10))
xmap.save("xmap.jpg")
print("Saved! Uploading...")
await ctx.send(file = discord.File("xmap.jpg"))
print("Uploaded!")
@client.command()
async def envoys(ctx, x, y):
await ctx.send("Calculating...")
time.sleep(2)
lowest = 1000000000
eco = [x, y]
for i in range(0, 2):
eco[i] = int(eco[i])
print("Crate's Coordinates at X = x, Y = y".format(x=eco[0], y=eco[1]))
# print(eco)
# print(spawnPoints.get('Oceania'))
for i in spawnPoints:
z = math.dist(spawnPoints[i], eco)
# print("From spawn, ".format(spawn = str(i)) + str(z) + " blocks away.")
if lowest >= z:
lowest = z
# print("The lowest distance is at: " + str(lowest) + ", at "+ i)
NearestLand = i
NearestCoords = spawnPoints[i]
print("newNearDist: " + str(math.dist(spawnPoints[i], eco)))
await ctx.send("The nearest warp point is on nl, and the distance is z.".format
(nl=NearestLand, z=round(lowest, 2)))
print(NearestCoords)
xmap = Image.open("map.jpg")
draw = ImageDraw.Draw(xmap)
draw.line(
((int(NearestCoords[0]) + 21472) / 10,
(int(NearestCoords[1]) + 10735) / 10,
(int(x) + 21472) / 10,
(int(y) + 10735) / 10),
fill = (255, 0, 0),
width = 10)
draw.ellipse(
((((int(x) + 21472) / 10) - 25),
(((int(y) + 10735) / 10) - 25),
(((int(x) + 21472) / 10) + 25),
(((int(y) + 10735) / 10) + 25)),
fill = (255, 0, 0),
width = 25)
#draw.line(((42975 - x)/10), ((21471 - y)/10))
xmap.save("xmap.jpg")
print("Saved! Uploading...")
await ctx.send(file = discord.File("xmap.jpg"))
print("Uploaded!")
client.run(os.getenv('TOKEN'))
进入节点,这是我目前所拥有的:
const Discord = require('discord.js');
const fs = require('file-system');
require('dotenv').config();
const client = new Discord.Client();
const prefix = require('config.json');
client.commands = new Discord.Collection();
client.on('ready', () => console.log('The Bot is ready!'));
client.user.setActivity("For Intruders", type: "WATCHING" );
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles)
const command = require(`./commands/$file`);
client.commands.set(command.name, command);
client.login(process.env.TOKEN)
我难过的是如何计算位置与输入位置之间的距离 这是我的 config.json。
"prefix": "!",
"spawnPoints":
"Oceania Spawn": "16801, 2761",
"Antartica Spawn": "8178, 8661",
"Europe Spawn": "-386, -4782",
"Asia Spawn": "12808, -3192",
"Africa Spawn": "2420, 3738",
"North America Spawn": "-10288, -4852",
"South America Spawn": "-6487, 1360",
"Soviet Nexus": "16507,-6595",
"Ryvendor Warp-Pad": "9640,-2390",
"Gulag Warp-Pad": "11741,-4596",
"Soviet-Serbia Base Warp-Pad": "-10137,-5374",
"KGB HQ Warp-Pad": "12958,-5627",
,
提前致谢!
【问题讨论】:
我投票结束这个问题,因为“如何计算位置与输入位置之间的距离”是一道数学题,而不是编程题。尝试将distance between points formula
放入搜索引擎,或专门搜索math.stackexchange.com。 (我也不明白你为什么说“x、y、z 坐标”,但显示的数据每点只有两个坐标。)
为清晰起见进行了编辑
【参考方案1】:
使用距离公式,将其变成一个函数。
例如,这是我的解释:
import math
def distanceFormula(x1,y1,x2,y2):
distance = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
return distance
如果是3维空间,加上z,做同样的事情,只是再加一个z1和z2,像这样:
import math
def distanceFormula(x1,y1,x2,y2, z1,z2):
distance = math.sqrt((x1 - x2)**2 + (y1 - y2)**2 + (z1 - z2)**2)
return distance
或者对于广义的 N 维空间,您可以传递两个包含 N 个值的列表或元组,并通过以下方式获取它们之间的距离:
import math
def distanceFormula(p1, p2):
distance = math.sqrt(sum((x - y)**2 for x, y in zip(p1, p2)))
return distance
如果你想使用模块,那么:
import numpy as np
point_a = np.array((x,y,z))
point_b = np.array((x2,y2,z2))
print(np.linalg.norm(point_a - point_b))
【讨论】:
欧几里得距离可以用scipy.spatial.distance.euclidean(x, y)
完成,但它返回一个numpy.float64
实例而不是float
实例
@BatWannaBe 如果你想使用 numpy,你可以改为使用 linalg.norm,它会返回一个数量。您可以使用任何尺寸的变量分配一个点。
我不确定你的意思,scipy 已经使用了 numpy 数组,并且linalg.norm
也返回了一个numpy.float64
实例。这不是什么大问题,因为numpy.float64
和float
存储相同的数据。但是,用户应该注意,因为类及其方法,例如__repr__
会有所不同。
哦,我以为你的意思是它实际上返回了语句'numpy.float64'而不是数字。我的错。以上是关于如何计算一组 x、y 坐标和位置变量之间的距离?的主要内容,如果未能解决你的问题,请参考以下文章