我正在生成的地图图层中的点之间动态生成 WKT 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代码。这假定具有x和y成员的“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));
}