/* --- CSU213 Fall 2006 Lecture Notes --------- Copyright 2006 Viera K. Proulx Lecture 3: September 11, 2006 Part 2: Playing Calder Building Mobiles ---------------- */ /* ;; A Mobile-Construction is (make-mobile Posn Mobile) (define-struct mconstruction (origin mobile)) ;; A Mobile is one of ;; --- (make-mend Number Number Color) ;; --- (make-mstrut Number Number Mobile Number Mobile) (define-struct mend (length weight color)) (define-struct mstrut (length loffset lmobile roffst rmobile)) ;; Examples: (define redmobile (make-mend 10 6 "red")) (define greenmobile (make-mend 10 4 "green")) (define bluemobile (make-mend 40 10 "blue")) (define leftmobile (make-mstrut 30 40 redmobile 20 greenmobile)) (define mymobile (make-mstrut 20 50 leftmobile 30 bluemobile)) (define themobile (make-mconstruction (make-posn 120 20) mymobile)) */ /* +----------------+ | MConstruct | +----------------+ | CartPt origin | | IMobile mobile | +----------------+ */ // to represent a mobile art object class MConstruct { CartPt origin; IMobile mobile; MConstruct(CartPt origin, IMobile mobile) { this.origin = origin; this.mobile = mobile; } } /* +--------+ | CartPt | +--------+ | int x | | int y | +--------+ */ // to represent a Cartesian point class CartPt { int x; int y; CartPt(int x, int y) { this.x = x; this.y = y; } } /* +----------------+ | MConstruct | +----------------+ +-| CartPt origin | | | IMobile mobile |-+ | +----------------+ | v v +--------+ +---------+ | CartPt | | IMobile |<-------------+ +--------+ +---------+ | | int x | +---------+ | | int y | | | +--------+ / \ | --- | | | ---------------------- | | | | +--------------+ +---------------+ | | MobileEnd | | MobileStrut | | +--------------+ +---------------+ | | int length | | int length | | | int weight | | int loffset | | | String color | | IMobile left |-+ | +--------------+ | int roffset | | | | IMobile right |-+ | +---------------+ | | | | +--+ */ // to represent the elements of a mobile art object interface IMobile { } // to represent the last part of a mobile art object class MobileEnd implements IMobile { int length; int weight; String color; MobileEnd(int length, int weight, String color) { this.length = length; this.weight = weight; this.color = color; } } // to represent the strut part of a mobile art object class MobileStrut implements IMobile { int length; int loffset; IMobile left; int roffset; IMobile right; MobileStrut(int length, int loffset, IMobile left, int roffset, IMobile right) { this.length = length; this.loffset = loffset; this.left = left; this.roffset = roffset; this.right = right; } } class Examples { Examples() {} IMobile redMobile = new MobileEnd(10, 6, "red"); IMobile greenMobile = new MobileEnd(10, 4, "green"); IMobile blueMobile = new MobileEnd(40, 10, "blue"); IMobile leftMobile = new MobileStrut(30, 40, this.redMobile, 20, this.greenMobile); IMobile myMobile = new MobileStrut(20, 50, this.leftMobile, 30, this.blueMobile); MConstruct theMobile = new MConstruct(new CartPt(120, 20), this.myMobile); /* + | | | | +---------------+---------+ | | | | | | | | | | | | +------------+------+ | | | | | | B10 R6 G4 */ }