2

我正在生成的地图图层中的点之间动态生成 W​​KT LineString 以在 OpenLayers 中显示。我想让点之间的线弯曲,并且我希望能够根据各种输入变量动态改变曲率。

这适用于网络监控应用程序,我们希望曲率基于点之间的延迟时间(不是原始延迟本身,而是在给定时间段内与“正常”值的偏差)。

虽然一些 GIS 应用程序和数据库支持CircularString对 WKT 的扩展,但 OpenLayers 对此一无所知。

所以我需要从线段中生成一条曲线:

现在,行字符串很简单:

LINESTRING(hop1_long hop1_lat, hop2_long hop2_lat)

我可以确定使线段“弯曲”的唯一方法是插入中间点:

LINESTRING(hop1_long hop1_lat, long1 lat1, long2 lat2, ..., hop2_long hop2_lat)

这对于我们的应用程序来说应该是完全足够的,但是我不知道如何生成中间点!

我假设有众所周知的方法/算法可以在二维平面中从线段中生成“弯曲”线。有没有人对如何实现这一点有任何想法,或者可能有帮助的书籍/文章/在线资源?

更新(2010-08-13):

贝塞尔曲线就是门票,在阅读完基本贝塞尔算法之后,实现起来非常容易。但是我必须编写一些代码来生成控制点。这是我想出的PHP代码。这假定具有xy成员的“Vector2d”类。

函数 get_control_points($to, $from, $mag_scale, $angle) {
  $dirX = $to->x - $from->x;
  $dirY = $to->y - $from->y;

  $mag = sqrt(($dirX * $dirX) + ($dirY * $dirY));
  如果(!$mag){
    返回数组($to, $from);
  }

  $length = $mag * $mag_scale;

  $dirX = $dirX / $mag;
  $dirY = $dirY / $mag;

  $sin = sin($角度);
  $cos = cos($角度);

  $rotX = $cos * $dirX - $sin * $dirY;
  $rotY = $sin * $dirX + $cos * $dirY;
  $rotNegX = $cos * -$dirX - $sin * $dirY;
  $rotNegY = $sin * $dirX - $cos * $dirY;

  // 翻转“向后”曲线的控制点
  如果 ($dirX x;
    $y1 = -$rotNegY * $length + $from->y;
    $x2 = -$rotX * $length + $to->x;
    $y2 = -$rotY * $length + $to->y;
  }
  // 或者生成“正常”控制点
  别的 {
    $x1 = $rotX * $length + $from->x;
    $y1 = $rotY * $length + $from->y;
    $x2 = $rotNegX * $length + $to->x;
    $y2 = $rotNegY * $length + $to->y;
  }

  返回数组(新Vector2d($x2,$y2),新Vector2d($x1,$y1));
}
4

1 回答 1

3

如果您想生成一系列构成曲线的线段,请先查看易于由单个变量参数化的曲线。例如,以 (cx,cy) 为中心、半径为 r 的圆由以下表达式参数化:

(x,y) = (cx,cy)+r*(cos(t), sin(t)),

其中 t 从 0 到 2π。

因此,您可以通过计算值 t k =π×k/30处的圆来创建半圆形,其中 k 从 0 到 30。这将为您提供由 30 个线段组成的半圆弧.

如果您想要更一般的曲线,请查看Bezier 曲线。它们由一个值 t 参数化,该值可以在 0 和 1 之间的均匀间隔内进行评估。

于 2010-08-09T21:35:11.720 回答