- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 6x6 的数字数组:
int[][] multi = new int[][]{
{4, 2, 3, 2, 5, 1},
{2, 5, 5, 4, 1, 1},
{2, 4, 6, 7, 2, 4},
{2, 1, 2, 3, 4, 3},
{3, 5, 1, 4, 5, 2},
{1, 2, 1, 4, 1, 2}
};
如果我的起始位置是multi[2][3]
。如何找到数组相对于该值的对角线边缘?例如,在点 multi[2][3]
处,值为 7。对角线应为点 multi[0][1]
、multi[0 ][5]
、multi[4][5]
和 multi[5][0]
。这是我的代码当前的作用:
if (LocationValue == 7) {//find possible moves
//There should be 4 potential moves
ArrayList<Point> Moves = new ArrayList<Point>();
Point DMove;
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x + i, y + i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x - i, y + i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x - i, y - i);
Moves.add(new Point(DMove));
}
for (int i = 0; i < multi.length; i++) {
DMove = new Point(x + i, y - i);
Moves.add(new Point(DMove));
}
ArrayList<Point> AlmostFinalMoves = FindPossibleMoves(Moves); //eliminate impossible moves
ArrayList<Point> FinalMoves = FindSideMoves(AlmostFinalMoves, x, y); //Get bishop moves
System.out.println("Possible Moves: " + FinalMoves);
}//End of IF
此方法然后消除不可能的值:
public static ArrayList<Point> FindPossibleMoves(ArrayList<Point> AllMoves) {
ArrayList<Point> FinalMoves = new ArrayList<Point>();
for (int i = 0; i < AllMoves.size(); i++) {
if (AllMoves.get(i).getX() >= 0 && AllMoves.get(i).getX() <= 5 && AllMoves.get(i).getY() >= 0 && AllMoves.get(i).getY() <= 5) {
FinalMoves.add(AllMoves.get(i));
}
}
return FinalMoves;
}
最后,此方法消除了不在数组边缘的所有移动。
public static ArrayList<Point> FindSideMoves(ArrayList<Point> AllPossibleMoves, int xloc, int yloc) {
ArrayList<Point> AlmostFinalSideMoves = new ArrayList<Point>();
ArrayList<Point> FinalSideMoves = new ArrayList<Point>();
for (int i = 0; i < AllPossibleMoves.size(); i++) {
if (AllPossibleMoves.get(i).getX() == 0) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 5) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 1 || AllPossibleMoves.get(i).getY() == 2 || AllPossibleMoves.get(i).getY() == 3 || AllPossibleMoves.get(i).getY() == 4 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 1) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 2) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 3) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
if (AllPossibleMoves.get(i).getX() == 4) {
if (AllPossibleMoves.get(i).getY() == 0 || AllPossibleMoves.get(i).getY() == 5) {
AlmostFinalSideMoves.add(AllPossibleMoves.get(i));
}
}
}
for (int i = 0; i < AlmostFinalSideMoves.size(); i++) {//Check to see if any possible moves match the original location. If so, do not include in list
if (AlmostFinalSideMoves.get(i).getX() == xloc && AlmostFinalSideMoves.get(i).getY() == yloc) {
//Do Nothing!
} else {
FinalSideMoves.add(AlmostFinalSideMoves.get(i));
}
}
return FinalSideMoves;
}
运行此程序会导致以下结果不正确。
Possible Moves: [java.awt.Point[x=0,y=3], java.awt.Point[x=0,y=5], java.awt.Point[x=2,y=5], java.awt.Point[x=4,y=5], java.awt.Point[x=5,y=3], java.awt.Point[x=5,y=0], java.awt.Point[x=2,y=0], java.awt.Point[x=0,y=1]]
查找二维方形数组中任意随机点的对角线的最简单方法是什么?另外,关于如何简化我的代码的建议将不胜感激。
谢谢!
最佳答案
如果索引之一等于 0 或数组的长度/高度,则数字位于正方形的边缘。假设数组已经是一个正方形(您可以自己检查):
int length = grid.length;
int height = grid[0].length;
假设您有原点的 x 和 y 坐标:
List<Point> findPossibleMoves(int x, int y) {
int length = grid.length;
int height = grid[0].length;
int originalX = x;
int originalY = y;
//add (1, 1), (-1, 1), (-1, -1), (1, -1) to the original position until you reach an edge
}
但是等等,我们真的需要循环吗?如果我们直接向 x 和 y 点添加某个值以一步到达边缘会怎么样?我们如何做到这一点?
以 (2, 3) 为例(数组中的值 = 7)。为了找到 (0, 0) 角点,我们使用以下逻辑:
将此应用到(长度,高度)角:
所有 4 个角都转换为代码:
List<Point> findPossibleMoves(int x, int y) {
List<Point> pointList = new ArrayList<Point>();
int length = grid.length;
int height = grid[0].length;
pointList.add(new Point(x - Math.min(x, y), y - Math.min(x, y)));
pointList.add(new Point(x + Math.min(length - x, y), y - Math.min(length - x, y)));
pointList.add(new Point(x - Math.min(x, height - y), y + Math.min(x, height - y)));
pointList.add(new Point(x + Math.min(length - x, height - y), y + Math.min(length - x, height - y)));
return pointList;
}
可以清理到:
List<Point> findPossibleMoves(int x, int y) {
List<Point> pointList = new ArrayList<Point>();
int length = grid.length;
int height = grid[0].length;
int to00 = Math.min(x, y);
int toL0 = Math.min(length - x, y);
int to0H = Math.min(x, height - y);
int toLH = Math.min(length - x, height - y);
pointList.add(new Point(x - to00, y - to00));
pointList.add(new Point(x + toL0, y - toL0));
pointList.add(new Point(x - to0H, y + to0H));
pointList.add(new Point(x + toLH, y + toLH));
return pointList;
}
关于java - 查找二维数组相对于任意位置的对角线边缘 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38468430/
当我编译我的 ionic 4 应用程序时,我得到这个错误:不开始。但是,如果我更改代码中的某些内容并再次编译,代码将成功编译。我查了一下它可能与路径(绝对/相对)有关。但在这些问题中,解决方案是使用相
现在我发现当我打开终端时,它总是显示这些消息: bash: /usr/local/texlive/2012/texmf/doc/man:: No such file or directory bash
我有一些具有不同 url 前缀的嵌套模块。现在我想在一个模块中导航而不指定前缀(在我的模块中,我不想知道在哪个前缀下可以访问该模块)。 这些是我的 app.module 的路线: const APP_
我想在java中进行日期范围搜索假设我想搜索从2019年10月22日到当前日期。但问题是在两周的 block 大小中进行日期范围搜索(考虑到这可能会有所不同,但以周为单位),例如这里开始日期将为 20
我正在尝试实现移动到单击鼠标的点。 但是我遇到了 X 轴镜像行为的问题。当我点击顶部 -> 它移动到底部,当我点击底部 -> 它移动到顶部。 这里是原始位置的例子 我点击了屏幕上有红十字的位置。 但它
尝试使用以下方法让对象跟随光标: int mx =(int) MouseInfo.getPointerInfo().getLocation().getX()-50; Player.setX(mx);
我有 4 个 JButton 设置在彼此下方。我希望当用户水平调整框架大小时它们左右移动。 例如:帧大小:400,400 按钮位置:300,200 现在我将框架大小调整为:600,400 Button
我想要做的是将一个元素(我用作背景的彩色 UIView)定位到我的 Storyboard 中,以便它从 ImageField 的中间开始。并填充所有内容,直到屏幕底部。我正在使用 xcode7 和 s
我正在编写一些 C++ 代码,它主要为共享它的其他两个项目提供一个类,但也包括一个小程序,以便在需要时可以从命令行使用它。该类必须加载一些资源,这些资源被写入资源文件夹中的多个文件。这些文件的路径当然
我能以某种方式随时获得相对于帧初始引用的加速度矢量吗? (我的意思是:xArbitraryZVertical 模式下的帧引用,我第一次得到 Core Motion 数据)我试图做什么:每次我得到 CM
saved 我希望 div#one 在父 div 的左边缘和提交按钮的左边缘之间的空间居中。 最佳答案 还有几种方法: saved 很难说 saved 文本没有居中在容器 d
所以我在页面上有一个带有背景图像的对象,在 mousemove 上它移动了相对于鼠标的背景位置。但我遇到的唯一问题是在鼠标进入对象时将背景图像动画化到鼠标的当前位置。 我能够将背景位置动画化回其原始位
我的一个网站中有一个图像缩放属性。我想相对于 div 的中心缩放图像。 缩放功能如下。 function zoom(zm) { img=document.getElementById("pic"
我正在尝试调整水平导航栏左侧的导航栏 Logo 的大小,然后让其余导航栏元素占据相同的垂直空间并在空间中垂直居中。导航栏元素目前不使用完整的垂直空间。我尝试过的每个尺寸属性都产生了另一个问题。感谢所有
我有一些 h2 文本当前在移动 View 中左对齐,位于居中的 div 上方。我怎样才能将它对齐到移动 View 中相对于 div 的左对齐(应用下面的 CSS 中提供的媒体查询)? CodePen
我想让文本元素(在本例中为 h2 副标题)居中,方法是让它忽略左侧的 float 图像。我更喜欢 h2 副标题与 h1 标题垂直居中对齐。有没有什么办法可以单独使用 CSS 来做到这一点? 这是一个示
您好! 我在这个网站上工作,但我一直遇到同样的问题。当我把边距放在百分比而不是像素时,它似乎是从包装器或页面中获取百分比。可能是一些愚蠢的错误,但我并没有真正使用过百分比。无论如何,我所说的类是“ L
我很好奇这是怎么做到的,我做了类似的东西,例如 Home About Search bar Container
我希望我的 firstLabel 位于同一行文本字段的右侧。 //css input[type="text"]{ display:block; margin-botto
我正在尝试绘制多边形,并希望能够单击我的框架以获取鼠标坐标,以便更快地将心理图像转换为 x/y 值。 我在用 System.out.println("("+ MouseInfo.getPointerI
我是一名优秀的程序员,十分优秀!