有没有更简单的方法来分割图像?

我有一个带有脚本的 HTML 文件,该文件给出了一个上面有扇区的地图。和A1、B2一样,但是有子扇区,每个扇区有1-9。例如,A1-5、B2-9 等。截至目前,我对每个子行业都有一堆 IF 语句。这是一个例子


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";

    }

有没有更简单的方法来做到这一点?

http://img3.mukewang.com/646f27220001918a06590667.jpg


皈依舞
浏览 103回答 2
2回答

Qyouu

首先我们需要得到相对于每个扇区的x和坐标,这意味着我们需要将坐标转换为和之间的相对坐标。像这样:y0161var xRelativeToSector = x % 161;var yRelativeToSector = y % 161;然后我们需要将这些相对坐标转换成列索引和行索引,我们有 3 种可能的列和 3 种可能的行,所以:var column = Math.floor(3 * xRelativeToSector / 161);var row = Math.floor(3 * yRelativeToSector / 161);最后,我们使用column和row来计算使用此公式的数字row * numberOfColumns + column。请注意,我们案例中的行是从下到上排序的,因此我们使用(2 - row)代替row。我们还需要添加1到结果中,因为公式给了我们一个从 0 开始的索引,而我们想要一个从 1 开始的索引。像这样:var isnumber = 3 * (2 - row) + column + 1;因此,您可以简单地将这段代码分组到一个函数中,并使用它来获取子部门编号:function getSubSectorNumber(x, y) {&nbsp; &nbsp; var xRelativeToSector = x % 161;&nbsp; &nbsp; var yRelativeToSector = y % 161;&nbsp; &nbsp; var column = Math.floor(3 * xRelativeToSector / 161);&nbsp; &nbsp; var row = Math.floor(3 * yRelativeToSector / 161);&nbsp; &nbsp; return 3 * (2 - row) + column + 1;}

哔哔one

数学的基本思想是使用除法和 mod 来得到余数。var width = 2160;var sectorW = 161;function getSector (x) {&nbsp; &nbsp;var sectorX = Math.floor(x / sectorW);&nbsp; &nbsp;var sub = x - (sectorW * sectorX % x);&nbsp; &nbsp;var subSectorX = Math.floor(sub / (sectorW / 3))&nbsp; &nbsp;return {&nbsp; &nbsp; &nbsp;sectorX: sectorX,&nbsp;&nbsp; &nbsp; &nbsp;subSectorX: subSectorX&nbsp; &nbsp;}}console.log( 100, getSector(100))console.log( 180, getSector(180))扩展它。我不确定你的实数系统是什么样的......但只是玩弄它。下面是基本思路所以一个数字10,10像A1 a1+-----------+-----------+-----------+|A1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|B1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|C1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 ||&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 ||&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |+-----------+-----------+-----------+|A2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|B2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|C2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 ||&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 ||&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |+-----------+-----------+-----------+|A3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|B3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|C3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;||&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 |&nbsp; a1 b1 c1 ||&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 |&nbsp; a2 b2 c2 ||&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |&nbsp; a3 b3 c3 |+-----------+-----------+-----------+var width = 2160;var sectorW = 161;function calcalateZone (x) {&nbsp; &nbsp;var sectorX = Math.floor(x / sectorW);&nbsp; &nbsp;var sub = x - (sectorW * sectorX % x);&nbsp; &nbsp;var subSectorX = Math.floor(sub / (sectorW / 3))&nbsp; &nbsp;return {&nbsp; &nbsp; &nbsp;sector: sectorX,&nbsp;&nbsp; &nbsp; &nbsp;subSector: subSectorX&nbsp; &nbsp;}}function getLetterCode (v, upper) {&nbsp;// use base 36 to get letter code&nbsp;var letterCase = upper ? "toUpperCase" : "toLowerCase";&nbsp;return (v + 10).toString(36)[letterCase]();}function getSector(x, y){&nbsp; const coorX = calcalateZone(x);&nbsp; const coorY = calcalateZone(y);&nbsp; return {&nbsp; &nbsp; sector: getLetterCode(coorX.sector, true) + (coorY.sector+1),&nbsp; &nbsp; subSector: getLetterCode(coorX.subSector, false) + (coorY.subSector+1)&nbsp; }}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))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript