- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
假设我有一个以 0° 旋转存储的数组:
0 0 1 0 0
0 0 1 0 0
1 1 1 0 0
0 0 0 0 0
0 0 0 0 0
如果我通过,我希望它以一个很好的近似值返回,例如 30° 作为参数,它会是这样的:
0 0 0 1 0
1 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
45°会是
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
I am aware of the solutions posted for 90° rotations.但我认为这对我没有帮助?
我没有任何示例代码,因为我目前甚至不知道从哪里开始寻找。如果有任何关键字我可以在谷歌上指出我可以适应的一些公式的方向,那也很好。
Spectre的C#代码解决方案:
class Rotation
{
public Rotation() {
A = new int[xs,ys]{
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{9,9,9,9,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
};
B = new int[xs, ys];
deg = (float)(Math.PI / 180.0);
}
public const int xs = 7; // matrix size
public const int ys = 7;
const int x0 = 3; // rotation center cell
const int y0 = 3;
readonly float deg;
public int[,] A;
public int[,] B;
//---------------------------------------------------------------------------
public void rotcv(float ang) {
rotcw(Rotation.x0, Rotation.y0, ang);
}
private void rotcw(int x0, int y0, float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
{
int x, y, ix0, iy0, ix1, iy1, q;
double xx, yy, fx, fy, c, s;
// circle kernel
c = Math.Cos(-ang); s = Math.Sin(-ang);
// rotate
for (y = 0; y < ys; y++)
for (x = 0; x < xs; x++)
{
// offset so (0,0) is center of rotation
xx = x - x0;
yy = y - y0;
// rotate (fx,fy) by ang
fx = ((xx * c) - (yy * s));
fy = ((xx * s) + (yy * c));
// offset back and convert to ints and weights
fx += x0; ix0 = (int)Math.Floor(fx); fx -= ix0; ix1 = ix0 + 1; if (ix1 >= xs) ix1 = ix0;
fy += y0; iy0 = (int)Math.Floor (fy); fy -= iy0; iy1 = iy0 + 1; if (iy1 >= ys) iy1 = iy0;
// bilinear interpolation A[fx][fy] -> B[x][y]
if ((ix0 >= 0) && (ix0 < xs) && (iy0 >= 0) && (iy0 < ys))
{
xx = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
yy = (A[ix0,iy0]) + ((A[ix1,iy0] - A[ix0,iy0]) * fx);
xx = xx + ((yy - xx) * fy); q =(int) xx;
}
else q = 0;
B[x,y] = q;
}
}
}
测试:
static void Main(string[] args)
{
Rotation rot = new Rotation();
for (int x = 0; x < Rotation.xs; x++) {
for (int y = 0; y < Rotation.xs; y++) {
Console.Write(rot.A[x,y] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
float rotAngle = 0;
while (true)
{
rotAngle += (float)(Math.PI/180f)*90;
rot.rotcv(rotAngle);
for (int x = 0; x < Rotation.xs; x++)
{
for (int y = 0; y < Rotation.xs; y++)
{
Console.Write(rot.B[x, y] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
}
}
最佳答案
好的,这是我们 promise 的。第一个 C++ 代码:
//---------------------------------------------------------------------------
#include <math.h>
//---------------------------------------------------------------------------
const int xs=7; // matrix size
const int ys=7;
const int x0=3; // rotation center cell
const int y0=3;
const float deg=M_PI/180.0;
int A[xs][ys]=
{
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{0,0,0,9,0,0,0},
{9,9,9,9,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0},
};
int B[xs][ys];
//---------------------------------------------------------------------------
void rotcw(int x0,int y0,float ang) // rotate A -> B by angle ang around (x0,y0) CW if ang>0
{
int x,y,ix0,iy0,ix1,iy1,q;
float xx,yy,fx,fy,c,s;
// circle kernel
c=cos(-ang); s=sin(-ang);
// rotate
for (y=0;y<ys;y++)
for (x=0;x<xs;x++)
{
// offset so (0,0) is center of rotation
xx=x-x0;
yy=y-y0;
// rotate (fx,fy) by ang
fx=float((xx*c)-(yy*s));
fy=float((xx*s)+(yy*c));
// offset back and convert to ints and weights
fx+=x0; ix0=floor(fx); fx-=ix0; ix1=ix0+1; if (ix1>=xs) ix1=ix0;
fy+=y0; iy0=floor(fy); fy-=iy0; iy1=iy0+1; if (iy1>=ys) iy1=iy0;
// bilinear interpolation A[ix0+fx][iy0+fy] -> B[x][y]
if ((ix0>=0)&&(ix0<xs)&&(iy0>=0)&&(iy0<ys))
{
xx=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
yy=float(A[ix0][iy0])+(float(A[ix1][iy0]-A[ix0][iy0])*fx);
xx=xx+((yy-xx)*fy); q=xx;
} else q=0;
B[x][y]=q;
}
}
//---------------------------------------------------------------------------
此处 7x7 预览 15 度步长:
可能需要将中心稍微调整一半的单元格或其他东西(我喜欢中心流血太多)
矩阵 A
是源,B
是目标 ...
您还可以添加阈值...例如:
if (q>=5) q=9; else q=0;
关于java - 如何将 2d 阵列旋转小于 90°,以获得最佳近似值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40584764/
我正在为我的雇主编写脚本,以从他们自己的站点获取某些数据。出于一长串原因,我需要从网站上获取数据,如图所示。我发现,其中一些数据是通过 js 调用检索的... 回想起来,我应该选择 Mechanize
我正在使用 python 和 cryptography.io 来签署和验证消息。我可以通过以下方式获得签名的 DER 编码字节表示: cryptography_priv_key.sign(messag
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
是否可以区分 ECDF?以下面得到的为例。 set.seed(1) a <- sort(rnorm(100)) b <- ecdf(a) plot(b) 我想对 b 求导以获得它的概率密度函数 (PD
我找到了如何从 navigator.mimeTypes 获取 mimetypes: function GetMimeTypes() { var message = ""; var mi
我在表单中使用单选按钮来隐藏/显示联系人表单中的成员 ID 字段。问题是,当 javascript 更改 html 中包含的隐藏 id 字段(该字段设置为“无”值)时,该字段将不再通过 post 可用
我正在做单元测试。我必须测试所有可能的if..else情况。但是在此if语句中: int32_t i32Res = snprintf(buffer, len, "The%d_String_%d", 0
我有一个 Facebook 应用程序,我想从中获取“喜欢”的总数。我想知道这是否可能。 其中 ID 是应用程序的 ID,ACCESS_TOKEN 是我尝试过的应用程序的当前访问 token : gra
如果我有多个计算实例尝试同时获取同一个 blob 的租约,则似乎经常会成功。我的印象是,一旦租约发出(并因此被客户获得),就不可能同时发出另一个租约? 我希望情况确实如此,我一直在 Azure 中使用
这是我的索引 POST /blogs/1 { "name" : "learn java", "popularity" : 100 } POST /blogs/2 { "name" : "l
我正在将 Symfony2 与 FOSUserBundle 一起使用。我需要为用户获得最高角色。 role_hierarchy: ROLE_CONTRIBUTOR: ROLE_USER
我正在向服务器发送基于 REST 的请求。我希望尽快得到答复,并希望了解可以进行的各种优化。 一种方法当然是在线程中并行发送这些请求。还有哪些其他选项可用于优化此功能? 在服务器上,可以添加哪些配置?
这可能是某种重复的问题,但我似乎找不到合适的解决方案。我正在使用 git4idea.history.GitHistoryUtils.history() 获取提交列表。如果 checkout 其中一个较
我正在做一个程序,可以输入每周的工资和那一周的总工作时间。它应该以小时工资率显示答案。但是我无法显示正确的“centavos/2 decimal places”公式并且它不想使用 float % fl
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我已经尝试了 mContext.getMainLooper() 和 Looper.getMainLooper()。两者都返回相同的结果,但我想知道哪种方法正确? 我还从 Android 开发人员链接中
我有一个“affiliates”表,其中包含“user”和“referredBy”列。 给定一个用户,我希望获得该用户推荐的所有“n 级”玩家。对于 n=1,我们只关心您直接推荐的玩家数量: SELE
我在 PostgreSQL 9.5 数据库中有两个表: project - id - name task - id - project_id - name - updated_
请帮助我怎样才能得到我预期的结果,在此先感谢并抱歉我的英语不好。 PHP: $dog = implode(',', $data['dogbreed']); $query .= "AND `do
我有 let impulse = CGVectorMake(CGFloat(Constants.impulse), 0) 如何在不创建另一个 CGVector 的情况下得到它的负值? 我正在考虑在 C
我是一名优秀的程序员,十分优秀!