如何删除远离细分的点?

时间:2017-04-04 12:39:51

标签: java algorithm coordinates points

我在这里阅读了如何保持两点之间的点(即:它是段的一部分,有一些不精确):How can I tell if a point is nearby a certain line?

因此,我用Java实现了这个小算法,我的代码是(请注意变量的名称应该是明确的!:):

    List<Cupple> returned = new ArrayList<>(points_to_test);

    for(Cupple c : points_to_test) {

        /*if(c == segment_first_point || c == segment_last_point) {
            continue;
        }*/

        if(Math.abs(Math.abs(
                (segment_last_point.getNumber(0) - segment_first_point.getNumber(0))
                        *
                        (segment_first_point.getNumber(1) - c.getNumber(1))
                        -
                        (segment_first_point.getNumber(0) - c.getNumber(0))
                                *
                                (segment_last_point.getNumber(1) - segment_first_point.getNumber(1))
        )
                /
                Math.sqrt(
                        Math.pow((segment_last_point.getNumber(0) - segment_first_point.getNumber(0)), 2)
                                +
                                Math.pow((segment_last_point.getNumber(1) - segment_first_point.getNumber(1)), 2)
                )

        ) > maximal_allowed_distance) {

            returned.remove(c);
        }
    }

    return returned;

确定你理解:

  1. returned是包含在段上或段附近的点的列表(以及确定点是否在段之外的“不精确”/最大距离是变量:{ {1}})

  2. maximal_allowed_distance是我的图表中存在的所有点:我的段+段中真正的点+几乎在段上的点(&lt; = { {1}})+远离细分的点(&gt; points_to_test)。 我的小算法的想法是我删除所有后者。

  3. maximal_allowed_distance是两段的极端

  4. maximal_allowed_distancesegment_[first|last]_point的当前点,我想知道它是远离段还是(根据c

    < / LI>
  5. points_to_test返回该点的X坐标,maximal_allowed_distance返回Y值。

  6. 然而,它不起作用。它没有返回优点(即:段中的点,考虑到getNumber(0))。

    你知道我是否误解了我在这个问题的第一行给你的答案吗?你看到我自己实现这个算法有什么错误吗?

1 个答案:

答案 0 :(得分:0)

注意,引用的方法确定距无限线的距离。如果您只需要有限段附近的点,您可以修改它。

在任何情况下算法都应该找到远离线的点。如果不这样做,请检查
a)是否找到要删除的要点 b)是否能够从c列表中删除属于points_to_test列表的对象returned

编辑
找到点和线段using vector arithmetics

之间的距离是一种相当有效的方法
// Copyright 2001 softSurfer, 2012 Dan Sunday
// This code may be freely used, distributed and modified for any purpose
// providing that this copyright notice is included with it.
// SoftSurfer makes no warranty for this code, and cannot be held
// liable for any real or imagined damage resulting from its use.
// Users of this code must verify correctness for their application.


// Assume that classes are already given for the objects:
//     Point and Vector with
//          coordinates {float x, y, z;} (z=0  for 2D)
//          appropriate operators for:
//               Point  = Point ± Vector
//               Vector = Point - Point
//               Vector = Scalar * Vector
//     Line with defining endpoints {Point P0, P1;}
//     Segment with defining endpoints {Point P0, P1;}
//===================================================================

// dot product (3D) which allows vector operations in arguments
#define dot(u,v)   ((u).x * (v).x + (u).y * (v).y + (u).z * (v).z)
#define norm(v)     sqrt(dot(v,v))     // norm = length of  vector
#define d(u,v)      norm(u-v)          // distance = norm of difference
// dist_Point_to_Segment(): get the distance of a point to a segment
//     Input:  a Point P and a Segment S (in any dimension)
//     Return: the shortest distance from P to S
float
dist_Point_to_Segment( Point P, Segment S)
{
     Vector v = S.P1 - S.P0;
     Vector w = P - S.P0;

     double c1 = dot(w,v);
     if ( c1 <= 0 )
          return d(P, S.P0);

     double c2 = dot(v,v);
     if ( c2 <= c1 )
          return d(P, S.P1);

     double b = c1 / c2;
     Point Pb = S.P0 + b * v;
     return d(P, Pb);
}