如何在坐标系中找到菱形半径? Javascript
How to find rhombus shaped radius in coordinate system? Javascript
我有一张与坐标系对齐的地图。现在我想调整我的 isEmptyWithin(x,y,radius)
函数以获得菱形结果而不是方形结果!
我得到的输出 (x=6,y=3,radius=1)
:
"2,5", "3,5", "4,5", "2,6", "3,6", "4,6", "2,7", "3,7", "4,7"
我寻求的输出(x=6,y=3,radius=1)
:
"3,5", "2,6", "3,6", "4,6", "3,7"
我得到的输出 (x=6,y=3,radius=2)
:
"1,4", "2,4", "3,4", "4,4", "5,4", "1,5", "2,5", "3,5", "4,5", "5,5", "1,6", "2,6", "3,6", "4,6", "5,6", "1,7", "2,7", "3,7", "4,7", "5,7", "1,8", "2,8", "3,8", "4,8", "5,8"
我寻求的输出(x=6,y=3,radius=2)
:
"3,4", "2,5", "3,5", "4,5", "1,6", "2,6", "3,6", "4,6", "5,6", "2,7", "3,7", "4,7", "3,8"
这里有一张图来展示我所寻求的!红色半径 = 1,蓝色半径 = 2,粉色半径 = 3
粉色和蓝色还应包括内部地图图块。
这是我当前的脚本,它将地图上的区域显示为正方形而不是菱形:
function isEmptyWithin1(x,y,radius){
var testXmin = x-radius;
var testYmin = y-radius;
var testXmax = x+radius;
var testYmax = y+radius;
let arr_xy = [];
for (let iy=testYmin;iy<=testYmax;iy++){
for(let ix=testXmin;ix<=testXmax;ix++){
for(let key in map)
{
if((map[key]["xy"]==(ix+","+iy))&&(map[key]["type"]!="grass")){
map[key]["type"] = "red";
arr_xy[arr_xy.length] = ix+","+iy;
}
}
}
}
if(arr_xy.length==0){
return false;
}
}
我找到了解决问题的方法!
首先我根据半径计算直径,然后按行转换数组
var int_diameter = 1+(radius*2);
for(var a = 0;a<int_diameter;a++){
test_arrayROWgen[a] = [];
test_arrayROWgen[a] = test_array.slice((a*int_diameter),((int_diameter)+(a*int_diameter)));
}
现在我有一个新的临时数组拆分成行。
我简单地将我给定的半径递减为 int_remove
并用它来删除每行开头和结尾的 int_remove
个数组条目。
一行完成后,我递减 int_remove
,如果 int_remove
为 ==0,我从递减切换到递增。
所以给定 radius=2
它将在下面的 5 rows
开始和结束 (2,1,0,1,2)
items/entrys
中删除
for(var i=0;i<test_arrayROWgen.length;i++){
if(int_remove!=0){
//remove at end of row
test_arrayROWgen[i].splice(test_arrayROWgen[i].length-int_remove,int_remove);
//remove at start of row
test_arrayROWgen[i].splice(0,int_remove);
}
int_remove = int_remove+int_sign;
if(int_remove==0){
int_sign = 1;
}
}
最后我在我的原型中得到了这样的结果:
我有一张与坐标系对齐的地图。现在我想调整我的 isEmptyWithin(x,y,radius)
函数以获得菱形结果而不是方形结果!
我得到的输出 (x=6,y=3,radius=1)
:
"2,5", "3,5", "4,5", "2,6", "3,6", "4,6", "2,7", "3,7", "4,7"
我寻求的输出(x=6,y=3,radius=1)
:
"3,5", "2,6", "3,6", "4,6", "3,7"
我得到的输出 (x=6,y=3,radius=2)
:
"1,4", "2,4", "3,4", "4,4", "5,4", "1,5", "2,5", "3,5", "4,5", "5,5", "1,6", "2,6", "3,6", "4,6", "5,6", "1,7", "2,7", "3,7", "4,7", "5,7", "1,8", "2,8", "3,8", "4,8", "5,8"
我寻求的输出(x=6,y=3,radius=2)
:
"3,4", "2,5", "3,5", "4,5", "1,6", "2,6", "3,6", "4,6", "5,6", "2,7", "3,7", "4,7", "3,8"
这里有一张图来展示我所寻求的!红色半径 = 1,蓝色半径 = 2,粉色半径 = 3
粉色和蓝色还应包括内部地图图块。
这是我当前的脚本,它将地图上的区域显示为正方形而不是菱形:
function isEmptyWithin1(x,y,radius){
var testXmin = x-radius;
var testYmin = y-radius;
var testXmax = x+radius;
var testYmax = y+radius;
let arr_xy = [];
for (let iy=testYmin;iy<=testYmax;iy++){
for(let ix=testXmin;ix<=testXmax;ix++){
for(let key in map)
{
if((map[key]["xy"]==(ix+","+iy))&&(map[key]["type"]!="grass")){
map[key]["type"] = "red";
arr_xy[arr_xy.length] = ix+","+iy;
}
}
}
}
if(arr_xy.length==0){
return false;
}
}
我找到了解决问题的方法!
首先我根据半径计算直径,然后按行转换数组
var int_diameter = 1+(radius*2);
for(var a = 0;a<int_diameter;a++){
test_arrayROWgen[a] = [];
test_arrayROWgen[a] = test_array.slice((a*int_diameter),((int_diameter)+(a*int_diameter)));
}
现在我有一个新的临时数组拆分成行。
我简单地将我给定的半径递减为 int_remove
并用它来删除每行开头和结尾的 int_remove
个数组条目。
一行完成后,我递减 int_remove
,如果 int_remove
为 ==0,我从递减切换到递增。
所以给定 radius=2
它将在下面的 5 rows
开始和结束 (2,1,0,1,2)
items/entrys
for(var i=0;i<test_arrayROWgen.length;i++){
if(int_remove!=0){
//remove at end of row
test_arrayROWgen[i].splice(test_arrayROWgen[i].length-int_remove,int_remove);
//remove at start of row
test_arrayROWgen[i].splice(0,int_remove);
}
int_remove = int_remove+int_sign;
if(int_remove==0){
int_sign = 1;
}
}
最后我在我的原型中得到了这样的结果: