Intersecting Rectangles in Racket

164 Views Asked by At

#lang racket

(define(rectangleList list rectangle1)(recursion list rectangle1'()))
;(define rectangleList(list '(2 4 6 1)'(1 8 4 4)'(0 5 4 0)))
;(define rectangle1 '(1 3 5 2))

(define(recursion rectangleList rectangle1 returnedList)
 (if(<(length rectangleList)1)
   returnedList

 (recursion(cdr rectangleList) rectangle1
  (if(Intersect(car rectangleList)rectangle1)
    (cons (car rectangleList) returnedList)
    returnedList
    )
 )

) )

(define(Intersect rectangleList rectangle1)
 (and(and(<(car rectangleList) (cadr rectangle1)))
      (and(> (cadr rectangleList) (car rectangle1)))
      (and(< (cdr rectangleList)(caddr rectangle1)))
      (and(> (caddr rectangleList) (cdr rectangle1)))))

I am having a problem with calling my racket code. I am unsure how to proceed. The code is taking a list of rectangles and then also taking a rectangle and seeing if the singular rectangle intersects with any of the other rectangles in the list. Then it should output the list of rectangles that it intersects with i.e Test Cases.

Problem Statement: Given a rectangle, R, and a list of rectangles, L, return the list containing the elements in L that intersect with R.

Any help on this matter would be greatly appreciated! :)

1

There are 1 best solutions below

2
MareeSky On
#lang racket

(define(rectangleList list rectangle1)(recursion list rectangle1'()))
;(define rectangleList(list '(2 4 6 1)'(1 8 4 4)'(0 5 4 0)))
;(define rectangle1 '(1 3 5 2))

;Recusion function
(define(recursion rectangleList rectangle1 returnedList)
  (if(<(length rectangleList)1)
    returnedList

 (recursion(cdr rectangleList) rectangle1
  (if(Intersect(car rectangleList)rectangle1)
    (cons (car rectangleList) returnedList)
    returnedList
    )
 )

) )

;Intersect function      
 (define(Intersect rectangleList rectangle1)
  ;R1.topx<R2.bottomx
   (and(and(< (list-ref rectangleList 0) (list-ref rectangle1 2)))
      ;R1.bottomx>R2.topx
      (and(> (list-ref rectangleList 2) (list-ref rectangle1 0)))
      ;R1.bottomy<R2.topy
      (and(< (list-ref rectangleList 3)(list-ref rectangle1 1)))
      ;R1.topy>R2.bottomy
      (and(> (list-ref rectangleList 1) (list-ref rectangle1 3)))))

You call it by:

(rectangleList ('(1 1 4 5) '(1 3 5 6)) '( 1 3 5 7))