-
Lenguaje
Java usando Applet
-
Descripción
Calcula la distancia entre dos puntos de coordenadas conocidas.
La fórmula final a despejar es:
D² = (X₂-X₁)² + (Y₂-Y₁)²
Donde:
(D) La distancia entre dos puntos.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class DistanciaEntreDosPuntosDeCoordenadas extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField field_x1, field_x2, field_y1, field_y2;
private Label label_distancia;
private Button button;
@Override
public void init() {
field_x1 = new TextField(4);
field_x2 = new TextField(4);
field_y1 = new TextField(4);
field_y2 = new TextField(4);
label_distancia = new Label();
button = new Button("Procesar");
setLayout(new BorderLayout());
Panel panel, subpanel;
panel = new Panel(new BorderLayout());
subpanel = new Panel(new GridLayout(4, 1));
subpanel.add(new Label("Ingresa el valor de x1:"));
subpanel.add(new Label("Ingresa el valor de x2:"));
subpanel.add(new Label("Ingresa el valor de y1:"));
subpanel.add(new Label("Ingresa el valor de y2:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(4, 1));
subpanel.add(field_x1);
subpanel.add(field_x2);
subpanel.add(field_y1);
subpanel.add(field_y2);
panel.add(subpanel);
add(panel, BorderLayout.NORTH);
panel = new Panel(new FlowLayout());
panel.add(button);
add(panel);
panel = new Panel(new BorderLayout());
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(new Label("Valor de distancia:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(label_distancia);
panel.add(subpanel);
add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
double distancia, x1, x2, y1, y2;
try {
x1 = Double.parseDouble(field_x1.getText());
x2 = Double.parseDouble(field_x2.getText());
y1 = Double.parseDouble(field_y1.getText());
y2 = Double.parseDouble(field_y2.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
distancia=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
label_distancia.setText(String.valueOf(distancia));
}
}
import java.awt.*;
import java.awt.event.*;
public class DistanciaEntreDosPuntosDeCoordenadas extends Applet implements ActionListener {
private static final long serialVersionUID = 1L;
private TextField field_x1, field_x2, field_y1, field_y2;
private Label label_distancia;
private Button button;
@Override
public void init() {
field_x1 = new TextField(4);
field_x2 = new TextField(4);
field_y1 = new TextField(4);
field_y2 = new TextField(4);
label_distancia = new Label();
button = new Button("Procesar");
setLayout(new BorderLayout());
Panel panel, subpanel;
panel = new Panel(new BorderLayout());
subpanel = new Panel(new GridLayout(4, 1));
subpanel.add(new Label("Ingresa el valor de x1:"));
subpanel.add(new Label("Ingresa el valor de x2:"));
subpanel.add(new Label("Ingresa el valor de y1:"));
subpanel.add(new Label("Ingresa el valor de y2:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(4, 1));
subpanel.add(field_x1);
subpanel.add(field_x2);
subpanel.add(field_y1);
subpanel.add(field_y2);
panel.add(subpanel);
add(panel, BorderLayout.NORTH);
panel = new Panel(new FlowLayout());
panel.add(button);
add(panel);
panel = new Panel(new BorderLayout());
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(new Label("Valor de distancia:"));
panel.add(subpanel, BorderLayout.WEST);
subpanel = new Panel(new GridLayout(1, 1));
subpanel.add(label_distancia);
panel.add(subpanel);
add(panel, BorderLayout.SOUTH);
button.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent actionEvent) {
double distancia, x1, x2, y1, y2;
try {
x1 = Double.parseDouble(field_x1.getText());
x2 = Double.parseDouble(field_x2.getText());
y1 = Double.parseDouble(field_y1.getText());
y2 = Double.parseDouble(field_y2.getText());
} catch (NumberFormatException numberFormatException) {
return;
}
distancia=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
label_distancia.setText(String.valueOf(distancia));
}
}