如何使用Java查找偏差 [英] How to find the deviation using Java

查看:118
本文介绍了如何使用Java查找偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下代码找出两条绘图线之间的偏差,但是由于某种原因,它感觉很不正确。

I would like to find out the deviation between two drawing lines using the following code but for some reason it just feels wrong.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.*;

public class DrawLine extends JPanel {  

  private static final long serialVersionUID = 1L;

  LinePoint[] lineA = new LinePoint[5];
  LinePoint[] lineB = new LinePoint[5];

  public void paintComponent(Graphics g) {

     for(int i = 0; i < lineA.length; i++) {
         lineA[i] =  new LinePoint();
         lineB[i] =  new LinePoint();

     }

     int initialLocationPoint = 10;
     for(int i = 0; i<lineA.length; i++) {
         lineA[i].x = initialLocationPoint;
         lineA[i].y = 100;
         lineB[i].x = initialLocationPoint;
         lineB[i].y = 100;
         g.drawOval((int)lineA[i].x, (int)lineA[i].y, 1, 1);
         g.drawOval((int)lineB[i].x, (int)lineB[i].y, 1, 1);
         initialLocationPoint +=1; 
     }

     System.out.println("DEVIATION: " + this.calculateDifference());

  }

  public double calculateDifference() {
      double deviation = 0.0;
      double deviationPerCordinate = 0.0;
      for (int i = 0; i < lineA.length; i++) {
          deviationPerCordinate = Math.sqrt( Math.pow(2, (lineB[i].x - lineA[i].x))  + Math.pow(2,(lineB[i].y - lineA[i].y)));
          System.out.println("deviationPerCordinate " + i + ": " + deviationPerCordinate);
          deviation = deviation + deviationPerCordinate;
      }
      return deviation;
  }

  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);

    JFrame frame = new JFrame("Draw Line");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBackground(Color.white);
    frame.setSize(400, 400);

    DrawLine panel = new DrawLine();

    frame.add(panel);

    frame.setVisible(true);
  }

结果:

deviationPerCordinate 0:1.4142135623730951

deviationPerCordinate 0: 1.4142135623730951

deviationPerCordinate 1:1.4142135623730951

deviationPerCordinate 1: 1.4142135623730951

偏差

deviationPerCordinate 3:1.4142135623730951

deviationPerCordinate 3: 1.4142135623730951

deviationPerCordinate 4:1.4142135623730951

deviationPerCordinate 4: 1.4142135623730951

DEVIATION:7.0710678118654755

DEVIATION: 7.0710678118654755

受保护的结果:

这不是返回0?

任何人都知道我在这里做错了吗?是公式吗?

Anyone knows what Am I doing wrong here? Is it the formula?

推荐答案

假定您使用欧几里德距离来计算偏差,则公式是错误的。使用Math库在Java中对值求平方的正确方法是 Math.pow(x,2)。就是说,这个 Math.pow(2,(lineB [i] .x-lineA [i] .x))应该是 Math。 pow((lineB [i] .x-lineA [i] .x),2),对于 y 坐标也是如此。

Asuming that you are using the Euclidean distance to calculate the deviation, you formula is wrong. The correct way to square a value in Java using Math library is Math.pow(x, 2). That being said, this Math.pow(2, (lineB[i].x - lineA[i].x)) should be Math.pow((lineB[i].x - lineA[i].x), 2) and same for the y coordinate.

这篇关于如何使用Java查找偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆