判断 已知点是否在 各点所组成的轨迹内

来源:互联网 发布:手机表格制作软件 编辑:程序博客网 时间:2024/06/06 20:06

package org;

public class PointInLine {

 /**
  * @param args
  */
 public static void main(String[] args) {
  PointInLine su = new PointInLine();
  float[][] a = { { 1, 2 }, { 56, 4 }, { 5, 6 }, { 7, 8 } };
  float[][] b = { { 2, 3 } };
  int c = 5;
  float m = 1f;
  su.pointToLine(a, b,m);
  
 }
 /**
  * 计算已知点是否在已知直线上
  * @param Point1 直线上的第一个点
  * @param Point2 直线上的第二个点
  * @return 返回 flase(不在直线上)或 true(在直线上)
  */
 public boolean pointToLine(float[][] Point1, float[][] Point2) {
  return this.pointToLine(Point1, Point2, 0);
 }

 /**
  *  计算已知点是否在已知直线上,如果没在已知直线上,点到直线的距离是否在限定范围内
  * @param Point1直线上的第一个点
  * @param Point2直线上的第二个点
  * @param bx  int 已知限定距离
  * @return 返回 flase(不在直线上)或 true(在直线上)
  */
 public boolean pointToLine(float[][] Point1, float[][] Point2, int bx) {
  float b;// 截距
  boolean bool=false;// 判断
  if (Point1.length > 0) {
   for (int i = 0; i <= Point1.length; i++) {
    float y2 = Point1[i + 1][1];
    float y1 = Point1[i][1];
    float x2 = Point1[i + 1][0];
    float x1 = Point1[i][0];
    System.out.println("y2   " + y2);
    System.out.println("y1   " + y1);
    System.out.println("x2   " + x2);
    System.out.println("x1   " + x1);
    // k=(y2-y1)/(x2-x1)
    float k = (y2 - y1) / (x2 - x1);
    System.out.println("k值   " + k);
    // b=y-k*x
    b = Point1[i][1] - k * Point1[i][0];
    System.out.println(b + "   为b值");
    // k=(b-Point1[i][j])/x;
    // 如果带入方程,方程式成立,则该点在此路径上,判断为真。否则为假。
    float ax = Point2[0][0];
    float ay = Point2[0][1];
    System.out.println("ax   " + ax);
    System.out.println("ay   " + ay);
    // 将已知点带入方程 是否符合
    if (ay == k * ax + b) {
     bool = true;
     System.out.println(bool + "在轨迹上");
     break;
    } else if(bool=excu(bx, k, b, ax, ay)){
      break;
     }
    }
  }
  return bool;
 }

 /**
  * 计算点到直线距离方法
  * @param bx  int型已知限定距离
  * @param k  斜率
  * @param b  截距
  * @param ax 点的X值
  * @param ay 点的Y值
  */
 public boolean excu(int bx, float k, float b, float ax, float ay) {
  boolean bool;
  // 点到直线的距离 d=am+bn+c/根号下 a*a+b*b
  double d = k * ax + (-ay) + b / Math.sqrt(k * k + 1);
  if (Math.abs(d) <= bx) {
   bool = true;
   System.out.println(bool + "在偏移轨迹上" + "         点到直线的距离为"
     + Math.abs(d));
  } else {
   bool = false;
   System.out.println(bool + "不在轨迹上" + "         点到直线的距离为"
     + Math.abs(d));
  }
  return bool;
 }
 /**
  * 计算点到直线距离方法
  * @param bx float型 已知限定距离
  * @param k  斜率
  * @param b  截距
  * @param ax 点的X值
  * @param ay 点的Y值
  * @return
  */
 public boolean excu(float bx, float k, float b, float ax, float ay) {
  boolean bool;
  // 点到直线的距离 d=am+bn+c/根号下 a*a+b*b
  double d = k * ax + (-ay) + b / Math.sqrt(k * k + 1);
  if (Math.abs(d) <= bx) {
   bool = true;
   System.out.println(bool + "在偏移轨迹上" + "         点到直线的距离为"
     + Math.abs(d));
  } else {
   bool = false;
   System.out.println(bool + "不在轨迹上");
  }
  return bool;
 }

 /**
  *  计算已知点是否在已知直线上,如果没在已知直线上,点到直线的距离是否在限定范围内
  * @param Point1直线上的第一个点
  * @param Point2直线上的第二个点
  * @param bx  float型已知限定距离
  * @return 返回 flase(不在直线上)或 true(在直线上)
  */
 public boolean pointToLine(float[][] Point1, float[][] Point2, float bx) {
  float b;// 截距
  boolean bool=false;// 判断
  if (Point1.length > 0) {
   for (int i = 0; i <= Point1.length; i++) {
    float y2 = Point1[i + 1][1];
    float y1 = Point1[i][1];
    float x2 = Point1[i + 1][0];
    float x1 = Point1[i][0];
    System.out.println("y2   " + y2);
    System.out.println("y1   " + y1);
    System.out.println("x2   " + x2);
    System.out.println("x1   " + x1);
    // k=(y2-y1)/(x2-x1)
    float k = (y2 - y1) / (x2 - x1);
    System.out.println("k值   " + k);
    // b=y-k*x
    b = Point1[i][1] - k * Point1[i][0];
    System.out.println(b + "   为b值");
    // k=(b-Point1[i][j])/x;
    // 如果带入方程,方程式成立,则该点在此路径上,判断为真。否则为假。
    float ax = Point2[0][0];
    float ay = Point2[0][1];
    System.out.println("ax   " + ax);
    System.out.println("ay   " + ay);
    // 将已知点带入方程 是否符合
    if (ay == k * ax + b) {
     bool = true;
     System.out.println(bool + "在轨迹上");
     break;
    } else if(bool=excu(bx, k, b, ax, ay)){
      break;
     }
    }
  }
  return bool;
 }
}