java - 找到电势为零的所有点并绘制它
问题描述
public class physics2 {
public static void main(String[]args){
double k=9*10^9; // Constant k
double q=6*10^(-9); // Charge of q
double Q= -1 * 10^(-9); // Charge of Q
int Pqx = 0; // X coordinate of charge q
int Pqy = 0; // Y coordinate of charge q
int PQx = 10; // X coordinate of charge Q
int PQy = 0; // Y coordinate of charge Q
for (double Px = 7.10102; Px <= 16.8989 ; Px+=0.00001) {
for (double Py = -8.98; Py <= 8.98; Py+=0.00001) {
double Vq = (k * q) / Math.sqrt(Math.pow((Px-Pqx),2)+Math.pow((Py-Pqy),2)); // Electric Potential of charge q
double VQ = (k * Q) / Math.sqrt(Math.pow((Px-PQx),2)+Math.pow((Py-PQy),2)); // Electric Potential of charge Q
while (Vq==-VQ){
System.out.println("["+Px+","+Py+"]");
}}
}
}
}
坐标系中有两个电荷。第一个在点 (0,0) 处,具有 +6 纳库仑。另一个在点 (10,0) 并具有 -1 纳库仑电荷。主题是找到并绘制所有电势为零的点。我写了那个,但它运行了很长时间,我无法得到任何输出。我哪里错了?我看不见。
解决方案
首先尝试替换while (Vq==-VQ)
为 if (Vq==-VQ)
. 我认为你的程序在那里陷入了无限循环。这应该可以解决没有打印的问题。
如果程序仍然很慢,那是由于您处理的点数较多。你应该以某种方式减少这个数字,也许首先尝试接近这两个点的点。
最后,这里很好地解释了如何比较 doubles。
推荐阅读
- javascript - 在不重新加载页面的情况下更改图像 src
- reactjs - 什么时候需要使用 reacts prevState?
- c# - 多个用户看到的全局变量
- opengl - glBegin(GL_LINES) 工作,但 glBegin(GL_POINTS) 不能正常工作?
- rust - 如何为盒装特征对象实现`serde::Serialize`?
- css - CSS:不位于标题下方的 Divs/文章 - 定位问题
- ios - 如何在Apple Maps中获取给定半径内所有附近地点的列表
- sql - 使用 sql server 2012 发送短信
- html - 更改每个类别的唯一 div 的背景颜色
- javascript - 切换按钮文本在 html 中没有改变?