Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (57 sloc) 2.429 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#lang planet dyoo/whalesong

;; A rewrite of the cloth simulation application from the Codea project
;; into world form.

;; 121 particles,
;; 100 constraints.
;;
;; There are a few things about the program that aren't stated up front, but are implied
;; by the original code:
;;
;; 1. The upper edge is fixed. The use of the orx, ory attributes is somewhat redundant.
;; 2. The particles

(define-struct constraint (p1 ;; particle
                           p2 ;; particle
                           d ;; number
                           ))

(define-struct particle (x ;; number
                         y ;; number
                         ox ;; number -- the very previous x position
                         oy ;; number -- the very previous y position
                         fx ;; number -- force in the x direction
                         fy ;; number -- force in the y direction
                         orx ;; number -- the original x position
                         ory ;; number -- the original y position
                         ))

;; The world consists of a list of particles and constraints.
(define-struct world (particles ;; (listof particle)
                      constraints ;; (listof constraint)
                      ))
                         
                         
                         
(define (new-particle x y)
  (make-particle x y x y 0 0 x y))
   


;; apply-global-forces: (listof parts) number number -> (listof parts)
;; Globally apply the forces on all the parts.
(define (apply-global-forces parts fx fy)
  (map (lambda (part) (apply-force part fx fy))
       parts))


;; apply-force: particle number number -> particle
;; Update a particle with the appropriate force.
(define (apply-force part fx fy)
  (make-particle (particle-x part)
                 (particle-y part)
                 (particle-ox part)
                 (particle-oy part)
                 fx
                 fy
                 (particle-orx part)
                 (particle-ory part)))


;; new-constraint: point point -> constraint
;; Construct a new initial constraint from points p1 and p2.
(define (new-constraint p1 p2)
  (make-constraint p1 p2 (partdist p1 p2)))



;; partdist: point point -> number
;; Computes the distance between two points.
(define (partdist p1 p2)
  (sqrt (sqr (- (point-x p1)
                (point-x p2)))
        (sqr (- (point-y p1)
                (point-y p2)))))
Something went wrong with that request. Please try again.