- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个二维数组,我想在其中对角迭代。我想限制两点之间的范围,创建一个45 *偏移的矩形区域。
一些例子:
| - - - - - - - - | - - - - - - - - | - - - - - - - -
| - - - * - - - - | - - - * - - - - | - - - - - * - -
| - - 1 1 1 - - - | - - 1 1 1 - - - | - - - - 1 - - -
| - - 1 1 1 1 - - | - 1 1 1 1 1 - - | - - - 1 - - - -
| - - - 1 1 1 1 - | - - 1 1 1 - - - | - - 1 - - - - -
| - - - - 1 1 1 - | - - - * - - - - | - * - - - - - -
| - - - - - * - - | - - - - - - - - | - - - - - - - -
| - - - - - - - - | - - - - - - - - | - - - - - - - -
*
=输入点
1
=指向迭代
最佳答案
这是一个有趣的问题!这是我要解决的方法:
让我们确定9种不同的可能情况:
(我将圆圈放在那里作为引导眼睛的方式。我将两个点可视化为彼此“环绕”。)
A是两个坐标都相同的平凡情况
B和F是基本上只需要画一条对角线的情况
C,D和E是点之间的y距离大于x距离的情况
G,H和I是点之间的x距离大于y距离的情况
从技术上讲,案例B到案例I分别对应两种情况,具体取决于是先给出顶部/左侧坐标还是底部/右侧坐标,但让我们为这些点定义所需的顺序,并在需要时进行切换。
可以将需要处理的区域分解为一个平行四边形(绿色)和两个三角形:
在C,D和E的情况下,可以“垂直”处理平行四边形(即,对于每个x位置,沿垂直方向运行一定数量的点),在G,H和I情况下,可以被“水平”处理。
垂直平行四边形中每一列的高度和水平平行四边形中每一行的宽度仅是两个点的y差与两个点的x差之间的绝对差。
我们仅处理4种情况即可涵盖所有情况:C,E,G和I。B和D可以看作是C的一种特殊情况,其中平行四边形的高度或宽度分别为0。同样,F只是E的特例,H可以与I一起处理。A也可以与C一起抛出,但是由于它很容易识别,因此我们将其分开处理以提高性能。
为了使程序通用,让我为Processor
定义一个接口,该接口与您的数组进行交互,并会为需要处理的所有坐标被调用:
public interface Processor {
public void process(int x, int y);
}
public void process(Processor processor, int x1, int y1, int x2, int y2) {
int dy = Math.abs(y2 - y1);
int dx = Math.abs(x2 - x1);
if (dx<=dy) {
if (dy==0) {
// Case A
processor.process(x1, y1);
return;
}
// Cases B, C, D, E, and F
if (y2>y1) processVertically (processor, x1, y1, x2, y2, dy - dx);
else processVertically (processor, x2, y2, x1, y1, dy - dx);
} else {
// Cases G, H, and I
if (x2>x1) processHorizontally(processor, x1, y1, x2, y2, dx - dy);
else processHorizontally(processor, x2, y2, x1, y1, dx - dy);
}
}
private void processVertically(Processor processor, int x1, int y1, int x2, int y2, int h) {
if (x2<x1) {
// Cases E and F
// Fill in parallelogram
int y = y2;
for (int x=x2; x<=x1; x++) {
for (int dy=0; dy<=h; dy++)
processor.process(x, y-dy);
y--;
}
// Fill in triangles
for (h-=2; h>=0; h-=2) {
x1++; y1++;
x2--; y2--;
for (int dy=0; dy<=h; dy++) {
processor.process(x1, y1+dy);
processor.process(x2, y2-dy);
}
}
} else {
// Cases B, C and D
// Fill in parallelogram
int y = y1;
for (int x=x1; x<=x2; x++) {
for (int dy=0; dy<=h; dy++)
processor.process(x, y+dy);
y++;
}
// Fill in triangles
for (h-=2; h>=0; h-=2) {
x1--; y1++;
x2++; y2--;
for (int dy=0; dy<=h; dy++) {
processor.process(x1, y1+dy);
processor.process(x2, y2-dy);
}
}
}
}
private void processHorizontally(Processor processor, int x1, int y1, int x2, int y2, int w) {
if (y2<y1) {
// Case G
// Fill in parallelogram
int x = x2;
for (int y=y2; y<=y1; y++) {
for (int dx=0; dx<=w; dx++)
processor.process(x-dx, y);
x--;
}
// Fill in triangles
for (w-=2; w>=0; w-=2) {
x1++; y1++;
x2--; y2--;
for (int dx=0; dx<=w; dx++) {
processor.process(x1+dx, y1);
processor.process(x2-dx, y2);
}
}
} else {
// Cases H and I
// Fill in parallelogram
int x = x1;
for (int y=y1; y<=y2; y++) {
for (int dx=0; dx<=w; dx++)
processor.process(x+dx, y);
x++;
}
// Fill in triangles
for (w-=2; w>=0; w-=2) {
x1++; y1--;
x2--; y2++;
for (int dx=0; dx<=w; dx++) {
processor.process(x1+dx, y1);
processor.process(x2-dx, y2);
}
}
}
}
process(...)
方法只是简单地指出我们有9种情况中的哪一种,并且可以直接处理案例A,或者如上所述调用
processHorizontally(...)
或
processVertically(...)
。然后,这些方法首先运行各个平行四边形,然后填充平行四边形周围的三角形。
processHorizontally(...)
和
processVertically(...)
完全相同,只是交换了x和y。
processor.process(...)
,以更优化的方式分别处理案例B和F)。 ..)。
process(...)
可能用负坐标调用!
关于java - 在两个点之间对角地遍历2D数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888720/
"
我真的不知道这个问题以前是否有人问过(我真的找不到) 所以,我正在学习如何创建基本的颜色切换游戏(随机颜色球下降,你需要旋转轮子与相同颜色的球碰撞) 通过这种轮换,我遇到了一个非常大的问题。我需要以某
我必须找到具有 M 对角线和 M << N 的对称方 NxN 矩阵的行列式.有没有比LU分解矩阵更快的方法? 最佳答案 是的,带(ed)矩阵有特殊的方法可以解决复杂度为 O(N*M^2) 的消元问题。
我有一个列数和行数相等的二维 numpy 数组。我想将它们排列成一个更大的阵列,对角线上有较小的阵列。应该可以指定起始矩阵在对角线上的频率。例如: a = numpy.array([[5, 7],
我是一名优秀的程序员,十分优秀!