2016-11-20 194 views
-7

我有2个坐标(A1和A2),通过直线连接(我的路径)。如何计算给定坐标(B1或B2)与直线的最短距离?如何计算距离路径的最短距离?

A1 and A2 connected by straight line

+2

这是数学问题不是Java? –

+0

咨询谷歌或一本好的数学书籍 – UnholySheep

+0

我对java相当新(并且数学不好)。不知道我应该使用哪个功能。 –

回答

0
  1. 数学

    wikipedia 的线和点之间的最短距离所解释的,可以被计算为 如下:

如果线通过两个点P1 =(X1,Y1)和P2 =(X2,Y2),则(X0,Y0)的距离线的距离是: Blockquote

  • 爪哇implimentaion

    class Point { 
        double x,y; 
    
        Point(double pX, double pY){ 
        this.x= pX; 
        this.y= pY; 
        } 
    
        public double distance(Point A1, Point A2){ 
        double numerator = Math.abs((A2.y - A1.y)*this.x + (A2.x - A1.x)*this.y + A2.x*A1.y - A2.y*A1.x); 
        double denominator = Math.sqrt(Math.pow(A2.y - A1.y,2) + Math.pow(A2.x - A1.x,2)); 
    
        return numerator/denominator;  
        } 
    
    } 
    
  • 为了计算由Points A1A2定义Point B和线之间的距离,可以使用方法distance这样的:

    public static void main (String[] args) throws java.lang.Exception 
    { 
        Point A1 = new Point(0,3); 
        Point A2 = new Point(2,0); 
        Point B = new Point(0,0); 
    
        System.out.println(
         B.distance(A1,A2) 
        ); 
    } 
    

    而且here是代码并在ideone运行。

    但恳求去降压的基本知识,选择了一些很好的和有趣的编码书或啧啧,并给它一去,快乐编码:)