我正在尝试用C++实验2D物理引擎.到目前为止,似乎最受欢迎的是Box2D.不幸的是,Box2D是一个严格的身体物理引擎,并不能真正帮助我想要尝试的东西.
我希望能够定义一个具有多个由弹簧连接的顶点的形状,这样当这个形状与刚性或其他非刚性形状碰撞时,它的形状将是灵活的.
现在我试图想办法在Box2D中只使用刚体来做这个,但似乎总有缺陷:
那么用C++做这种物理的最佳方法是什么?优选地,不必编写整个物理引擎.也许我只是错过了Box2D的一个功能.也许这不是正确的选择.那么什么是正确的选择?
我正在尝试在Mathematica中编写一个程序,它将模拟带电球轴承在充电时充电的方式(它们相互排斥).到目前为止,我的程序使滚珠轴承不会从屏幕上移开,并计算它们撞到盒子侧面的次数.到目前为止,我的球轴承随机移动,但我需要知道如何使它们相互排斥.
到目前为止,这是我的代码:
Manipulate[
(*If the number of points has been reduced, discard points*)
If[ballcount < Length[contents],
contents = Take[contents, ballcount]];
(*If the number of points has been increased, generate some random points*)
If[ballcount > Length[contents],
contents =
Join[contents,
Table[{RandomReal[{-size, size}, {2}], {Cos[#], Sin[#]} &[
RandomReal[{0, 2 \[Pi]}]]}, {ballcount - Length[contents]}]]];
Grid[{{Graphics[{PointSize[0.02],
(*Draw the container*)
Line[size {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}, {-1, -1}}],
Blend[{Blue, Red}, charge/0.3],
Point[
(*Start the main dynamic actions*)
Dynamic[
(*Reset the collision counter*)
collision …Run Code Online (Sandbox Code Playgroud) 在2D像素阵列中,我需要一种有效的算法来选择p%最大的像素.
这可以通过选择点自适应地完成,然后重复调整太靠近的点的位置.但这并不高效,因为它需要多次迭代和距离计算.
它不一定是完美的,它只需要尽可能多地避免点集群.
假设我有一个像这样的2列表:
| user_id | int(11) | NO | UNI | NULL | |
| utm | point | NO | MUL | NULL | |
Run Code Online (Sandbox Code Playgroud)
如您所见,它非常简单.utm是Point数据类型.我这样插入:
INSERT INTO mytable(user_id, utm) VALUES(1, PointFromWKB(point(50, 50)));
Run Code Online (Sandbox Code Playgroud)
然后,我创建一个Spatial索引.
ALTER TABLE mytable ...add spatial index on(utm) or something. (forgot)
Run Code Online (Sandbox Code Playgroud)
好吧,一切都很好.现在,我想选择*距离<99999. 但它不起作用!
//This is supposed to select all where the distance is less than 99999999.
set @mypoint = PointFromWKB(point(20,20))
select * from mytable where GLength(LineString(utm, @mypoint)) < 9999999;
Empty set (0.00 sec)
select …Run Code Online (Sandbox Code Playgroud) 我有一个包含很多多边形的地图,其中一个有一个点,如下所示:

多边形边缘的x和y坐标保存在这样的数据库中(例如):
Polygon(Point(11824, 10756), Point(11822, 10618), Point(11912, 10517), Point(12060, 10529), Point(12158, 10604), Point(12133, 10713), Point(12027, 10812), Point(11902, 10902)),
Polygon(Point(11077, 13610), Point(10949, 13642), Point(10828, 13584), Point(10772, 13480), Point(10756, 13353), Point(10849, 13256), Point(10976, 13224), Point(11103, 13294), Point(11171, 13414), Point(11135, 13558)),
Polygon(Point(11051.801757813, 11373.985351563), Point(11165.717773438, 11275.469726563), Point(11281.733398438, 11255.646484375), Point(11381.07421875, 11333.15625), Point(11440.202148438, 11467.706054688), Point(11404.73046875, 11584.534179688), Point(11301.662109375, 11643.852539063), Point(11169.486328125, 11644.079101563), Point(11067.555664063, 11579.676757813), Point(11018.21484375, 11454.750976563)),
Polygon(Point(12145, 13013), Point(12069.065429688, 13014.67578125), Point(12012.672851563, 12953.833984375), Point(11973.942382813, 12910.14453125), Point(11958.610351563, 12853.736328125), Point(11988.58203125, 12780.668945313), Point(12046.806640625, 12735.046875), Point(12117.080078125, 12729.838867188), Point(12185.567382813, 12743.389648438), Point(12225.575195313, 12803.530273438), Point(12255.934570313, 12859.2109375), …Run Code Online (Sandbox Code Playgroud) 如果我能做到这一点:
>>> from django.contrib.gis.geos import GEOSGeometry
>>> from django.contrib.gis.geos import Point
>>> point = GEOSGeometry('POINT(1 5)')
>>> print point
POINT (1.0000000000000000 5.0000000000000000)
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做:
>>> lat = 1
>>> lon = 5
>>> point = GEOSGeometry('POINT(lat lon)')
GEOS_ERROR: ParseException: Expected number but encountered word: 'lat'
GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSWKTReader_read_r".
Run Code Online (Sandbox Code Playgroud)
如何使用变量创建GEOSGeometry对象?
我现在在这个问题上挣扎了一个小时......
我有一个里面有一个矩形的图像:
这是我为寻找角点而编写的代码:
import cv2
import numpy as np
img = cv2.imread('rect.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
points = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
points = np.int0(points)
for point in points:
x, y = point.ravel()
cv2.circle(img, (x, y), 3, (0, 255, 0), -1)
print(points[0])
print(points[1])
print(points[2])
print(points[3])
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.imwrite('rect.png', img)
Run Code Online (Sandbox Code Playgroud)
这是结果:
如您所见,它运行完美。我想要的是沿着上/下点(x1,x2 - x3,x4)画一条线。
我从现在开始生产的是这个......
cv2.line(img, (points[0]), (points[1]), (0, 255, 0), thickness=3, lineType=8)
cv2.imshow('img', img)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
任何的想法 ?
结果应该是这样的:
两条线必须通过点的坐标。print(points[0])上面给出了下一个输出,例如:
[[561 168]]
[[155 168]] …Run Code Online (Sandbox Code Playgroud) 我有一个基本代表一个屏幕的坐标系.
我有任意数量的职位.例如:
population = [
{x: 100.44, 200.54},
{x: 123.45, 678.9},
{x: 1300.23, 435.81},
{x: 462.23, 468.37},
{x: 956.58, 385.38},
];
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种找到最无人居住点的算法.
白色迷你圆圈代表人口和红色Xs标记点,对我来说似乎非常无人居住:
我的目标是运行一个动画,随机将所有这些白色迷你圆圈移动到随机方向,一旦圆圈离开屏幕,它就会被传送到最无人居住的地点,这样大空空间的数量就会减少.
我试图通过计算从每个整数坐标到每个圆的距离之和,然后选择具有最高距离和的坐标来实现这一点.仅此一点似乎已经非常耗费CPU,但我注意到这个算法会使圆圈传送到我的坐标系的边界.所以我还添加了从每个整数坐标到每个边界整数坐标的距离之和.那时,脚本基本上冻结了.所以这绝对不是正确的方法.
我的想法已经不多了.我想我不需要一个完美的算法,而是一个在精度和性能之间保持平衡的算法.最后,我希望能够在1920x1080画布上每秒多次运行该算法,其中大约有80个这样的小环.理想情况下,算法会有一个参数来调整精度,从而调整它使用的CPU时间.
这是我上面提到的方法.我注释掉导致脚本冻结的行:
let circles = [
{x: 60.44, y: 190.54},
{x: 103.45, y: 18.9},
{x: 390.23, y: 135.81},
{x: 302.23, y: 28.37},
{x: 56.58, y: 85.38},
]
function getDistance(p1, p2) {
return Math.sqrt((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2)
}
function drawCircle(ctx,x,y,r,c) {
ctx.beginPath()
ctx.arc(x, y, r, 0, 2 * Math.PI, …Run Code Online (Sandbox Code Playgroud)Point我根据点数据集创建了 Shapely 对象的列表。我如何绘制下面的点列表?
points = [Point(-4.85624511894443, 37.1837967179202),
Point(-4.855703975302475, 37.18401757756585),
Point(-4.85516283166052, 37.1842384372115),
Point(-4.85343407576431, 37.182006629169),
Point(-4.85347524651836, 37.1804461589773),
Point(-4.855792124429867, 37.18108913443582),
Point(-4.85624511894443, 37.1837967179202)]
Run Code Online (Sandbox Code Playgroud)