- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,它将找到两点之间距一个点的距离的一小部分。问题是,我需要一种算法来查找两点之间的 X 距离以及 Y 距离,而不仅仅是直线距离本身。
For example, 2/3 the distance from A to B. (A and B being on a graph)
try {
String a;
a = x1_tf.getText().toString();
double x1 = Integer.parseInt(a);
a = y1_tf.getText().toString();
double y1 = Integer.parseInt(a);
a = x2_tf.getText().toString();
double x2 = Integer.parseInt(a);
a = y2_tf.getText().toString();
double y2 = Integer.parseInt(a);
double Ax,Ay, x = 0,y = 0,nx,ny,fx = 0,fy = 0;
double numerator = Integer.parseInt(fracx_tf.getText().toString());
double denominator = Integer.parseInt(fracy_tf.getText().toString());
if(fromwhat_combobox.getSelectedIndex()==0) {
Ax = Integer.parseInt(x1_tf.getText().toString());
Ay = Integer.parseInt(y1_tf.getText().toString());
//problem below
x += Math.sqrt(Math.pow(Ax, 2)) + Math.sqrt(Math.pow(Ay, 2));
y += Math.sqrt(Math.pow(Ay, 2)) + Math.sqrt(Math.pow(Ax, 2));
//problem lies here ^^^^
}
else {
Ax = Integer.parseInt(x2_tf.getText().toString());
Ay = Integer.parseInt(y2_tf.getText().toString());
//problem below
x += Math.sqrt(Math.pow(Ax, 2)) + Math.sqrt(Math.pow(Ay, 2));
y += Math.sqrt(Math.pow(Ay, 2)) + Math.sqrt(Math.pow(Ax, 2));
//problem lies here ^^^^
}
nx = (x*numerator)/denominator;
ny = (y*numerator)/denominator;
fx = Ax + nx;
if((y2-y1)/(x2-x1)>=0) {
fy = Ay + ny;
}
else {
fy = Ay - ny;
}
String answer = "("+fx+","+fy+") is "+numerator+"/"+denominator+" way from ";
output_pane.setText(answer);
}catch(Exception e){}
所有其他代码都工作正常,我只需要一个算法的帮助,该算法可以找到 x 网格和 y 网格上两点之间的距离。我唯一能想到的是一个复杂的 for 循环或一个巨大的 if 循环嵌套。
只是为了澄清:
fromwhat_combobox 是选择使用什么点,IE,2/5 到哪个点的距离,Ax 和 Ay 代表该点的坐标。其余的很容易计算。
完整代码...
import java.awt.event.FocusEvent;
public class DistanceBetweenPointsCALC extends javax.swing.JFrame {
public DistanceBetweenPointsCALC() {
initComponents(); //default init components
cinitComponents(); // custom init components
oinitComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
calc_button = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
output_pane = new javax.swing.JTextPane();
y1_tf = new javax.swing.JTextField();
x2_tf = new javax.swing.JTextField();
y2_tf = new javax.swing.JTextField();
x1_tf = new javax.swing.JTextField();
frac_checkbox = new javax.swing.JCheckBox();
fromwhat_combobox = new javax.swing.JComboBox();
fracx_tf = new javax.swing.JTextField();
fracy_tf = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
calc_button.setText("Calc");
jScrollPane1.setViewportView(output_pane);
y1_tf.setText("Y1");
x2_tf.setText("X2");
y2_tf.setText("Y2");
x1_tf.setText("X1");
frac_checkbox.setText("Fraction of the Distance");
fromwhat_combobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "From X1", "From X2" }));
fracx_tf.setText("frac x");
fracy_tf.setText("frac y");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(calc_button, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(x2_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(x1_tf))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(y1_tf, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
.addComponent(y2_tf))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(fromwhat_combobox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(fracy_tf)
.addComponent(fracx_tf)))
.addGroup(layout.createSequentialGroup()
.addComponent(frac_checkbox)
.addGap(0, 254, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(x1_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(y1_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fromwhat_combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(x2_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(y2_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fracx_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(calc_button)
.addComponent(fracy_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(frac_checkbox)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
public void cinitComponents() {
calc_button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calc_buttonAP(evt);
}
});
frac_checkbox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
frac_checkboxAP(evt);
}
});
fracx_tf.setEnabled(false);
fracy_tf.setEnabled(false);
fromwhat_combobox.setEnabled(false);
}
public void oinitComponents() {
x1_tf.addFocusListener(new java.awt.event.FocusListener() {
String DEFAULT_TEXT = "X1";
@Override
public void focusGained(FocusEvent e){
String s = x1_tf.getText();
if(s.equals(DEFAULT_TEXT)) { x1_tf.selectAll(); }
}
@Override
public void focusLost(FocusEvent e) {
String s = x1_tf.getText();
if(s.length()<=0) { x1_tf.setText("X1"); }
}
});
y1_tf.addFocusListener(new java.awt.event.FocusListener() {
String DEFAULT_TEXT = "Y1";
@Override
public void focusGained(FocusEvent e){
String s = y1_tf.getText();
if(s.equals(DEFAULT_TEXT)) { y1_tf.selectAll(); }
}
@Override
public void focusLost(FocusEvent e) {
String s = y1_tf.getText();
if(s.length()<=0) { y1_tf.setText("Y1"); }
}
});
x2_tf.addFocusListener(new java.awt.event.FocusListener() {
String DEFAULT_TEXT = "X2";
@Override
public void focusGained(FocusEvent e){
String s = x2_tf.getText();
if(s.equals(DEFAULT_TEXT)) { x2_tf.selectAll(); }
}
@Override
public void focusLost(FocusEvent e) {
String s = x2_tf.getText();
if(s.length()<=0) { x2_tf.setText("X2"); }
}
});
y2_tf.addFocusListener(new java.awt.event.FocusListener() {
String DEFAULT_TEXT = "Y2";
@Override
public void focusGained(FocusEvent e){
String s = y2_tf.getText();
if(s.equals(DEFAULT_TEXT)) { y2_tf.selectAll(); }
}
@Override
public void focusLost(FocusEvent e) {
String s = y2_tf.getText();
if(s.length()<=0) { y2_tf.setText("Y2"); }
}
});
}
public static void main(String args[]) {
/*Look and feel set to Windows*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DistanceBetweenPointsCALC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DistanceBetweenPointsCALC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DistanceBetweenPointsCALC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DistanceBetweenPointsCALC.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DistanceBetweenPointsCALC().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JButton calc_button;
public static javax.swing.JCheckBox frac_checkbox;
public static javax.swing.JTextField fracx_tf;
public static javax.swing.JTextField fracy_tf;
public static javax.swing.JComboBox fromwhat_combobox;
public static javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane output_pane;
public static javax.swing.JTextField x1_tf;
public static javax.swing.JTextField x2_tf;
public static javax.swing.JTextField y1_tf;
public static javax.swing.JTextField y2_tf;
// End of variables declaration
/**
* Action(s) performed below
*/
public void calc_buttonAP(java.awt.event.ActionEvent evt) {
if(frac_checkbox.isSelected()) {
try {
String a;
a = x1_tf.getText().toString();
double x1 = Integer.parseInt(a);
a = y1_tf.getText().toString();
double y1 = Integer.parseInt(a);
a = x2_tf.getText().toString();
double x2 = Integer.parseInt(a);
a = y2_tf.getText().toString();
double y2 = Integer.parseInt(a);
double Ax,Ay, x = 0,y = 0,nx,ny,fx = 0,fy = 0;
double numerator = Integer.parseInt(fracx_tf.getText().toString());
double denominator = Integer.parseInt(fracy_tf.getText().toString());
if(fromwhat_combobox.getSelectedIndex()==0) {
Ax = Integer.parseInt(x1_tf.getText().toString());
Ay = Integer.parseInt(y1_tf.getText().toString());
x += Math.sqrt(Math.pow(Ax, 2)) + Math.sqrt(Math.pow(Ay, 2));
y += Math.sqrt(Math.pow(Ay, 2)) + Math.sqrt(Math.pow(Ax, 2));
//problem lies here ^^^^
}
else {
Ax = Integer.parseInt(x2_tf.getText().toString());
Ay = Integer.parseInt(y2_tf.getText().toString());
x += Math.sqrt(Math.pow(Ax, 2)) + Math.sqrt(Math.pow(Ay, 2));
y += Math.sqrt(Math.pow(Ay, 2)) + Math.sqrt(Math.pow(Ax, 2));
//problem lies here ^^^^
}
nx = (x*numerator)/denominator;
ny = (y*numerator)/denominator;
fx = Ax + nx;
if((y2-y1)/(x2-x1)>=0) {
fy = Ay + ny;
}
else {
fy = Ay - ny;
}
String answer = "("+fx+","+fy+") is "+numerator+"/"+denominator+" way from ";
output_pane.setText(answer);
}catch(Exception e){}
}
else if(!frac_checkbox.isSelected()) {
try {
String a;
a = x1_tf.getText().toString();
double x1 = Integer.parseInt(a);
a = y1_tf.getText().toString();
double y1 = Integer.parseInt(a);
a = x2_tf.getText().toString();
double x2 = Integer.parseInt(a);
a = y2_tf.getText().toString();
double y2 = Integer.parseInt(a);
double finalAnswer = Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
double var1 = x2-x1;
double var2 = y2-y1;
double var3 = Math.pow(var1, 2);
double var4 = Math.pow(var2, 2);
double var5 = var3+var4;
String answer = "Formula : sqrt( (x2 - x1)^2 + (y2 - y1)^2 )\n"
+ "sqrt( ("+x2+" - "+x1+")^2 + ("+y2+" - "+y1+")^2 )\n"
+ "sqrt( ("+var1+")^2 + ("+var2+")^2 )\n"
+ "sqrt( "+var3+" + "+var4+" )\n"
+ "sqrt( "+var5+" )\n"
+ "answer = "+ finalAnswer;
output_pane.setText(answer);
}catch(Exception e){ e.printStackTrace(); }
}
}
public void frac_checkboxAP(java.awt.event.ActionEvent evt) {
if(!frac_checkbox.isSelected()) {
fracx_tf.setEnabled(false);
fracy_tf.setEnabled(false);
fromwhat_combobox.setEnabled(false);
}
else if(frac_checkbox.isSelected()) {
fracx_tf.setEnabled(true);
fracy_tf.setEnabled(true);
fromwhat_combobox.setEnabled(true);
}
}
/**
* EOF
*/
}
最佳答案
这是java代码中的距离公式
Math.sqrt( Math.pow( (p1.getX() - p2.getX()) , 2 )) - Math.pow( p1.getY() - p2.getY()) , 2)));
读取 x1 - x2 平方减去 y1 - y2 平方的平方根
a = x1_tf.getText().toString();
double x1 = Integer.parseInt(a);
a = y1_tf.getText().toString();
double y1 = Integer.parseInt(a);
a = x2_tf.getText().toString();
double x2 = Integer.parseInt(a);
a = y2_tf.getText().toString();
double y2 = Integer.parseInt(a);
Math.sqrt( Math.pow( (x1 - x2) , 2 ) - Math.pow( (y1 - y2) , 2) );
关于Java 和代数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390154/
我已经将一些变量声明为 bool 值,我希望 C++ 在我进行一些 bool 加法时知道该做什么,但它并没有按照我希望的方式发生。我如何让它工作。 #include using namespace s
我正在创建小型配置文件查看器,我想在 SQL 中模拟环。怎么运行的?对于简单:我的 table 上有 5 个用户。当我从中间获得一个到我的实验(如用户编号 2、3、4)时,下一个和上一个用户是 CUR
我计划使用低级 API 实现 FUSE 文件系统,目前正在尝试了解 fuse_entry_param结构体。 不知道是什么unsigned long fuse_entry_param::generat
从事 JS 工作,刚刚开始学习 boolean 代数。想知道是否有办法简化这个表达式: (!variableOne || !variableTwo) 我记得听说过两个“not”意味着你可以改变符号,但
我有这个 F-代数 (introduced in a previous question),我想在它上面施放一个有效的代数。通过绝望的试验,我设法组合了一个有效的 monadic catamorphi
我在 Google 上到处寻找德摩根定律的 bool 代数(不是集合论)证明,但找不到。 Stack Overflow 也缺少 DeMorgan 定律问题。 作为我的 CIS 251 类(class)
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
正在解决这个问题。 验证闰年的基本规则如下: - 大多数能被4整除的年份都是闰年。 - 异常(exception):世纪年不是闰年,除非它们可以被 400 整除。 这就是答案: if(y%4==0 &
假设算法的最坏情况运行时间可以描述为: T(n) = O(n) + O(r^2) + O(n-r) n 是输入大小,r 是根据算法创建分区的索引。 这个等式可以进一步简化吗?如果变量都是 n 那么它将
有没有办法在 JS 中使用 bool 代数? 例如,我想遍历一个包含 true 和 false 的数组,并将其简化为只有 true 或 false。 用 bool 代数来做这件事似乎是一种优雅的方式.
假设我有两个数据框 df1 df2 我可以加入 df1_keys 和 df2_keys。 我想做的事: (A-B) (A-B) U (B-A) A=df1 和 B=df2。 根据我在 document
这个问题已经有答案了: How can I remove a flag in C? (3 个回答) How to set, clear, and toggle a single bit? (27 个回
大家 我实现了自定义聚合 pig UDF。UDF 实现了 Algebraic 接口(interface),并且有 3 个类 - Initial、Intermed 和 Final 在不同阶段进行工作。它
我想制作一个 bool 向量,它是通过比较两个输入 bool 向量创建的。我可以使用 for 循环,但有更好的方法吗? 我理想的解决方案是这样的: df['A'] = [True, False, Fa
我想编写一个 pig 代码来执行分组并生成 31 个字段的总和,但在此之前我需要做一些自定义处理,为此我编写了一个 eval 函数。我想如果我可以将 GROUP 和 SUM 操作包含到 UDF 中,我
我试图通过 boolean 代数来理解使用 4 个与非门如何等同于 1 个异或门。 如果我们从维基百科看这张图片 http://en.wikipedia.org/wiki/XOR_gate#Alter
我是一名优秀的程序员,十分优秀!