[ create a new paste ] login | about

Link: http://codepad.org/yLQp846C    [ raw code | output | fork ]

fisherro - Scheme, pasted on Jan 8:
;;; http://programmingpraxis.com/2013/01/02/four-points-determine-a-square/

(define candidates
        '(((0 0) (0 1) (1 1) (1 0)) ; the unit square
          ((0 0) (2 1) (3 -1) (1 -2)) ; square not aligned to axis
          ((0 0) (1 1) (0 1) (1 0)) ; unit square, in different order
          ((0 0) (0 2) (3 2) (3 0)) ; rectangle
          ((0 0) (3 4) (8 4) (5 0)) ; rhombus
          ((0 0) (0 0) (1 1) (0 0)) ; degenerate
          ((0 0) (0 0) (1 0) (0 1)))) ; degenerate

(define (distance a b)
 (sqrt (+ (expt (- (car a)
                   (car b))
                2)
          (expt (- (cadr a)
                   (cadr b))
                2))))

;;; First, find the distances from the first point and each of the other three.
;;; Then look to see if two of them are the same. If not, it isn't a square.
;;; The third distance will be the diagonal. If it is the same as the distance
;;; between the other two points, then we have a square.
(map (lambda (points)
      (let ((distances (map (lambda (point)
                              (distance point (car points)))
                            (cdr points))))
           (display
            (cond ((= (car distances) (cadr distances))
                   (= (distance (cadr points) (caddr points))
                      (caddr distances)))
                  ((= (car distances) (caddr distances))
                   (= (distance (cadr points) (cadddr points))
                      (cadr distances)))
                  ((= (cadr distances) (caddr distances))
                   (= (distance (caddr points) (cadddr points))
                      (car distances)))
                   (else #f)))
           (newline)))
     candidates)


Output:
1
2
3
4
5
6
7
#t
#t
#t
#f
#f
#f
#f


Create a new paste based on this one


Comments: