15,851,335 members

See more:

I'm new to programming, and I've been trying to make functions (in C) that compute the nearest neighbors of a site *i* in a square lattice of NxN sites and, in a triangular lattice.

So far, for the square lattice , we have 4 nearest neighbors is easy, the first four nearest neighbors of a site*i* can be written as:

i+1, for the right neighbor,

i-1, for the left neighbor, and so on for the upper and bottom neighbors.

The problem is when I consider a triangular lattice example. We have 6 nearest neighbors. I can't find a similar formula using the label**C**i, for the other sites' neighbors than the left and right respectively. Should I include some angles in my formulation?

**What I have tried:**

The following is the code corresponding to the neighbors of a square lattice. There, I'm not considering still periodic boundary conditions. This program gives us the nearest four neighbors of a site (introduced by the user)

I still don't know how to apply something similar to a triangular lattice. That is, introducing some angles? I'm a bit confused.

Someone recommended that I need to consider (X,Y) coordinate of square lattice:

Then, let's consider this 4x4 data as a Triangle lattice. If you shift the rows with odd Y values to the right by 0.5, the resulting shape will look like a Triangle lattice. And, to consider the neighborhood problem, introduce another coordinate here (U,V) as:

On this (U,V) coordinate system, enumerating the 6-neighbors should be:

All that is left is the coordinate transformation between (X,Y) and (U,V). This should be:

BUT...I've been struggling with incorporating this info into the code I already have for a square lattice. Could anyone give me some advice regarding this? Thank you very much in advance :)

So far, for the square lattice , we have 4 nearest neighbors is easy, the first four nearest neighbors of a site

i+1, for the right neighbor,

i-1, for the left neighbor, and so on for the upper and bottom neighbors.

The problem is when I consider a triangular lattice example. We have 6 nearest neighbors. I can't find a similar formula using the label

The following is the code corresponding to the neighbors of a square lattice. There, I'm not considering still periodic boundary conditions. This program gives us the nearest four neighbors 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 neighbor 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 apply something similar to a triangular lattice. That is, introducing some angles? I'm a bit confused.

Someone recommended that I need to consider (X,Y) coordinate of square lattice:

//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. If you shift the rows with odd Y values to the right by 0.5, the resulting shape will look like a Triangle lattice. And, to consider the neighborhood problem, introduce another coordinate here (U,V) as:

//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) coordinate system, enumerating the 6-neighbors should be:

(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). This should be:

//(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; }

BUT...I've been struggling with incorporating this info into the code I already have for a square lattice. Could anyone give me some advice regarding this? Thank you very much in advance :)

Comments

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

I had problems including links in my post, sorry.

Here we can visualize both the square and triangle. (note: borrowed from this post: https://stackoverflow.com/a/21660173/327541[^])

L--->1

U--->2

UR-->3

R--->4

D--->5

DL--->6?

Am I correct?

//(X,Y) --> (U,V)

inline void XY2UV( int X, int Y, int &U, int &V )

{

U = X*2 + ( Y%2 );

V = Y;

}

But I've been working with 1 coordinate, not two. Here is where I'm stuck now :(

Btw, thank you very much for your help, it was very useful to clarify things! I will let you know if I come to any conclusions :)

Looking forward to seeing this solved!

Should I open a new thread considering this new approach?

Post it as a solution to your last question and I'll upvote it. Mark it as solved.