通过坐标创建图 - CreateGraphFromCoordinates
函数简介
根据坐标点数据创建图(支持自动连接)。支持数组与对象两种 JSON 格式;可按最大距离阈值连接,并可选择使用欧几里得距离作为边权重。
支持的 JSON 格式:
// 数组格式
[{"name":"A","x":0,"y":0},{"name":"B","x":1,"y":1}]
// 对象格式
{"A":{"x":0,"y":0},"B":{"x":1,"y":1}}
接口名称
CreateGraphFromCoordinates
DLL调用
int64_t CreateGraphFromCoordinates(int64_t instance, char* json,
bool connectAll, double maxDistance,
bool useEuclideanDistance);
参数说明
参数名 | 类型 | 说明 |
---|---|---|
instance | 长整数型 | OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。 |
json | 字符串 | 坐标节点 JSON 数据,支持数组与对象两种格式。 |
connectAll | 布尔型 | 是否连接所有节点,默认 true。 |
maxDistance | 双精度型 | 最大连接距离阈值,默认无穷大(不限制)。 |
useEuclideanDistance | 布尔型 | 是否使用欧几里得距离作为边权重,默认 true。 |
示例
// 创建OLA实例
int64_t instance = CreateCOLAPlugInterFace();
// 使用数组格式坐标数据
char* json = "[{\"name\":\"A\",\"x\":0,\"y\":0},{\"name\":\"B\",\"x\":3,\"y\":4},{\"name\":\"C\",\"x\":6,\"y\":8}]";
// 按欧氏距离、阈值10自动连接全部可连节点
int64_t graphPtr = CreateGraphFromCoordinates(instance, json, true, 10.0, true);
if (graphPtr == 0) {
printf("创建失败\n");
} else {
printf("创建成功\n");
}
// 释放资源
DeleteGraph(instance, graphPtr);
DestroyCOLAPlugInterFace(instance);
返回值
返回图的指针,失败返回0。
注意事项
- 返回的图指针需要调用 DeleteGraph 释放内存。
- connectAll 为 true 时,所有节点间距离小于 maxDistance 的会被连接。
- useEuclideanDistance 为 true 时,边权重为节点间的欧几里得距离。
- 确保 JSON 格式正确;节点名需唯一。