首页 > 代码库 > JAVA:距离后方交会算法GUI实现

JAVA:距离后方交会算法GUI实现

编译环境:

操作系统:Win8.1  64位

IDE平台:Visual Studio 2013 Ultimate

一、原理

在待测点P上观测距离距离S1,S2,S3,进而确定P的平面坐标:




二、代码实现

距离后方交会.java

package text;

import java.awt.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.*;
import javax.swing.border.Border;

public class 距离后方交会 extends JFrame
{
   public static final int TEXTAREA_ROWS = 20;
   public static final int TEXTAREA_COLUMNS = 40; 
   public 距离后方交会()
   {
    final JTextField A_X = new JTextField("37477");
      final JTextField A_Y = new JTextField("16307");
      final JTextField B_X = new JTextField("37327");
      final JTextField B_Y = new JTextField("16078");    
      final JTextField C_X = new JTextField("37163");
      final JTextField C_Y = new JTextField("16046");
      final JTextField S_1 = new JTextField("200");
      final JTextField S_2 = new JTextField("200");
      final JTextField S_3 = new JTextField("200");
      JPanel northPanel = new JPanel();
      JPanel WestPanel = new JPanel();
      JLabel pic= new JLabel();
      pic.setIcon(new ImageIcon("G:/workplace/距离后方交会/4.png"));
      WestPanel.add(pic);
      getContentPane().add(WestPanel, BorderLayout.WEST);
      JPanel northPane2 = new JPanel();
    //距离前方交会
          northPane2.setLayout(new GridLayout(3, 4));
          northPane2.add(new JLabel("A_X: ", SwingConstants.CENTER));
          northPane2.add(A_X);
          northPane2.add(new JLabel("A_Y: ", SwingConstants.CENTER));
          northPane2.add(A_Y);
          northPane2.add(new JLabel("B_X: ", SwingConstants.CENTER));
          northPane2.add(B_X);
          northPane2.add(new JLabel("B_Y: ", SwingConstants.CENTER));
          northPane2.add(B_Y);
          northPane2.add(new JLabel("C_X: ", SwingConstants.CENTER));
          northPane2.add(C_X);
          northPane2.add(new JLabel("C_Y: ", SwingConstants.CENTER));
          northPane2.add(C_Y);
          northPane2.add(new JLabel("S1: ", SwingConstants.CENTER));
          northPane2.add(S_1);
          northPane2.add(new JLabel("S2: ", SwingConstants.CENTER));
          northPane2.add(S_2);
          northPane2.add(new JLabel("S3: ", SwingConstants.CENTER));
          northPane2.add(S_3);
      Border etched = BorderFactory.createEtchedBorder();
      Border titled = BorderFactory.createTitledBorder(etched, "距离后方交会");
      northPanel.setBorder(titled);
      getContentPane().add(northPane2, BorderLayout.NORTH);
      
      final JTextArea textArea = new JTextArea(20,40);
      JScrollPane scrollPane = new JScrollPane(textArea);

      getContentPane().add(scrollPane, BorderLayout.CENTER);

      // add button to append text into the text area

      JPanel southPanel = new JPanel();

      JButton insertButton = new JButton("计算P点坐标");
      insertButton.addActionListener(new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
          //坐标初始化
            	Double X1=Double.valueOf(A_X.getText()).doubleValue();
            	Double Y1=Double.valueOf(A_Y.getText()).doubleValue();
            	Double X2=Double.valueOf(B_X.getText()).doubleValue();
            	Double Y2=Double.valueOf(B_Y.getText()).doubleValue();
            	Double X3=Double.valueOf(C_X.getText()).doubleValue();
            	Double Y3=Double.valueOf(C_Y.getText()).doubleValue();
            	Double S1=Double.valueOf(S_1.getText()).doubleValue();
            	Double S2=Double.valueOf(S_2.getText()).doubleValue();
            	Double S3=Double.valueOf(S_3.getText()).doubleValue();
            	
          //A、B、P  	
            	Double D_AB=Math.sqrt(Math.pow((X1-X2),2)+Math.pow((Y1-Y2),2));
            	Double α_AB=Math.acos((D_AB*D_AB+S1*S1-S2*S2)/(2*D_AB*S1));
            	Double α_1=Math.atan(Math.abs(Y2-Y1)/Math.abs(X2-X1));
            	Double α_2=α_AB-α_1;
            	Double XP1=X1+S1*Math.cos(α_2);
            	Double YP1=Y1-S1*Math.sin(α_2);
            	
          //B、C、P
            	Double D_BC=Math.sqrt( Math.pow((X3-X2),2)+Math.pow((Y3-Y2),2));
            	Double α_BC=Math.acos((D_BC*D_BC+S2*S2-S3*S3)/(2*D_BC*S2));
            	Double α_3=Math.atan(Math.abs(Y3-Y2)/Math.abs(X3-X2));
            	Double α_4=α_BC-α_3;
            	Double XP2=X1+S1*Math.cos(α_4);
            	Double YP2=Y1-S1*Math.sin(α_4);
            	
             	textArea.append("=====距离后方交会===="+"\n"+"参考值1:"+ "( " +XP1+","+YP1+")"
                        +  "\n"+"参考值2:"+"( " +XP2+","+YP2+")"
                        +  "\n"+"误差距离约:"+Math.round(miss(XP1,YP1,XP2,YP2))+"米");
            	//textArea.append("测试:"+β_2);
            }
            public double miss(double XP1,double YP1,double XP2,double YP2){
            	return Math.sqrt(Math.pow((XP1-XP2),2)+Math.pow((YP1-YP2),2));
            }
            public double convert(String input){
            	Double a=Double.valueOf(input.split("°")[0]).doubleValue();
            	Double b=Double.valueOf(input.split("°")[1].split("'")[0]).doubleValue();
            	return a+b/60;
            }
         });

     add(insertButton, BorderLayout.SOUTH);
      pack();
   }
}

Test.java

package text;

import java.awt.*;
import javax.swing.*;

public class Test
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               JFrame frame = new 距离后方交会();
               frame.setTitle("距离后方交会");
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

三、运行结果



下载:距离后方交会算法JAVA工程包





JAVA:距离后方交会算法GUI实现