gpt4 book ai didi

Java 和代数

转载 作者:行者123 更新时间:2023-12-01 13:45:43 25 4
gpt4 key购买 nike

我正在编写一个程序,它将找到两点之间距一个点的距离的一小部分。问题是,我需要一种算法来查找两点之间的 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) );

参见Distance Formula

关于Java 和代数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com