我可以获得关于直线多边形外壳算法的帮助吗(如果可能,可以使用javascript)
编辑标题和此处以更好地反映我的问题。 我的第一个描述并没有得到我想要的答案,我也不知道这种形状有一个别致的名字。 我研究了格雷厄姆扫描算法,但它们也不能完全覆盖我想要的答案,因为如果形状变得奇怪,它们往往会“抄近路” 根据我找到的一些文章,我开始写我自己的算法,它确实可以在很多情况下工作,但我不断遇到我似乎无法完全消除的问题,我相信问题可能是我试图计算每个角的地方 这就是我算法的逻辑- 第一个=找到拐角,左上角、右上角、右下角、左下角 第二个=进入一个while循环,向目标移动,从左上角移动到右上角,然后从右下角移动到左下角,再返回左上角 3rd=在该while循环的每次迭代中,我们找到每个磁贴的邻居(如果它们存在于该列表中),并选择下一个要移动到的磁贴,根据优先级决定的顺序,下一个磁贴位于第一位 优先级解释,如果优先级是到达右上角,我们会先尝试向左移动,然后向上、向右和向下移动,但前提是该方向存在瓷砖,并且该瓷砖不是我们所站的上一块瓷砖。 到达右上角后,我们移动优先级列表,使第一优先级变为最后一优先级。所以 优先级=[左、上、右、下] 变成 优先级=[上、右、下、左] 在大多数情况下,我的代码都按预期执行,就像逻辑看起来很合理一样,但也有一些异常时刻,它似乎没有根据我给它的优先级选择正确的磁贴,而是在整个阵列中徘徊。如果有人能在我犯错误的地方帮助我,我将不胜感激 这是我当前的算法代码>我可以获得关于直线多边形外壳算法的帮助吗(如果可能,可以使用javascript),javascript,algorithm,multidimensional-array,polygon,Javascript,Algorithm,Multidimensional Array,Polygon,编辑标题和此处以更好地反映我的问题。 我的第一个描述并没有得到我想要的答案,我也不知道这种形状有一个别致的名字。 我研究了格雷厄姆扫描算法,但它们也不能完全覆盖我想要的答案,因为如果形状变得奇怪,它们往往会“抄近路” 根据我找到的一些文章,我开始写我自己的算法,它确实可以在很多情况下工作,但我不断遇到我似乎无法完全消除的问题,我相信问题可能是我试图计算每个角的地方 这就是我算法的逻辑- 第一个=找到拐角,左上角、右上角、右下角、左下角 第二个=进入一个while循环,向目标移动,从左上角移动到右
draw_hull = function() {
var list = [],
coordinates = [],
original_list = this.territory,
_x = 0, // coords are in arrays, [x,y] style.
_y = 1; // this is just for readers readability
// make list to be looped through, for each tile I deliberately added 4,
//it'll look more readable in the final product.
for (var i = 0; i < original_list.length; i++) {
list.push( [ (original_list[i][_x] * 32) + 8, (original_list[i][_y] * 32) + 8 ] );
list.push( [ (original_list[i][_x] * 32) + 24, (original_list[i][_y] * 32) + 8 ] );
list.push( [ (original_list[i][_x] * 32) + 24, (original_list[i][_y] * 32) + 24 ] );
list.push( [ (original_list[i][_x] * 32) + 8, (original_list[i][_y] * 32) + 24 ] );
}
// find corners
var topleft = 0, topright = 0, bottomright = 0, bottomleft = 0, hull = [];
for (var i = 0; i < list.length; i++) {
var x = list[i][_x];
var y = list[i][1];
if (x <= list[topleft][_x] && y <= list[topleft][_y]) topleft = i;
if (x >= list[topright][_x] && y <= list[topright][_y]) topright = i;
if (x >= list[bottomright][_x] && y >= list[bottomright][_y]) bottomright = i;
if (x <= list[bottomleft][_x] && y >= list[bottomleft][_y]) bottomleft = i;
}
// start drawing paths from one corner to the next, repeating until a full loop has been made
var priorities = ["l","u","r","d"], // travel the outline in this order, left up right down
current = topleft, // current tile
last = topleft; // last tile
target = [topright,bottomright,bottomleft,topleft],
target_iterator = 0, // target iterator so we know which corner we're striving towards
xx = 0, // an iterator to make sure this loop doesnt go on forever
done = false;
while(!done) {
// add the current tile to our hull list.
hull.push(current);
var next = {},
cx = list[current][_x],
cy = list[current][_y];
for (var i = 0; i < list.length; i++) {
var x = list[i][_x], y = list[i][_y];
if (x === cx && y === cy -16) { next.up = i; continue; }
if (x === cx && y === cy +16) { next.down = i; continue; }
if (x === cx -16 && y === cy) { next.left = i; continue; }
if (x === cx +16 && y === cy) { next.right = i; continue; }
}
var i = 0, check = priorities;
for (var i = 0; i < priorities.length; i++) {
var check = priorities[i];
// skip this if next check doesnt exist, or is the tile we just came from
if (next[check] == null || next[check] == undefined|| next[check] === last) continue;
var visited = false;
for (var j = 1; j < hull.length; j++) {
if (hull[j] === next[check]) {
visited = true;
continue;
}
}
if (visited) {
continue;
}
break;
}
last = current;
current = next[check];
xcoords = list[current][_x];
ycoords = list[current][_y];
coordinates.push([xcoords,ycoords]);
if (current === target[target_iterator]) {
priorities.push(priorities.shift());
target_iterator++;
if (target_iterator === 4) break;
}
xx++;
if (xx > 50) {break; debugger;}
}
if (xx < 50) this.hull = coordinates;
}
draw_hull=function(){
变量列表=[],
坐标=[],
原始_列表=此区域,
_x=0,//坐标在数组中,[x,y]样式。
_y=1;//这只是为了读者的可读性
//要循环的制作列表,对于我故意添加的每个瓷砖4,
//在最终产品中,它看起来更具可读性。
对于(变量i=0;ifunction drawShape(coords) {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.beginPath();
for(var i = 0; i < coords.length; i++) {
if(i === 0) {
ctx.moveTo(coords[i].x, coords[i].y);
} else {
ctx.lineTo(coords[i].x, coords[i].y);
}
}
ctx.closePath();
ctx.stroke();
}