Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 Ci, 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)

#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 :)
Posted
Updated 3-Jan-23 5:27am
v2
Comments
Graeme_Grant 2-Jan-23 21:13pm    
If I am understanding correctly, you have triangles next to each other. The first will have point at the top and a base at the bottom, then the next will be inverted. Correct?
Auyik 3-Jan-23 11:13am    
@Graeme_Grant, yes you are correct. It's like the diagram showed in this link: https://i.stack.imgur.com/1N4pq.png

I had problems including links in my post, sorry.
Auyik 3-Jan-23 11:31am    
I've included some links showing diagrams for both the square and triangular lattices I was talking about :)
[no name] 3-Jan-23 10:58am    
I count 8 "neighbours" in a square if you include the diagonals.
Auyik 3-Jan-23 11:16am    
Yes, you are right. But for now, I only considered 4 nearest neighbors and ignore the diagonals when I was talking about the square grid:)

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