[ create a new paste ] login | about

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

hurracane - Scheme, pasted on Feb 27:
  (define (touching_rectangle-circle? otherObj)
    ;I know rectangle is the first object because the type is 'rectangle-circle
    (let* ((width (obj 'getWidth))
           (height (obj 'getHeight))
           (obj_X ((obj 'getOrigin) 'getX))
           (obj_Y ((obj 'getOrigin) 'getY))
           (otherObj_X ((otherObj 'getOrigin) 'getX))
           (otherObj_Y ((otherObj 'getOrigin) 'getY))
           (radius (obj 'getRadius))
           (circleDistance_X (abs (- otherObj_X obj_X (/ width 2))))
           (circleDistance_Y (abs (- otherObj_Y obj_Y (/ height 2)))))
      
      ;(println circleDistance_X "-" circleDistance_Y "-" (otherObj 'getOrigin))
      (cond ;Easy cases where the circle could not possibly touch the rect
        ((> circleDistance_X (+ (/ width 2) radius)) #f)
        ((> circleDistance_Y (+ (/ height 2) radius)) #f)
        ;Intersection is guaranteed, excludes calculation for corners
        ((<= circleDistance_X (/ width 2)) #t)
        ((<= circleDistance_Y (/ height 2)) #t)
        ;The difficult case where it may be touching the corner
        (else (let ((cornerDistance_squared (+ (expt (- circleDistance_X (/ width 2)) 2)
                                               (expt (- circleDistance_Y (/ height 2)) 2))))
                (<= cornerDistance_squared (expt radius 2)))))))


Output:
No errors or program output.


Create a new paste based on this one


Comments: