目前,我有以下代码。
import tkinter as tk
import math
from tkinter.simpledialog import askfloat
import scipy as sp
import sympy as smp
from scipy.integrate import quad
from scipy.integrate import cumulative_trapezoid
def calcular_distancia(p1, p2):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
return math.sqrt(dx**2 + dy**2)
def agregar_punto(event):
global puntos, carga
puntos.append((event.x, event.y))
canvas.create_oval(event.x - 2, event.y - 2, event.x + 2, event.y + 2, fill="red")
if len(puntos) == 2:
distancia = calcular_distancia(puntos[0], puntos[1])
label_distancia.config(text=f"Distancia: {distancia:.2f} m")
canvas.create_line(puntos[0][0], puntos[0][1], puntos[1][0], puntos[1][1], fill="blue", width=3)
carga = askfloat("Carga de la Línea", "Ingresa la carga de la línea en Coulombs (C):")
label_carga.config(text=f"Carga de la línea: {carga:.2e} C")
dibujar_campos()
def dibujar_campos():
canvas.delete("campo")
for x in range(0, 401, 20):
for y in range(0, 401, 20):
punto = (x, y)
campo = calcular_campo(punto)
dibujar_flecha(punto, campo)
def calcular_distancia_al_centro(punto):
centro_x = (puntos[0][0] + puntos[1][0]) / 2
centro_y = (puntos[0][1] + puntos[1][1]) / 2
dx = punto[0] - centro_x
dy = punto[1] - centro_y
return math.sqrt(dx**2 + dy**2)
def calcular_campo(punto):
campo_total = [0, 0]
for p in puntos:
dx = punto[0] - p[0]
dy = punto[1] - p[1]
distancia = math.sqrt(dx**2 + dy**2)
campo_magnitud = (9 * 10**9 * carga) / (distancia**2) # Fórmula simplificada del campo
angulo = math.atan2(dy, dx)
campo_x = campo_magnitud * math.cos(angulo)
campo_y = campo_magnitud * math.sin(angulo)
campo_total[0] += campo_x
campo_total[1] += campo_y
return campo_total
def dibujar_flecha(punto, campo):
escala = 1e-9 # Escala para ajustar el tamaño de la flecha
x1, y1 = punto
x2 = x1 + campo[0] * escala
y2 = y1 + campo[1] * escala
canvas.create_line(x1, y1, x2, y2, fill="green", arrow=tk.LAST, tags="campo")
def carga(punto):
global campo_total
dx =
Q = campo_total*r**2/8.99*e**9
r = sqrt(dx3**2+dy3**2)
angulo =
def click_derecho(event):
puntos = []
carga = 0.0
root = tk.Tk()
root.title("Campo Eléctrico con Línea de Dos Puntos")
canvas = tk.Canvas(root, width=400, height=400)
canvas.pack()
# Crear cuadrícula (cada cuadro representa 1 metro)
for x in range(0, 401, 20):
canvas.create_line(x, 0, x, 400, fill="lightgray")
for y in range(0, 401, 20):
canvas.create_line(0, y, 400, y, fill="lightgray")
canvas.bind("<Button-1>", agregar_punto)
label_instrucciones = tk.Label(root, text="Haz clic en los puntos para agregarlos a la línea.")
label_instrucciones.pack()
label_distancia = tk.Label(root, text="Distancia: 0.00 m")
label_distancia.pack()
label_carga = tk.Label(root, text="Carga de la línea: 0.00 C")
label_carga.pack()
root.mainloop()
不幸的是,我在思考一个方程时遇到问题,该方程可以找到从用户指定的线到用户生成的点的最短距离。任何帮助都会非常惊人。谢谢
我尝试过 dx dy 方法,但这并没有返回正确的费用值。
直线的方程为:
Ax + By + C = 0
该点的坐标:
x,y
最短距离是:
d = |Ax1 + By1 + C| / (A2 + B2)½
那么这就是您的解决方案:
import math
# for line
A = float(input("Enter the coefficient A: "))
B = float(input("Enter the coefficient B: "))
C = float(input("Enter the coefficient C: "))
# point coordinates
x0 = float(input("Enter the x-coordinate of the point: "))
y0 = float(input("Enter the y-coordinate of the point: "))
# Calculate the shortest distance
distance = abs(A * x0 + B * y0 + C) / math.sqrt(A ** 2 + B ** 2)
print("Shortest distance from the line to the point:", distance)