JAVA求线段长度

发布于 2025-01-08 16:24:54 字数 2234 浏览 2 评论 0原文

我想找到一条线的长度。我已经使用鼠标事件初始化了坐标 x1,y1,x2,y2。当我尝试找到如下所示的距离时:

public class LineFunctions {
    static int dpi;
    static double pixel_per_cm;
    public LineFunctions(){
        Toolkit tk;
        tk=Main.frame.getToolkit();
        dpi=tk.getScreenResolution();
        pixel_per_cm=dpi/2.54;
    }
    public double lengthInPixel(int x1,int y1,int x2,int y2){
        double length_in_pixel;
        double x=Math.pow((x2-x1), 2);
        double y=Math.pow((y2-y1), 2);
        length_in_pixel = Math.sqrt(x+y);
        return length_in_pixel;
    }
    public double lengthInCm(int x1,int y1,int x2,int y2){

        double length_in_pixel=lengthInPixel(x1,y1,x2,y2);
        double length_in_cm = length_in_pixel / pixel_per_cm;
        System.out.println("Length in Cm= "+length_in_cm);
        return length_in_cm;
    }
}

Main.java

public class Main {
    public static JFrame frame;
    public static void main(String ar[]) {
        frame=new JFrame("LINE FRAME");
    }
    static class ImageComponent extends JComponent implements MouseListener {
        Line2D line;
            int x1,y1,x2,y2;
            public ImageComponent() {
                addMouseListener(this);
                addMouseMotionListener(this);
    }
            @Override
            public void paintComponent(Graphics g){
                  super.paintComponent(g);
                  Graphics2D gd=(Graphics2D)g;
                  line=new Line2D.Double(x1,y1,x2,y2);
                  gd.draw(line);
            }
            @Override
    public void mousePressed(MouseEvent e) {

        x1=e.getX();
                    y1=e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
                    x2=e.getX();
                    y2=e.getY();
                    LineFunctions lf=new LineFunctions();
                    double length=lf.lengthInCm(x1,y1,x2,y2);
                    System.out.println("Line Length="+length);
                    repaint();                    
    }
    .......................
    ........................
}

此代码从坐标中查找线的长度,但是当我从 RULER 找到屏幕上绘制的线的长度时,它不会我看到的长度与我从上面的代码中得到的长度相同。那么,这有什么问题以及如何找到线的确切长度?

I want to find a length of line. I have initialize a coordinates x1,y1,x2,y2 using mouse Events. When i try to find distance as shown below:

public class LineFunctions {
    static int dpi;
    static double pixel_per_cm;
    public LineFunctions(){
        Toolkit tk;
        tk=Main.frame.getToolkit();
        dpi=tk.getScreenResolution();
        pixel_per_cm=dpi/2.54;
    }
    public double lengthInPixel(int x1,int y1,int x2,int y2){
        double length_in_pixel;
        double x=Math.pow((x2-x1), 2);
        double y=Math.pow((y2-y1), 2);
        length_in_pixel = Math.sqrt(x+y);
        return length_in_pixel;
    }
    public double lengthInCm(int x1,int y1,int x2,int y2){

        double length_in_pixel=lengthInPixel(x1,y1,x2,y2);
        double length_in_cm = length_in_pixel / pixel_per_cm;
        System.out.println("Length in Cm= "+length_in_cm);
        return length_in_cm;
    }
}

Main.java

public class Main {
    public static JFrame frame;
    public static void main(String ar[]) {
        frame=new JFrame("LINE FRAME");
    }
    static class ImageComponent extends JComponent implements MouseListener {
        Line2D line;
            int x1,y1,x2,y2;
            public ImageComponent() {
                addMouseListener(this);
                addMouseMotionListener(this);
    }
            @Override
            public void paintComponent(Graphics g){
                  super.paintComponent(g);
                  Graphics2D gd=(Graphics2D)g;
                  line=new Line2D.Double(x1,y1,x2,y2);
                  gd.draw(line);
            }
            @Override
    public void mousePressed(MouseEvent e) {

        x1=e.getX();
                    y1=e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
                    x2=e.getX();
                    y2=e.getY();
                    LineFunctions lf=new LineFunctions();
                    double length=lf.lengthInCm(x1,y1,x2,y2);
                    System.out.println("Line Length="+length);
                    repaint();                    
    }
    .......................
    ........................
}

This code finds the length of line from the coordinates, but when i find the length of a line drawn on my screen from the RULER it doesn't saw me the same length as i got from the above code. So, What is the Problem with this and how can i find the exact length of line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澜川若宁 2025-01-15 16:24:54

您可以使用 GraphicsDeviceGraphicsConfiguration 的 getNormalizingTransform() 来确定所需的几何图形。特别是,身份变换表示“用户空间中的 72 个单位等于设备空间中的 1 英寸”。

另请考虑 Math.hypot() 以获得更高的准确性。

You can use the getNormalizingTransform() of the GraphicsConfiguration of your GraphicsDevice to determine the required geometry. In particular, an identity transform indicates that "72 units in user space equals 1 inch in device space."

Also consider Math.hypot() for more accuracy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文