要在两个变量之间进行线性插值a并b给出一个分数f,我目前正在使用此代码:
float lerp(float a, float b, float f)
{
return (a * (1.0 - f)) + (b * f);
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能是一种更有效的方法.我正在使用没有FPU的微控制器,因此浮点运算是在软件中完成的.它们相当快,但它仍然可以添加或增加100个周期.
有什么建议?
为了清楚起见,在上面的代码中,我们可以省略指定1.0为显式浮点文字.
说我有以下多边形和点:
>>> poly = Polygon([(0, 0), (2, 8), (14, 10), (6, 1)])
>>> point = Point(12, 4)
Run Code Online (Sandbox Code Playgroud)
我可以计算点到多边形的距离......
>>> dist = point.distance(poly)
>>> print(dist)
2.49136439561
Run Code Online (Sandbox Code Playgroud)
...但我想知道最短距离所测量的多边形边界上的点的坐标.
我最初的方法是通过它与多边形的距离缓冲点,并找到该圆与多边形相切的点:
>>> buff = point.buffer(dist)
Run Code Online (Sandbox Code Playgroud)
但是,我不确定如何计算这一点.两个多边形不相交,所以list(poly.intersection(buff))不会给我这一点.
我是否在正确的轨道上?有更简单的方法吗?
我试图将一个点插入到LineStringShapely 中,然后相应地分割线串。但是,由于精度误差,Shapely 认为插值点不在 上linestring,因此该split操作不起作用。
这是一个例子:
from shapely.ops import split
from shapely.geometry import LineString, Point
### Initialize point and line
line = LineString([(0.123,0.456),(5.678,7.890),(12.135,6.789)])
point = Point(4.785,8.382)
### Interpolate point onto line
new_point = line.interpolate(line.project(point))
print new_point
>> POINT (5.593949278213755 7.777518800043393)
### BUT: line does not intersect the interpolated point
line.intersects(new_point)
>> False
### EVEN THOUGH: distance between them is essentially (not exactly) zero
line.distance(new_point)
>> 0.0
### THEREFORE: line cannot be split using the new …Run Code Online (Sandbox Code Playgroud) 我有一组LineString,这些LineString与其他LineString相交,并且我想在这些交点将LineString分成单独的段。我有一个解决方案,但我认为这不是最好的方法。
假设我们正在处理一个LineString:
>>> import shapely
>>> from shapely.geometry import *
>>> import geopandas as gpd
>>>
>>> MyLine=LineString([(0,0),(5,0),(10,3)])
>>> MyLine
<shapely.geometry.linestring.LineString object at 0x1277EEB0>
>>>
Run Code Online (Sandbox Code Playgroud)
与该LineString相交的2行:
>>> IntersectionLines=gpd.GeoSeries([LineString([(2,1.5),(3,-.5)]), LineString([(5,.5),(7,.5)])])
>>> IntersectionLines
0 LINESTRING (2 1.5, 3 -0.5)
1 LINESTRING (5 0.5, 7 0.5)
dtype: object
>>>
Run Code Online (Sandbox Code Playgroud)
我可以得到交点如下:
>>> IntPoints=MyLine.intersection(IntersectionLines.unary_union)
>>> IntPointCoords=[x.coords[:][0] for x in IntPoints]
>>> IntPointCoords
[(2.75, 0.0), (5.833333333333333, 0.5)]
>>>
Run Code Online (Sandbox Code Playgroud)
然后,我提取起点和终点,并用这些点和相交点创建对,这些对点将用于形成线段:
>>> StartPoint=MyLine.coords[0]
>>> EndPoint=MyLine.coords[-1]
>>> SplitCoords=[StartPoint]+IntPointCoords+[EndPoint]
>>>
>>> def pair(list):
... for i in range(1, len(list)): …Run Code Online (Sandbox Code Playgroud)