[英]Computing the nearest neighbours of a choosen site in both, a square and triangular lattice
I'm new in programming, and I've been trying to make functions (in C) that compute the nearest neighbours of a site i in a square lattice of NxN sites and, in a triangular lattice.我是编程的新手,我一直在尝试制作函数(在 C 中)来计算 NxN 站点的方格和三角格中站点i的最近邻居。
So far, for the square lattice Square lattice example.至此,以方形格子为例。 We have 4 nearest neighbours is easy, the first four nerest neighbours of a site i can be written as:
我们有 4 个最近的邻居很容易,站点i的前四个最近的邻居可以写为:
The problem is when I consider a triangular lattice triangular lattice example.问题是当我考虑三角格子三角格子示例时。 We have 6 nearest neighbours .
我们有 6 个最近的邻居。 I can't find a similar formula using the label i , for the other sites neigbours than the left and right respectively.
我找不到使用 label i的类似公式,分别用于左侧和右侧以外的其他站点。 Should I include some angles in my formulation?
我应该在我的公式中包含一些角度吗?
Note: The site i is chosen at random, next, I have to find its nearest neighbours in each case.注意:站点i是随机选择的,接下来,我必须在每种情况下找到它最近的邻居。
EDIT: The following, is the code corresponding to the neighbours of a square lattice.编辑:以下是与正方形格子的邻居对应的代码。 There, I'm not considering still periodic boundary conditions.
在那里,我没有考虑仍然是周期性的边界条件。 This program gives us the nearest four neighbours of a site (introduced by the user)
这个程序给了我们一个站点最近的四个邻居(由用户介绍)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define L 5 // The lattice is LxL
int site;
int calculate_neighbours(int i){ //we define a function that calculate the desired neighbours of a site introduced by the user
/*We define functions for each neigbours*/
//Right neighbour
int nr;
nr=i+1;
/*If the selected site is is the right border of the lattice*/
for(int k=0;k<L+1;k++){
if(site==k*L){
nr=L*(k-1)+1;
}
}
printf("\n right= %d",nr);
//////////////////////////////////////////
//Left neighbour
int nl;
nl=i-1;
/*If the selected site is is the left border of the lattice*/
for(int k=1;k<L+1;k++){
if(site==(k-1)*L+1){
nl=L*k;
}
}
printf("\n left= %d",nl);
//////////////////////////////////
//Upper neigbour
int nu;
nu=i-L;
/*If the selected site is in the upper border of the lattice*/
for(int p=L-1;p>-1;p--){
if(site==L-p){
nu=L*L-p;
}
}
printf("\n up= %d",nu);
////////////////////////////////////////
//Bottom neighbour
int nd;
nd=i+L;
/*If The chosen site is in the bottom border*/
for(int p=L-1;p>-1;p--){
if(site==L*L-p){
nd=L-p;
}
}
printf("\n down= %d",nd);
/////////////////////////
/*Main function*/
int main(void){
int cont, M[L][L];
cont=1;
/*Print the matrix elements in order*/
while(cont<L){
printf("The M-matrix is:\n");
for (int m=0;m<L;m++){
printf("\n\n");
for(int n=0;n<L;n++){
M[m][n]=cont++;
printf("%5d",M[m][n]);
}
}
}
/*Print the nearest neighbours*/
printf("\n\nTamaño de la red L= %d",L);
printf("\nNumero de sitios LxL=%d",L*L);
printf("\n\nintroduzca un sitio= ");
scanf("%d",&site);
if(site==0 || site<0){printf("\nDebes introducir un numero mayor a cero!\n");}
else if(site>0 && site<L*L+1){
calculate_neighbours(site);
printf("\n");
}
else {
printf("\nExcediste el tamaño de la red\n");
}
}
I still don't know how to start applying something similar to a triangular lattice.我仍然不知道如何开始应用类似于三角格子的东西。 That is, introducing some angles?
也就是引入一些角度? I'm a bit confused.
我有点困惑。
Consider (X,Y) coordinate of square lattice:考虑方格的 (X,Y) 坐标:
//4x4 Square lattice
Y=0 : 0 1 2 3 //0-3 is X value
Y=1 : 0 1 2 3
Y=2 : 0 1 2 3
Y=3 : 0 1 2 3
Then, let's consider this 4x4 data as a Triangle lattice.然后,让我们将此 4x4 数据视为三角形格。 If you shift the rows with odd Y values to the right by 0.5, the result shape will look like a Triangle lattice.
如果将具有奇数 Y 值的行向右移动 0.5,结果形状将看起来像三角形格子。 And, to consider the neighborhood problem, introduce another coordinate here (U,V) as:
并且,为了考虑邻域问题,在这里引入另一个坐标 (U,V) 为:
//4x4 Triangle lattice
V=0 : 0 2 4 6 //U value for Even rows is {0,2,4,6}
V=1 : 1 3 5 7 //U value for Odd rows is {1,3,5,7}
V=2 : 0 2 4 6
V=3 : 1 3 5 7
On this (U,V) cooridnate system, enumerating the 6-neighbors is very easy:在这个 (U,V) 坐标系上,枚举 6 个邻居非常容易:
(U+2,V) //right
(U-2,V) //left
(U-1,V-1) //up left
(U+1,V-1) //up right
(U-1,V+1) //down left
(U+1,V+1) //down right
All that is left is the coordinate transformation between (X,Y) and (U,V).剩下的就是 (X,Y) 和 (U,V) 之间的坐标变换。 This is also easy:
这也很容易:
//(X,Y) --> (U,V)
inline void XY2UV( int X, int Y, int &U, int &V )
{
U = X*2 + ( Y%2 );
V = Y;
}
//(U,V) --> (X,Y)
inline void UV2XY( int U, int V, int &X, int &Y )
{
X = U / 2;
Y = V;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.