How to get the diagonals of a hexagon?

2019-07-14 02:50发布

问题:

The shape is like this:

  A,B,C
 D,E,F,G
H,I,J,K,L
 M,N,O,P
  Q,R,S

and I want to constraint the diagonals to a certain sum, i.e.:

A+D+H = B+E+I+M = ... = L+P+S = CONST_SUM


I thought of flattening the list and trying to work out some mathematical formulas to obtain the correct elements, by computing the hops at each level. So far, I have only this:

matrix([[1,2,3],[4,5,6,7][8,9,10,11,12],[13,14,15,16],[17,18,19]).

check(M) :-
    matrix(M),
    flatten(M, L),
    check_sum(L, 1).

However, the math approach doesn't seem to go well in the paper..Any ideas?


EDIT:

I couldn't work out the math rules (the hops for every diagonal), maybe there is another approach that I should follow, and not the flattening one...

回答1:

One thing you can do is write the hexagon skewed like this:

A,B,C . .
D,E,F,G .
H,I,J,K,L
. M,N,O,P
. . Q,R,S

Then one kind of diagonals will have fixed x entry (“columns”), another fixed y entry (“rows”), and a third fixed xy difference (“descending diagonals”). If you flatten this, you have to somehow represent the empty cells surrounding the shape, and either dkip them while iterating or exclude them from the iteration in the first place. You pay for the simpler formula by extra memory requirements.