zoukankan      html  css  js  c++  java
  • 点到直线、线段的距离

    补充数学知识:

    直线方程一般式 Ax + By + C = 0
    点斜式 y-y1 = k(x - x1)
    斜切式 y = kx +b
    两点式 (y-y1)/(y2-y1) =(x-x1)/(x2-x1) x1不等于x2 y1不等于要y2
    截距式 x/a + y/b = 1 a,b都不等于0

    斜率,亦称“角系数”,表示一条直线相对于横坐标轴的倾斜程度。一条直线与某平面直角坐标系横坐标轴正半轴方向的夹角的正切值即该直线相对于该坐标系的斜率。
    表达式
    k=tanα=(y2-y1)/(x2-x1)
    对于一次函数y=kx+b(斜截式)k即该函数图像的斜率。
    相关公式
    当直线L的斜率存在时,斜截式y=kx+b 当k=0时 y=b 
    当直线L的斜率存在时,点斜式y2—y1=k(X2—X1),
    当直线L在两坐标轴上存在非零截距时,有截距式ax + by +c =0 
    对于任意函数上任意一点,其斜率等于其切线与x轴正方向的夹角,即tanα  斜率计算:ax+by+c=0中,k=-a/b.   
    直线斜率公式:k=(y2-y1)/(x2-x1)   
    两条垂直相交直线的斜率相乘积为-1:k1*k2=-1

     

    证明:已知一点(x0,y0)  到直线l Ax + By +c =0 的直线距离

     

    PointToLineDis.java

     

    1. package com.mapbar.algorithm;  
    2.   
    3. import com.vividsolutions.jts.geom.Coordinate;  
    4. import com.vividsolutions.jts.geom.Geometry;  
    5. import com.vividsolutions.jts.geom.GeometryFactory;  
    6. import com.vividsolutions.jts.geom.Point;  
    7. import com.vividsolutions.jts.io.ParseException;  
    8. import com.vividsolutions.jts.io.WKTReader;  
    9.   
    10. /** 
    11.  * 点到直线,线段的距离 
    12.  *  
    13.  * @author chenlly 
    14.  *  
    15.  */  
    16.   
    17. public class PointToLineDis {  
    18.   
    19.     private static GeometryFactory factory = new GeometryFactory();  
    20.   
    21.     private static WKTReader reader = new WKTReader();  
    22.   
    23.     // 直线方程一般式 Ax + By + C = 0;  
    24.   
    25.     private double A;  
    26.   
    27.     private double B;  
    28.   
    29.     private double C;  
    30.   
    31.     /** 
    32.      * 求直线方程的一般式 
    33.      *  
    34.      * @param point1 
    35.      * @param point2 
    36.      *            直线l经过的两个点 
    37.      */  
    38.     public void lineExp(Point point1, Point point2) {  
    39.         /** 
    40.          * 由起始点和终止点构成的直线方程一般式的系数A 
    41.          */  
    42.         A = (point1.getY() - point2.getY())  
    43.                 / Math.sqrt(Math.pow((point1.getY() - point2.getY()), 2)  
    44.                         + Math.pow((point1.getX() - point2.getX()), 2));  
    45.   
    46.         /** 
    47.          * 由起始点和终止点构成的直线方程一般式的系数 
    48.          */  
    49.         B = (point2.getX() - point1.getX())  
    50.                 / Math.sqrt(Math.pow((point1.getY() - point2.getY()), 2)  
    51.                         + Math.pow((point1.getX() - point2.getX()), 2));  
    52.   
    53.         /** 
    54.          * 由起始点和终止点构成的直线方程一般式的系数 
    55.          */  
    56.         C = (point1.getX() * point2.getY() - point2.getX() * point1.getY())  
    57.                 / Math.sqrt(Math.pow((point1.getY() - point2.getY()), 2)  
    58.                         + Math.pow((point1.getX() - point2.getX()), 2));  
    59.   
    60.     }  
    61.   
    62.     /** 
    63.      * 点到直线方程的距离 此公式需要证明 
    64.      *  
    65.      * @param x 
    66.      * @param y 
    67.      * @return 
    68.      */  
    69.     public double alLine(double x, double y) {  
    70.         double d = Math.abs(A * (x) + B * (y) + C)/Math.sqrt(Math.pow(A,2)+Math.pow(B,2));  
    71.         return d;  
    72.     }  
    73.   
    74.     /** 
    75.      * 点到线段的距离 计算线段和计算点到直线的距离类似,不同Y点在包含l线段的投影可能不是线段上的点,投影可能在起点之前或者终点之后 
    76.      *  
    77.      * @param x 
    78.      * @param y 
    79.      * @return 
    80.      */  
    81.     public double alSegmentDis(double x, double y, Point point1, Point point2) {  
    82.         double dis = 0;  
    83.         double a, b, c;  
    84.         a = lineDis(point1.getX(), point1.getY(), point2.getX(), point2.getY());// 线段的长度  
    85.         b = lineDis(point1.getX(), point1.getY(), x, y); // point1到点的距离  
    86.         c = lineDis(point2.getX(), point2.getY(), x, y);//point2到点的距离  
    87.         if (c + b == a) {// 点在线段上  
    88.             dis = 0;  
    89.             return dis;  
    90.         }  
    91.         if (c * c >= a * a + b * b) { // 组成直角三角形或钝角三角形,投影在point1延长线上,  
    92.             dis = b;  
    93.             return dis;  
    94.         }  
    95.         if (b * b >= a * a + c * c) {// 组成直角三角形或钝角三角形,投影在point2延长线上,  
    96.             dis = c;  
    97.             return dis;  
    98.         }  
    99.         // 组成锐角三角形,则求三角形的高  
    100.         double p = (a + b + c) / 2;// 半周长  
    101.         double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));// 海伦公式求面积  
    102.         dis = 2 * s / a;// 返回点到线的距离(利用三角形面积公式求高)  
    103.         return dis;  
    104.     }  
    105.   
    106.     /** 
    107.      * 计算两点之间的距离 
    108.      * @param x1 
    109.      * @param y1 
    110.      * @param x2 
    111.      * @param y2 
    112.      * @return 
    113.      */  
    114.     private double lineDis(double x1, double y1, double x2, double y2) {  
    115.         double lineLength = 0;  
    116.         lineLength = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));  
    117.         return lineLength;  
    118.     }  
    119.   
    120.     public static void main(String[] args) throws ParseException {  
    121.         PointToLineDis pl = new PointToLineDis();  
    122.         // 直线经过的两个点  
    123.         String line = "LINESTRING (-5 6, -4 8)";  
    124.         Geometry geometry = reader.read(line);  
    125.         Coordinate[] coords = geometry.getCoordinates();  
    126.         Point point1 = factory.createPoint(coords[0]);  
    127.         Point point2 = factory.createPoint(coords[1]);  
    128.         // 求直线方程一般式  
    129.         pl.lineExp(point1, point2);  
    130.         // 点(x,y)  
    131.         double x = 2;  
    132.         double y = -1;  
    133.         double d1 = pl.alLine(x, y);  
    134.         System.out.println("result:" + d1);// result:9.391485505499116  
    135.   
    136.         double x1 = -3;  
    137.         double y1 = 3;  
    138.         double d2 = pl.alSegmentDis(x1, y1, point1, point2);  
    139.         System.out.println("result:" + d2);// result:3.605551275463989  
    140.   
    141.     }  
    142. }  
  • 相关阅读:
    Azure终于支持大容量虚拟机了-最高32核,448G内存
    Windows Azure 不能ping通的解决方案
    一个使用微软Azure blob实现文件下载功能的实例-附带源文件
    从技术角度看云计算的特点
    DNS记录
    转载:Vue相关开源项目库汇总(史上最全)
    SSL CA
    MVC 5 中启用Session
    2015年的JavaScript:Angular之类的框架将被库取代
    sql server 2014 express
  • 原文地址:https://www.cnblogs.com/bobzhangfw/p/3296794.html
Copyright © 2011-2022 走看看