Last active
August 29, 2015 14:13
-
-
Save egisatoshi/9565415977048c1bbb6b to your computer and use it in GitHub Desktop.
Find all triangles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define $points | |
{[3 1] [4 5] [7 7] [8 1] [1 9] [3 8] [3 1]}) | |
(define $on-a-line? | |
(match-lambda [[integer integer] [integer integer] [integer integer]] | |
{[[[$x1 $y1] [$x2 $y2] [$x3 $y3]] | |
(eq? (abs (* (- y2 y1) (- x3 x1))) | |
(abs (* (- y3 y1) (- x2 x1))))]})) | |
; Enumerate triangles | |
(match-all points (list [integer integer]) | |
[<join _ <cons $p1 | |
<join _ <cons $p2 | |
<join _ <cons (& ^?(on-a-line? p1 p2 $) $p3) | |
_>>>>>> | |
[p1 p2 p3]]) | |
;=>{[[3 1] [4 5] [7 7]] [[3 1] [4 5] [8 1]] [[3 1] [7 7] [8 1]] [[4 5] [7 7] [8 1]] [[3 1] [7 7] [1 9]] [[3 1] [8 1] [1 9]] [[4 5] [7 7] [1 9]] [[4 5] [8 1] [1 9]] [[7 7] [8 1] [1 9]] [[3 1] [4 5] [3 8]] [[3 1] [7 7] [3 8]] [[3 1] [8 1] [3 8]] [[3 1] [1 9] [3 8]] [[4 5] [7 7] [3 8]] [[4 5] [8 1] [3 8]] [[4 5] [1 9] [3 8]] [[7 7] [8 1] [3 8]] [[7 7] [1 9] [3 8]] [[8 1] [1 9] [3 8]] [[4 5] [7 7] [3 1]] [[4 5] [8 1] [3 1]] [[4 5] [1 9] [3 1]] [[4 5] [3 8] [3 1]] [[7 7] [8 1] [3 1]] [[7 7] [1 9] [3 1]] [[7 7] [3 8] [3 1]] [[8 1] [1 9] [3 1]] [[8 1] [3 8] [3 1]] [[1 9] [3 8] [3 1]]} | |
; Enumerate tiplets of points on a line | |
(match-all points (list [integer integer]) | |
[<join _ <cons $p1 | |
<join _ <cons $p2 | |
<join _ <cons (& ?(on-a-line? p1 p2 $) $p3) | |
_>>>>>> | |
[p1 p2 p3]]) | |
;=>{[[3 1] [4 5] [1 9]] [[3 1] [4 5] [3 1]] [[3 1] [7 7] [3 1]] [[3 1] [8 1] [3 1]] [[3 1] [1 9] [3 1]] [[3 1] [3 8] [3 1]]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment