Here you can find the source of distance(int q1, int r1, int q2, int r2)
public static final int distance(int q1, int r1, int q2, int r2)
//package com.java2s; public class Main { public static final int distance(int q1, int r1, int q2, int r2) { return (Math.abs(q1 - q2) + Math.abs(r1 - r2) + Math.abs(q1 + r1 - q2 - r2)) / 2;//from w w w . j a va 2 s . co m } }