convex_hull/test.html

54 lines
1.7 KiB
HTML
Raw Normal View History

2024-12-13 07:20:28 +00:00
<body></body>
<script>
//计算凸包
function convex_hull(points) {
var points = [
{ x: 0, y: 0 }, { x: 1, y: 1 }, { x: 2, y: 2 },
{ x: 0, y: 3 }, { x: 3, y: 0 }, { x: 3, y: 3 },
{ x: 2, y: 1 }, { x: 1, y: 2 }
];
//长度
var n = points.length;
var pointsPtr = Module._malloc(n * 8); //8字节一个点
for (let i = 0; i < n; i++) {
Module.setValue(pointsPtr + i * 8, points[i].x, 'i32');
Module.setValue(pointsPtr + i * 8 + 4, points[i].y, 'i32');
}
//多少点
var resultPtr = Module._malloc(n * 8);
var resultSizePtr = Module._malloc(4);
Module._convexHull(pointsPtr, n, resultPtr, resultSizePtr);
var resultSize = Module.getValue(resultSizePtr, 'i32');
var resultPoints = [];
//读
for (let i = 0; i < resultSize; i++) {
var x = Module.getValue(resultPtr + i * 8, 'i32');
var y = Module.getValue(resultPtr + i * 8 + 4, 'i32');
resultPoints.push({ x: x, y: y });
}
console.log(resultPoints);
// 释放
Module._free(pointsPtr);
Module._free(resultPtr);
Module._free(resultSizePtr);
}
//loadwasm
function loadwasm() {
var wasm_url = "./convex_hull.js";
var script = document.createElement('script');
script.src = wasm_url;
script.onload = function () {
//loadwasm
console.log(Module)
Module.onRuntimeInitialized = function () {
convex_hull()
}
}
document.body.appendChild(script);
}
loadwasm()
</script>