【问题标题】:Get points x distance inside a polygon获取多边形内的点x距离
【发布时间】:2020-06-17 22:34:14
【问题描述】:

我想不出办法来做到这一点。我有一个 vector2 点列表,我需要该多边形内的所有点,距离为x

所以我有一个 Green 点的 List 并寻找与各个绿点有 x 距离的 Red 点的 List

我正在考虑获得 2 个虚点,1 个对上一点和下一点的统一。 然后向这两个点的中心移动x 距离。但是如果内角不是 90 度,它就会移到多边形之外。

Vector2 me = point; // point
Vector2 next = n_point; // on left
Vector2 previous = p_point; // on right
//Debug.DrawLine(me, (me - next), Color.green);
// 2 points ep1 & ep2 
Vector2 center = Vector2.Lerp(ep1,ep2, 0.5f); 
Vector2 finalpoint = Vector2.Lerp(me,center,0.1f); //move towards center

我想我想多了。有没有一种超级简单的方法来做到这一点?

【问题讨论】:

  • en.wikipedia.org/wiki/Parallel_curve。它是一个多边形使它更容易,但不是“超级简单”
  • 画一个圆,圆心为红点,半径为距离。然后在圆圈内找到绿色点。九十度不重要。 d= sqrt(x2 + y2).
  • 点的顺序是顺时针还是逆时针?

标签: c# unity3d vector


【解决方案1】:

假设所有边缘都是水平或垂直的,我会简单地分别考虑每种可能的情况。

获取方向向量。

Vector2 from = me - previous;
Vector2 to = next - me;

我还假设总会有转机。即,如果from 是水平的,那么to 是垂直的,反之亦然。 xy0.0f 并且另一个坐标不为零。 我还假设 x 轴指向右侧,y 轴指向上方。 假设点是按顺时针排列的。

float x, y;
if (from.x > 0.0f) { // from points to the right
    y = me.y - distance;
    if (to.y > 0.0f) x = me.x + distance else x = me.x - distance;
} else if (from.x < 0.0f) { // from points to the left
    y = me.y + distance;
    if (to.y > 0.0f) x = me.x + distance else x = me.x - distance;
} else if (from.y > 0.0f) { // from points upwards
    x = me.x + distance;
    if (to.x > 0.0f) y = me.y - distance else y = me.y + distance;
} else { // from.y < 0.0f, points downwards
    x = me.x - distance;
    if (to.x > 0.0f) y = me.y - distance else y = me.y + distance;
}
Vector2 me_inner = new Vector2(x, y);

我希望我得到了所有的迹象。

【讨论】:

  • 这行得通,因为所有假设都是正确的。谢谢。
【解决方案2】:

我想到了两种方法

选项1:

  1. 为每条线定义一条法线,即向外的垂直线
  2. 将每个顶点的法线定义为该顶点所属线的法线的平均值。
  3. 沿法线移动顶点 X 个单位。

这很容易实现,但对于某些类型的几何体可能会出现自相交问题。

选项2:

  1. 为每条线定义一条法线,即向外的垂直线
  2. 沿法线移动每个线段 X 单位。
  3. 为每对连续的线段确定是否:
  4. 两条线段相交,如果相交,则以交点为顶点。即将交点添加到您的点列表中。
  5. 如果它们不相交,则在线条的起点和终点之间插入一条新线段。即将开始和结束顶点都插入到您的点列表中。

这应该可以更好地处理自相交,但可能仍然存在问题。而且实现起来有点麻烦。这在一定程度上取决于您需要新线定位的精确程度,以及它应该处理不同类型的几何体。

【讨论】:

    猜你喜欢
    • 2010-10-28
    • 2016-08-26
    • 2012-06-14
    • 1970-01-01
    • 2021-12-06
    • 2017-01-08
    • 2022-08-06
    • 1970-01-01
    • 2020-01-06
    相关资源
    最近更新 更多