Here you can find the source of distance(int q1, int r1, int q2, int r2)
public static int distance(int q1, int r1, int q2, int r2)
//package com.java2s; //License from project: Apache License public class Main { /**//from w w w . j a va 2s . c om * Distance between two hexes. If neighbors the distance is 1. */ public static int distance(int q1, int r1, int q2, int r2) { int x1 = q1; int z1 = r1; int x2 = q2; int z2 = r2; int y1 = -(x1 + z1); int y2 = -(x2 + z2); return (Math.abs(x1 - x2) + Math.abs(y1 - y2) + Math.abs(z1 - z2)) / 2; } }