[英]Is there an easier way to sector an image?
I have an HTML file with a script that is given a map that has sectors on it.我有一个带有脚本的 HTML 文件,该脚本给出了一个带有扇区的地图。 Like A1, B2, but there are sub-sectors, 1-9 in each sector.
像A1、B2,但是有子扇区,每个扇区有1-9个。 For example, A1-5, B2-9, etc. As of right now, I have a bunch of IF statements for each sub-sector.
例如,A1-5、B2-9 等。截至目前,我对每个子行业都有一堆 IF 语句。 Here's an example
这是一个例子
if (x > 2093-161/3 && x < 2093 && y < 1932-161*2/3 && y > 1771) { var isnumber = "9"; } if (x > 2093-161*2/3 && x < 2093-161/3 && y < 1932-161*2/3 && y > 1771) { var isnumber = "8"; } if (x > 1932 && x < 2093-161*2/3 && y < 1932-161*2/3 && y > 1771) { var isnumber = "7"; } if (x > 2093-161/3 && x < 2093 && y < 1932-161*1/3 && y > 1932-161*2/3) { var isnumber = "6"; } if (x > 2093-161*2/3 && x < 2093-161/3 && y < 1932-161*1/3 && y > 1932-161*2/3) { var isnumber = "5"; } if (x > 1932 && x < 2093-161*2/3 && y < 1932-161*1/3 && y > 1932-161*2/3) { var isnumber = "4"; } if (x > 2093-161/3 && x < 2093 && y < 161*12 && y > 1932-161*1/3) { var isnumber = "3"; } if (x > 2093-161*2/3 && x < 2093-161/3 && y < 161*12 && y > 1932-161*1/3) { var isnumber = "2"; } if (x > 1932 && x < 2093-161*2/3 && y < 161*12 && y > 1932-161*1/3) { var isnumber = "1"; }
Is there an easier way to do this?有没有更简单的方法来做到这一点?
Basic idea with Math using division and mod to get the remaining.数学的基本思想使用除法和 mod 来得到剩余的。
var width = 2160; var sectorW = 161; function getSector (x) { var sectorX = Math.floor(x / sectorW); var sub = x - (sectorW * sectorX % x); var subSectorX = Math.floor(sub / (sectorW / 3)) return { sectorX: sectorX, subSectorX: subSectorX } } console.log( 100, getSector(100)) console.log( 180, getSector(180))
expanding on it.扩展它。 I am not sure what you real number system looks like... but just playing with it.
我不确定你的实数系统是什么样的......但只是玩它。 Below is the basic idea
下面是基本思路
So a number with 10,10
like A1 a1
所以像
A1 a1
一样有10,10
的数字
+-----------+-----------+-----------+ |A1 |B1 |C1 | | a1 b1 c1 | a1 b1 c1 | a1 b1 c1 | | a2 b2 c2 | a2 b2 c2 | a2 b2 c2 | | a3 b3 c3 | a3 b3 c3 | a3 b3 c3 | +-----------+-----------+-----------+ |A2 |B2 |C2 | | a1 b1 c1 | a1 b1 c1 | a1 b1 c1 | | a2 b2 c2 | a2 b2 c2 | a2 b2 c2 | | a3 b3 c3 | a3 b3 c3 | a3 b3 c3 | +-----------+-----------+-----------+ |A3 |B3 |C3 | | a1 b1 c1 | a1 b1 c1 | a1 b1 c1 | | a2 b2 c2 | a2 b2 c2 | a2 b2 c2 | | a3 b3 c3 | a3 b3 c3 | a3 b3 c3 | +-----------+-----------+-----------+
var width = 2160; var sectorW = 161; function calcalateZone (x) { var sectorX = Math.floor(x / sectorW); var sub = x - (sectorW * sectorX % x); var subSectorX = Math.floor(sub / (sectorW / 3)) return { sector: sectorX, subSector: subSectorX } } function getLetterCode (v, upper) { // use base 36 to get letter code var letterCase = upper ? "toUpperCase" : "toLowerCase"; return (v + 10).toString(36)[letterCase](); } function getSector(x, y){ const coorX = calcalateZone(x); const coorY = calcalateZone(y); return { sector: getLetterCode(coorX.sector, true) + (coorY.sector+1), subSector: getLetterCode(coorX.subSector, false) + (coorY.subSector+1) } } console.log( 100, 100, getSector(100, 100)) console.log( 180, 100, getSector(180, 100)) console.log( 100, 180, getSector(100, 180)) console.log( 180, 180, getSector(180, 180))
First we need to get the x
and y
coordinates relative to each sector, meaning that we need to convert the coordinates to relative ones that are between 0
and 161
.首先,我们需要获得相对于每个扇区的
x
和y
坐标,这意味着我们需要将坐标转换为0
到161
之间的相对坐标。 Like so:像这样:
var xRelativeToSector = x % 161;
var yRelativeToSector = y % 161;
Then we need to convert those relative coordinates into column and row indexes, we have 3 possible columns and 3 possible rows, so:然后我们需要将这些相对坐标转换为列和行索引,我们有 3 个可能的列和 3 个可能的行,所以:
var column = Math.floor(3 * xRelativeToSector / 161);
var row = Math.floor(3 * yRelativeToSector / 161);
Finally, we use column
and row
to calculate the number using this formula row * numberOfColumns + column
.最后,我们使用
column
和row
使用此公式row * numberOfColumns + column
计算数字。 Note that the rows in our case are ordered bottom-to-top, so we use (2 - row)
instead of row
.请注意,我们案例中的行是从下到上排序的,因此我们使用
(2 - row)
而不是row
。 We also need to add 1
to the result because the formula gives us a 0-based index and we want a 1-based index.我们还需要给结果加
1
,因为公式给了我们一个从 0 开始的索引,而我们想要一个从 1 开始的索引。 Like so:像这样:
var isnumber = 3 * (2 - row) + column + 1;
So you can simply group this code in a function and use it to get the subsector number:因此,您可以简单地将此代码分组到一个函数中并使用它来获取子扇区编号:
function getSubSectorNumber(x, y) {
var xRelativeToSector = x % 161;
var yRelativeToSector = y % 161;
var column = Math.floor(3 * xRelativeToSector / 161);
var row = Math.floor(3 * yRelativeToSector / 161);
return 3 * (2 - row) + column + 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.