I'm making a program that will find a fraction of the distance between two points from a point. The Problem is, is that I need an algorithm to find the X distance between two points as well as the Y distance, not just the straight distance in itself.
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){}
All of the other code works fine, I just need help with an algorithm that finds the distance between two points on the x-grid as well as the y-grid. The only thing I can think of is a complicated for-loop or a giant nest of if loops.
JUST TO CLARIFY :
fromwhat_combobox is a selection of what point to use, IE, 2/5 the distance from which point, Ax and Ay represent the coordinates of that point. The rest is easy to figure.
Complete code...
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
*/
}
Here is the distance formula in java code
Math.sqrt( Math.pow( (p1.getX() - p2.getX()) , 2 )) - Math.pow( p1.getY() - p2.getY()) , 2)));
Read square root of x1 - x2 squared minus y1 - y2 squared
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) );
See Distance Formula