Quantcast
Viewing all articles
Browse latest Browse all 6

Answer by DavidC for Golfing: How many unit-length squares in a list of 2d coordinates?

Mathematica 65 chars

f@d_ := Length@Select[Subsets[d, {4}], Sort[g@#] == {1, 1, 1, 1, √2, √2} &]

Tests

f[{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 2}, {1, 2}}]

2

f[{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 2}, {1, 2}, {2, 2}, {2, 1}}]

3

f[{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 2}, {1, 2}, {2, 2}, {2, 1}, {2,0}}]

4

f[{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 2}, {1, 2}, {2, 2}, {2, 1}, {2,0}, {9, 9}}]

4

Explanation

Generates all subsets of 4 points and checks all the inter-point distances.The sorted inter-point distances for a unit square are: `{1, 1, 1, 1, √2, √2}.

Length then counts the number of unit squares.


Viewing all articles
Browse latest Browse all 6

Trending Articles