Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

xPp1dZ - 0985930802 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 520

T H E N A T U R E O F C O D E

T H E N A T U R E O F C O D E
S I M U L A T I N G N A T U R A L S Y S T E M S W I T H P R O C E S S I N G
D A N I E L S H I F F M A N
T
H
E

N
A
T
U
R
E

O
F

C
O
D
E
D
A
N
I
E
L

S
H
I
F
F
M
A
N
How can we capture the unpredictable
evolutionary and emergent properties of
nature in software?
How can understanding the mathematical
principles behind our physical world help us
to create digital worlds?
This book focuses on a range of
programming strategies and techniques
behind computer simulations of natural
systems, from elementary concepts in
mathematics and physics to more advanced
algorithms that enable sophisticated visual
results. Using the open-source language
Processing, readers will progress from
building a basic physics engine to creating
intelligent moving objects and complex
systems, setting the foundation for further
experiments in generative design.
http://natureofcode.com
The Nature of Code The Nature of Code
?V $>KFBI 3EFCCJ>K
QEB MR?IFPEBO TLRIA DL EBOB, ?RQ QEBOB FP KLKB, FQfP LKIV JB.
SBOPFLK 0005, DBKBO>QBA $B@BJ?BO 4, 2012
/<=G =G H<9 :=B5@ ," F9@95G9 69:CF9 H<9 6CC? ;C9G HC DF=BH. ,@95G9 DCGH 5BM 7CFF97H=CBG HC
#=H$I6 (<HHD://;=H<I6.7CA/G<=::A5B//<9-*5HIF9-C:-CC89/=GGI9G) 6M 11:59 ,) (!./) 979A69F
5, 2012.
F
Dedication Dedication
&LO JV DO>KAJLQEBO, "BII> ->KBI 'OBBKCFBIA (/@QL?BO 13, 1915 - !MOFI 3, 2010)
"BII> ->KBI T>P ?LOK FK .BT 9LOH #FQV. ! MFLKBBOFKD TLJ>K FK J>QEBJ>QF@P, PEB B>OKBA EBO
0E$ FK 1939 COLJ .BT 9LOH 5KFSBOPFQV RKABO QEB PRMBOSFPFLK LC 2F@E>OA #LRO>KQ. 3EB TLOHBA
CLO 2>JL-7LLIAOFADB (KLT 427) >KA >Q QEB 2>KA #LOMLO>QFLK TFQE 2F@E>OA "BIIJ>K. ,>QBO, PEB
Q>RDEQ J>QEBJ>QF@P >Q QEB #LIIBDB LC .LQOB $>JB AB .>JRO 5KFSBOPFQV FK "BIJLKQ, #>IFCLOKF>,
>KA >Q 5#,!. 4EB "BII> ->KBI 0OFWB CLO LRQPQ>KAFKD DO>AR>QB TLOH ?V > TLJ>K LO JFKLOFQV
T>P BPQ>?IFPEBA >Q .95fP #LRO>KQ )KPQFQRQB FK 1995.
4EB .>QROB LC #LAB (S005)
FF
%AFQLO %AFQLO 3E>KKLK &OV
)IIRPQO>QFLKP )IIRPQO>QFLKP :>KK>E ->OPE
#LSBO $BPFDK #LSBO $BPFDK $>SFA 7FIPLK
)KQBOFLO $BPFDK )KQBOFLO $BPFDK $>SFA 7FIPLK
7B? 3FQB $BPFDK 7B? 3FQB $BPFDK 3QBSB +IFPB
%AFQLOF>I >KA $BPFDK !PPFPQ>KQ %AFQLOF>I >KA $BPFDK !PPFPQ>KQ %S>K %JLIL
->DF@ "LLH ,B>A $BSBILMBOP ->DF@ "LLH ,B>A $BSBILMBOP 2RKB ->APBK, 3QBSB +IFPB
->DF@ "LLH 2BPB>O@EBOP ->DF@ "LLH 2BPB>O@EBOP %S>K %JLIL, -FDRBI "BOJRABW, ,RFP> 0BFOBO> (LOP
)KABU )KABU 7LOA#L )KABUFKD 3BOSF@BP
#LMVOFDEQ 2012 ?V $>KFBI 3EFCCJ>K
)3".-13: 978-0985930806
)3".-10: 0985930802
4EFP TLOH FP IF@BKPBA RKABO QEB #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I 3.0
5KMLOQBA ,F@BKPB. 4L SFBT > @LMV LC QEFP IF@BKPB, SFPFQ @OB>QFSB@LJJLKP.LOD
(EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/?V-K@/3.0/) LO PBKA > IBQQBO QL #OB>QFSB #LJJLKP, 444
#>PQOL 3QOBBQ, 3RFQB 900, -LRKQ>FK 6FBT, #>IFCLOKF> 94041, 53!.
!II LC QEB ?LLHfP PLRO@B @LAB FP IF@BKPBA RKABO QEB '.5 ,BPPBO 'BKBO>I 0R?IF@ ,F@BKPB
(EQQM://@OB>QFSB@LJJLKP.LOD/IF@BKPBP/,'0,/2.1/) >P MR?IFPEBA ?V QEB &OBB 3LCQT>OB
&LRKA>QFLK; BFQEBO SBOPFLK 2.1 LC QEB ,F@BKPB, LO (>Q VLRO LMQFLK) >KV I>QBO SBOPFLK.
4EFP ?LLH T>P DBKBO>QBA ?V QEB ->DF@ "LLH 0OLGB@Q (EQQM://TTT.J>DF@?LLHMOLGB@Q.@LJ).
$BAF@>QFLK
FFF
Acknowledgments Acknowledgments
"*52 D<?91 .?<B;1 B@ :<C2@ 6; 0<:=960.A21 .;1 D<;12?3B9 D.F@. ,2
@=2;1 A52 2.?962? =.?A@ <3 <B? 96C2@ 92.?;6;4 ./<BA <B? 2;C6?<;:2;A
A5?<B45 =2?02=A6<; .;1 6;A2?.0A6<;. ,2 2E=20A A52 =5F@60.9 D<?91 .?<B;1
B@ A< /25.C2 0<;@6@A2;A9F D6A5 <B? =2?02=AB.9 :2:<?F, 2.4. 63 D2 1?<= .
?<08 6A D699 3.99 1B2 A< 4?.C6AF, 63 . 4B@A <3 D6;1 /9<D@, 9645A2? </720A@ D699
/2 A<@@21 /F A52 D6;1 3B?A52?. *56@ 09.@@ 3<0B@2@ <; B;12?@A.;16;4,
@6:B9.A6;4, .;1 6;0<?=<?.A6;4 :<A6<;-/.@21 292:2;A@ <3 <B? =5F@60.9
D<?91 6;A< A52 1646A.9 D<?91@ A5.A D2 0?2.A2. &B? 5<=2 6@ A< 0?2.A2
6;AB6A6C2, ?605, .;1 :<?2 @.A6@3F6;4 2E=2?62;02@ /F 1?.D6;4 3?<: A52
=2?02=AB.9 :2:<?62@ <3 <B? B@2?@."
James Tu Dynamic Bodies course description, Spring 2003, ITP
4EB .>QROB LC #LAB (S005)
FS
A.1 A little bit of history A.1 A little bit of history
)K 2003, >P > DO>AR>QB PQRABKQ >Q QEB )KQBO>@QFSB 4BIB@LJJRKF@>QFLKP 0OLDO>J ()40) FK QEB
4FP@E 3@ELLI LC QEB !OQP >Q .BT 9LOH 5KFSBOPFQV, ) BKOLIIBA FK > @LROPB @>IIBA $VK>JF@
"LAFBP. 4EB @LROPB T>P Q>RDEQ ?V FKQBO>@QFLK ABPFDKBO >KA )40 >AGRK@Q MOLCBPPLO *>JBP 4R.
!Q QEB QFJB, JV TLOH T>P CL@RPBA LK > PBOFBP LC PLCQT>OB BUMBOFJBKQP QE>Q DBKBO>QBA OB>I-
QFJB gKLK-MELQLOB>IFPQF@h FJ>DBOV. 4EB >MMIF@>QFLKP FKSLISBA @>MQROFKD FJ>DBP COLJ > IFSB
PLRO@B >KA gM>FKQFKDh QEB @LILOP TFQE BIBJBKQP QE>Q JLSBA >?LRQ QEB P@OBBK >@@LOAFKD QL
S>OFLRP ORIBP. 4EB $VK>JF@ "LAFBP @LROPBdTEF@E @LSBOBA SB@QLOP, CLO@BP, LP@FII>QFLKP,
M>OQF@IB PVPQBJP, OB@ROPFLK, PQBBOFKD, >KA PMOFKDPd>IFDKBA MBOCB@QIV TFQE JV TLOH.
) E>A ?BBK RPFKD QEBPB @LK@BMQP FKCLOJ>IIV FK JV LTK MOLGB@QP, ?RQ E>A KBSBO Q>HBK QEB
QFJB QL @ILPBIV BU>JFKB QEB P@FBK@B ?BEFKA QEB >IDLOFQEJP LO IB>OK L?GB@Q-LOFBKQBA
QB@EKFNRBP QL CLOJ>IFWB QEBFO FJMIBJBKQ>QFLK. 4E>Q SBOV PBJBPQBO, ) >IPL BKOLIIBA FK
&LRKA>QFLKP LC 'BKBO>QFSB !OQ 3VPQBJP, > @LROPB Q>RDEQ ?V 0EFIFM '>I>KQBO, QE>Q CL@RPBA LK
QEB QEBLOV >KA MO>@QF@B LC DBKBO>QFSB >OQ, @LSBOFKD QLMF@P PR@E >P @E>LP, @BIIRI>O >RQLJ>Q>,
DBKBQF@ >IDLOFQEJP, KBRO>I KBQTLOHP, >KA CO>@Q>IP. "LQE 4RfP @LROPB >KA '>I>KQBOfP @LROPB
LMBKBA JV BVBP QL > TLOIA LC PFJRI>QFLK >IDLOFQEJP >KA QB@EKFNRBP QE>Q @>OOFBA JB QEOLRDE
QEB KBUQ PBSBO>I VB>OP LC TLOH >KA QB>@EFKD, >KA PBOSBA >P QEB CLRKA>QFLK >KA FKPMFO>QFLK
CLO QEFP ?LLH.
"RQ QEBOBfP >KLQEBO MFB@B LC QEB MRWWIB JFPPFKD COLJ QEFP PQLOV.
'>I>KQBOfP @LROPB T>P JLPQIV QEBLOV-?>PBA, TEFIB 4RfP T>P Q>RDEQ RPFKD ->@OLJBAF>
$FOB@QLO >KA QEB ,FKDL MOLDO>JJFKD I>KDR>DB. 4E>Q PBJBPQBO, ) IB>OKBA J>KV LC QEB
>IDLOFQEJP ?V QO>KPI>QFKD QEBJ FKQL #++ (QEB I>KDR>DB ) T>P RPFKD NRFQB >THT>OAIV >Q QEB
QFJB, TBII ?BCLOB #++ @OB>QFSB @LAFKD BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP >KA #FKABO E>A
>OOFSBA). 4LT>OAP QEB BKA LC QEB PBJBPQBO, ) AFP@LSBOBA PLJBQEFKD @>IIBA 0OL@BPPFKD
(EQQM://TTT.MOL@BPPFKD.LOD). 0OL@BPPFKD T>P FK >IME> QEBK (SBOPFLK 0055) >KA, E>SFKD E>A
PLJB BUMBOFBK@B TFQE *>S>, FQ FKQOFDRBA JB BKLRDE QL >PH QEB NRBPQFLK: #LRIA QEFP LMBK-
PLRO@B, >OQFPQ-COFBKAIV MOLDO>JJFKD I>KDR>DB >KA BKSFOLKJBKQ ?B QEB OFDEQ MI>@B QL ABSBILM
> PRFQB LC QRQLOF>IP >KA BU>JMIBP >?LRQ MOLDO>JJFKD >KA PFJRI>QFLK? 7FQE QEB PRMMLOQ LC
QEB )40 >KA 0OL@BPPFKD @LJJRKFQFBP, ) BJ?>OHBA LK TE>Q E>P KLT ?BBK >K >IJLPQ BFDEQ-
VB>O GLROKBV LC QB>@EFKD > S>OFBQV LC MOLDO>JJFKD @LK@BMQP >KA QEBFO >MMIF@>QFLKP RPFKD
0OL@BPPFKD.
)fA IFHB QL CFOPQ QE>KH 2BA "ROKP, )40fP CLRKABO, TEL E>P PRMMLOQBA >KA BK@LRO>DBA JB FK JV
TLOH CLO LSBO QBK VB>OP. $>K /f3RIIFS>K, QEB @E>FO LC )40, E>P ?BBK JV QB>@EFKD JBKQLO >KA
T>P QEB CFOPQ QL PRDDBPQ QE>Q ) QOV QB>@EFKD > @LROPB LK 0OL@BPPFKD, DFSFKD JB > OB>PLK QL
PQ>OQ >PPBJ?IFKD MOLDO>JJFKD QRQLOF>IP FK QEB CFOPQ MI>@B. 3E>TK 6>K %SBOV, ABSBILMBO
BUQO>LOAFK>FOB >KA >RQELO LC ,FC AB8FC=8 )98=5, E>P >IPL ?BBK > OF@E PLRO@B LC EBIM >KA
FKPMFO>QFLK >Q )40 LSBO QEB VB>OP. )40 C>@RIQV JBJ?BOP #I>V 3EFOHV, $>KKV 2LWFK, +>QEBOFKB
$FIILK, ->OF>KKB 0BQFQ, ->OFK> :ROHLT, >KA 4LJ )DLB E>SB MOLSFABA > DOB>Q AB>I LC PRMMLOQ
>KA CBBA?>@H QEOLRDELRQ QEB TOFQFKD LC QEFP ?LLH. 4EB OBPQ LC QEB C>@RIQV >KA PQ>CC >Q )40
E>SB >IPL J>AB QEFP MLPPF?IB: "OF>K +FJ, %AT>OA 'LOALK, 'BLODB !DRALT, *LEK $R>KB,
->OILK %S>KP, ->QQ "BODBO, -BD>K $BJ>OBPQ, -FALOF 9>PRA>, >KA 2L? 2V>K.
!@HKLTIBADJBKQP
S
4EB PQRABKQP LC )40, QLL KRJBOLRP QL JBKQFLK, E>SB ?BBK >K >J>WFKD PLRO@B LC CBBA?>@H
QEOLRDELRQ QEFP MOL@BPP. -R@E LC QEB J>QBOF>I FK QEFP ?LLH @LJBP COLJ JV @LROPB LC QEB P>JB
QFQIB, TEF@E )fSB KLT Q>RDEQ CLO CFSB VB>OP. ) E>SB PQ>@HP LC AO>CQ MOFKQLRQP LC QEB ?LLH TFQE
KLQBP P@O>TIBA >ILKD QEB J>ODFKP >P TBII >P > S>PQ >O@EFSB LC PQRABKQ BJ>FIP TFQE @LOOB@QFLKP,
@LJJBKQP, >KA DBKBOLRP TLOAP LC BK@LRO>DBJBKQ.
) >J >IPL FKAB?QBA QL QEB BKBODBQF@ >KA PRMMLOQFSB @LJJRKFQV LC 0OL@BPPFKD MOLDO>JJBOP
>KA >OQFPQP. ) TLRIAKfQ ?B TOFQFKD QEFP ?LLH FC FQ TBOBKfQ CLO #>PBV 2B>P >KA "BK &OV, TEL
@OB>QBA 0OL@BPPFKD. )fSB IB>OKBA E>IC LC TE>Q ) HKLT PFJMIV COLJ OB>AFKD QEOLRDE QEB
0OL@BPPFKD PLRO@B @LAB; QEB BIBD>KQ PFJMIF@FQV LC QEB 0OL@BPPFKD I>KDR>DB, TB?PFQB, >KA )$%
E>P J>AB MOLDO>JJFKD >@@BPPF?IB >KA CRK CLO >II LC JV PQRABKQP. )fSB OB@BFSBA >ASF@B >KA
FKPMFO>QFLK COLJ J>KV 0OL@BPPFKD MOLDO>JJBOP FK@IRAFKD !KAOZP #LIR?OF, *BO 4ELOM, ->OFRP
7>QW, +>OPQBK 3@EJFAQ, 2L?BOQ (LADFK, 3B?-,BB $BIFPIB, >KA )O> 'OBBK?BOD. (B>QEBO $BTBV-
(>D?LOD MOLSFABA > DOB>Q AB>I LC BU@BIIBKQ CBBA?>@H LK #E>MQBO 10 (.BRO>I .BQTLOHP). 3@LQQ
-ROO>V MOLSFABA PLJB OB>IIV EBIMCRI >ASF@B >?LRQ FKIFKB 36'P LSBO B-J>FI. ->KV LC QEB QFQIBP FK
QEB &ROQEBO 2B>AFKD PB@QFLK TBOB PRDDBPQBA ?V 'LI>K ,BSFK.
) >J FKAB?QBA QL 3E>KKLK &OV, TEL BAFQBA QEFP ?LLH BSBOV PQBM LC QEB T>V. 4EB HKLTIBADB
QE>Q ) TLRIA >IT>VP E>SB EBO @>OBCRI >KA QELRDEQCRI CBBA?>@H LK JV TOFQFKD >IILTBA JB QL
MILT >EB>A, >T>OB QE>Q BSBOVQEFKD TLRIA @LJB LRQ PLRKAFKD ?BQQBO >CQBO PEB DLQ EBO E>KAP
LK JV @E>MQBOP.
! PMB@F>I JBKQFLK DLBP QL :>KK>E ->OPE TEL TLOHBA QFOBIBPPIV QL @OB>QB LSBO > ERKAOBA
FIIRPQO>QFLKP CLO QEFP ?LLH, ABSBILMFKD > COFBKAIV >KA FKCLOJ>I ILLH. ) BPMB@F>IIV T>KQ QL QE>KH
EBO CLO EBO M>QFBK@B >KA TFIIFKDKBPP QL DL TFQE QEB CILT >P TB @E>KDBA QEB FIIRPQO>QFLK
OBNRFOBJBKQP PBSBO>I QFJBP. ) >IPL T>KQ QL QE>KH $>SFA 7FIPLK, TEL @>JB QL JV OBP@RB >Q QEB
I>PQ JFKRQB >KA ABPFDKBA QEB FKQBOFLO I>VLRQ >KA @LSBO CLO QEB ?LLH. ) >J M>OQF@RI>OIV DO>QBCRI
QL 3QBSB +IFPB, TEL ABPFDKBA >KA ?RFIQ QEB ?LLHfP TB? PFQB, EBIMFKD JB QL ABSBILM > "M>V TE>Q
VLR T>KQ" JLABI CLO QEB AFDFQ>I 0$&.
!P )fII BUMI>FK > ?FQ JLOB FK QEB MOBC>@B, QEFP ?LLH T>P DBKBO>QBA TFQE > KBT LMBK-PLRO@B
PVPQBJ CLO MR?IFPEFKD @>IIBA g4EB ->DF@ "LLH.h ! @O>@H QB>J LC )40 MOLDO>JJBOP, ABPFDKBOP,
>KA >OQFPQP TLOHBA LSBO QEB @LROPB LC JLOB QE>K > VB>O QL ABSBILM QEFP PVPQBJ, TEF@E
DBKBO>QBP > ?LLH FK > S>OFBQV LC CLOJ>QP (0$&, (4-,, >KA JLOB) COLJ LKB PFKDIB !3#))$/#
CFIB, >II ABPFDKBA TFQE #33 I>VLRQ. 2RKB ->APBK ?BD>K QEB MOLGB@Q >KA ABSBILMBA QEB LOFDFK>I
2R?V / 3FK>QO> CO>JBTLOH. ) >J MOBQQV PROB )fA ?B PQORDDIFKD TFQE MRQQFKD QEB MFB@BP LC QEB ?LLH
QLDBQEBO TBII FKQL 2013 FC FQ T>PKfQ CLO 2RKBfP ABAF@>QFLK QL PBBFKD QEB MOLGB@Q QEOLRDE QL QEB
BKA. 3QBSB +IFPB @LKQOF?RQBA @LRKQIBPP ?RD CFUBP >KA BKDFKBBOBA QEB PVPQBJ QE>Q >IILTP RP QL
OBPQVIB @LAB @LJJBKQP QL QEB PFAB LC QEB @LAB ?IL@HP QEBJPBISBP. -FDRBI "BOJRABW, %S>K
%JLIL, >KA ,RFP> 0BOBFO> (LOP @LKQOF?RQBA FK J>KV T>VP, IB>OKFKD QEB FKP >KA LRQP LC
!3#))$/# >P TBII >P #33 0>DBA -BAF>. )40 OBPB>O@EBO 'OBD "LOBKPQBFK MOLSFABA >
QOBJBKALRP >JLRKQ LC >ASF@B >KA PRMMLOQ >ILKD QEB T>V OBD>OAFKD QEB >OB>P LC MR?IFPEFKD CLO
QEB TB? >KA MOFKQ. 0OFK@B (EQQM://MOFK@BUJI) FP QEB BKDFKB QEB ->DF@ "LLH RPBP QL DBKBO>QB >
0$& COLJ >K (4-, AL@RJBKQ >KA )fA IFHB QL QE>KH -F@E>BI $>V, #%/ LC 0OFK@B8-,, TEL T>P
>KPTBOBA J>KV LC LRO NRBPQFLKP (>Q IFDEQKFKD PMBBA) >ILKD QEB T>V.
4EB .>QROB LC #LAB (S005)
SF
&FK>IIV )fA IFHB QL QE>KH JV C>JFIV: JV TFCB, !IFHF #>ILVBO>P, TEL PRMMLOQBA QEFP MOLGB@Q
QEOLRDELRQ TEFIB E>SFKD EBO LTK DF>KQ QLJB QL TOFQB, >KA JV @EFIAOBK, %IF>P >KA /IVJMF>,
JLQFS>QFLK CLO CFKFPEFKD QEFP RM PL QE>Q ) @LRIA PMBKA JLOB QFJB E>KDFKD LRQ TFQE QEBJ. )fA
>IPL IFHB QL QE>KH JV C>QEBO, "BOK>OA 3EFCCJ>K, TEL DBKBOLRPIV IBKQ EFP J>QEBJ>QF@>I
BUMBOQFPB >KA MOLSFABA CBBA?>@H >ILKD QEB T>V, >P TBII >P JV JLQEBO, $LOFP 9>CCB 3EFCCJ>K,
>KA ?OLQEBO, *LK>QE>K 3EFCCJ>K, TEL TBOB >IT>VP QOBJBKALRPIV PRMMLOQFSB FK >PHFKD QEB
NRBPQFLK: g(LT FP QEB ?LLH @LJFKD >ILKD?h
A.2 Kickstarter A.2 Kickstarter
4EBOB FP >KLQEBO LOD>KFW>QFLK >KA @LJJRKFQV QE>Q E>P J>AB QEFP ?LLH MLPPF?IB: +F@HPQ>OQBO.
)K 2008, ) @LJMIBQBA TLOH LK JV CFOPQ ?LLH, (95FB=B; ,FC79GG=B;, MR?IFPEBA ?V -LOD>K
+>RCJ>KK/%IPBSFBO. (95FB=B; ,FC79GG=B; QLLH >IJLPQ QEOBB VB>OP QL CFKFPE. ) AFAKfQ Q>HB > ILQ
LC @>OB FK @ELLPFKD > MR?IFPEBO LO QEFKHFKD >?LRQ QEB QBOJP. ) GRPQ QELRDEQ d g2B>IIV? 9LR
T>KQ QL MR?IFPE > ?LLH ?V JB? /+, )fII AL FQ.h 5KCLOQRK>QBIV, JV BUMBOFBK@B T>P KLQ BKQFOBIV
MLPFQFSB. ) E>A CFSB AFCCBOBKQ BAFQLOP >PPFDKBA QL JB QEOLRDELRQ QEB MOL@BPP, >KA ) OB@BFSBA
IFQQIB QL KL CBBA?>@H LK QEB @LKQBKQ FQPBIC. 4EB MR?IFPEBO LRQPLRO@BA QEB QVMBPBQQFKD, TEF@E
OBPRIQBA FK > DOB>Q AB>I LC JFPQ>HBP >KA FK@LKPFPQBK@FBP FK MOLAR@QFLK. )K >AAFQFLK, ) CLRKA
QEB MOF@FKD LC QEB ?LLH QL ?B LCC QEB J>OH. -V DL>I T>P QL TOFQB > COFBKAIV, FKBUMBKPFSB
(?I>@H >KA TEFQB), M>MBO?>@H FKQOLAR@QFLK QL MOLDO>JJFKD FK 0OL@BPPFKD, >KA QEB ?LLH
BKABA RM OBQ>FIFKD CLO > "QBUQ?LLH" MOF@B LC $50.
.LT, ) T>KQ QL BJME>PFWB QE>Q JV MR?IFPEBO E>A DLLA FKQBKQFLKP. 4EBV ELKBPQIV T>KQBA QL
MOLAR@B QEB ?BPQ ?LLH MLPPF?IB, LKB QE>Q ) TLRIA ?B E>MMV TFQE, QE>Q QEBV TLRIA ?B E>MMV
TFQE, >KA QE>Q OB>ABOP TLRIA BKGLV. !KA QEBV TLOHBA E>OA QL J>HB QEFP E>MMBK.
5KCLOQRK>QBIV, QEBV E>A QL TLOH TFQEFK > SBOV QFDEQ ?RADBQ, >KA >P > OBPRIQ TBOB PQOBQ@EBA
BUQOBJBIV QEFK. )K >AAFQFLK, ) ALKfQ QEFKH QEBV TBOB QBOOF?IV C>JFIF>O TFQE QEB TLOIA LC LMBK-
PLRO@B g@OB>QFSBh @LAFKD BKSFOLKJBKQP IFHB 0OL@BPPFKD; QEBFO TLOIA FP @LJMRQBO P@FBK@B
QBUQ?LLHP.
!P > OBPRIQ, CLO QEFP .>QROB LC #LAB ?LLH, ) CBIQ FQ T>P FJMLOQ>KQ QL QOV PBIC-MR?IFPEFKD. 3FK@B )
AFAKfQ DBQ BAFQFKD PRMMLOQ COLJ QEB MR?IFPEBO, TEV KLQ EFOB >K BAFQLO? ) T>PKfQ E>MMV TFQE QEB
MOF@FKD, PL TEV KLQ PBQ QEB MOF@B JVPBIC (LO, FK QEB @>PB LC QEB 0$&, IBQ QEB ?RVBO PBQ QEB
MOF@B)? 4EBK QEBOBfP QEB NRBPQFLK LC J>OHBQFKD d ALBP > MR?IFPEBO >AA S>IRB >KA EBIM VLR
OB>@E >K >RAFBK@B? )K PLJB @>PBP, QEB >KPTBO FP VBP. 4EB /f2BFIIV g->HBh PBOFBP, CLO
BU>JMIB, ALBP > TLKABOCRI GL? LC @OB>QFKD > @LJJRKFQV >OLRKA QEBFO ?LLHP >KA MOLAR@QP.
3QFII, FK QEB @>PB LC IB>OKFKD QL MOLDO>J FK 0OL@BPPFKD, OB>@EFKD QEB >RAFBK@B FP >P PFJMIB >P
LKB 52, d MOL@BPPFKD.LOD.
5KCLOQRK>QBIV, ) NRF@HIV AFP@LSBOBA QE>Q QEBOB FP LKB QEFKD > MR?IFPEBO LCCBOP QE>Q ) T>P KLQ
DBQQFKD COLJ JV PBIC-MR?IFPEFKD M>QE. /KB SBOV FJMLOQ>KQ, EFDEIV @OR@F>I ABQ>FI d > AB>AIFKB.
/K JV LTK, ) CILRKABOBA CLO QTL VB>OP, P>VFKD ) T>P DLFKD QL TOFQB QEB .>QROB LC #LAB ?LLH
?RQ LKIV AO>CQFKD > IFQQIB ?FQ EBOB >KA QEBOB. /K JV IFPQ LC QEFKDP ) KBBABA QL AL, FQ T>P >IT>VP
>Q QEB ?LQQLJ. 4EBK >ILKD @>JB +F@HPQ>OQBO, >KA TFQE >K >RAFBK@B PFQQFKD >KA T>FQFKD (>KA
!@HKLTIBADJBKQP
SFF
E>SFKD PMBKQ @>PE JLKBV), ) IFSBA FK CB>O LC KLQ JBBQFKD JV AB>AIFKB. !KA QEB C>@Q QE>Q VLR >OB
OB>AFKD QEFP KLT FP >K FKAF@>QFLK QE>Q FQ TLOHBA.
-LPQ FJMLOQ>KQIV, PBIC-MR?IFPEFKD QEB ?LLH E>P >IILTBA JB > DOB>Q AB>I LC CIBUF?FIFQV FK ELT )
MOF@B >KA AFPQOF?RQB QEB @LKQBKQ. /K %IPBSFBOfP TB? PFQB, VLR @>K MRO@E>PB (95FB=B;
,FC79GG=B; >P >K B-?LLH CLO $53.95. 4E>QfP OFDEQ, CFCQV-QEOBB ALII>OP >KA KFKBQV-CFSB @BKQP.
)K@FABKQ>IIV, CLO B>@E B-?LLH PLIA ) DBQ > OLV>IQV LC 5%, TEF@E FP $2.70. 4E>QfP OFDEQ, QTL ALII>OP
>KA PBSBKQV @BKQP. )C ) PBIC-MR?IFPE, ) @>K J>HB QEB ?LLH J>PPFSBIV @EB>MBO. 3BIIFKD > AFDFQ>I
@LMV CLO $10, )fJ OBAR@FKD QEB @LPQ QL QEB OB>ABO ?V LSBO BFDEQV MBO@BKQ >KA QOFMIFKD QEB JLKBV
M>FA QL JB. )fJ Q>HFKD QEFP BSBK CROQEBO TFQE QEB 0$& >KA >IILTFKD ?RVBOP QL PBQ QEB MOF@B
QEBJPBISBP.
)K >AAFQFLK, ?V LTKFKD >II QEB @LKQBKQ, ) >J >?IB QL OBIB>PB QEB BKQFOB ?LLH LKIFKB CLO COBB >P
TBII >P BUMBOFJBKQ TFQE KBT AFDFQ>I CLOJ>QP. 4EB O>T QBUQ LC QEB ?LLH, >P TBII >P >II QEB @LAB
>KA FIIRPQO>QFLKP, FP IF@BKPBA RKABO > #OB>QFSB #LJJLKP !QQOF?RQFLK-.LK#LJJBO@F>I IF@BKPB
>KA FP >S>FI>?IB LK 'FQER?, TEBOB OB>ABOP @>K PR?JFQ FPPRBP (KLQ QL JBKQFLK MRII OBNRBPQP!)
TFQE @LOOB@QFLKP >KA @LJJBKQP. &FK>IIV, ?V RPFKD JLOB CIBUF?IB MOFKQ-LK-ABJ>KA PBOSF@BP, ) @>K
JLOB B>PFIV J>HB @E>KDBP >KA HBBM QEB ?LLH @ROOBKQ, OBIB>PFKD KBT BAFQFLKP >P LCQBK >P )
IFHB. (! LKB-QFJB MRO@E>PB LC > AFDFQ>I @LMV LC QEB ?LLH FK@IRABP IFCBQFJB RMDO>ABP CLO COBB.)
3L QE>KH VLR QL +F@HPQ>OQBO, ?LQE QEB @LJM>KV (BPMB@F>IIV &OBA "BKBKPLK, TEL @LKSFK@BA JB
QL Q>HB QEB MIRKDB FK QEB CFOPQ MI>@B >KA >ASFPBA JB LK ELT QL IF@BKPB QEB ?LLH) >P TBII >P >II
QEB ?>@HBOP TEL QLLH > @E>K@B LK QEFP ?LLH. 3LJB LC QEBPB ?>@HBOP, QEOLRDE DBKBOLPFQV
?BVLKA QEB @>II LC ARQV, B>OKBA >K BUQO> QE>KH-VLR >P M>OQ LC QEBFO OBT>OA:
i !IBU>KAOB ".
i 2L?BOQ (LADFK
i *LL9LRK 0>BH
i !KDBI> -@.>JBB ("LVE>K)
i "L? )MMLIFQL
!II LC QEB ?>@HBOP AFOB@QIV @LKQOF?RQBA QL QEB CFKFPEFKD LC QEFP ?LLH. *RPQ QEB PEBBO >@Q LC
PFDKFKD RM QL @LKQOF?RQB JLKBV CLO AO>CQ >KA CFK>I SBOPFLKP IFQ > CFOB FK JB QL CFKFPE, KLQ QL
JBKQFLK MOLSFABA JB TFQE QEB OBPLRO@BP QL M>V CLO ABPFDK >KA BAFQFKD TLOH (>KA PLJB
?>?VPFQQFKD AROFKD 3>QROA>V JLOKFKD TOFQFKD PBPPFLKP).
)K >AAFQFLK QL @LKQOF?RQFKD CRKAP, +F@HPQ>OQBO ?>@HBOP OB>A MOB-OBIB>PB SBOPFLKP LC QEB @E>MQBOP
>KA MOLSFABA QLKP LC CBBA?>@H, @>Q@EFKD J>KV BOOLOP >KA MLFKQFKD LRQ @LKCRPFKD PB@QFLKP LC
QEB ?LLH. 4TL PR@E OB>ABOP QE>Q )fA IFHB QL QE>KH >OB &OBABOFH 6>KELRQQB >KA (>KP AB 7LIC,
TELPB BUMBOQ HKLTIBADB LC .BTQLKF>K MEVPF@P T>P BKLOJLRPIV EBIMCRI FK QEB OBSFPFKD LC
#E>MQBOP 2 >KA 3.
4EB .>QROB LC #LAB (S005)
SFFF
Preface Preface
P.1 What is this book? P.1 What is this book?
!Q )40 (EQQM://FQM.KVR.BAR), ) QB>@E > @LROPB BKQFQIBA )KQOLAR@QFLK QL #LJMRQ>QFLK>I -BAF>. )K
QEFP @LROPB, QEB PQRABKQP IB>OK QEB ?>PF@P LC MOLDO>JJFKD (S>OF>?IBP, @LKAFQFLK>IP, ILLMP,
L?GB@QP, >OO>VP) >P TBII >P > PROSBV LC >MMIF@>QFLKP OBI>QBA QL J>HFKD FKQBO>@QFSB MOLGB@QP
(FJ>DBP, MFUBIP, @LJMRQBO SFPFLK, KBQTLOHFKD, A>Q>, 3$). 4EB @LROPB JLPQIV CLIILTP QEB
J>QBOF>I CLRKA FK JV FKQOL ?LLH (95FB=B; ,FC79GG=B;; FK J>KV T>VP, /<9 *5HIF9 C: CC89
PBOSBP >P > CLIILT-RM. /K@B VLRfSB IB>OKBA QEB ?>PF@P >KA PBBK >K >OO>V LC >MMIF@>QFLKP,
VLRO KBUQ PQBM JFDEQ ?B QL ABISB ABBMIV FKQL > M>OQF@RI>O >OB>. &LO BU>JMIB, VLR @LRIA CL@RP
LK @LJMRQBO SFPFLK (>KA OB>A > ?LLH IFHB 'OBD "LOBKPQBFKfP )5?=B; /<=B;G .99). )K QEB JLPQ
?>PF@ PBKPB, QEFP ?LLH FP LKB MLPPF?IB KBUQ PQBM FK > TLOIA LC J>KV. )Q MF@HP RM BU>@QIV
TEBOB (95FB=B; ,FC79GG=B; IB>SBP LCC, ABJLKPQO>QFKD JLOB >AS>K@BA MOLDO>JJFKD
QB@EKFNRBP TFQE 0OL@BPPFKD QE>Q CL@RP LK >IDLOFQEJP >KA PFJRI>QFLK.
4EB DL>I LC QEFP ?LLH FP PFJMIB. 7B T>KQ QL Q>HB > ILLH >Q PLJBQEFKD QE>Q K>QRO>IIV L@@ROP FK
LRO MEVPF@>I TLOIA, QEBK ABQBOJFKB ELT TB @>K TOFQB @LAB QL PFJRI>QB QE>Q L@@ROOBK@B.
3L QEBK TE>Q FP QEFP ?LLH BU>@QIV? )P FQ > P@FBK@B ?LLH? 4EB >KPTBO FP > OBPLRKAFKD KL.
4ORB, TB JFDEQ BU>JFKB QLMF@P QE>Q @LJB COLJ MEVPF@P LO ?FLILDV, ?RQ FQ TLKfQ ?B LRO GL? QL
FKSBPQFD>QB QEBPB QLMF@P TFQE > M>OQF@RI>OIV EFDE IBSBI LC >@>ABJF@ OFDLO. )KPQB>A, TBfOB DLFKD
QL DI>K@B >Q P@FBKQFCF@ @LK@BMQP >KA DO>? QEB M>OQP QE>Q TB KBBA FK QEB PBOSF@B LC ?RFIAFKD >
M>OQF@RI>O PLCQT>OB BU>JMIB.
)P QEFP >K >OQ LO ABPFDK ?LLH? ) TLRIA >IPL P>V KL; >CQBO >II, TB >OB DLFKD QL CL@RP LK
>IDLOFQEJP >KA QEBFO >CCFIF>QBA MOLDO>JJFKD QB@EKFNRBP. 3ROB, QEB OBPRIQP TFII >II ?B SFPR>I FK
K>QROB (J>KFCBPQBA >P >KFJ>QBA 0OL@BPPFKD PHBQ@EBP), ?RQ QEBV TFII BUFPQ JLOB >P
ABJLKPQO>QFLKP LC QEB >IDLOFQEJP >KA MOLDO>JJFKD QB@EKFNRBP QEBJPBISBP, AO>TK LKIV TFQE
PFJMIB PE>MBP >KA DO>VP@>IB. )Q FP JV ELMB, ELTBSBO, QE>Q ABPFDKBOP >KA >OQFPQP @>K
FK@LOMLO>QB >II LC QEB J>QBOF>I EBOB FKQL QEBFO MO>@QF@B QL J>HB KBT, BKD>DFKD TLOH.
0OBC>@B
FU
)K QEB BKA, FC QEFP ?LLH FP >KVQEFKD, FQ FP OB>IIV GRPQ > DLLA LIA-C>PEFLKBA MOLDO>JJFKD ?LLH.
7EFIB > P@FBKQFCF@ QLMF@ J>V PBBA > @E>MQBO (.BTQLKF>K MEVPF@P, @BIIRI>O DOLTQE, BSLIRQFLK) LO
QEB OBPRIQP JFDEQ FKPMFOB >K >OQFPQF@ MOLGB@Q, QEB @LKQBKQ FQPBIC TFII >IT>VP ?LFI ALTK QL QEB @LAB
FJMIBJBKQ>QFLK, TFQE > M>OQF@RI>O CL@RP LK L?GB@Q-LOFBKQBA MOLDO>JJFKD.
P.2 A word about Processing P.2 A word about Processing
) >J RPFKD 0OL@BPPFKD FK QEFP ?LLH CLO > KRJ?BO LC OB>PLKP. &LO LKB, FQfP QEB I>KDR>DB >KA
BKSFOLKJBKQ TFQE TEF@E ) >J JLPQ @LJCLOQ>?IB, >KA FQfP TE>Q ) BKGLV RPFKD CLO JV MBOPLK>I
TLOH. 4TL, FQfP COBB, LMBK-PLRO@B, >KA TBII PRFQBA QL ?BDFKKBOP. 4EBOB FP >K >@QFSB, BKBODBQF@
@LJJRKFQV LC MBLMIB TEL MOLDO>J TFQE 0OL@BPPFKD; CLO J>KV, FQfP QEB CFOPQ MOLDO>JJFKD
I>KDR>DB QEBVfSB IB>OKBA. )K QEFP PBKPB, ) ELMB QE>Q ) @>K OB>@E > TFAB >RAFBK@B >KA
ABJLKPQO>QB QEB @LK@BMQP FK > COFBKAIV J>KKBO ?V RPFKD 0OL@BPPFKD.
!II QE>Q P>FA, QEBOB FP KLQEFKD QE>Q QFBP TE>Q TB >OB ALFKD FK QEFP ?LLH PQOF@QIV QL 0OL@BPPFKD.
4EFP ?LLH @LRIA E>SB ?BBK TOFQQBK RPFKD !@QFLK3@OFMQ, *>S>3@OFMQ, *>S> (TFQELRQ 0OL@BPPFKD),
LO >KV KRJ?BO LC LQEBO LMBK-PLRO@B g@OB>QFSB @LAFKDh BKSFOLKJBKQP IFHB LMBK&O>JBTLOHP,
#FKABO, LO QEB KBTIV OBIB>PBA ML@LAB. )Q FP JV ELMB QE>Q >CQBO )fSB @LJMIBQBA QEFP ?LLH, )fII ?B
>?IB QL OBIB>PB SBOPFLKP LC QEB BU>JMIBP QE>Q ORK FK LQEBO BKSFOLKJBKQP. )C >KVLKB FP
FKQBOBPQBA FK EBIMFKD QL MLOQ QEB BU>JMIBP, MIB>PB CBBI COBB QL @LKQ>@Q JB
(A>KFBIPEFCCJ>K.KBQ).
!II LC QEB BU>JMIBP FK QEFP ?LLH E>SB ?BBK QBPQBA TFQE 0OL@BPPFKD 2.0?6, ?RQ CLO QEB JLPQ
M>OQ, QEBV PELRIA >IPL TLOH TFQE B>OIFBO SBOPFLKP LC 0OL@BPPFKD. )fII ?B HBBMFKD QEBJ RM-QL-A>QB
TFQE TE>QBSBO QEB I>QBPQ SBOPFLK FP. 4EB JLPQ OB@BKQ @LAB @>K >IT>VP ?B CLRKA LK 'FQ(R?
(EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP).
P.3 What do you need to know? P.3 What do you need to know?
4EB MOBOBNRFPFQB CLO RKABOPQ>KAFKD QEB J>QBOF>I FK QEFP ?LLH @LRIA ?B PQ>QBA >P: gLKB PBJBPQBO
LC MOLDO>JJFKD FKPQOR@QFLK TFQE 0OL@BPPFKD (FK@IRAFKD C>JFIF>OFQV TFQE L?GB@Q-LOFBKQBA
MOLDO>JJFKD).h 4E>Q P>FA, QEBOBfP KL OB>PLK TEV VLR @LRIAKfQ OB>A QEFP ?LLH E>SFKD IB>OKBA
MOLDO>JJFKD RPFKD > AFCCBOBKQ I>KDR>DB LO ABSBILMJBKQ BKSFOLKJBKQ. 4EB HBV EBOB FP QE>Q
VLR E>SB BUMBOFBK@B TFQE MOLDO>JJFKD.
)C VLRfSB KBSBO TOFQQBK >KV @LAB ?BCLOB, VLR >OB DLFKD QL PQORDDIB, ?B@>RPB QEFP ?LLH
>PPRJBP HKLTIBADB LC >II QEB ?>PF@P. ) TLRIA PRDDBPQ MF@HFKD RM >K FKQOLAR@QLOV ?LLH LK
0OL@BPPFKD, > KRJ?BO LC TEF@E >OB IFPQBA LK QEB 0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/
IB>OKFKD/?LLHP/).
)C VLR >OB >K BUMBOFBK@BA MOLDO>JJBO, ?RQ E>SBKfQ TLOHBA TFQE 0OL@BPPFKD, VLR @>K MOL?>?IV
MF@H FQ RM ?V ALTKIL>AFKD 0OL@BPPFKD (EQQM://MOL@BPPFKD.LOD/ALTKIL>A/), MLHFKD QEOLRDE QEB
4EB .>QROB LC #LAB (S005)
U
BU>JMIBP, >KA OB>AFKD QEOLRDE QEB 'BQQFKD 3Q>OQBA (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/
DBQQFKDPQ>OQBA/) M>DB.
) PELRIA >IPL MLFKQ LRQ QE>Q BUMBOFBK@B TFQE L?GB@Q-LOFBKQBA MOLDO>JJFKD FP @OR@F>I. 7BfII
OBSFBT PLJB LC QEB ?>PF@P FK QEB ?LLHfP FKQOLAR@QFLK, ?RQ ) TLRIA PRDDBPQ OB>AFKD QEB
0OL@BPPFKD QRQLOF>I LK L?GB@QP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP) CFOPQ.
P.4 What are you using to read this book? P.4 What are you using to read this book?
!OB VLR OB>AFKD QEFP ?LLH LK > +FKAIB? 0OFKQBA M>MBO? /K VLRO I>MQLM FK 0$& CLOJ? /K >
Q>?IBQ PELTFKD >K >KFJ>QBA (4-,5 SBOPFLK? !OB VLR PQO>MMBA QL > @E>FO, >?PLO?FKD QEB
@LKQBKQ AFOB@QIV FKQL VLRO ?O>FK SF> > PBOFBP LC BIB@QOLABP, QR?BP, >KA @>OQOFADBP?
4EB ?LLH VLR >OB OB>AFKD OFDEQ KLT T>P DBKBO>QBA TFQE QEB ->DF@ "LLH MOLGB@Q
(EQQM://TTT.J>DF@?LLHMOLGB@Q.@LJ). 4EB ->DF@ "LLH FP >K LMBK-PLRO@B CO>JBTLOH CLO PBIC-
MR?IFPEFKD ABSBILMBA >Q )40 (EQQM://FQM.KVR.BAR). 4EB FAB> EBOB FP QE>Q VLR LKIV KBBA QL TOFQB
QEB ?LLH LK@B >P > PFJMIB QBUQ CFIB. /K@B VLRfSB TOFQQBK VLRO @LKQBKQ, VLR MOBPP > J>DF@
?RQQLK, >KA LRQ @LJBP VLRO ?LLH FK > S>OFBQV LC CLOJ>QPd0$&, (4-,5, MOFKQBA E>OA@LMV,
+FKAIB, BQ@. %SBOVQEFKD FP ABPFDKBA >KA PQVIBA RPFKD #33. !P LC QEB CFOPQ OBIB>PB, QEB LKIV
SBOPFLKP >S>FI>?IB TFII ?B AFDFQ>I 0$&, MOFKQBA E>OA@LMV, >KA (4-,5 (TEF@E TFII FK@IRAB
>KFJ>QBA SBOPFLKP LC QEB BU>JMIBP RPFKD 0OL@BPPFKD.GP). (LMBCRIIV LSBO QEB @LROPB LC QEB
KBUQ VB>O, QEB ?LLH TFII ?B >S>FI>?IB FK >AAFQFLK>I CLOJ>QP. )C VLRfA IFHB QL EBIM TFQE QEFP,
MIB>PB @LKQ>@Q JB (A>KFBIPEFCCJ>K.KBQ).
P.5 The story of this book P.5 The story of this book
)C VLR DI>K@B LSBO QEB ?LLHfP Q>?IB LC @LKQBKQP, VLRfII KLQF@B QEBOB >OB QBK @E>MQBOP, B>@E
LKB @LSBOFKD > AFCCBOBKQ QLMF@. !KA FK LKB PBKPB, QEFP ?LLH FP GRPQ QE>Qd> PROSBV LC QBK
@LK@BMQP >KA >PPL@F>QBA @LAB BU>JMIBP. .BSBOQEBIBPP, FK MRQQFKD QLDBQEBO QEB J>QBOF>I, )
E>A >IT>VP FJ>DFKBA PLJBQEFKD LC > IFKB>O K>OO>QFSB. "BCLOB VLR ?BDFK OB>AFKD QEB
@E>MQBOP, )fA IFHB QL T>IH VLR QEOLRDE QEFP PQLOV. ) QEFKH FQfP FJMLOQ>KQ QL CFOPQ E>SB > PBKPB LC
QEB LSBO>II GLROKBV.
0OBC>@B
UF
Part I: Inanimate Objects. Part I: Inanimate Objects.
! PL@@BO ?>II IFBP FK QEB DO>PP. ! HF@H I>RK@EBP FQ FKQL QEB >FO. 'O>SFQV MRIIP FQ ?>@H ALTK. !
EB>SV DRPQ LC TFKA HBBMP FQ >CIL>Q > JLJBKQ ILKDBO RKQFI FQ C>IIP >KA ?LRK@BP LCC QEB EB>A LC >
GRJMFKD MI>VBO. 4EB PL@@BO ?>II FP KLQ >IFSB; FQ J>HBP KL @ELF@BP >P QL ELT FQ TFII JLSB
QEOLRDELRQ QEB TLOIA. 2>QEBO, FQ FP >K FK>KFJ>QB L?GB@Q T>FQFKD QL ?B MRPEBA >KA MRIIBA ?V QEB
CLO@BP LC FQP BKSFOLKJBKQ.
(LT TLRIA TB JLABI > PL@@BO ?>II JLSFKD FK 0OL@BPPFKD? )C VLRfSB BSBO MOLDO>JJBA > @FO@IB
JLSFKD >@OLPP > TFKALT, QEBK VLRfSB MOL?>?IV TOFQQBK QEB CLIILTFKD IFKB LC @LAB.
9LR AO>T PLJB PE>MB >Q IL@>QFLK R. 7FQE B>@E CO>JB LC >KFJ>QFLK, VLR FK@OBJBKQ QEB S>IRB LC
R, OBAO>T QEB PE>MB >KA SLFI>dQEB FIIRPFLK LC JLQFLK! ->V?B VLR QLLH FQ > PQBM LO QTL CROQEBO,
>KA FK@IRABA > S IL@>QFLK, >P TBII >P S>OF>?IBP CLO PMBBA >ILKD QEB L >KA M >UBP.
0>OQ ) LC QEFP PQLOV TFII Q>HB RP LKB PQBM CROQEBO. 7BfOB DLFKD QL Q>HB QEBPB S>OF>?IBP RMJ??>
>KA SMJ??> >KA IB>OK ELT QLDBQEBO QEBV CLOJ > SB@QLO (Chapter 1 Chapter 1), QEB ?RFIAFKD ?IL@H LC
JLQFLK. 7B TLKfQ DBQ >KV KBT CRK@QFLK>IFQV LRQ LC QEFP, ?RQ FQ TFII ?RFIA > PLIFA CLRKA>QFLK CLO
QEB OBPQ LC QEB ?LLH.
/K@B TB HKLT > IFQQIB PLJBQEFKD >?LRQ SB@QLOP, TBfOB DLFKD QL NRF@HIV OB>IFWB QE>Q > CLO@B
(Chapter 2 Chapter 2) FP > SB@QLO. +F@H > PL@@BO ?>II >KA VLR >OB >MMIVFKD > CLO@B. 7E>Q ALBP > CLO@B
@>RPB >K L?GB@Q QL AL? !@@LOAFKD QL )P>>@ .BTQLK, CLO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK. 4E>Q
CLO@B @>RPBP >K L?GB@Q QL >@@BIBO>QB. -LABIFKD CLO@BP TFII >IILT RP QL @OB>QB PVPQBJP TFQE
AVK>JF@ JLQFLK TEBOB L?GB@QP JLSB >@@LOAFKD QL > S>OFBQV LC ORIBP.
.LT, QE>Q PL@@BO ?>II QL TEF@E VLR >MMIFBA > CLO@B JFDEQ E>SB >IPL ?BBK PMFKKFKD. )C >K L?GB@Q
JLSBP >@@LOAFKD QL FQP >@@BIBO>QFLK, FQ @>K PMFK >@@LOAFKD QL FQP >KDRI>O >@@BIBO>QFLK (Chapter Chapter
33). 5KABOPQ>KAFKD QEB ?>PF@P LC >KDIBP >KA QOFDLKLJBQOV TFII >IILT RP QL JLABI OLQ>QFKD
L?GB@QP >P TBII >P DO>PM QEB MOFK@FMIBP ?BEFKA LP@FII>QFKD JLQFLK, IFHB > MBKARIRJ PTFKDFKD LO
> PMOFKD ?LRK@FKD.
/K@B TBfSB Q>@HIBA QEB ?>PF@P LC JLQFLK >KA CLO@BP CLO >K FKAFSFAR>I FK>KFJ>QB L?GB@Q, TBfII
IB>OK ELT QL J>HB QELRP>KAP RMLK QELRP>KAP LC QELPB L?GB@QP >KA J>K>DB QEBJ FK > PFKDIB
PVPQBJ @>IIBA > M>OQF@IB PVPQBJ (Chapter 4 Chapter 4). 0>OQF@IB PVPQBJP TFII >IILT RP QL ILLH >Q PLJB
>AS>K@BA CB>QROBP LC L?GB@Q-LOFBKQBA MOLDO>JJFKD, K>JBIV FKEBOFQ>K@B >KA MLIVJLOMEFPJ.
)K #E>MQBOP 1 QEOLRDE 4, >II LC QEB BU>JMIBP TFII ?B TOFQQBK COLJ gP@O>Q@EhdJB>KFKD QEB @LAB
CLO QEB >IDLOFQEJP AOFSFKD QEB JLQFLK LC QEB L?GB@QP TFII ?B TOFQQBK AFOB@QIV FK 0OL@BPPFKD. 7BfOB
@BOQ>FKIV KLQ QEB CFOPQ MOLDO>JJBOP BSBO QL @LKPFABO QEB FAB> LC PFJRI>QFKD MEVPF@P FK
>KFJ>QFLK, PL KBUQ TBfII BU>JFKB ELT MEVPF@P IF?O>OFBP (Chapter 5 Chapter 5) @>K ?B RPBA QL JLABI JLOB
R = R + 1;
R = R + RMJ??>;
S = S + SMJ??>;
4EB .>QROB LC #LAB (S005)
UFF
>AS>K@BA >KA PLMEFPQF@>QBA ?BE>SFLOP. 7BfII ILLH >Q "LU2$ (EQQM://TTT.?LU2A.LOD) >KA
QLUF@IF?P' 6BOIBQ 0EVPF@P M>@H>DB (EQQM://QLUF@IF?P.LOD/).
Part II: Its alive! Part II: Its alive!
7E>Q ALBP FQ JB>K QL JLABI IFCB? .LQ >K B>PV NRBPQFLK QL >KPTBO, ?RQ TB @>K ?BDFK ?V
?RFIAFKD L?GB@QP QE>Q E>SB >K >?FIFQV QL MBO@BFSB QEBFO BKSFOLKJBKQ. ,BQfP QEFKH >?LRQ QEFP CLO
> JLJBKQ. ! ?IL@H QE>Q C>IIP LCC > Q>?IB JLSBP >@@LOAFKD QL CLO@BP, >P ALBP > ALIMEFK
PTFJJFKD QEOLRDE QEB T>QBO. "RQ QEBOB FP > HBV AFCCBOBK@B. 4EB ?IL@H @>KKLQ AB@FAB QL IB>M
LCC QE>Q Q>?IB. 4EB ALIMEFK @>K AB@FAB QL IB>M LRQ LC QEB T>QBO. 4EB ALIMEFK @>K E>SB AOB>JP
>KA ABPFOBP. )Q @>K CBBI ERKDBO LO CB>O, >KA QELPB CBBIFKDP @>K FKCLOJ FQP JLSBJBKQP. "V
BU>JFKFKD QB@EKFNRBP ?BEFKA JLABIFKD >RQLKLJLRP >DBKQP (Chapter 6 Chapter 6), TB TFII ?OB>QEB IFCB
FKQL LRO FK>KFJ>QB L?GB@QP, >IILTFKD QEBJ QL J>HB AB@FPFLKP >?LRQ QEBFO JLSBJBKQP
>@@LOAFKD QL QEBFO RKABOPQ>KAFKD LC QEBFO BKSFOLKJBKQ.
4EOLRDE @LJ?FKFKD QEB @LK@BMQ LC >RQLKLJLRP >DBKQP TFQE TE>Q TB IB>OKBA >?LRQ
JLABIFKD PVPQBJP FK #E>MQBO 4, TBfII ILLH >Q JLABIP LC DOLRM ?BE>SFLO QE>Q BUEF?FQ QEB
MOLMBOQFBP LC @LJMIBUFQV. ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP gJLOB
QE>K QEB PRJ LC FQP M>OQP.h 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV
PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU,
FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. 4EFP TFII IB>A RP >T>V COLJ QEFKHFKD MROBIV >?LRQ
JLABIFKD JLQFLK >KA FKQL QEB OB>IJ LC ORIB-?>PBA PVPQBJP. 7E>Q @>K TB JLABI TFQE @BIIRI>O
>RQLJ>Q> (Chapter 7 Chapter 7), > PVPQBJ LC @BIIP IFSFKD LK > DOFA? 7E>Q QVMBP LC M>QQBOKP @>K TB
DBKBO>QB TFQE CO>@Q>IP (Chapter 8 Chapter 8), QEB DBLJBQOV LC K>QROB?
Part III: Intelligence Part III: Intelligence
7B J>AB QEFKDP JLSB. 4EBK TB D>SB QELPB QEFKDP ELMBP >KA AOB>JP >KA CB>OP, >ILKD TFQE
ORIBP QL IFSB ?V. 4EB I>PQ PQBM FK QEFP ?LLH TFII ?B QL J>HB LRO @OB>QFLKP BSBK PJ>OQBO. #>K
TB >MMIV QEB ?FLILDF@>I MOL@BPP LC BSLIRQFLK QL @LJMRQ>QFLK>I PVPQBJP (Chapter 9 Chapter 9) FK LOABO
QL BSLISB LRO L?GB@QP? 4>HFKD FKPMFO>QFLK COLJ QEB ERJ>K ?O>FK, @>K TB MOLDO>J >K >OQFCF@F>I
KBRO>I KBQTLOH (Chapter 10 Chapter 10) QE>Q @>K IB>OK COLJ FQP JFPQ>HBP >KA >IILT LRO L?GB@QP QL >A>MQ
QL QEBFO BKSFOLKJBKQ?
7BBH 1 7BBH 1 )KQOLAR@QFLK >KA 6B@QLOP (#E>MQBO 1)
7BBH 2 7BBH 2 &LO@BP (#E>MQBO 2)
P.6 This book as a syllabus P.6 This book as a syllabus
7EFIB QEB @LKQBKQ FK QEFP ?LLH @BOQ>FKIV J>HBP CLO >K FKQBKPB >KA EFDEIV @LJMOBPPBA
PBJBPQBO, ) E>SB ABPFDKBA FQ QL CFQ FKQL > CLROQBBK-TBBH @LROPB. .BSBOQEBIBPP, FQfP TLOQE
JBKQFLKFKD QE>Q ) CFKA QE>Q QEB ?LLH @E>MQBOP PLJBQFJBP TLOH ?BQQBO BUM>KABA >@OLPP
JRIQFMIB TBBHP. &LO BU>JMIB, QEB PVII>?RP CLO JV @LROPB DBKBO>IIV TLOHP LRQ >P CLIILTP:
0OBC>@B
UFFF
7BBH 3 7BBH 3 /P@FII>QFLKP (#E>MQBO 3)
7BBH 4 7BBH 4 0>OQF@IB 3VPQBJP (#E>MQBO 4)
7BBH 5 7BBH 5 0EVPF@P ,F?O>OFBP 0>OQ ) (#E>MQBO 5)
7BBH 6 7BBH 6 0EVPF@P ,F?O>OFBP 0>OQ )) & 3QBBOFKD (#E>MQBOP 5-6)
7BBH 7 7BBH 7 0OBPBKQ JFAQBOJ MOLGB@QP >?LRQ JLQFLK
7BBH 8 7BBH 8 #LJMIBU 3VPQBJP: &IL@HFKD >KA 1$ #BIIRI>O !RQLJ>Q> (#E>MQBOP 6-7)
7BBH 9 7BBH 9 #LJMIBU 3VPQBJP: 2$ #BIIRI>O !RQLJ>Q> >KA &O>@Q>IP (#E>MQBOP 7-8)
7BBH 10 7BBH 10 'BKBQF@ !IDLOFQEJP (#E>MQBO 9)
7BBH 11 7BBH 11 .BRO>I .BQTLOHP (#E>MQBO 10)
7BBHP 12-13 7BBHP 12-13 &FK>I MOLGB@Q TLOHPELM
7BBH 14 7BBH 14 &FK>I MOLGB@Q MOBPBKQ>QFLK
)C VLR >OB @LKPFABOFKD RPFKD QEFP QBUQ CLO > @LROPB LO TLOHPELM, MIB>PB CBBI COBB QL @LKQ>@Q JB.
) ELMB QL BSBKQR>IIV OBIB>PB > @LJM>KFLK PBQ LC SFABLP >KA PIFAB MOBPBKQ>QFLKP >P
PRMMIBJBKQ>OV BAR@>QFLK>I J>QBOF>IP.
P.7 The Ecosystem Project P.7 The Ecosystem Project
!P JR@E >P )fA IFHB QL MOBQBKA VLR @LRIA IB>OK BSBOVQEFKD ?V @ROIFKD RM FK > @LJCV @E>FO >KA
OB>AFKD PLJB MOLPB >?LRQ MOLDO>JJFKD, QL IB>OK MOLDO>JJFKD, VLRfOB OB>IIV DLFKD QL E>SB QL
AL PLJB MOLDO>JJFKD. 9LR JFDEQ CFKA FQ EBIMCRI QL HBBM FK JFKA > MOLGB@Q FAB> (LO QTL) QL
ABSBILM >P > PBQ LC BUBO@FPBP TEFIB DLFKD COLJ @E>MQBO QL @E>MQBO. )K C>@Q, TEBK QB>@EFKD QEB
.>QROB LC #LAB @LROPB >Q )40, ) E>SB LCQBK CLRKA QE>Q PQRABKQP BKGLV ?RFIAFKD > PFKDIB MOLGB@Q,
PQBM ?V PQBM, TBBH ?V TBBH, LSBO QEB @LROPB LC > PBJBPQBO.
!Q QEB BKA LC B>@E @E>MQBO, VLRfII CFKA > PBOFBP LC BUBO@FPBP CLO LKB PR@E MOLGB@QdBUBO@FPBP
QE>Q ?RFIA LK B>@E LQEBO, LKB QLMF@ >Q > QFJB. #LKPFABO QEB CLIILTFKD P@BK>OFL. 9LRfSB ?BBK
>PHBA ?V > P@FBK@B JRPBRJ QL ABSBILM QEB PLCQT>OB CLO > KBT BUEF?FQd4EB $FDFQ>I %@LPVPQBJ,
> TLOIA LC >KFJ>QBA, MOL@BARO>I @OB>QROBP QE>Q IFSB LK > MOLGB@QFLK P@OBBK CLO SFPFQLOP QL BKGLV
>P QEBV BKQBO QEB JRPBRJ. ) ALKfQ JB>K QL PRDDBPQ QE>Q QEFP FP > M>OQF@RI>OIV FKKLS>QFSB LO
@OB>QFSB @LK@BMQ. 2>QEBO, TBfII RPB QEFP BU>JMIB MOLGB@Q FAB> >P > IFQBO>I OBMOBPBKQ>QFLK LC QEB
@LKQBKQ FK QEB ?LLH, ABJLKPQO>QFKD ELT QEB BIBJBKQP CFQ QLDBQEBO FK > PFKDIB PLCQT>OB MOLGB@Q.
) BK@LRO>DB VLR QL ABSBILM VLRO LTK FAB>, LKB QE>Q FP JLOB >?PQO>@Q >KA @OB>QFSB FK FQP
QEFKHFKD.
P.8 Where do I find the code online and submit P.8 Where do I find the code online and submit
feedback? feedback?
&LO >II QEFKDP ?LLH-OBI>QBA, MIB>PB SFPFQ QEB .>QROB LC #LAB TB?PFQB
(EQQM://TTT.K>QROBLC@LAB.@LJ). 4EB O>T PLRO@B QBUQ LC QEB ?LLH >KA >II LC QEB FIIRPQO>QFLKP >OB
4EB .>QROB LC #LAB (S005)
UFS
LK 'FQ(R? (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB). 0IB>PB IB>SB CBBA?>@H >KA
PR?JFQ @LOOB@QFLKP RPFKD 'FQ(R? FPPRBP.
4EB PLRO@B @LAB CLO >II LC QEB BU>JMIBP (>KA BUBO@FPBP) FP >IPL >S>FI>?IB LK 'FQ(R?
(EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB-%U>JMIBP). 4EB @E>MQBOP QEBJPBISBP
FK@IRAB @LAB PKFMMBQP FK-IFKB TFQE QEB QBUQ. (LTBSBO, ) T>KQ QL JBKQFLK QE>Q FK J>KV @>PBP, )
E>SB PELOQBKBA LO PFJMIFCFBA QEB @LAB PKFMMBQP FK LOABO QL FIIRPQO>QB > PMB@FCF@ MLFKQ. )K >II
@>PBP, QEB CRII @LAB TFQE @LJJBKQP @>K ?B CLRKA SF> 'FQ(R?.
)C VLR E>SB NRBPQFLKP >?LRQ QEB @LAB FQPBIC, ) TLRIA PRDDBPQ MLPQFKD QEBJ LK QEB 0OL@BPPFKD
CLORJ (EQQM://CLORJ.MOL@BPPFKD.LOD).
0OBC>@B
US
Table of Contents Table of Contents
Acknowledgments
!.1 ! IFQQIB ?FQ LC EFPQLOV
!.2 +F@HPQ>OQBO
Preface
0.1 7E>Q FP QEFP ?LLH?
0.2 ! TLOA >?LRQ 0OL@BPPFKD
0.3 7E>Q AL VLR KBBA QL HKLT?
0.4 7E>Q >OB VLR RPFKD QL OB>A QEFP ?LLH?
0.5 4EB gPQLOVh LC QEFP ?LLH
0.6 4EFP ?LLH >P > PVII>?RP
0.7 4EB %@LPVPQBJ 0OLGB@Q
0.8 7EBOB AL ) CFKA QEB @LAB LKIFKB >KA PR?JFQ CBBA?>@H?
Introduction
).1 2>KALJ 7>IHP
).2 4EB 2>KALJ 7>IHBO #I>PP
).3 0OL?>?FIFQV >KA .LK-5KFCLOJ $FPQOF?RQFLKP
iv
S
SFF
ix
FU
U
U
UF
UF
UFFF
UFS
UFS
1
1
2
7
4EB .>QROB LC #LAB (S005)
USF
).4 ! .LOJ>I $FPQOF?RQFLK LC 2>KALJ .RJ?BOP
).5 ! #RPQLJ $FPQOF?RQFLK LC 2>KALJ .RJ?BOP
).6 0BOIFK .LFPB (! 3JLLQEBO !MMOL>@E)
).7 /KT>OA
Chapter 1. Vectors
1.1 6B@QLOP, 9LR #LJMIBQB -B
1.2 6B@QLOP CLO 0OL@BPPFKD 0OLDO>JJBOP
1.3 6B@QLO !AAFQFLK
1.4 -LOB 6B@QLO ->QE
1.5 6B@QLO ->DKFQRAB
1.6 .LOJ>IFWFKD 6B@QLOP
1.7 6B@QLO -LQFLK: 6BIL@FQV
1.8 6B@QLO -LQFLK: !@@BIBO>QFLK
1.9 3Q>QF@ SP. .LK-3Q>QF@ &RK@QFLKP
1.10 )KQBO>@QFSFQV TFQE !@@BIBO>QFLK
Chapter 2. Forces
2.1 &LO@BP >KA .BTQLKfP ,>TP LC -LQFLK
2.2 &LO@BP >KA 0OL@BPPFKDd.BTQLKfP 3B@LKA ,>T >P > &RK@QFLK
2.3 &LO@B !@@RJRI>QFLK
2.4 $B>IFKD TFQE ->PP
2.5 #OB>QFKD &LO@BP
2.6 'O>SFQV LK %>OQE >KA -LABIFKD > &LO@B
2.7 &OF@QFLK
2.8 !FO >KA &IRFA 2BPFPQ>K@B
2.9 'O>SFQ>QFLK>I !QQO>@QFLK
2.10 %SBOVQEFKD !QQO>@QP (LO 2BMBIP) %SBOVQEFKD
Chapter 3. Oscillation
11
14
17
26
27
28
30
33
37
42
43
45
49
54
57
63
63
67
68
70
73
77
80
83
88
97
101
4>?IB LC #LKQBKQP
USFF
3.1 !KDIBP
3.2 !KDRI>O -LQFLK
3.3 4OFDLKLJBQOV
3.4 0LFKQFKD FK QEB $FOB@QFLK LC -LSBJBKQ
3.5 0LI>O SP. #>OQBPF>K #LLOAFK>QBP
3.6 /P@FII>QFLK !JMIFQRAB >KA 0BOFLA
3.7 /P@FII>QFLK TFQE !KDRI>O 6BIL@FQV
3.8 7>SBP
3.9 4OFDLKLJBQOV >KA &LO@BP: 4EB 0BKARIRJ
3.10 3MOFKD &LO@BP
Chapter 4. Particle Systems
4.1 7EV 7B .BBA 0>OQF@IB 3VPQBJP
4.2 ! 3FKDIB 0>OQF@IB
4.3 4EB !OO>V,FPQ
4.4 4EB 0>OQF@IB 3VPQBJ #I>PP
4.5 ! 3VPQBJ LC 3VPQBJP
4.6 )KEBOFQ>K@B >KA 0LIVJLOMEFPJ: !K )KQOLAR@QFLK
4.7 )KEBOFQ>K@B ">PF@P
4.8 0>OQF@IBP TFQE )KEBOFQ>K@B
4.9 0LIVJLOMEFPJ ">PF@P
4.10 0>OQF@IB 3VPQBJP TFQE 0LIVJLOMEFPJ
4.11 0>OQF@IB 3VPQBJP TFQE &LO@BP
4.12 0>OQF@IB 3VPQBJP TFQE 2BMBIIBOP
4.13 )J>DB 4BUQROBP >KA !AAFQFSB "IBKAFKD
Chapter 5. Physics Libraries
5.1 7E>Q )P "LU2$ >KA 7EBK )P )Q 5PBCRI?
5.2 'BQQFKD "LU2$ FK 0OL@BPPFKD
5.3 "LU2$ ">PF@P
101
104
108
109
112
116
119
122
127
134
143
144
145
149
155
157
160
162
166
168
170
173
178
183
189
190
192
192
4EB .>QROB LC #LAB (S005)
USFFF
5.4 ,FSFKD FK > "LU2$ 7LOIA
5.5 "RFIAFKD > "LU2$ ?LAV
5.6 4EOBBfP #LJM>KV: "LAFBP >KA 3E>MBP >KA &FUQROBP
5.7 "LU2$ >KA 0OL@BPPFKD: 2BRKFQBA >KA )Q &BBIP 3L 'LLA
5.8 &FUBA "LU2$ /?GB@QP
5.9 ! #ROSV "LRKA>OV
5.10 #LJMIBU &LOJP
5.11 &BBIFKD !QQ>@EBAd"LU2$ *LFKQP
5.12 "OFKDFKD )Q !II ">@H (LJB QL &LO@BP
5.13 #LIIFPFLK %SBKQP
5.14 ! "OFBC )KQBOIRABd)KQBDO>QFLK -BQELAP
5.15 6BOIBQ 0EVPF@P TFQE QLUF@IF?P
5.16 0>OQF@IBP >KA 3MOFKDP FK QLUF@IF?P
5.17 0RQQFKD )Q !II 4LDBQEBO: ! 3FJMIB )KQBO>@QFSB 3MOFKD
5.18 #LKKB@QBA 3VPQBJP 0>OQ ): 3QOFKD
5.19 #LKKB@QBA 3VPQBJP 0>OQ )): &LO@B-$FOB@QBA 'O>ME
5.20 !QQO>@QFLK >KA 2BMRIPFLK "BE>SFLOP
Chapter 6. Autonomous Agents
6.1 &LO@BP COLJ 7FQEFK
6.2 6BEF@IBP >KA 3QBBOFKD
6.3 4EB 3QBBOFKD &LO@B
6.4 !OOFSFKD "BE>SFLO
6.5 9LRO /TK $BPFOBP: $BPFOBA 6BIL@FQV
6.6 &ILT &FBIAP
6.7 4EB $LQ 0OLAR@Q
6.8 0>QE &LIILTFKD
6.9 0>QE &LIILTFKD TFQE -RIQFMIB 3BDJBKQP
6.10 #LJMIBU 3VPQBJP
6.11 'OLRM "BE>SFLOP (LO: ,BQfP KLQ ORK FKQL B>@E LQEBO)
196
198
200
203
209
211
215
222
232
234
238
241
244
247
249
253
256
260
260
262
263
270
274
276
282
286
294
298
300
4>?IB LC #LKQBKQP
UFU
6.12 #LJ?FK>QFLKP
6.13 &IL@HFKD
6.14 !IDLOFQEJF@ %CCF@FBK@V (LO: 7EV ALBP JV $(*%! ORK PL PILTIV?)
6.15 ! &BT ,>PQ .LQBP: /MQFJFW>QFLK 4OF@HP
Chapter 7. Cellular Automata
7.1 7E>Q )P > #BIIRI>O !RQLJ>QLK?
7.2 %IBJBKQ>OV #BIIRI>O !RQLJ>Q>
7.3 (LT QL 0OLDO>J >K %IBJBKQ>OV #!
7.4 $O>TFKD >K %IBJBKQ>OV #!
7.5 7LICO>J #I>PPFCF@>QFLK
7.6 4EB '>JB LC ,FCB
7.7 0OLDO>JJFKD QEB '>JB LC ,FCB
7.8 /?GB@Q-/OFBKQBA #BIIP
7.9 6>OF>QFLKP LC 4O>AFQFLK>I #!
Chapter 8. Fractals
8.1 7E>Q )P > &O>@Q>I?
8.2 2B@ROPFLK
8.3 4EB #>KQLO 3BQ TFQE > 2B@ROPFSB &RK@QFLK
8.4 4EB +L@E #ROSB >KA QEB !OO>V,FPQ 4B@EKFNRB
8.5 4OBBP
8.6 ,-PVPQBJP
Chapter 9. The Evolution of Code
9.1 'BKBQF@ !IDLOFQEJP: )KPMFOBA ?V !@QR>I %SBKQP
9.2 7EV 5PB 'BKBQF@ !IDLOFQEJP?
9.3 $>OTFKF>K .>QRO>I 3BIB@QFLK
9.4 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ): #OB>QFKD > 0LMRI>QFLK
306
308
315
317
323
324
325
330
336
340
342
345
349
351
355
356
358
363
366
374
382
390
391
392
394
395
4EB .>QROB LC #LAB (S005)
UU
9.5 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ )): 3BIB@QFLK
9.6 4EB 'BKBQF@ !IDLOFQEJ, 0>OQ ))): 2BMOLAR@QFLK
9.7 #LAB CLO #OB>QFKD QEB 0LMRI>QFLK
9.8 'BKBQF@ !IDLOFQEJP: 0RQQFKD )Q !II 4LDBQEBO
9.9 'BKBQF@ !IDLOFQEJP: ->HB 4EBJ 9LRO /TK
9.10 %SLISFKD &LO@BP: 3J>OQ 2L@HBQP
9.11 3J>OQ 2L@HBQP: 0RQQFKD )Q !II 4LDBQEBO
9.12 )KQBO>@QFSB 3BIB@QFLK
9.13 %@LPVPQBJ 3FJRI>QFLK
Chapter 10. Neural Networks
10.1 !OQFCF@F>I .BRO>I .BQTLOHP: )KQOLAR@QFLK >KA !MMIF@>QFLK
10.2 4EB 0BO@BMQOLK
10.3 3FJMIB 0>QQBOK 2B@LDKFQFLK 5PFKD > 0BO@BMQOLK
10.4 #LAFKD QEB 0BO@BMQOLK
10.5 ! 3QBBOFKD 0BO@BMQOLK
10.6 )QfP > g.BQTLOH,h 2BJBJ?BO?
10.7 .BRO>I .BQTLOH $F>DO>J
10.8 !KFJ>QFKD &BBA &LOT>OA
Further Reading
"LLHP
0>MBOP >KA !OQF@IBP
Index
397
399
402
409
413
420
425
431
435
444
445
448
450
452
460
466
468
473
481
481
482
484
4>?IB LC #LKQBKQP
UUF
Introduction Introduction
I! .: AD< D6A5 ;.AB?2.J
Woody Allen
(BOB TB >OB: QEB ?BDFKKFKD. 7BII, >IJLPQ QEB ?BDFKKFKD. )C FQfP ?BBK > TEFIB PFK@B VLRfSB ALKB
>KV MOLDO>JJFKD FK 0OL@BPPFKD (LO >KV J>QE, CLO QE>Q J>QQBO), QEFP FKQOLAR@QFLK TFII DBQ VLRO
JFKA ?>@H FKQL @LJMRQ>QFLK>I QEFKHFKD ?BCLOB TB >MMOL>@E PLJB LC QEB JLOB AFCCF@RIQ >KA
@LJMIBU J>QBOF>I.
)K #E>MQBO 1, TBfOB DLFKD QL Q>IH >?LRQ QEB @LK@BMQ LC > SB@QLO >KA ELT FQ TFII PBOSB >P QEB
?RFIAFKD ?IL@H CLO PFJRI>QFKD JLQFLK QEOLRDELRQ QEFP ?LLH. "RQ ?BCLOB TB Q>HB QE>Q PQBM, IBQfP
QEFKH >?LRQ TE>Q FQ JB>KP CLO PLJBQEFKD QL PFJMIV JLSB >OLRKA QEB P@OBBK. ,BQfP ?BDFK TFQE
LKB LC QEB ?BPQ-HKLTK >KA PFJMIBPQ PFJRI>QFLKP LC JLQFLKdQEB O>KALJ T>IH.
I.1 Random Walks I.1 Random Walks
)J>DFKB VLR >OB PQ>KAFKD FK QEB JFAAIB LC > ?>I>K@B ?B>J. %SBOV QBK PB@LKAP, VLR CIFM > @LFK.
(B>AP, Q>HB > PQBM CLOT>OA. 4>FIP, Q>HB > PQBM ?>@HT>OA. 4EFP FP > O>KALJ T>IHd> M>QE ABCFKBA
>P > PBOFBP LC O>KALJ PQBMP. 3QBMMFKD LCC QE>Q ?>I>K@B ?B>J >KA LKQL QEB CILLO, VLR @LRIA
MBOCLOJ > O>KALJ T>IH FK QTL AFJBKPFLKP ?V CIFMMFKD QE>Q P>JB @LFK QTF@B TFQE QEB CLIILTFKD
OBPRIQP:
4EB .>QROB LC #LAB (S005)
1
Flip 1 Flip 1 Flip 2 Flip 2 Result Result
(B>AP (B>AP 3QBM CLOT>OA.
(B>AP 4>FIP 3QBM OFDEQ.
4>FIP (B>AP 3QBM IBCQ.
4>FIP 4>FIP 3QBM ?>@HT>OA.
9BP, QEFP J>V PBBJ IFHB > M>OQF@RI>OIV RKPLMEFPQF@>QBA >IDLOFQEJ. .BSBOQEBIBPP, O>KALJ
T>IHP @>K ?B RPBA QL JLABI MEBKLJBK> QE>Q L@@RO FK QEB OB>I TLOIA, COLJ QEB JLSBJBKQP LC
JLIB@RIBP FK > D>P QL QEB ?BE>SFLO LC > D>J?IBO PMBKAFKD > A>V >Q QEB @>PFKL. !P CLO RP, TB
?BDFK QEFP ?LLH PQRAVFKD > O>KALJ T>IH TFQE QEOBB DL>IP FK JFKA.
1. 7B KBBA QL OBSFBT > MOLDO>JJFKD @LK@BMQ @BKQO>I QL QEFP ?LLHdL?GB@Q-LOFBKQBA
MOLDO>JJFKD. 4EB O>KALJ T>IHBO TFII PBOSB >P > QBJMI>QB CLO ELT TB TFII RPB
L?GB@Q-LOFBKQBA ABPFDK QL J>HB QEFKDP QE>Q JLSB >OLRKA > 0OL@BPPFKD TFKALT.
2. 4EB O>KALJ T>IH FKPQFD>QBP QEB QTL NRBPQFLKP QE>Q TB TFII >PH LSBO >KA LSBO >D>FK
QEOLRDELRQ QEFP ?LLH: g(LT AL TB ABCFKB QEB ORIBP QE>Q DLSBOK QEB ?BE>SFLO LC LRO
L?GB@QP?h >KA QEBK, g(LT AL TB FJMIBJBKQ QEBPB ORIBP FK 0OL@BPPFKD?h
3. 4EOLRDELRQ QEB ?LLH, TBfII MBOFLAF@>IIV KBBA > ?>PF@ RKABOPQ>KAFKD LC
O>KALJKBPP, MOL?>?FIFQV, >KA 0BOIFK KLFPB. 4EB O>KALJ T>IH TFII >IILT RP QL
ABJLKPQO>QB > CBT HBV MLFKQP QE>Q TFII @LJB FK E>KAV I>QBO.
I.2 The Random Walker Class I.2 The Random Walker Class
,BQfP OBSFBT > ?FQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKD (//0) CFOPQ ?V ?RFIAFKD > 4;FE?L L?GB@Q.
4EFP TFII ?B LKIV > @ROPLOV OBSFBT. )C VLR E>SB KBSBO TLOHBA TFQE //0 ?BCLOB, VLR J>V T>KQ
PLJBQEFKD JLOB @LJMOBEBKPFSB; )fA PRDDBPQ PQLMMFKD EBOB >KA OBSFBTFKD QEB ?>PF@P LK QEB
0OL@BPPFKD TB?PFQB (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/) ?BCLOB @LKQFKRFKD.
!K :-50.? :-50.? FK 0OL@BPPFKD FP >K BKQFQV QE>Q E>P ?LQE A>Q> >KA CRK@QFLK>IFQV. 7B >OB ILLHFKD QL
ABPFDK > 4;FE?L L?GB@Q QE>Q ?LQE HBBMP QO>@H LC FQP A>Q> (TEBOB FQ BUFPQP LK QEB P@OBBK) >KA
E>P QEB @>M>?FIFQV QL MBOCLOJ @BOQ>FK >@QFLKP (PR@E >P AO>T FQPBIC LO Q>HB > PQBM).
! .7,>> .7,>> FP QEB QBJMI>QB CLO ?RFIAFKD >@QR>I FKPQ>K@BP LC L?GB@QP. 4EFKH LC > @I>PP >P QEB
@LLHFB @RQQBO; QEB L?GB@QP >OB QEB @LLHFBP QEBJPBISBP.
,BQfP ?BDFK ?V ABCFKFKD QEB 4;FE?L @I>PPdTE>Q FQ JB>KP QL ?B > 4;FE?L L?GB@Q. 4EB 4;FE?L
LKIV KBBAP QTL MFB@BP LC A>Q>d> KRJ?BO CLO FQP U-IL@>QFLK >KA LKB CLO FQP V-IL@>QFLK.
=F;MM 4;FE?L U
)KQOLAR@QFLK
2
%SBOV @I>PP JRPQ E>SB > @LKPQOR@QLO, > PMB@F>I CRK@QFLK QE>Q FP @>IIBA TEBK QEB L?GB@Q FP CFOPQ
@OB>QBA. 9LR @>K QEFKH LC FQ >P QEB L?GB@QfP M?NOJ(). 4EBOB, TBfII FKFQF>IFWB QEB 4;FE?LfP PQ>OQFKD
IL@>QFLK (FK QEFP @>PB, QEB @BKQBO LC QEB TFKALT).
&FK>IIV, FK >AAFQFLK QL A>Q>, @I>PPBP @>K ?B ABCFKBA TFQE CRK@QFLK>IFQV. )K QEFP BU>JMIB, > 4;FE?L
E>P QTL CRK@QFLKP. 7B CFOPQ TOFQB > CRK@QFLK QE>Q >IILTP QEB L?GB@Q QL AFPMI>V FQPBIC (>P > TEFQB
ALQ).
4EB PB@LKA CRK@QFLK AFOB@QP QEB 4;FE?L L?GB@Q QL Q>HB > PQBM. .LT, QEFP FP TEBOB QEFKDP DBQ >
?FQ JLOB FKQBOBPQFKD. 2BJBJ?BO QE>Q CILLO LK TEF@E TB TBOB Q>HFKD O>KALJ PQBMP? 7BII, KLT
TB @>K RPB > 0OL@BPPFKD TFKALT FK QE>Q P>JB @>M>@FQV. 4EBOB >OB CLRO MLPPF?IB PQBMP. ! PQBM
QL QEB OFDEQ @>K ?B PFJRI>QBA ?V FK@OBJBKQFKD R (R++); QL QEB IBCQ ?V AB@OBJBKQFKD R (R--);
CLOT>OA ?V DLFKD ALTK > MFUBI (S++); >KA ?>@HT>OA ?V DLFKD RM > MFUBI (S--). (LT AL TB MF@H
COLJ QEBPB CLRO @ELF@BP? %>OIFBO TB PQ>QBA QE>Q TB @LRIA CIFM QTL @LFKP. )K 0OL@BPPFKD,
ELTBSBO, TEBK TB T>KQ QL O>KALJIV @ELLPB COLJ > IFPQ LC LMQFLKP, TB @>K MF@H > O>KALJ
KRJ?BO RPFKD L;H>IG().
4EB >?LSB IFKB LC @LAB MF@HP > O>KALJ CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 4 >KA @LKSBOQP FQ
QL >K FKQBDBO, TFQE > OBPRIQ LC 0, 1, 2, LO 3. 4B@EKF@>IIV PMB>HFKD, QEB EFDEBPQ KRJ?BO TFII KBSBO
?B 4.0, ?RQ O>QEBO 3.999999999 (TFQE >P J>KV 9P >P QEBOB >OB AB@FJ>I MI>@BP); PFK@B QEB
MOL@BPP LC @LKSBOQFKD QL >K FKQBDBO ILMP LCC QEB AB@FJ>I MI>@B, QEB EFDEBPQ CHN TB @>K DBQ FP 3.
.BUQ, TB Q>HB QEB >MMOLMOF>QB PQBM (IBCQ, OFDEQ, RM, LO ALTK) ABMBKAFKD LK TEF@E O>KALJ
KRJ?BO T>P MF@HBA.
Objects have data. CHN R;
CHN S;
Objects have a constructor where they are
initialized.
4;FE?L() U
R = QC>NB/2;
S = B?CABN/2;
W
Objects have functions. PIC> >CMJF;S() U
MNLIE?(0);
JICHN(R,S);
W
PIC> MN?J() U
0, 1, 2, or 3 CHN =BIC=? = CHN(L;H>IG(4));
4EB .>QROB LC #LAB (S005)
3
.LT QE>Q TBfSB TOFQQBK QEB @I>PP, FQfP QFJB QL J>HB >K >@QR>I 4;FE?L L?GB@Q FK QEB J>FK M>OQ
LC LRO PHBQ@EdM?NOJ() >KA >L;Q(). !PPRJFKD TB >OB ILLHFKD QL JLABI > PFKDIB O>KALJ
T>IH, TB AB@I>OB LKB DIL?>I S>OF>?IB LC QVMB 4;FE?L.
4EBK TB @OB>QB QEB L?GB@Q FK M?NOJ() ?V @>IIFKD QEB @LKPQOR@QLO TFQE QEB H?Q LMBO>QLO.
Example I.1: Traditional random walk
!57< H=A9 MCI G99 H<9 56CJ9 !L5AD@9 <958=B; =B H<=G 6CC?, =H A95BG H<9F9 =G 5
7CFF9GDCB8=B; 7C89 9L5AD@9 5J5=@56@9 CB #=H$I6 (<HHD://;=H<I6.7CA/G<=::A5B//<9-*5HIF9-
C:-CC89-!L5AD@9G).
&FK>IIV, AROFKD B>@E @V@IB QEOLRDE >L;Q(), TB >PH QEB 4;FE?L QL Q>HB > PQBM >KA AO>T > ALQ.
3FK@B TB LKIV AO>T QEB ?>@HDOLRKA LK@B FK M?NOJ(), O>QEBO QE>K @IB>OFKD FQ @LKQFKR>IIV
B>@E QFJB QEOLRDE >L;Q(), TB PBB QEB QO>FI LC QEB O>KALJ T>IH FK LRO 0OL@BPPFKD TFKALT.
The random choice determines our step. C@ (=BIC=? == 0) U
R++;
W ?FM? C@ (=BIC=? == 1) U
R--;
W ?FM? C@ (=BIC=? == 2) U
S++;
W ?FM? U
S--;
W
W
W
A Walker object 4;FE?L Q;
PIC> M?NOJ() U
MCT?(640,360);
Create the Walker. M = D;M 17BA;H();
<;=EALIOH>(255);
W
PIC> >L;Q() U
Call functions on the Walker. M.IJ;F();
M.:?IFB7O();
W
)KQOLAR@QFLK
4
4EBOB >OB > @LRMIB FJMOLSBJBKQP TB @LRIA J>HB QL QEB O>KALJ T>IHBO. &LO LKB, QEFP
4;FE?LfP PQBM @ELF@BP >OB IFJFQBA QL CLRO LMQFLKPdRM, ALTK, IBCQ, >KA OFDEQ. "RQ >KV DFSBK MFUBI
FK QEB TFKALT E>P BFDEQ MLPPF?IB KBFDE?LOP, >KA > KFKQE MLPPF?FIFQV FP QL PQ>V FK QEB P>JB
MI>@B.
4L FJMIBJBKQ > 4;FE?L L?GB@Q QE>Q @>K PQBM QL >KV KBFDE?LOFKD MFUBI (LO PQ>V MRQ), TB @LRIA
MF@H > KRJ?BO ?BQTBBK 0 >KA 8 (KFKB MLPPF?IB @ELF@BP). (LTBSBO, > JLOB BCCF@FBKQ T>V QL
TOFQB QEB @LAB TLRIA ?B QL PFJMIV MF@H COLJ QEOBB MLPPF?IB PQBMP >ILKD QEB U->UFP (-1, 0, LO 1)
>KA QEOBB MLPPF?IB PQBMP >ILKD QEB V->UFP.
4>HFKD QEFP CROQEBO, TB @LRIA RPB CIL>QFKD MLFKQ KRJ?BOP (F.B. AB@FJ>I KRJ?BOP) CLO R >KA S
FKPQB>A >KA JLSB >@@LOAFKD QL >K >O?FQO>OV O>KALJ S>IRB ?BQTBBK -1 >KA 1.
F64B?2 !.1
PIC> MN?J() U
Yields -1, 0, or 1 CHN MN?JR = CHN(L;H>IG(3))-1;
CHN MN?JS = CHN(L;H>IG(3))-1;
R += MN?JR;
S += MN?JS;
W
PIC> MN?J() U
Yields any floating point number between
-1.0 and 1.0
@FI;N MN?JR = L;H>IG(-1, 1);
@FI;N MN?JS = L;H>IG(-1, 1);
4EB .>QROB LC #LAB (S005)
5
!II LC QEBPB S>OF>QFLKP LK QEB gQO>AFQFLK>Ih O>KALJ T>IH E>SB LKB QEFKD FK @LJJLK: >Q >KV
JLJBKQ FK QFJB, QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK > DFSBK AFOB@QFLK FP BNR>I
QL QEB MOL?>?FIFQV QE>Q QEB 4;FE?L TFII Q>HB > PQBM FK >KV AFOB@QFLK. )K LQEBO TLOAP, FC QEBOB
>OB CLRO MLPPF?IB PQBMP, QEBOB FP > 1 FK 4 (LO 25%) @E>K@B QEB 4;FE?L TFII Q>HB >KV DFSBK PQBM.
7FQE KFKB MLPPF?IB PQBMP, FQfP > 1 FK 9 (LO 11.1%) @E>K@B.
#LKSBKFBKQIV, QEFP FP ELT QEB L;H>IG() CRK@QFLK TLOHP. 0OL@BPPFKDfP O>KALJ KRJ?BO
DBKBO>QLO (TEF@E LMBO>QBP ?BEFKA QEB P@BKBP) MOLAR@BP TE>Q FP HKLTK >P > gRKFCLOJh
AFPQOF?RQFLK LC KRJ?BOP. 7B @>K QBPQ QEFP AFPQOF?RQFLK TFQE > 0OL@BPPFKD PHBQ@E QE>Q @LRKQP
B>@E QFJB > O>KALJ KRJ?BO FP MF@HBA >KA DO>MEP FQ >P QEB EBFDEQ LC > OB@Q>KDIB.
Example I.2: Random number distribution
R += MN?JR;
S += MN?JS;
W
An array to keep track of how often random
numbers are picked
CHN89 L;H>IG!IOHNM;
PIC> M?NOJ() U
MCT?(640,240);
L;H>IG!IOHNM = H?Q CHN8209;
W
PIC> >L;Q() U
<;=EALIOH>(255);
Pick a random number and increase the
count.
CHN CH>?R = CHN(L;H>IG(L;H>IG!IOHNM.F?HANB));
L;H>IG!IOHNM8CH>?R9++;
MNLIE?(0);
@CFF(175);
CHN Q = QC>NB/L;H>IG!IOHNM.F?HANB;
)KQOLAR@QFLK
6
4EB >?LSB P@OBBKPELQ PELTP QEB OBPRIQ LC QEB PHBQ@E ORKKFKD CLO > CBT JFKRQBP. .LQF@B ELT
B>@E ?>O LC QEB DO>ME AFCCBOP FK EBFDEQ. /RO P>JMIB PFWB (F.B. QEB KRJ?BO LC O>KALJ KRJ?BOP
TBfSB MF@HBA) FP O>QEBO PJ>II >KA QEBOB >OB PLJB L@@>PFLK>I AFP@OBM>K@FBP, TEBOB @BOQ>FK
KRJ?BOP >OB MF@HBA JLOB LCQBK. /SBO QFJB, TFQE > DLLA O>KALJ KRJ?BO DBKBO>QLO, QEFP TLRIA
BSBK LRQ.
Pseudo-Random Numbers Pseudo-Random Numbers
4EB O>KALJ KRJ?BOP TB DBQ COLJ QEB L;H>IG() CRK@QFLK >OB KLQ QORIV O>KALJ;
QEBOBCLOB QEBV >OB HKLTK >P gMPBRAL-O>KALJ.h 4EBV >OB QEB OBPRIQ LC > J>QEBJ>QF@>I
CRK@QFLK QE>Q PFJRI>QBP O>KALJKBPP. 4EFP CRK@QFLK TLRIA VFBIA > M>QQBOK LSBO QFJB, ?RQ
QE>Q QFJB MBOFLA FP PL ILKD QE>Q CLO RP, FQfP GRPQ >P DLLA >P MROB O>KALJKBPP!
Graphing the results @IL (CHN R = 0; R < L;H>IG!IOHNM.F?HANB; R++) U
L?=N(R*Q,B?CABN-L;H>IG!IOHNM8R9,Q-1,L;H>IG!IOHNM8R9);
W
W
#OB>QB > O>KALJ T>IHBO QE>Q E>P > QBKABK@V QL JLSB ALTK >KA QL QEB OFDEQ. (7BfII PBB
QEB PLIRQFLK QL QEFP FK QEB KBUQ PB@QFLK.)
Exercise I.1 Exercise I.1
I.3 Probability and Non-Uniform Distributions I.3 Probability and Non-Uniform Distributions
2BJBJ?BO TEBK VLR CFOPQ PQ>OQBA MOLDO>JJFKD FK 0OL@BPPFKD? 0BOE>MP VLR T>KQBA QL AO>T >
ILQ LC @FO@IBP LK QEB P@OBBK. 3L VLR P>FA QL VLROPBIC: g/E, ) HKLT. )fII AO>T >II QEBPB @FO@IBP >Q
O>KALJ IL@>QFLKP, TFQE O>KALJ PFWBP >KA O>KALJ @LILOP.h )K > @LJMRQBO DO>MEF@P PVPQBJ, FQfP
LCQBK B>PFBPQ QL PBBA > PVPQBJ TFQE O>KALJKBPP. )K QEFP ?LLH, ELTBSBO, TBfOB ILLHFKD QL ?RFIA
PVPQBJP JLABIBA LK TE>Q TB PBB FK K>QROB. $BC>RIQFKD QL O>KALJKBPP FP KLQ > M>OQF@RI>OIV
QELRDEQCRI PLIRQFLK QL > ABPFDK MOL?IBJdFK M>OQF@RI>O, QEB HFKA LC MOL?IBJ QE>Q FKSLISBP
@OB>QFKD >K LOD>KF@ LO K>QRO>I-ILLHFKD PFJRI>QFLK.
7FQE > CBT QOF@HP, TB @>K @E>KDB QEB T>V TB RPB L;H>IG() QL MOLAR@B gKLK-RKFCLOJh
AFPQOF?RQFLKP LC O>KALJ KRJ?BOP. 4EFP TFII @LJB FK E>KAV QEOLRDELRQ QEB ?LLH >P TB ILLH >Q >
KRJ?BO LC AFCCBOBKQ P@BK>OFLP. 7EBK TB BU>JFKB DBKBQF@ >IDLOFQEJP, CLO BU>JMIB, TBfII KBBA >
JBQELALILDV CLO MBOCLOJFKD gPBIB@QFLKhdTEF@E JBJ?BOP LC LRO MLMRI>QFLK PELRIA ?B
PBIB@QBA QL M>PP QEBFO $.! QL QEB KBUQ DBKBO>QFLK? 2BJBJ?BO QEB @LK@BMQ LC PROSFS>I LC QEB
CFQQBPQ? ,BQfP P>V TB E>SB > MLMRI>QFLK LC JLKHBVP BSLISFKD. .LQ BSBOV JLKHBV TFII E>SB >
4EB .>QROB LC #LAB (S005)
7
BNR>I @E>K@B LC OBMOLAR@FKD. 4L PFJRI>QB $>OTFKF>K BSLIRQFLK, TB @>KfQ PFJMIV MF@H QTL
O>KALJ JLKHBVP QL ?B M>OBKQP. 7B KBBA QEB JLOB gCFQh LKBP QL ?B JLOB IFHBIV QL ?B @ELPBK.
7B KBBA QL ABCFKB QEB gMOL?>?FIFQV LC QEB CFQQBPQ.h &LO BU>JMIB, > M>OQF@RI>OIV C>PQ >KA PQOLKD
JLKHBV JFDEQ E>SB > 90% @E>K@B LC MOL@OB>QFKD, TEFIB > TB>HBO LKB E>P LKIV > 10%
@E>K@B.
,BQfP M>RPB EBOB >KA Q>HB > ILLH >Q MOL?>?FIFQVfP ?>PF@ MOFK@FMIBP. &FOPQ TBfII BU>JFKB PFKDIB
BSBKQ MOL?>?FIFQV, F.B. QEB IFHBIFELLA QE>Q > DFSBK BSBKQ TFII L@@RO.
)C VLR E>SB > PVPQBJ TFQE > @BOQ>FK KRJ?BO LC MLPPF?IB LRQ@LJBP, QEB MOL?>?FIFQV LC QEB
L@@ROOBK@B LC > DFSBK BSBKQ BNR>IP QEB KRJ?BO LC LRQ@LJBP QE>Q NR>IFCV >P QE>Q BSBKQ
AFSFABA ?V QEB QLQ>I KRJ?BO LC >II MLPPF?IB LRQ@LJBP. ! @LFK QLPP FP > PFJMIB BU>JMIBdFQ E>P
LKIV QTL MLPPF?IB LRQ@LJBP, EB>AP LO Q>FIP. 4EBOB FP LKIV LKB T>V QL CIFM EB>AP. 4EB
MOL?>?FIFQV QE>Q QEB @LFK TFII QROK RM EB>AP, QEBOBCLOB, FP LKB AFSFABA ?V QTL: 1/2 LO 50%.
4>HB > AB@H LC CFCQV-QTL @>OAP. 4EB MOL?>?FIFQV LC AO>TFKD >K >@B COLJ QE>Q AB@H FP:
DKC8;H E< 79;I / DKC8;H E< 97H:I = 4 / 52 = 0.077 = T 8%
4EB MOL?>?FIFQV LC AO>TFKD > AF>JLKA FP:
DKC8;H E< :?7CED:I / DKC8;H E< 97H:I = 13 / 52 = 0.25 = 25%
7B @>K >IPL @>I@RI>QB QEB MOL?>?FIFQV LC JRIQFMIB BSBKQP L@@ROOFKD FK PBNRBK@B. 4L AL QEFP,
TB PFJMIV JRIQFMIV QEB FKAFSFAR>I MOL?>?FIFQFBP LC B>@E BSBKQ.
4EB MOL?>?FIFQV LC > @LFK QROKFKD RM EB>AP QEOBB QFJBP FK > OLT FP:
(1/2) * (1/2) * (1/2) = 1/8 (EH 0.125)
jJB>KFKD QE>Q > @LFK TFII QROK RM EB>AP QEOBB QFJBP FK > OLT LKB LRQ LC BFDEQ QFJBP (B>@E
gQFJBh ?BFKD QEOBB QLPPBP).
4EBOB >OB > @LRMIB LC T>VP FK TEF@E TB @>K RPB QEB L;H>IG() CRK@QFLK TFQE MOL?>?FIFQV FK
@LAB. /KB QB@EKFNRB FP QL CFII >K >OO>V TFQE > PBIB@QFLK LC KRJ?BOPdPLJB LC TEF@E >OB
OBMB>QBAdQEBK @ELLPB O>KALJ KRJ?BOP COLJ QE>Q >OO>V >KA DBKBO>QB BSBKQP ?>PBA LK
QELPB @ELF@BP.
7E>Q FP QEB MOL?>?FIFQV LC AO>TFKD QTL >@BP FK > OLT COLJ > AB@H LC CFCQV-QTL @>OAP?
Exercise I.2 Exercise I.2
CHN89 MNO@@ = H?Q CHN859
1 is stored in the array twice, making it
more likely to be picked.
MNO@@809 = 1;
MNO@@819 = 1;
)KQOLAR@QFLK
8
2RKKFKD QEFP @LAB TFII MOLAR@B > 40% @E>K@B LC MOFKQFKD QEB S>IRB 1, > 20% @E>K@B LC MOFKQFKD
2, >KA > 40% @E>K@B LC MOFKQFKD 3.
7B @>K >IPL >PH CLO > O>KALJ KRJ?BO (IBQfP J>HB FQ PFJMIB >KA GRPQ @LKPFABO O>KALJ CIL>QFKD
MLFKQ S>IRBP ?BQTBBK 0 >KA 1) >KA >IILT >K BSBKQ QL L@@RO LKIV FC LRO O>KALJ KRJ?BO FP TFQEFK
> @BOQ>FK O>KDB. &LO BU>JMIB:
4EFP JBQELA @>K >IPL ?B >MMIFBA QL JRIQFMIB LRQ@LJBP. ,BQfP P>V QE>Q /RQ@LJB ! E>P > 60%
@E>K@B LC E>MMBKFKD, /RQ@LJB ", > 10% @E>K@B, >KA /RQ@LJB #, > 30% @E>K@B. 7B
FJMIBJBKQ QEFP FK @LAB ?V MF@HFKD > O>KALJ CIL>Q >KA PBBFKD FKQL TE>Q O>KDB FQ C>IIP.
i 69HK99B 0.00 5B8 0.60 (60%) Q> +IH7CA9 A
i 69HK99B 0.60 5B8 0.70 (10%) Q> +IH7CA9 B
i 69HK99B 0.70 5B8 1.00 (30%) Q> +IH7CA9 C
7B @LRIA RPB QEB >?LSB JBQELALILDV QL @OB>QB > O>KALJ T>IHBO QE>Q QBKAP QL JLSB QL QEB
OFDEQ. (BOB FP >K BU>JMIB LC > 4;FE?L TFQE QEB CLIILTFKD MOL?>?FIFQFBP:
i 7<5B79 C: ACJ=B; ID: 20%
MNO@@829 = 2;
MNO@@839 = 3;
MNO@@849 = 3;
Picking a random element from an array CHN CH>?R = CHN(L;H>IG(MNO@@.F?HANB));
A probability of 10% @FI;N JLI< = 0.10;
A random floating point value between 0
and 1
@FI;N L = L;H>IG(1);
If our random number is less than 0.1, try
again!
C@ (L < JLI<) U
// NLS ;A;CH!
W
@FI;N HOG = L;H>IG(1);
If random number is less than 0.6 C@ (HOG < 0.6) U
JLCHNFH("-ON=IG? A");
Between 0.6 and 0.7 W ?FM? C@ (HOG < 0.7) U
JLCHNFH("-ON=IG? ");
Greater than 0.7 W ?FM? U
JLCHNFH("-ON=IG? !");
W
4EB .>QROB LC #LAB (S005)
9
i 7<5B79 C: ACJ=B; 8CKB: 20%
i 7<5B79 C: ACJ=B; @9:H: 20%
i 7<5B79 C: ACJ=B; F=;<H: 40%
Example I.3: Walker that tends to move to the right
PIC> MN?J() U
@FI;N L = L;H>IG(1);
A 40% chance of moving to the right! C@ (L < 0.4) U
R++;
W ?FM? C@ (L < 0.6) U
R--;
W ?FM? C@ (L < 0.8) U
S++;
W ?FM? U
S--;
W
W
#OB>QB > O>KALJ T>IHBO TFQE AVK>JF@ MOL?>?FIFQFBP. &LO BU>JMIB, @>K VLR DFSB FQ >
50% @E>K@B LC JLSFKD FK QEB AFOB@QFLK LC QEB JLRPB?
Exercise I.3 Exercise I.3
)KQOLAR@QFLK
10
I.4 A Normal Distribution of Random Numbers I.4 A Normal Distribution of Random Numbers
,BQfP DL ?>@H QL QE>Q MLMRI>QFLK LC PFJRI>QBA 0OL@BPPFKD JLKHBVP. 9LRO MOLDO>J DBKBO>QBP >
QELRP>KA -LKHBV L?GB@QP, B>@E TFQE > EBFDEQ S>IRB ?BQTBBK 200 >KA 300 (>P QEFP FP > TLOIA
LC JLKHBVP QE>Q E>SB EBFDEQP ?BQTBBK 200 >KA 300 MFUBIP).
$LBP QEFP >@@RO>QBIV ABMF@Q QEB EBFDEQP LC OB>I-TLOIA ?BFKDP? 4EFKH LC > @OLTABA PFABT>IH FK
.BT 9LOH #FQV. 0F@H >KV MBOPLK LCC QEB PQOBBQ >KA FQ J>V >MMB>O QE>Q QEBFO EBFDEQ FP O>KALJ.
.BSBOQEBIBPP, FQfP KLQ QEB HFKA LC O>KALJ QE>Q L;H>IG() MOLAR@BP. 0BLMIBfP EBFDEQP >OB KLQ
RKFCLOJIV AFPQOF?RQBA; QEBOB >OB > DOB>Q AB>I JLOB MBLMIB LC >SBO>DB EBFDEQ QE>K QEBOB >OB
SBOV Q>II LO SBOV PELOQ LKBP. 4L PFJRI>QB K>QROB, TB J>V T>KQ FQ QL ?B JLOB IFHBIV QE>Q LRO
JLKHBVP >OB LC >SBO>DB EBFDEQ (250 MFUBIP), VBQ PQFII >IILT QEBJ QL ?B, LK L@@>PFLK, SBOV PELOQ
LO SBOV Q>II.
! AFPQOF?RQFLK LC S>IRBP QE>Q @IRPQBO >OLRKA >K >SBO>DB (OBCBOOBA QL >P QEB gJB>Kh) FP HKLTK >P
> gKLOJ>Ih AFPQOF?RQFLK. )Q FP >IPL @>IIBA QEB '>RPPF>K AFPQOF?RQFLK (K>JBA CLO J>QEBJ>QF@F>K
#>OI &OFBAOF@E '>RPP) LO, FC VLR >OB &OBK@E, QEB ,>MI>@F>K AFPQOF?RQFLK (K>JBA CLO 0FBOOB-3FJLK
,>MI>@B). "LQE J>QEBJ>QF@F>KP TBOB TLOHFKD @LK@ROOBKQIV FK QEB B>OIV KFKBQBBKQE @BKQROV LK
ABCFKFKD PR@E > AFPQOF?RQFLK.
7EBK VLR DO>ME QEB AFPQOF?RQFLK, VLR DBQ PLJBQEFKD QE>Q ILLHP IFHB QEB CLIILTFKD, FKCLOJ>IIV
HKLTK >P > ?BII @ROSB:
4EB @ROSB FP DBKBO>QBA ?V > J>QEBJ>QF@>I CRK@QFLK QE>Q ABCFKBP QEB MOL?>?FIFQV LC >KV DFSBK
S>IRB L@@ROOFKD >P > CRK@QFLK LC QEB JB>K (LCQBK TOFQQBK >P _, QEB 'OBBH IBQQBO AI) >KA
PQ>KA>OA ABSF>QFLK (b, QEB 'OBBH IBQQBO G=;A5).
4EB JB>K FP MOBQQV B>PV QL RKABOPQ>KA. )K QEB @>PB LC LRO EBFDEQ S>IRBP ?BQTBBK 200 >KA
300, VLR MOL?>?IV E>SB >K FKQRFQFSB PBKPB LC QEB JB>K (F.B. >SBO>DB) >P 250. (LTBSBO, TE>Q FC
) TBOB QL P>V QE>Q QEB PQ>KA>OA ABSF>QFLK FP 3 LO 15? 7E>Q ALBP QEFP JB>K CLO QEB KRJ?BOP? 4EB
@FI;N B = L;H>IG(200,300);
F64B?2 !.2 F64B?2 !.3
4EB .>QROB LC #LAB (S005)
11
DO>MEP >?LSB PELRIA DFSB RP > EFKQ. 4EB DO>ME LK QEB IBCQ PELTP RP QEB AFPQOF?RQFLK TFQE >
SBOV ILT PQ>KA>OA ABSF>QFLK, TEBOB QEB J>GLOFQV LC QEB S>IRBP @IRPQBO @ILPBIV >OLRKA QEB
JB>K. 4EB DO>ME LK QEB OFDEQ PELTP RP > EFDEBO PQ>KA>OA ABSF>QFLK, TEBOB QEB S>IRBP >OB
JLOB BSBKIV PMOB>A LRQ COLJ QEB >SBO>DB.
4EB KRJ?BOP TLOH LRQ >P CLIILTP: 'FSBK > MLMRI>QFLK, 68% LC QEB JBJ?BOP LC QE>Q
MLMRI>QFLK TFII E>SB S>IRBP FK QEB O>KDB LC LKB PQ>KA>OA ABSF>QFLK COLJ QEB JB>K, 98%
TFQEFK QTL PQ>KA>OA ABSF>QFLKP, >KA 99.7% TFQEFK QEOBB PQ>KA>OA ABSF>QFLKP. 'FSBK >
PQ>KA>OA ABSF>QFLK LC 5 MFUBIP, LKIV 0.3% LC QEB JLKHBV EBFDEQP TFII ?B IBPP QE>K 235 MFUBIP
(QEOBB PQ>KA>OA ABSF>QFLKP ?BILT QEB JB>K LC 250) LO DOB>QBO QE>K 265 MFUBIP (QEOBB
PQ>KA>OA ABSF>QFLKP >?LSB QEB JB>K LC 250).
Calculating Mean and Standard Deviation Calculating Mean and Standard Deviation
#LKPFABO > @I>PP LC QBK PQRABKQP TEL OB@BFSB QEB CLIILTFKD P@LOBP (LRQ LC 100) LK >
QBPQ:
85, 82, 88, 86, 85, 93, 98, 40, 73, 83
(30 80,9 4> ?30 ,A0=,20: 81.3 (30 80,9 4> ?30 ,A0=,20: 81.3
4EB PQ>KA>OA ABSF>QFLK FP @>I@RI>QBA >P QEB PNR>OB OLLQ LC QEB >SBO>DB LC QEB PNR>OBP
LC ABSF>QFLKP >OLRKA QEB JB>K. )K LQEBO TLOAP, Q>HB QEB AFCCBOBK@B COLJ QEB JB>K CLO
B>@E MBOPLK >KA PNR>OB FQ (S>OF>K@B). #>I@RI>QB QEB >SBO>DB LC >II QEBPB S>IRBP >KA
Q>HB QEB PNR>OB OLLQ >P QEB PQ>KA>OA ABSF>QFLK.
Score Score Difference from Mean Difference from Mean Variance Variance
85 85-81.3 = 3.7
(3.7)
2
= 13.69
40 40-81.3 = -41.3
(-41.3)
2
= 1705.69
BQ@.
Average Variance: Average Variance: 254.23 254.23
(30 >?,9/,=/ /0A4,?4:9 4> ?30 ><@,=0 =::? :1 ?30 ,A0=,20 A,=4,9.0: 15.13 (30 >?,9/,=/ /0A4,?4:9 4> ?30 ><@,=0 =::? :1 ?30 ,A0=,20 A,=4,9.0: 15.13
,R@HFIV CLO RP, QL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP FK > 0OL@BPPFKD PHBQ@E, TB
ALKfQ E>SB QL AL >KV LC QEBPB @>I@RI>QFLKP LROPBISBP. )KPQB>A, TB @>K J>HB RPB LC > @I>PP
HKLTK >P /;H>IG, TEF@E TB DBQ CLO COBB >P M>OQ LC QEB ABC>RIQ *>S> IF?O>OFBP FJMLOQBA FKQL
)KQOLAR@QFLK
12
0OL@BPPFKD (PBB QEB *>S>$L@P (EQQM://AL@P.LO>@IB.@LJ/G>S>PB/6/AL@P/>MF/G>S>/RQFI/
2>KALJ.EQJI) CLO JLOB FKCLOJ>QFLK).
4L RPB QEB 2>KALJ @I>PP, TB JRPQ CFOPQ AB@I>OB > S>OF>?IB LC QVMB 2>KALJ >KA @OB>QB QEB
2>KALJ L?GB@Q FK M?NOJ().
)C TB T>KQ QL MOLAR@B > O>KALJ KRJ?BO TFQE > KLOJ>I (LO '>RPPF>K) AFPQOF?RQFLK B>@E QFJB TB
ORK QEOLRDE >L;Q(), FQfP >P B>PV >P @>IIFKD QEB CRK@QFLK H?RN%;OMMC;H().
(BOBfP QEB QEFKD. 7E>Q >OB TB PRMMLPBA QL AL TFQE QEFP S>IRB? 7E>Q FC TB T>KQBA QL RPB FQ, CLO
BU>JMIB, QL >PPFDK QEB U-MLPFQFLK LC > PE>MB TB AO>T LK P@OBBK?
4EB H?RN%;OMMC;H() CRK@QFLK OBQROKP > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP TFQE QEB
CLIILTFKD M>O>JBQBOP: 5 A95B C: N9FC >KA 5 GH5B85F8 89J=5H=CB C: CB9. ,BQfP P>V TB T>KQ >
JB>K LC 320 (QEB @BKQBO ELOFWLKQ>I MFUBI FK > TFKALT LC TFAQE 640) >KA > PQ>KA>OA ABSF>QFLK LC
60 MFUBIP. 7B @>K >AGRPQ QEB S>IRB QL LRO M>O>JBQBOP ?V JRIQFMIVFKD FQ ?V QEB PQ>KA>OA
ABSF>QFLK >KA >AAFKD QEB JB>K.
Example I.4: Gaussian distribution
We use the variable name generator
because what we have here can be thought
of as a random number generator.
/;H>IG A?H?L;NIL;
PIC> M?NOJ() U
MCT?(640,360);
A?H?L;NIL = H?Q /;H>IG();
W
PIC> >L;Q() U
Asking for a Gaussian random number.
(Note nextGaussian() returns a double and
must be converted to float.)
@FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H();
W
PIC> >L;Q() U
4EB .>QROB LC #LAB (S005)
13
"V AO>TFKD QEB BIIFMPBP LK QLM LC B>@E LQEBO TFQE PLJB QO>KPM>OBK@V, TB @>K >@QR>IIV PBB
QEB AFPQOF?RQFLK. 4EB ?OFDEQBPQ PMLQ FP KB>O QEB @BKQBO, TEBOB JLPQ LC QEB S>IRBP @IRPQBO, ?RQ
BSBOV PL LCQBK @FO@IBP >OB AO>TK C>OQEBO QL QEB OFDEQ LO IBCQ LC QEB @BKQBO.
Note that nextGaussian() returns a double. @FI;N HOG = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H();
@FI;N M> = 60;
@FI;N G?;H = 320;
Multiply by the standard deviation and add
the mean.
@FI;N R = M> * HOG + G?;H;
HI0NLIE?();
@CFF(255,10);
?FFCJM?(R,180,16,16);
W
#LKPFABO > PFJRI>QFLK LC M>FKQ PMI>QQBO AO>TK >P > @LIIB@QFLK LC @LILOBA ALQP. -LPQ LC
QEB M>FKQ @IRPQBOP >OLRKA > @BKQO>I IL@>QFLK, ?RQ PLJB ALQP AL PMI>QQBO LRQ QLT>OAP QEB
BADBP. #>K VLR RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL DBKBO>QB QEB
IL@>QFLKP LC QEB ALQP? #>K VLR >IPL RPB > KLOJ>I AFPQOF?RQFLK LC O>KALJ KRJ?BOP QL
DBKBO>QB > @LILO M>IBQQB?
Exercise I.4 Exercise I.4
! '>RPPF>K O>KALJ T>IH FP ABCFKBA >P LKB FK TEF@E QEB PQBM PFWB (ELT C>O QEB L?GB@Q
JLSBP FK > DFSBK AFOB@QFLK) FP DBKBO>QBA TFQE > KLOJ>I AFPQOF?RQFLK. )JMIBJBKQ QEFP
S>OF>QFLK LC LRO O>KALJ T>IH.
Exercise I.5 Exercise I.5
I.5 A Custom Distribution of Random Numbers I.5 A Custom Distribution of Random Numbers
4EBOB TFII @LJB > QFJB FK VLRO IFCB TEBK VLR AL KLQ T>KQ > RKFCLOJ AFPQOF?RQFLK LC O>KALJ
S>IRBP, LO > '>RPPF>K LKB.,BQfP FJ>DFKB CLO > JLJBKQ QE>Q VLR >OB > O>KALJ T>IHBO FK
PB>O@E LC CLLA. -LSFKD O>KALJIV >OLRKA > PM>@B PBBJP IFHB > OB>PLK>?IB PQO>QBDV CLO
CFKAFKD PLJBQEFKD QL B>Q. !CQBO >II, VLR ALKfQ HKLT TEBOB QEB CLLA FP, PL VLR JFDEQ >P TBII
PB>O@E O>KALJIV RKQFI VLR CFKA FQ. 4EB MOL?IBJ, >P VLR J>V E>SB KLQF@BA, FP QE>Q O>KALJ
T>IHBOP OBQROK QL MOBSFLRPIV SFPFQBA IL@>QFLKP J>KV QFJBP (QEFP FP HKLTK >P gLSBOP>JMIFKD.h)
/KB PQO>QBDV QL >SLFA PR@E > MOL?IBJ FP QL, BSBOV PL LCQBK, Q>HB > SBOV I>ODB PQBM. 4EFP
>IILTP QEB T>IHBO QL CLO>DB O>KALJIV >OLRKA > PMB@FCF@ IL@>QFLK TEFIB MBOFLAF@>IIV GRJMFKD
SBOV C>O >T>V QL OBAR@B QEB >JLRKQ LC LSBOP>JMIFKD. 4EFP S>OF>QFLK LK QEB O>KALJ T>IH
)KQOLAR@QFLK
14
(HKLTK >P > ,ZSV CIFDEQ) OBNRFOBP > @RPQLJ PBQ LC MOL?>?FIFQFBP. 4ELRDE KLQ >K BU>@Q
FJMIBJBKQ>QFLK LC > ,ZSV CIFDEQ, TB @LRIA PQ>QB QEB MOL?>?FIFQV AFPQOF?RQFLK >P CLIILTP: QEB
ILKDBO QEB PQBM, QEB IBPP IFHBIV FQ FP QL ?B MF@HBA; QEB PELOQBO QEB PQBM, QEB JLOB IFHBIV.
%>OIFBO FK QEFP MOLILDRB, TB P>T QE>Q TB @LRIA DBKBO>QB @RPQLJ MOL?>?FIFQV AFPQOF?RQFLKP ?V
CFIIFKD >K >OO>V TFQE S>IRBP (PLJB ARMIF@>QBA PL QE>Q QEBV TLRIA ?B MF@HBA JLOB COBNRBKQIV) LO
?V QBPQFKD QEB OBPRIQ LC L;H>IG(). 7B @LRIA FJMIBJBKQ > ,ZSV CIFDEQ ?V P>VFKD QE>Q QEBOB FP >
1% @E>K@B LC QEB T>IHBO Q>HFKD > I>ODB PQBM.
(LTBSBO, QEFP OBAR@BP QEB MOL?>?FIFQFBP QL > CFUBA KRJ?BO LC LMQFLKP. 7E>Q FC TB T>KQBA QL
J>HB > JLOB DBKBO>I ORIBdQEB EFDEBO > KRJ?BO, QEB JLOB IFHBIV FQ FP QL ?B MF@HBA? 3.145
TLRIA ?B JLOB IFHBIV QL ?B MF@HBA QE>K 3.144, BSBK FC QE>Q IFHBIFELLA FP GRPQ > QFKV ?FQ DOB>QBO. )K
LQEBO TLOAP, FC R FP QEB O>KALJ KRJ?BO, TB @LRIA J>M QEB IFHBIFELLA LK QEB V->UFP TFQE S = R.
)C TB @>K CFDROB LRQ ELT QL DBKBO>QB > AFPQOF?RQFLK LC O>KALJ KRJ?BOP >@@LOAFKD QL QEB >?LSB
DO>ME, QEBK TB TFII ?B >?IB QL >MMIV QEB P>JB JBQELALILDV QL >KV @ROSB CLO TEF@E TB E>SB >
CLOJRI>.
/KB PLIRQFLK FP QL MF@H QTL O>KALJ KRJ?BOP FKPQB>A LC LKB. 4EB CFOPQ O>KALJ KRJ?BO FP GRPQ
QE>Q, > O>KALJ KRJ?BO. 4EB PB@LKA LKB, ELTBSBO, FP TE>Q TBfII @>II > gNR>IFCVFKD O>KALJ
S>IRB.h )Q TFII QBII RP TEBQEBO QL RPB QEB CFOPQ LKB LO QEOLT FQ >T>V >KA MF@H >KLQEBO LKB.
.RJ?BOP QE>Q E>SB >K B>PFBO QFJB NR>IFCVFKD TFII ?B MF@HBA JLOB LCQBK, >KA KRJ?BOP QE>Q
O>OBIV NR>IFCV TFII ?B MF@HBA FKCOBNRBKQIV. (BOB >OB QEB PQBMP (CLO KLT, IBQfP @LKPFABO LKIV
O>KALJ S>IRBP ?BQTBBK 0 >KA 1):
1. 0F@H > O>KALJ KRJ?BO: 21
@FI;N L = L;H>IG(1);
A 1% chance of taking a large step C@ (L < 0.01) U
RMN?J = L;H>IG(-100,100);
SMN?J = L;H>IG(-100,100);
W ?FM? U
RMN?J = L;H>IG(-1,1);
SMN?J = L;H>IG(-1,1);
W
F64B?2 !.4
4EB .>QROB LC #LAB (S005)
15
2. #LJMRQB > MOL?>?FIFQV 0 QE>Q 21 PELRIA NR>IFCV. ,BQfP QOV: 0 = 21.
3. 0F@H >KLQEBO O>KALJ KRJ?BO: 22
4. )C 22 FP IBPP QE>K 0, QEBK TB E>SB CLRKA LRO KRJ?BOd21!
5. )C 22 FP KLQ IBPP QE>K 0, DL ?>@H QL PQBM 1 >KA PQ>OQ LSBO.
(BOB TB >OB P>VFKD QE>Q QEB IFHBIFELLA QE>Q > O>KALJ S>IRB TFII NR>IFCV FP BNR>I QL QEB
O>KALJ KRJ?BO FQPBIC. ,BQfP P>V TB MF@H 0.1 CLO 21. 4EFP JB>KP QE>Q 21 TFII E>SB > 10% @E>K@B
LC NR>IFCVFKD. )C TB MF@H 0.83 CLO 21 QEBK FQ TFII E>SB > 83% @E>K@B LC NR>IFCVFKD. 4EB EFDEBO
QEB KRJ?BO, QEB DOB>QBO QEB IFHBIFELLA QE>Q TB TFII >@QR>IIV RPB FQ.
(BOB FP > CRK@QFLK (K>JBA CLO QEB -LKQB #>OIL JBQELA, TEF@E T>P K>JBA CLO QEB -LKQB
#>OIL @>PFKL) QE>Q FJMIBJBKQP QEB >?LSB >IDLOFQEJ, OBQROKFKD > O>KALJ S>IRB ?BQTBBK 0 >KA
1.
@FI;N GIHN?=;LFI() U
We do this forever until we find a
qualifying random value.
QBCF? (NLO?) U
Pick a random value. @FI;N L1 = L;H>IG(1);
Assign a probability. @FI;N JLI<;<CFCNS = L1;
Pick a second random value. @FI;N L2 = L;H>IG(1);
Does it qualify? If so, were done! C@ (L2 < JLI<;<CFCNS) U
L?NOLH L1;
W
W
W
)KQOLAR@QFLK
16
5PB > @RPQLJ MOL?>?FIFQV AFPQOF?RQFLK QL S>OV QEB PFWB LC > PQBM Q>HBK ?V QEB O>KALJ
T>IHBO. 4EB PQBM PFWB @>K ?B ABQBOJFKBA ?V FKCIRBK@FKD QEB O>KDB LC S>IRBP MF@HBA. #>K
VLR J>M QEB MOL?>?FIFQV BUMLKBKQF>IIVdF.B. J>HFKD QEB IFHBIFELLA QE>Q > S>IRB FP MF@HBA
BNR>I QL QEB S>IRB PNR>OBA?
(,>QBO TBfII PBB ELT QL AL QEFP JLOB BCCF@FBKQIV RPFKD SB@QLOP.)
A uniform distribution of step sizes.
Change this!
@FI;N MN?JMCT? = L;H>IG(0,10);
@FI;N MN?JR = L;H>IG(-MN?JMCT?,MN?JMCT?);
@FI;N MN?JS = L;H>IG(-MN?JMCT?,MN?JMCT?);
R += MN?JR;
S += MN?JS;
Exercise I.6 Exercise I.6
I.6 Perlin Noise (A Smoother Approach) I.6 Perlin Noise (A Smoother Approach)
! DLLA O>KALJ KRJ?BO DBKBO>QLO MOLAR@BP KRJ?BOP QE>Q E>SB KL OBI>QFLKPEFM >KA PELT KL
AFP@BOKF?IB M>QQBOK. !P TB >OB ?BDFKKFKD QL PBB, > IFQQIB ?FQ LC O>KALJKBPP @>K ?B > DLLA QEFKD
TEBK MOLDO>JJFKD LOD>KF@, IFCBIFHB ?BE>SFLOP. (LTBSBO, O>KALJKBPP >P QEB PFKDIB DRFAFKD
MOFK@FMIB FP KLQ KB@BPP>OFIV K>QRO>I. !K >IDLOFQEJ HKLTK >P g0BOIFK KLFPB,h K>JBA CLO FQP
FKSBKQLO +BK 0BOIFK, Q>HBP QEFP @LK@BMQ FKQL >@@LRKQ. 0BOIFK ABSBILMBA QEB KLFPB CRK@QFLK TEFIB
TLOHFKD LK QEB LOFDFK>I /FCB JLSFB FK QEB B>OIV 1980P; FQ T>P ABPFDKBA QL @OB>QB MOL@BARO>I
QBUQROBP CLO @LJMRQBO-DBKBO>QBA BCCB@QP. )K 1997 0BOIFK TLK >K !@>ABJV !T>OA FK QB@EKF@>I
>@EFBSBJBKQ CLO QEFP TLOH. 0BOIFK KLFPB @>K ?B RPBA QL DBKBO>QB S>OFLRP BCCB@QP TFQE K>QRO>I
NR>IFQFBP, PR@E >P @ILRAP, I>KAP@>MBP, >KA M>QQBOKBA QBUQROBP IFHB J>O?IB.
0BOIFK KLFPB E>P > JLOB LOD>KF@ >MMB>O>K@B ?B@>RPB FQ MOLAR@BP > K>QRO>IIV LOABOBA
(gPJLLQEh) PBNRBK@B LC MPBRAL-O>KALJ KRJ?BOP. 4EB DO>ME LK QEB IBCQ ?BILT PELTP 0BOIFK
KLFPB LSBO QFJB, TFQE QEB U->UFP OBMOBPBKQFKD QFJB; KLQB QEB PJLLQEKBPP LC QEB @ROSB. 4EB
DO>ME LK QEB OFDEQ PELTP MROB O>KALJ KRJ?BOP LSBO QFJB. (4EB @LAB CLO DBKBO>QFKD QEBPB
DO>MEP FP >S>FI>?IB FK QEB >@@LJM>KVFKD ?LLH ALTKIL>AP.)
4EB .>QROB LC #LAB (S005)
17
0OL@BPPFKD E>P > ?RFIQ-FK FJMIBJBKQ>QFLK LC QEB 0BOIFK KLFPB >IDLOFQEJ: QEB CRK@QFLK HICM?().
4EB HICM?() CRK@QFLK Q>HBP LKB, QTL, LO QEOBB >ODRJBKQP, >P KLFPB FP @LJMRQBA FK LKB, QTL,
LO QEOBB AFJBKPFLKP. ,BQfP PQ>OQ ?V ILLHFKD >Q LKB-AFJBKPFLK>I KLFPB.
Noise Detail Noise Detail
4EB 0OL@BPPFKD KLFPB OBCBOBK@B (EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB=.EQJI) QBIIP RP
QE>Q KLFPB FP @>I@RI>QBA LSBO PBSBO>I gL@Q>SBP.h #>IIFKD QEB HICM?"?N;CF()
(EQQM://MOL@BPPFKD.LOD/OBCBOBK@B/KLFPB$BQ>FI=.EQJI) CRK@QFLK TFII @E>KDB ?LQE QEB
KRJ?BO LC L@Q>SBP >KA QEBFO FJMLOQ>K@B OBI>QFSB QL LKB >KLQEBO. 4EFP FK QROK @E>KDBP
ELT QEB KLFPB CRK@QFLK ?BE>SBP.
!K LKIFKB IB@QROB ?V +BK 0BOIFK IBQP VLR IB>OK JLOB >?LRQ ELT KLFPB TLOHP COLJ
0BOIFK EFJPBIC (EQQM://TTT.KLFPBJ>@EFKB.@LJ/Q>IH1/).
#LKPFABO AO>TFKD > @FO@IB FK LRO 0OL@BPPFKD TFKALT >Q > O>KALJ U-IL@>QFLK.
.LT, FKPQB>A LC > O>KALJ U-IL@>QFLK, TB T>KQ > 0BOIFK KLFPB U-IL@>QFLK QE>Q FP gPJLLQEBO.h
9LR JFDEQ QEFKH QE>Q >II VLR KBBA QL AL FP OBMI>@B L;H>IG() TFQE HICM?(), F.B.
7EFIB @LK@BMQR>IIV QEFP FP BU>@QIV TE>Q TB T>KQ QL ALd@>I@RI>QB >K U-S>IRB QE>Q O>KDBP
?BQTBBK 0 >KA QEB TFAQE >@@LOAFKD QL 0BOIFK KLFPBdQEFP FP KLQ QEB @LOOB@Q FJMIBJBKQ>QFLK.
7EFIB QEB >ODRJBKQP QL QEB L;H>IG() CRK@QFLK PMB@FCV > O>KDB LC S>IRBP ?BQTBBK >
JFKFJRJ >KA > J>UFJRJ, HICM?() ALBP KLQ TLOH QEFP T>V. )KPQB>A, QEB LRQMRQ O>KDB FP
F64B?2 !.5: %<6@2 F64B?2 !.6: (.;1<:
A random x-location @FI;N R = L;H>IG(0,QC>NB);
?FFCJM?(R,180,16,16);
A noise x-location? @FI;N R = HICM?(0,QC>NB);
)KQOLAR@QFLK
18
CFUBAdFQ >IT>VP OBQROKP > S>IRB ?BQTBBK 0 >KA 1. 7BfII PBB FK > JLJBKQ QE>Q TB @>K DBQ
>OLRKA QEFP B>PFIV TFQE 0OL@BPPFKDfP G;J() CRK@QFLK, ?RQ CFOPQ TB JRPQ BU>JFKB TE>Q BU>@QIV
HICM?() BUMB@QP RP QL M>PP FK >P >K >ODRJBKQ.
7B @>K QEFKH LC LKB-AFJBKPFLK>I 0BOIFK KLFPB >P > IFKB>O PBNRBK@B LC S>IRBP LSBO QFJB. &LO
BU>JMIB:
Time Time Noise Value Noise Value
0 0.365
1 0.363
2 0.363
3 0.364
4 0.366
.LT, FK LOABO QL >@@BPP > M>OQF@RI>O KLFPB S>IRB FK 0OL@BPPFKD, TB E>SB QL M>PP > PMB@FCF@
"JLJBKQ FK QFJB" QL QEB HICM?() CRK@QFLK. &LO BU>JMIB:
!@@LOAFKD QL QEB >?LSB Q>?IB, HICM?(3) TFII OBQROK 0.364 >Q QFJB BNR>IP 3. 7B @LRIA FJMOLSB
QEFP ?V RPFKD > S>OF>?IB CLO QFJB >KA >PHFKD CLO > KLFPB S>IRB @LKQFKRLRPIV FK >L;Q().
4EB >?LSB @LAB OBPRIQP FK QEB P>JB S>IRB MOFKQBA LSBO >KA LSBO. 4EFP E>MMBKP ?B@>RPB TB
>OB >PHFKD CLO QEB OBPRIQ LC QEB HICM?() CRK@QFLK >Q QEB P>JB MLFKQ FK QFJBd3dLSBO >KA LSBO.
)C TB FK@OBJBKQ QEB QFJB S>OF>?IB N, ELTBSBO, TBfII DBQ > AFCCBOBKQ OBPRIQ.
@FI;N H = HICM?(3);
@FI;N N = 3;
PIC> >L;Q() U
We need the noise value for a specific
moment in time.
@FI;N H = HICM?(N);
JLCHNFH(H);
W
Typically we would start at time = 0, though
this is arbitrary.
@FI;N N = 0;
PIC> >L;Q() U
@FI;N H = HICM?(N);
JLCHNFH(H);
4EB .>QROB LC #LAB (S005)
19
(LT NRF@HIV TB FK@OBJBKQ N >IPL >CCB@QP QEB PJLLQEKBPP LC QEB KLFPB. )C TB J>HB I>ODB
GRJMP FK QFJB, QEBK TB >OB PHFMMFKD >EB>A >KA QEB S>IRBP TFII ?B JLOB O>KALJ.
4OV ORKKFKD QEB @LAB PBSBO>I QFJBP, FK@OBJBKQFKD N ?V 0.01, 0.02, 0.05, 0.1, 0.0001, >KA VLR
TFII PBB AFCCBOBKQ OBPRIQP.
Now, we move forward in time! N += 0.01;
W
F64B?2 !.7
Mapping Noise Mapping Noise
.LT TBfOB OB>AV QL >KPTBO QEB NRBPQFLK LC TE>Q QL AL TFQE QEB KLFPB S>IRB. /K@B TB E>SB
QEB S>IRB TFQE > O>KDB ?BQTBBK 0 >KA 1, FQfP RM QL RP QL J>M QE>Q O>KDB QL TE>Q TB T>KQ. 4EB
B>PFBPQ T>V QL AL QEFP FP TFQE 0OL@BPPFKDfP G;J() CRK@QFLK. 4EB G;J() CRK@QFLK Q>HBP CFSB
>ODRJBKQP. &FOPQ RM FP QEB S>IRB TB T>KQ QL J>M, FK QEFP @>PB H. 4EBK TB E>SB QL DFSB FQ QEB
S>IRBfP @ROOBKQ O>KDB (JFKFJRJ >KA J>UFJRJ), CLIILTBA ?V LRO ABPFOBA O>KDB.
)K QEFP @>PB, TB HKLT QE>Q KLFPB E>P > O>KDB ?BQTBBK 0 >KA 1, ?RQ TBfA IFHB QL AO>T LRO
@FO@IB TFQE > O>KDB ?BQTBBK 0 >KA QEB TFKALTfP TFAQE.
F64B?2 !.8
)KQOLAR@QFLK
20
7B @>K >MMIV QEB BU>@Q P>JB ILDF@ QL LRO O>KALJ T>IHBO, >KA >PPFDK ?LQE FQP U- >KA V-S>IRBP
>@@LOAFKD QL 0BOIFK KLFPB.
Example I.5: Perlin noise walker
@FI;N N = 0;
PIC> >L;Q() U
@FI;N H = HICM?(N);
Using map() to customize the range of
Perlin noise
@FI;N R = G;J(H,0,1,0,QC>NB);
?FFCJM?(R,180,16,16);
N += 0.01;
W
=F;MM 4;FE?L U
@FI;N R,S;
@FI;N NR,NS;
4;FE?L() U
NR = 0;
NS = 10000;
W
PIC> MN?J() U
x- and y-location mapped from noise R = G;J(HICM?(NR), 0, 1, 0, QC>NB);
S = G;J(HICM?(NS), 0, 1, 0, B?CABN);
Move forward through time. NR += 0.01;
NS += 0.01;
W
W
4EB .>QROB LC #LAB (S005)
21
.LQF@B ELT QEB >?LSB BU>JMIB OBNRFOBP >K >AAFQFLK>I M>FO LC S>OF>?IBP: NR >KA NS. 4EFP FP
?B@>RPB TB KBBA QL HBBM QO>@H LC QTL QFJB S>OF>?IBP, LKB CLO QEB U-IL@>QFLK LC QEB 4;FE?L
L?GB@Q >KA LKB CLO QEB V-IL@>QFLK. "RQ QEBOB FP PLJBQEFKD > ?FQ LAA >?LRQ QEBPB S>OF>?IBP.
7EV ALBP NR PQ>OQ >Q 0 >KA NS >Q 10,000? 7EFIB QEBPB KRJ?BOP >OB >O?FQO>OV @ELF@BP, TB
E>SB SBOV PMB@FCF@>IIV FKFQF>IFWBA LRO QTL QFJB S>OF>?IBP TFQE AFCCBOBKQ S>IRBP. 4EFP FP ?B@>RPB
QEB KLFPB CRK@QFLK FP ABQBOJFKFPQF@: FQ DFSBP VLR QEB P>JB OBPRIQ CLO > PMB@FCF@ QFJB N B>@E
>KA BSBOV QFJB. )C TB >PHBA CLO QEB KLFPB S>IRB >Q QEB P>JB QFJB N CLO ?LQE R >KA S, QEBK R
>KA S TLRIA >IT>VP ?B BNR>I, JB>KFKD QE>Q QEB 4;FE?L L?GB@Q TLRIA LKIV JLSB >ILKD >
AF>DLK>I. )KPQB>A, TB PFJMIV RPB QTL AFCCBOBKQ M>OQP LC QEB KLFPB PM>@B, PQ>OQFKD >Q 0 CLO R
>KA 10,000 CLO S PL QE>Q R >KA S @>K >MMB>O QL >@Q FKABMBKABKQIV LC B>@E LQEBO.
)K QORQE, QEBOB FP KL >@QR>I @LK@BMQ LC QFJB >Q MI>V EBOB. )QfP > RPBCRI JBQ>MELO QL EBIM RP
RKABOPQ>KA ELT QEB KLFPB CRK@QFLK TLOHP, ?RQ OB>IIV TE>Q TB E>SB FP PM>@B, O>QEBO QE>K
QFJB. 4EB DO>ME >?LSB ABMF@QP > IFKB>O PBNRBK@B LC KLFPB S>IRBP FK > LKB-AFJBKPFLK>I
PM>@B, >KA TB @>K >PH CLO > S>IRB >Q > PMB@FCF@ U-IL@>QFLK TEBKBSBO TB T>KQ. )K BU>JMIBP,
VLR TFII LCQBK PBB > S>OF>?IB K>JBA RI@@ QL FKAF@>QB QEB U-LCCPBQ >ILKD QEB KLFPB DO>ME,
O>QEBO QE>K N CLO QFJB (>P KLQBA FK QEB AF>DO>J).
F64B?2 !.9
)K QEB >?LSB O>KALJ T>IHBO, QEB OBPRIQ LC QEB KLFPB CRK@QFLK FP J>MMBA AFOB@QIV QL QEB
4;FE?LfP IL@>QFLK. #OB>QB > O>KALJ T>IHBO TEBOB VLR FKPQB>A J>M QEB OBPRIQ LC QEB
HICM?() CRK@QFLK QL > 4;FE?LfP PQBM PFWB.
Exercise I.7 Exercise I.7
Two-Dimensional Noise Two-Dimensional Noise
4EFP FAB> LC KLFPB S>IRBP IFSFKD FK > LKB-AFJBKPFLK>I PM>@B FP FJMLOQ>KQ ?B@>RPB FQ IB>AP RP
OFDEQ FKQL > AFP@RPPFLK LC QTL-AFJBKPFLK>I PM>@B. ,BQfP QEFKH >?LRQ QEFP CLO > JLJBKQ. 7FQE
LKB-AFJBKPFLK>I KLFPB, TB E>SB > PBNRBK@B LC S>IRBP FK TEF@E >KV DFSBK S>IRB FP PFJFI>O QL
FQP KBFDE?LO. "B@>RPB QEB S>IRB FP FK LKB AFJBKPFLK, FQ LKIV E>P QTL KBFDE?LOP: > S>IRB QE>Q
@LJBP ?BCLOB FQ (QL QEB IBCQ LK QEB DO>ME) >KA LKB QE>Q @LJBP >CQBO FQ (QL QEB OFDEQ).
)KQOLAR@QFLK
22
4TL-AFJBKPFLK>I KLFPB TLOHP BU>@QIV QEB P>JB T>V @LK@BMQR>IIV. 4EB AFCCBOBK@B LC @LROPB FP
QE>Q TB >OBKfQ ILLHFKD >Q S>IRBP >ILKD > IFKB>O M>QE, ?RQ S>IRBP QE>Q >OB PFQQFKD LK > DOFA. 4EFKH
LC > MFB@B LC DO>ME M>MBO TFQE KRJ?BOP TOFQQBK FKQL B>@E @BII. ! DFSBK S>IRB TFII ?B PFJFI>O QL
>II LC FQP KBFDE?LOP: >?LSB, ?BILT, QL QEB OFDEQ, QL QEB IBCQ, >KA >ILKD >KV AF>DLK>I.
)C VLR TBOB QL SFPR>IFWB QEFP DO>ME M>MBO TFQE B>@E S>IRB J>MMBA QL QEB ?OFDEQKBPP LC > @LILO,
VLR TLRIA DBQ PLJBQEFKD QE>Q ILLHP IFHB @ILRAP. 7EFQB PFQP KBUQ QL IFDEQ DO>V, TEF@E PFQP KBUQ QL
DO>V, TEF@E PFQP KBUQ QL A>OH DO>V, TEF@E PFQP KBUQ QL ?I>@H, TEF@E PFQP KBUQ QL A>OH DO>V, BQ@.
4EFP FP TEV KLFPB T>P LOFDFK>IIV FKSBKQBA. 9LR QTB>H QEB M>O>JBQBOP > ?FQ LO MI>V TFQE @LILO QL
J>HB QEB OBPRIQFKD FJ>DB ILLH JLOB IFHB J>O?IB LO TLLA LO >KV LQEBO LOD>KF@ QBUQROB.
,BQfP Q>HB > NRF@H ILLH >Q ELT QL FJMIBJBKQ QTL-AFJBKPFLK>I KLFPB FK 0OL@BPPFKD. )C VLR
T>KQBA QL @LILO BSBOV MFUBI LC > TFKALT O>KALJIV, VLR TLRIA KBBA > KBPQBA ILLM, LKB QE>Q
>@@BPPBA B>@E MFUBI >KA MF@HBA > O>KALJ ?OFDEQKBPP.
F64B?2 !.10: 1D %<6@2 F64B?2 !.11: 2D %<6@2
FI;>.CR?FM();
@IL (CHN R = 0; R < QC>NB; R++) U
@IL (CHN S = 0; S < B?CABN; S++) U
A random brightness! @FI;N <LCABN = L;H>IG(255);
JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN);
W
W
OJ>;N?.CR?FM();
4EB .>QROB LC #LAB (S005)
23
4L @LILO B>@E MFUBI >@@LOAFKD QL QEB HICM?() CRK@QFLK, TBfII AL BU>@QIV QEB P>JB QEFKD, LKIV
FKPQB>A LC @>IIFKD L;H>IG() TBfII @>II HICM?().
4EFP FP > KF@B PQ>OQ @LK@BMQR>IIVdFQ DFSBP VLR > KLFPB S>IRB CLO BSBOV (R,S) IL@>QFLK FK LRO
QTL-AFJBKPFLK>I PM>@B. 4EB MOL?IBJ FP QE>Q QEFP TLKfQ E>SB QEB @ILRAV NR>IFQV TB T>KQ.
*RJMFKD COLJ MFUBI 200 QL MFUBI 201 FP QLL I>ODB LC > GRJM QEOLRDE KLFPB. 2BJBJ?BO, TEBK
TB TLOHBA TFQE LKB-AFJBKPFLK>I KLFPB, TB FK@OBJBKQBA LRO QFJB S>OF>?IB ?V 0.01 B>@E
CO>JB, KLQ ?V 1! ! MOBQQV DLLA PLIRQFLK QL QEFP MOL?IBJ FP QL GRPQ RPB AFCCBOBKQ S>OF>?IBP CLO
QEB KLFPB >ODRJBKQP. &LO BU>JMIB, TB @LRIA FK@OBJBKQ > S>OF>?IB @>IIBA RI@@ B>@E QFJB TB
JLSB ELOFWLKQ>IIV, >KA > SI@@ S>OF>?IB B>@E QFJB TB JLSB SBOQF@>IIV QEOLRDE QEB KBPQBA
ILLMP.
Example I.6: 2D Perlin noise
A Perlin noise brightness! <BE7J 8H?=>J = C7F(DE?I;(N,O),0,1,0,255);
Start xoff at 0. <BE7J NE<< = 0.0;
@IL (CHN R = 0; R < QC>NB; R++) U
For every xoff, start yoff at 0. <BE7J OE<< = 0.0;
@IL (CHN S = 0; S < B?CABN; S++) U
Use xoff and yoff for noise(). <BE7J 8H?=>J =
C7F(DE?I;(NE<<,OE<<),0,1,0,255);
Use x and y for pixel location. JCR?FM8R+S*QC>NB9 = =IFIL(<LCABN);
Increment yoff. OE<< += 0.01;
W
Increment xoff. NE<< += 0.01;
W
0I>V TFQE @LILO, HICM?"?N;CF(), >KA QEB O>QB >Q TEF@E RI@@ >KA SI@@ >OB
FK@OBJBKQBA QL >@EFBSB AFCCBOBKQ SFPR>I BCCB@QP.
Exercise I.8 Exercise I.8
)KQOLAR@QFLK
24
7BfSB BU>JFKBA PBSBO>I QO>AFQFLK>I RPBP LC 0BOIFK KLFPB FK QEFP PB@QFLK. 7FQE LKB-AFJBKPFLK>I
KLFPB, TB RPBA PJLLQE S>IRBP QL >PPFDK QEB IL@>QFLK LC >K L?GB@Q QL DFSB QEB >MMB>O>K@B LC
T>KABOFKD. 7FQE QTL-AFJBKPFLK>I KLFPB, TB @OB>QBA > @ILRAV M>QQBOK TFQE PJLLQEBA S>IRBP LK
> MI>KB LC MFUBIP. )QfP FJMLOQ>KQ QL OBJBJ?BO, ELTBSBO, QE>Q 0BOIFK KLFPB S>IRBP >OB GRPQ
QE>QdS>IRBP. 4EBV >OBKfQ FKEBOBKQIV QFBA QL MFUBI IL@>QFLKP LO @LILO. !KV BU>JMIB FK QEFP ?LLH
QE>Q E>P > S>OF>?IB @LRIA ?B @LKQOLIIBA SF> 0BOIFK KLFPB. 7EBK TB JLABI > TFKA CLO@B, FQP
PQOBKDQE @LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB. 3>JB DLBP CLO QEB >KDIBP ?BQTBBK QEB ?O>K@EBP
FK > CO>@Q>I QOBB M>QQBOK, LO QEB PMBBA >KA AFOB@QFLK LC L?GB@QP JLSFKD >ILKD > DOFA FK > CILT
CFBIA PFJRI>QFLK.
!AA > QEFOA >ODRJBKQ QL KLFPB QE>Q FK@OBJBKQP LK@B MBO @V@IB QEOLRDE >L;Q() QL
>KFJ>QB QEB QTL-AFJBKPFLK>I KLFPB.
Exercise I.9 Exercise I.9
5PB QEB KLFPB S>IRBP >P QEB BIBS>QFLKP LC > I>KAP@>MB. 3BB QEB P@OBBKPELQ ?BILT >P >
OBCBOBK@B.
Exercise I.10 Exercise I.10
4EB .>QROB LC #LAB (S005)
25
F64B?2 !.12: *?22 D6A5 '2?96; ;<6@2 F64B?2 !.13: F9<D 36291 D6A5 '2?96; ;<6@2
I.7 Onward I.7 Onward
7B ?BD>K QEFP @E>MQBO ?V Q>IHFKD >?LRQ ELT O>KALJKBPP @>K ?B > @ORQ@E. )K J>KV T>VP, FQfP
QEB JLPQ L?SFLRP >KPTBO QL QEB HFKAP LC NRBPQFLKP TB >PH @LKQFKRLRPIVdELT PELRIA QEFP
L?GB@Q JLSB? 7E>Q @LILO PELRIA FQ ?B? 4EFP L?SFLRP >KPTBO, ELTBSBO, @>K >IPL ?B > I>WV
LKB.
!P TB CFKFPE LCC QEB FKQOLAR@QFLK, FQfP >IPL TLOQE KLQFKD QE>Q TB @LRIA GRPQ >P B>PFIV C>II FKQL
QEB QO>M LC RPFKD 0BOIFK KLFPB >P > @ORQ@E. (LT PELRIA QEFP L?GB@Q JLSB? 0BOIFK KLFPB! 7E>Q
@LILO PELRIA FQ ?B? 0BOIFK KLFPB! (LT C>PQ PELRIA FQ DOLT? 0BOIFK KLFPB!
4EB MLFKQ LC >II LC QEFP FP KLQ QL P>V QE>Q VLR PELRIA LO PELRIAKfQ RPB O>KALJKBPP. /O QE>Q
VLR PELRIA LO PELRIAKfQ RPB 0BOIFK KLFPB. 4EB MLFKQ FP QE>Q QEB ORIBP LC VLRO PVPQBJ >OB
ABCFKBA ?V VLR, >KA QEB I>ODBO VLRO QLLI?LU, QEB JLOB @ELF@BP VLRfII E>SB >P VLR FJMIBJBKQ
QELPB ORIBP. 4EB DL>I LC QEFP ?LLH FP QL CFII VLRO QLLI?LU. )C >II VLR HKLT FP O>KALJ, QEBK VLRO
ABPFDK QEFKHFKD FP IFJFQBA. 3ROB, 0BOIFK KLFPB EBIMP, ?RQ VLRfII KBBA JLOB. ! ILQ JLOB.
) QEFKH TBfOB OB>AV QL ?BDFK.
)KQOLAR@QFLK
26
Chapter 1. Vectors Chapter 1. Vectors
I(<42?, (<42?. ,5.AK@ <B? C20A<?, +60A<??J
Captain Oveur (Airplane)
4EFP ?LLH FP >II >?LRQ ILLHFKD >Q QEB TLOIA >OLRKA RP >KA @LJFKD RM TFQE @IBSBO T>VP QL
PFJRI>QB QE>Q TLOIA TFQE @LAB. $FSFABA FKQL QEOBB M>OQP, QEB ?LLH TFII PQ>OQ ?V ILLHFKD >Q ?>PF@
MEVPF@PdELT >K >MMIB C>IIP COLJ > QOBB, > MBKARIRJ PTFKDP FK QEB >FO, QEB B>OQE OBSLISBP
>OLRKA QEB PRK, BQ@. !?PLIRQBIV BSBOVQEFKD @LKQ>FKBA TFQEFK QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH
OBNRFOBP QEB RPB LC QEB JLPQ ?>PF@ ?RFIAFKD ?IL@H CLO MOLDO>JJFKD JLQFLKdQEB A0.?:= A0.?:=. !KA PL
QEFP FP TEBOB TB ?BDFK LRO PQLOV.
.LT, QEB TLOA SB@QLO @>K JB>K > ILQ LC AFCCBOBKQ QEFKDP. 6B@QLO FP QEB K>JB LC > .BT 7>SB
OL@H ?>KA CLOJBA FK 3>@O>JBKQL, #! FK QEB B>OIV 1980P. )QfP QEB K>JB LC > ?OB>HC>PQ @BOB>I
J>KRC>@QROBA ?V +BIILDDfP #>K>A>. )K QEB CFBIA LC BMFABJFLILDV, > SB@QLO FP RPBA QL ABP@OF?B
>K LOD>KFPJ QE>Q QO>KPJFQP FKCB@QFLK COLJ LKB ELPQ QL >KLQEBO. )K QEB #++ MOLDO>JJFKD
I>KDR>DB, > SB@QLO (PQA::SB@QLO) FP >K FJMIBJBKQ>QFLK LC > AVK>JF@>IIV OBPFW>?IB >OO>V A>Q>
PQOR@QROB. 7EFIB >II QEBPB ABCFKFQFLKP >OB FKQBOBPQFKD, QEBVfOB KLQ TE>Q TBfOB ILLHFKD CLO. 7E>Q
TB T>KQ FP @>IIBA > E@.74/0,9 A0.?:= E@.74/0,9 A0.?:= (K>JBA CLO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA >KA >IPL
HKLTK >P > DBLJBQOF@ SB@QLO). 7EBK VLR PBB QEB QBOJ gSB@QLOh FK QEFP ?LLH, VLR @>K >PPRJB FQ
OBCBOP QL > %R@IFAB>K SB@QLO, ABCFKBA >P 5B 9BH=HM H<5H <5G 6CH< A5;B=HI89 5B8 8=F97H=CB.
! SB@QLO FP QVMF@>IIV AO>TK >P > >OOLT; QEB AFOB@QFLK FP FKAF@>QBA ?V TEBOB QEB >OOLT FP
MLFKQFKD, >KA QEB J>DKFQRAB ?V QEB IBKDQE LC QEB >OOLT FQPBIC.
4EB .>QROB LC #LAB (S005)
27
)K QEB >?LSB FIIRPQO>QFLK, QEB SB@QLO FP AO>TK >P >K >OOLT COLJ MLFKQ ! QL MLFKQ " >KA PBOSBP
>P >K FKPQOR@QFLK CLO ELT QL QO>SBI COLJ ! QL ".
F64B?2 1.1: A C20A<? (1?.D; .@ .; .??<D) 5.@ :.4;6AB12 (92;4A5 <3 .??<D) .;1 16?20A6<; (D5605 D.F
6A 6@ =<6;A6;4).
1.1 Vectors, You Complete Me 1.1 Vectors, You Complete Me
"BCLOB TB AFSB FKQL JLOB LC QEB ABQ>FIP >?LRQ SB@QLOP, IBQfP ILLH >Q > ?>PF@ 0OL@BPPFKD
BU>JMIB QE>Q ABJLKPQO>QBP TEV TB PELRIA @>OB >?LRQ SB@QLOP FK QEB CFOPQ MI>@B. )C VLRfSB
OB>A >KV LC QEB FKQOLAR@QLOV 0OL@BPPFKD QBUQ?LLHP LO Q>HBK > @I>PP LK MOLDO>JJFKD TFQE
0OL@BPPFKD (>KA ELMBCRIIV VLRfSB ALKB LKB LC QEBPB QEFKDP QL EBIM MOBM>OB VLR CLO QEFP
?LLH), VLR MOL?>?IV, >Q LKB MLFKQ LO >KLQEBO, IB>OKBA ELT QL TOFQB > PFJMIB ?LRK@FKD ?>II
PHBQ@E.
!3 F<B .?2 ?2.16;4 A56@ /<<8 .@ . 'DF <? 6; =?6;A, A52; F<B D699 <;9F @22 @0?22;@5<A@ <3 A52 0<12.
$<A6<;, <3 0<B?@2, 6@ . 82F 292:2;A <3 <B? 16@0B@@6<;, @< A< A52 2EA2;A =<@@6/92, A52 @A.A60 @0?22;@5<A@
D699 6;09B12 A?.69@ A< 46C2 . @2;@2 <3 A52 /25.C6<?. F<? :<?2 ./<BA 5<D A< 1?.D A?.69@, @22 A52 0<12
2E.:=92@ .C.69./92 3<? 1<D;9<.1.
#E>MQBO 1. 6B@QLOP
28
,L@>QFLK ,L@>QFLK L 5B8 M
3MBBA 3MBBA LGD998 5B8 MGD998
Example 1.1: Bouncing ball with no vectors
)K QEB >?LSB BU>JMIB, TB E>SB > SBOV PFJMIB TLOIAd> ?I>KH @>KS>P TFQE > @FO@RI>O PE>MB (>
g?>IIh) QO>SBIFKD >OLRKA. 4EFP ?>II E>P PLJB MOLMBOQFBP, TEF@E >OB OBMOBPBKQBA FK QEB @LAB >P
S>OF>?IBP.
)K > JLOB >AS>K@BA PHBQ@E, TB @LRIA FJ>DFKB E>SFKD J>KV JLOB S>OF>?IBP:
Variables for location and speed of ball. @FI;N R = 100;
@FI;N S = 100;
@FI;N RMJ??> = 1;
@FI;N SMJ??> = 3.3;
Remember how Processing works? setup()
is executed once when the sketch starts
and draw() loops forever and ever (until you
quit).
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
<;=EALIOH>(255);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Move the ball according to its speed. R = R + RMJ??>;
S = S + SMJ??>;
Check for bouncing. C@ ((R > QC>NB) VV (R < 0)) U
RMJ??> = RMJ??> * -1;
W
C@ ((S > B?CABN) VV (S < 0)) U
SMJ??> = SMJ??> * -1;
W
MNLIE?(0);
@CFF(175);
Display the ball at the location (x,y). ?FFCJM?(R,S,16,16);
W
4EB .>QROB LC #LAB (S005)
29
!@@BIBO>QFLK !@@BIBO>QFLK L5779@9F5H=CB 5B8 M5779@9F5H=CB
4>ODBQ IL@>QFLK 4>ODBQ IL@>QFLK LH5F;9H 5B8 MH5F;9H
7FKA 7FKA LK=B8 5B8 MK=B8
&OF@QFLK &OF@QFLK L:F=7H=CB 5B8 M:F=7H=CB
)QfP ?B@LJFKD @IB>OBO QE>Q CLO BSBOV @LK@BMQ FK QEFP TLOIA (TFKA, IL@>QFLK, >@@BIBO>QFLK, BQ@.),
TBfII KBBA QTL S>OF>?IBP. !KA QEFP FP LKIV > QTL-AFJBKPFLK>I TLOIA. )K > 3$ TLOIA, TBfII KBBA
R, S, T, RMJ??>, SMJ??>, TMJ??>, >KA PL LK.
7LRIAKfQ FQ ?B KF@B FC TB @LRIA PFJMIFCV LRO @LAB >KA RPB CBTBO S>OF>?IBP?
)KPQB>A LC:
7B @LRIA PFJMIV E>SBj
4>HFKD QEFP CFOPQ PQBM FK RPFKD SB@QLOP TLKfQ >IILT RP QL AL >KVQEFKD KBT. *RPQ >AAFKD SB@QLOP
TLKfQ J>DF@>IIV J>HB VLRO 0OL@BPPFKD PHBQ@EBP PFJRI>QB MEVPF@P. (LTBSBO, QEBV TFII
PFJMIFCV VLRO @LAB >KA MOLSFAB > PBQ LC CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP QE>Q
E>MMBK LSBO >KA LSBO >KA LSBO >D>FK TEFIB MOLDO>JJFKD JLQFLK.
!P >K FKQOLAR@QFLK QL SB@QLOP, TBfOB DLFKD QL IFSB FK QTL AFJBKPFLKP CLO NRFQB PLJB QFJB (>Q
IB>PQ RKQFI TB DBQ QEOLRDE QEB CFOPQ PBSBO>I @E>MQBOP). !II LC QEBPB BU>JMIBP @>K ?B C>FOIV
B>PFIV BUQBKABA QL QEOBB AFJBKPFLKP (>KA QEB @I>PP TB TFII RPBd.3?=NILd>IILTP CLO QEOBB
AFJBKPFLKP.) (LTBSBO, FQfP B>PFBO QL PQ>OQ TFQE GRPQ QTL.
@FI;N R;
@FI;N S;
@FI;N RMJ??>;
@FI;N SMJ??>;
3?=NIL FI=;NCIH;
3?=NIL MJ??>;
1.2 Vectors for Processing Programmers 1.2 Vectors for Processing Programmers
/KB T>V QL QEFKH LC > SB@QLO FP QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP. #LKPFABO ELT VLR JFDEQ
DL >?LRQ MOLSFAFKD FKPQOR@QFLKP QL T>IH COLJ LKB MLFKQ QL >KLQEBO.
(BOB >OB PLJB SB@QLOP >KA MLPPF?IB QO>KPI>QFLKP:
#E>MQBO 1. 6B@QLOP
30
(-15, 3) (-15, 3) 25@? :=:H99B GH9DG K9GH; HIFB 5B8 K5@? H<F99 GH9DG BCFH<.
(3, 4) (3, 4) 25@? H<F99 GH9DG 95GH; HIFB 5B8 K5@? :=J9 GH9DG BCFH<.
(2, -1) (2, -1) 25@? HKC GH9DG 95GH; HIFB 5B8 K5@? CB9 GH9D GCIH<.
9LRfSB MOL?>?IV ALKB QEFP ?BCLOB TEBK MOLDO>JJFKD JLQFLK. &LO BSBOV CO>JB LC >KFJ>QFLK
(F.B. > PFKDIB @V@IB QEOLRDE 0OL@BPPFKDfP >L;Q() ILLM), VLR FKPQOR@Q B>@E L?GB@Q LK QEB P@OBBK
QL JLSB > @BOQ>FK KRJ?BO LC MFUBIP ELOFWLKQ>IIV >KA > @BOQ>FK KRJ?BO LC MFUBIP SBOQF@>IIV.
&LO BSBOV CO>JB:
90B 7:.,?4:9 = A07:.4?D ,;;740/ ?: .@==09? 7:.,?4:9 90B 7:.,?4:9 = A07:.4?D ,;;740/ ?: .@==09? 7:.,?4:9
)C SBIL@FQV FP > SB@QLO (QEB AFCCBOBK@B ?BQTBBK QTL MLFKQP), TE>Q FP IL@>QFLK? )P FQ > SB@QLO QLL?
4B@EKF@>IIV, LKB JFDEQ >ODRB QE>Q IL@>QFLK FP KLQ > SB@QLO, PFK@B FQfP KLQ ABP@OF?FKD ELT QL
JLSB COLJ LKB MLFKQ QL >KLQEBOdFQfP PFJMIV ABP@OF?FKD > PFKDRI>O MLFKQ FK PM>@B.
F64B?2 1.2
F64B?2 1.3
4EB .>QROB LC #LAB (S005)
31
IL@>QFLK IL@>QFLK L,M
SBIL@FQV SBIL@FQV LGD998,MGD998
.BSBOQEBIBPP, >KLQEBO T>V QL ABP@OF?B > IL@>QFLK FP QEB M>QE Q>HBK COLJ QEB LOFDFK QL OB>@E
QE>Q IL@>QFLK. (BK@B, > IL@>QFLK @>K ?B QEB SB@QLO OBMOBPBKQFKD QEB AFCCBOBK@B ?BQTBBK
IL@>QFLK >KA LOFDFK.
,BQfP BU>JFKB QEB RKABOIVFKD A>Q> CLO ?LQE IL@>QFLK >KA SBIL@FQV. )K QEB ?LRK@FKD ?>II
BU>JMIB, TB E>A QEB CLIILTFKD:
.LQF@B ELT TB >OB PQLOFKD QEB P>JB A>Q> CLO ?LQEdQTL CIL>QFKD MLFKQ KRJ?BOP, >K R >KA > S.
)C TB TBOB QL TOFQB > SB@QLO @I>PP LROPBISBP, TBfA PQ>OQ TFQE PLJBQEFKD O>QEBO ?>PF@:
!Q FQP @LOB, > .3?=NIL FP GRPQ > @LKSBKFBKQ T>V QL PQLOB QTL S>IRBP (LO QEOBB, >P TBfII PBB FK
3$ BU>JMIBP).
!KA PL QEFP j
F64B?2 1.4
=F;MM .3?=NIL U
@FI;N R;
@FI;N S;
.3?=NIL(@FI;N R:, @FI;N S:) U
R = R:;
S = S:;
W
W
#E>MQBO 1. 6B@QLOP
32
?B@LJBP j
.LT QE>Q TB E>SB QTL SB@QLO L?GB@QP (IL@>QFLK >KA SBIL@FQV), TBfOB OB>AV QL FJMIBJBKQ QEB
>IDLOFQEJ CLO JLQFLKd7:.,?4:9 = 7:.,?4:9 + A07:.4?D 7:.,?4:9 = 7:.,?4:9 + A07:.4?D. )K %U>JMIB 1.1, TFQELRQ SB@QLOP, TB E>A:
)K >K FAB>I TLOIA, TB TLRIA ?B >?IB QL OBTOFQB QEB >?LSB >P:
(LTBSBO, FK 0OL@BPPFKD, QEB >AAFQFLK LMBO>QLO + FP OBPBOSBA CLO MOFJFQFSB S>IRBP (FKQBDBOP,
CIL>QP, BQ@.) LKIV. 0OL@BPPFKD ALBPKfQ HKLT ELT QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KV JLOB
QE>K FQ HKLTP ELT QL >AA QTL .$IHN L?GB@QP LO .'G;A? L?GB@QP. &LOQRK>QBIV CLO RP, QEB
.3?=NIL @I>PP FK@IRABP CRK@QFLKP CLO @LJJLK J>QEBJ>QF@>I LMBO>QFLKP.
@FI;N R = 100;
@FI;N S = 100;
@FI;N RMJ??> = 1;
@FI;N SMJ??> = 3.3;
.3?=NIL FI=;NCIH = H?Q .3?=NIL(100,100);
.3?=NIL P?FI=CNS = H?Q .3?=NIL(1,3.3);
Add each speed to each location. R = R + RMJ??>;
S = S + SMJ??>;
Add the velocity vector to the location
vector.
FI=;NCIH = FI=;NCIH + P?FI=CNS;
1.3 Vector Addition 1.3 Vector Addition
"BCLOB TB @LKQFKRB ILLHFKD >Q QEB .3?=NIL @I>PP >KA FQP ;>>() JBQELA (MROBIV CLO QEB P>HB LC
IB>OKFKD PFK@B FQfP >IOB>AV FJMIBJBKQBA CLO RP FK 0OL@BPPFKD FQPBIC), IBQfP BU>JFKB SB@QLO
>AAFQFLK RPFKD QEB KLQ>QFLK CLRKA FK J>QE >KA MEVPF@P QBUQ?LLHP.
6B@QLOP >OB QVMF@>IIV TOFQQBK BFQEBO FK ?LIAC>@B QVMB LO TFQE >K >OOLT LK QLM. &LO QEB MROMLPBP
LC QEFP ?LLH, QL AFPQFKDRFPE > A0.?:= A0.?:= COLJ > >.,7,= >.,7,= (G75@5F OBCBOP QL > PFKDIB S>IRB, PR@E >P >K
FKQBDBO LO > CIL>QFKD MLFKQ KRJ?BO), TBfII RPB QEB >OOLT KLQ>QFLK:
i 6B@QLO: u

i 3@>I>O: x
,BQfP P>V ) E>SB QEB CLIILTFKD QTL SB@QLOP:
4EB .>QROB LC #LAB (S005)
33
%>@E SB@QLO E>P QTL @LJMLKBKQP, >K R >KA > S. 4L >AA QTL SB@QLOP QLDBQEBO, TB PFJMIV >AA
?LQE RfP >KA ?LQE SfP.
)K LQEBO TLOAP:
w

= u

+ v

@>K ?B TOFQQBK >P:


w
x
= u
x
+ v
x
w
y
= u
y
+ v
y
4EBK, OBMI>@FKD O >KA P TFQE QEBFO S>IRBP COLJ &FDROB 1.6, TB DBQ:
w
x
= 5 + 3
TEF@E JB>KP QE>Q:
&FK>IIV, TB TOFQB QE>Q >P > SB@QLO:
w

= (8, 6)
F64B?2 1.5
F64B?2 1.6
#E>MQBO 1. 6B@QLOP
34
.LT QE>Q TB RKABOPQ>KA ELT QL >AA QTL SB@QLOP QLDBQEBO, TB @>K ILLH >Q ELT >AAFQFLK FP
FJMIBJBKQBA FK QEB .3?=NIL @I>PP FQPBIC. ,BQfP TOFQB > CRK@QFLK @>IIBA ;>>() QE>Q Q>HBP >KLQEBO
.3?=NIL L?GB@Q >P FQP >ODRJBKQ.
.LT QE>Q TB PBB ELT ;>>() FP TOFQQBK FKPFAB LC .3?=NIL, TB @>K OBQROK QL LRO ?LRK@FKD ?>II
BU>JMIB TFQE FQP 7:.,?4:9 + A07:.4?D 7:.,?4:9 + A07:.4?D >IDLOFQEJ >KA FJMIBJBKQ SB@QLO >AAFQFLK:
!KA EBOB TB >OB, OB>AV QL OBTOFQB QEB ?LRK@FKD ?>II BU>JMIB RPFKD .3?=NIL.
Example 1.2: Bouncing ball with PVectors!
=F;MM .3?=NIL U
@FI;N R;
@FI;N S;
.3?=NIL(@FI;N R:, @FI;N S:) U
R = R:;
S = S:;
W
New! A function to add another PVector to
this PVector. Simply add the L components
and the M components together.
LE?: 7::(+0;9JEH L) Q
O = O + L.O;
N = N + L.N;
S
W
Add the current velocity to the location. FI=;NCIH = FI=;NCIH + P?FI=CNS;
FI=;NCIH.;>>(P?FI=CNS);
Instead of a bunch of floats, we now just
have two PVector variables.
+0;9JEH BE97J?ED;
+0;9JEH L;BE9?JO;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
BE97J?ED = D;M +0;9JEH(100,100);
L;BE9?JO = D;M +0;9JEH(2.5,5);
W
PIC> >L;Q() U
<;=EALIOH>(255);
BE97J?ED.7::(L;BE9?JO);
4EB .>QROB LC #LAB (S005)
35
.LT, VLR JFDEQ CBBI PLJBTE>Q AFP>MMLFKQBA. !CQBO >II, QEFP J>V FKFQF>IIV >MMB>O QL E>SB
J>AB QEB @LAB JLOB @LJMIF@>QBA QE>K QEB LOFDFK>I SBOPFLK. 7EFIB QEFP FP > MBOCB@QIV
OB>PLK>?IB >KA S>IFA @OFQFNRB, FQfP FJMLOQ>KQ QL RKABOPQ>KA QE>Q TB E>SBKfQ CRIIV OB>IFWBA QEB
MLTBO LC MOLDO>JJFKD TFQE SB@QLOP GRPQ VBQ. ,LLHFKD >Q > PFJMIB ?LRK@FKD ?>II >KA LKIV
FJMIBJBKQFKD SB@QLO >AAFQFLK FP GRPQ QEB CFOPQ PQBM. !P TB JLSB CLOT>OA FKQL > JLOB @LJMIBU
TLOIA LC JRIQFMIB L?GB@QP >KA JRIQFMIB 1:=.0> 1:=.0> (TEF@E TBfII FKQOLAR@B FK #E>MQBO 2), QEB
?BKBCFQP LC .3?=NIL TFII ?B@LJB JLOB >MM>OBKQ.
7B PELRIA, ELTBSBO, KLQB >K FJMLOQ>KQ >PMB@Q LC QEB >?LSB QO>KPFQFLK QL MOLDO>JJFKD TFQE
SB@QLOP. %SBK QELRDE TB >OB RPFKD .3?=NIL L?GB@QP QL ABP@OF?B QTL S>IRBPdQEB R >KA S LC
IL@>QFLK >KA QEB R >KA S LC SBIL@FQVdTB PQFII LCQBK KBBA QL OBCBO QL QEB L >KA M @LJMLKBKQP
LC B>@E .3?=NIL FKAFSFAR>IIV. 7EBK TB DL QL AO>T >K L?GB@Q FK 0OL@BPPFKD, QEBOBfP KL
JB>KP CLO RP QL P>V:
4EB ?FFCJM?() CRK@QFLK ALBP KLQ >IILT CLO > .3?=NIL >P >K >ODRJBKQ. !K BIIFMPB @>K LKIV
?B AO>TK TFQE QTL P@>I>O S>IRBP, >K R-@LLOAFK>QB >KA > S-@LLOAFK>QB. !KA PL TB JRPQ AFD
FKQL QEB .3?=NIL L?GB@Q >KA MRII LRQ QEB L >KA M @LJMLKBKQP RPFKD L?GB@Q-LOFBKQBA ALQ
PVKQ>U.
4EB P>JB FPPRB >OFPBP TEBK QBPQFKD FC QEB @FO@IB E>P OB>@EBA QEB BADB LC QEB TFKALT, >KA
TB KBBA QL >@@BPP QEB FKAFSFAR>I @LJMLKBKQP LC ?LQE SB@QLOP: FI=;NCIH >KA P?FI=CNS.
We still sometimes need to refer to the
individual components of a PVector and
can do so using the dot syntax: location.x,
velocity.y, etc.
?< ((BE97J?ED.N > M?:J>) RR (BE97J?ED.N < 0)) Q
L;BE9?JO.N = L;BE9?JO.N * -1;
S
?< ((BE97J?ED.O > >;?=>J) RR (BE97J?ED.O < 0)) Q
L;BE9?JO.O = L;BE9?JO.O * -1;
S
MNLIE?(0);
@CFF(175);
;BB?FI;(BE97J?ED.N,BE97J?ED.O,16,16);
W
?FFCJM?(FI=;NCIH,16,16);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
C@ ((FI=;NCIH.R > QC>NB) VV (FI=;NCIH.R < 0)) U
P?FI=CNS.R = P?FI=CNS.R * -1;
W
#E>MQBO 1. 6B@QLOP
36
&FKA PLJBQEFKD VLRfSB MOBSFLRPIV J>AB FK 0OL@BPPFKD RPFKD PBM>O>QB R >KA S S>OF>?IBP
>KA RPB .3?=NILP FKPQB>A.
Exercise 1.1 Exercise 1.1
4>HB LKB LC QEB T>IHBO BU>JMIBP COLJ QEB FKQOLAR@QFLK >KA @LKSBOQ FQ QL RPB .3?=NILP.
Exercise 1.2 Exercise 1.2
%UQBKA QEB ?LRK@FKD ?>II TFQE SB@QLOP BU>JMIB FKQL 3$. #>K VLR DBQ > PMEBOB QL ?LRK@B
>OLRKA > ?LU?
Exercise 1.3 Exercise 1.3
1.4 More Vector Math 1.4 More Vector Math
!AAFQFLK T>P OB>IIV GRPQ QEB CFOPQ PQBM. 4EBOB >OB J>KV J>QEBJ>QF@>I LMBO>QFLKP QE>Q >OB
@LJJLKIV RPBA TFQE SB@QLOP. "BILT FP > @LJMOBEBKPFSB IFPQ LC QEB LMBO>QFLKP >S>FI>?IB >P
CRK@QFLKP FK QEB .3?=NIL @I>PP. 7BfII DL QEOLRDE > CBT LC QEB HBV LKBP KLT. !P LRO BU>JMIBP
DBQ JLOB PLMEFPQF@>QBA FK I>QBO @E>MQBOP, TBfII @LKQFKRB QL OBSB>I QEB ABQ>FIP LC JLOB CRK@QFLKP.
i ;>>() d >AA SB@QLOP
i MO<() d PR?QO>@Q SB@QLOP
i GOFN() d P@>IB QEB SB@QLO TFQE JRIQFMIF@>QFLK
i >CP() d P@>IB QEB SB@QLO TFQE AFSFPFLK
i G;A() d @>I@RI>QB QEB J>DKFQRAB LC > SB@QLO
i M?N+;A() - PBQ QEB J>DKFQRAB LC > SB@QLO
i HILG;FCT?() d KLOJ>IFWB QEB SB@QLO QL > RKFQ IBKDQE LC 1
i FCGCN() d IFJFQ QEB J>DKFQRAB LC > SB@QLO
i B?;>CHA2"() d QEB EB>AFKD LC > SB@QLO BUMOBPPBA >P >K >KDIB
i LIN;N?() d OLQ>QB > 2$ SB@QLO ?V >K >KDIB
4EB .>QROB LC #LAB (S005)
37
i F?LJ() d IFKB>O FKQBOMLI>QB QL >KLQEBO SB@QLO
i >CMN() d QEB %R@IFAB>K AFPQ>K@B ?BQTBBK QTL SB@QLOP (@LKPFABOBA >P MLFKQP)
i ;HAF??NQ??H() d CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP
i >IN() d QEB ALQ MOLAR@Q LC QTL SB@QLOP
i =LIMM() d QEB @OLPP MOLAR@Q LC QTL SB@QLOP (LKIV OBIBS>KQ FK QEOBB AFJBKPFLKP)
i L;H>IG2"() - J>HB > O>KALJ 2$ SB@QLO
i L;H>IG3"() - J>HB > O>KALJ 3$ SB@QLO
(>SFKD >IOB>AV @LSBOBA >AAFQFLK, IBQfP PQ>OQ TFQE PR?QO>@QFLK. 4EFP LKBfP KLQ PL ?>A; GRPQ Q>HB
QEB MIRP PFDK >KA OBMI>@B FQ TFQE > JFKRP!
Vector subtraction Vector subtraction
w

= u

@>K ?B TOFQQBK >P:


w
x
= u
x
v
x
w
y
= u
y
v
y
>KA PL QEB CRK@QFLK FKPFAB .3?=NIL ILLHP IFHB:
4EB CLIILTFKD BU>JMIB ABJLKPQO>QBP SB@QLO PR?QO>@QFLK ?V Q>HFKD QEB AFCCBOBK@B ?BQTBBK
QTL MLFKQPdQEB JLRPB IL@>QFLK >KA QEB @BKQBO LC QEB TFKALT.
F64B?2 1.7: +20A<? )B/A?.0A6<;
PIC> MO<(.3?=NIL P) U
R = R - P.R;
S = S - P.S;
W
#E>MQBO 1. 6B@QLOP
38
Example 1.3: Vector subtraction
Basic Number Properties with Vectors Basic Number Properties with Vectors
"LQE >AAFQFLK >KA PR?QO>@QFLK TFQE SB@QLOP CLIILT QEB P>JB >IDB?O>F@ ORIBP >P TFQE OB>I
KRJ?BOP.
(30 .:88@?,?4A0 =@70: (30 .:88@?,?4A0 =@70: u

+ v

= v

+ u

(30 ,>>:.4,?4A0 =@70: (30 ,>>:.4,?4A0 =@70: u

+ ( v

+ w

) = ( u

+ v

) + w

&>K@V QBOJFKLILDV >KA PVJ?LIP >PFAB, QEFP FP OB>IIV NRFQB > PFJMIB @LK@BMQ. 7BfOB GRPQ
P>VFKD QE>Q @LJJLK PBKPB MOLMBOQFBP LC >AAFQFLK >MMIV QL SB@QLOP >P TBII.
3 + 2 = 2 + 3
(3 + 2) + 1 = 3 + (2 + 1)
PIC> M?NOJ() U
MCT?(200,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Two PVectors, one for the mouse location
and one for the center of the window
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
PVector subtraction! GIOM?.MO<(=?HN?L);
Draw a line to represent the vector. NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
4EB .>QROB LC #LAB (S005)
39
Vector multiplication Vector multiplication
-LSFKD LK QL JRIQFMIF@>QFLK, TB E>SB QL QEFKH > IFQQIB ?FQ AFCCBOBKQIV. 7EBK TB Q>IH >?LRQ
JRIQFMIVFKD > SB@QLO, TE>Q TB QVMF@>IIV JB>K FP >.,7492 >.,7492 > SB@QLO. )C TB T>KQBA QL P@>IB >
SB@QLO QL QTF@B FQP PFWB LO LKB-QEFOA LC FQP PFWB (IB>SFKD FQP AFOB@QFLK QEB P>JB), TB TLRIA P>V:
g-RIQFMIV QEB SB@QLO ?V 2h LO g-RIQFMIV QEB SB@QLO ?V 1/3.h .LQB QE>Q TB >OB JRIQFMIVFKD >
SB@QLO ?V > P@>I>O, > PFKDIB KRJ?BO, KLQ >KLQEBO SB@QLO.
4L P@>IB > SB@QLO, TB JRIQFMIV B>@E @LJMLKBKQ (R >KA S) ?V > P@>I>O.
w

= u

* n
@>K ?B TOFQQBK >P:
w
x
= u
x
* n
w
y
= u
y
* n
,BQfP ILLH >Q >K BU>JMIB TFQE SB@QLO
KLQ>QFLK.
u

= (3, 7)
n = 3
w

= u

* n
w
x
= 3 * 3
w
y
= 7 * 3
w

= (9, 21)
4EBOBCLOB, QEB CRK@QFLK FKPFAB QEB .3?=NIL
@I>PP FP TOFQQBK >P:
!KA FJMIBJBKQFKD JRIQFMIF@>QFLK FK @LAB FP >P PFJMIB >P:
F64B?2 1.8: )0.96;4 . C20A<?
PIC> GOFN(@FI;N H) U
With multiplication, the components of the
vector are multiplied by a number.
R = R * H;
S = S * H;
W
.3?=NIL O = H?Q .3?=NIL(-3,7);
This PVector is now three times the size
and is equal to (-9,21).
O.GOFN(3);
#E>MQBO 1. 6B@QLOP
40
Example 1.4: Multiplying a vector
$FSFPFLK TLOHP GRPQ IFHB JRIQFMIF@>QFLKdTB
PFJMIV OBMI>@B QEB JRIQFMIF@>QFLK PFDK
(>PQBOFPH) TFQE QEB AFSFPFLK PFDK (CLOT>OA
PI>PE).
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
Multiplying a vector! The vector is now half
its original size (multiplied by 0.5).
GIOM?.GOFN(0.5);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
F64B?2 1.9
PIC> >CP(@FI;N H) U
R = R / H;
S = S / H;
W
.3?=NIL O = H?Q .3?=NIL(8,-4);
4EB .>QROB LC #LAB (S005)
41
More Number Properties with Vectors More Number Properties with Vectors
!P TFQE >AAFQFLK, ?>PF@ >IDB?O>F@ ORIBP LC JRIQFMIF@>QFLK >KA AFSFPFLK >MMIV QL SB@QLOP.
4EB >PPL@F>QFSB ORIB: (n * m) * v

= n * (m* v

)
4EB AFPQOF?RQFSB ORIB TFQE 2 P@>I>OP, 1 SB@QLO: (n * m) * v

= n * v

+ m* v

4EB AFPQOF?RQFSB ORIB TFQE 2 SB@QLOP, 1 P@>I>O: ( u

+ v

) * n = u

* n + v

* n
Dividing a vector! The vector is now half its
original size (divided by 2).
O.>CP(2);
1.5 Vector Magnitude 1.5 Vector Magnitude
-RIQFMIF@>QFLK >KA AFSFPFLK, >P TB GRPQ P>T, >OB JB>KP ?V TEF@E QEB IBKDQE LC QEB SB@QLO @>K
?B @E>KDBA TFQELRQ >CCB@QFKD AFOB@QFLK. 0BOE>MP VLRfOB TLKABOFKD: g/+, PL ELT AL ) HKLT
TE>Q QEB IBKDQE LC > SB@QLO FP? ) HKLT QEB @LJMLKBKQP (R >KA S), ?RQ ELT ILKD (FK MFUBIP) FP
QEB >@QR>I >OOLT?h 5KABOPQ>KAFKD ELT QL @>I@RI>QB QEB IBKDQE (>IPL HKLTK >P 8,294?@/0 8,294?@/0) LC
> SB@QLO FP FK@OBAF?IV RPBCRI >KA FJMLOQ>KQ.
.LQF@B FK QEB >?LSB AF>DO>J ELT QEB
SB@QLO, AO>TK >P >K >OOLT >KA QTL
@LJMLKBKQP (R >KA S), @OB>QBP > OFDEQ
QOF>KDIB. 4EB PFABP >OB QEB @LJMLKBKQP >KA
QEB EVMLQBKRPB FP QEB >OOLT FQPBIC. 7BfOB
SBOV IR@HV QL E>SB QEFP OFDEQ QOF>KDIB,
?B@>RPB LK@B RMLK > QFJB, > 'OBBH
J>QEBJ>QF@F>K K>JBA 0VQE>DLO>P
ABSBILMBA > ILSBIV CLOJRI> QL ABP@OF?B QEB
OBI>QFLKPEFM ?BQTBBK QEB PFABP >KA
EVMLQBKRPB LC > OFDEQ QOF>KDIB.
4EB 0VQE>DLOB>K QEBLOBJ FP 5 PNR>OBA
MIRP 6 PNR>OBA BNR>IP 7 PNR>OBA.
!OJBA TFQE QEFP CLOJRI>, TB @>K KLT
@LJMRQB QEB J>DKFQRAB LC v

>P CLIILTP:
v

= v
x
* v
x
+ v
y
* v
y
LO FK .3?=NIL:
F64B?2 1.10: *52 92;4A5 <? I:.4;6AB12J <3 .
C20A<? C 6@ <3A2; D?6AA2; .@: C
F64B?2 1.11: *52 'FA5.4<?2.; *52<?2:
#E>MQBO 1. 6B@QLOP
42
Example 1.5: Vector magnitude
@FI;N G;A() U
L?NOLH MKLN(R*R + S*S);
W
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
The magnitude (i.e. length) of a vector can
be accessed via the mag() function. Here it
is used as the width of a rectangle drawn at
the top of the window.
@FI;N G = GIOM?.G;A();
@CFF(0);
L?=N(0,0,G,10);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
1.6 Normalizing Vectors 1.6 Normalizing Vectors
#>I@RI>QFKD QEB J>DKFQRAB LC > SB@QLO FP LKIV QEB ?BDFKKFKD. 4EB J>DKFQRAB CRK@QFLK LMBKP
QEB ALLO QL J>KV MLPPF?FIFQFBP, QEB CFOPQ LC TEF@E FP 9:=8,74E,?4:9 9:=8,74E,?4:9. .LOJ>IFWFKD OBCBOP QL QEB
MOL@BPP LC J>HFKD PLJBQEFKD gPQ>KA>OAh LO, TBII, gKLOJ>I.h )K QEB @>PB LC SB@QLOP, IBQfP
>PPRJB CLO QEB JLJBKQ QE>Q > PQ>KA>OA SB@QLO E>P > IBKDQE LC 1. 4L KLOJ>IFWB > SB@QLO,
4EB .>QROB LC #LAB (S005)
43
QEBOBCLOB, FP QL Q>HB > SB@QLO LC >KV IBKDQE >KA, HBBMFKD FQ MLFKQFKD FK QEB P>JB AFOB@QFLK,
@E>KDB FQP IBKDQE QL 1, QROKFKD FQ FKQL TE>Q FP @>IIBA > @94? A0.?:= @94? A0.?:=.
3FK@B FQ ABP@OF?BP > SB@QLOfP AFOB@QFLK
TFQELRQ OBD>OA QL FQP IBKDQE, FQfP RPBCRI QL
E>SB QEB RKFQ SB@QLO OB>AFIV >@@BPPF?IB.
7BfII PBB QEFP @LJB FK E>KAV LK@B TB PQ>OQ
QL TLOH TFQE CLO@BP FK #E>MQBO 2.
&LO >KV DFSBK SB@QLO u

, FQP RKFQ SB@QLO


(TOFQQBK >P u

) FP @>I@RI>QBA >P CLIILTP:


u

=
u

)K LQEBO TLOAP, QL KLOJ>IFWB > SB@QLO, PFJMIV AFSFAB B>@E @LJMLKBKQ ?V FQP J>DKFQRAB. 4EFP
FP MOBQQV FKQRFQFSB. 3>V > SB@QLO FP LC IBKDQE 5. 7BII, 5 AFSFABA ?V 5 FP 1. 3L, ILLHFKD >Q LRO
OFDEQ QOF>KDIB, TB QEBK KBBA QL P@>IB QEB EVMLQBKRPB ALTK ?V AFSFAFKD ?V 5. )K QE>Q MOL@BPP
QEB PFABP PEOFKH, AFSFABA ?V 5 >P TBII.
)K QEB .3?=NIL @I>PP, TB QEBOBCLOB TOFQB
LRO KLOJ>IFW>QFLK CRK@QFLK >P CLIILTP:
/C @LROPB, QEBOBfP LKB PJ>II FPPRB. 7E>Q FC QEB J>DKFQRAB LC QEB SB@QLO FP 0? 7B @>KfQ AFSFAB
?V 0! 3LJB NRF@H BOOLO @EB@HFKD TFII CFU QE>Q OFDEQ RM:
F64B?2 1.12
F64B?2 1.13
PIC> HILG;FCT?() U
@FI;N G = G;A();
>CP(G);
W
PIC> HILG;FCT?() U
@FI;N G = G;A();
C@ (G != 0) U
>CP(G);
W
W
#E>MQBO 1. 6B@QLOP
44
Example 1.6: Normalizing a vector
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL =?HN?L = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
GIOM?.MO<(=?HN?L);
In this example, after the vector is
normalized, it is multiplied by 50 so that it is
viewable onscreen. Note that no matter
where the mouse is, the vector will have the
same length (50) due to the normalization
process.
GIOM?.HILG;FCT?();
GIOM?.GOFN(50);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,GIOM?.R,GIOM?.S);
W
1.7 Vector Motion: Velocity 1.7 Vector Motion: Velocity
!II QEFP SB@QLO J>QE PQRCC PLRKAP IFHB PLJBQEFKD TB PELRIA HKLT >?LRQ, ?RQ TEV? (LT TFII FQ
>@QR>IIV EBIM RP TOFQB @LAB? 4EB QORQE LC QEB J>QQBO FP QE>Q TB KBBA QL E>SB PLJB M>QFBK@B. )Q
TFII Q>HB PLJB QFJB ?BCLOB QEB >TBPLJBKBPP LC RPFKD QEB .3?=NIL @I>PP CRIIV @LJBP QL IFDEQ.
4EFP FP >@QR>IIV > @LJJLK L@@ROOBK@B TEBK CFOPQ IB>OKFKD > KBT A>Q> PQOR@QROB. &LO BU>JMIB,
TEBK VLR CFOPQ IB>OK >?LRQ >K >OO>V, FQ JFDEQ PBBJ IFHB JR@E JLOB TLOH QL RPB >K >OO>V QE>K QL
GRPQ E>SB PBSBO>I S>OF>?IBP PQ>KA CLO JRIQFMIB QEFKDP. "RQ QE>Q MI>K NRF@HIV ?OB>HP ALTK TEBK
VLR KBBA > ERKAOBA, LO > QELRP>KA, LO QBK QELRP>KA QEFKDP. 4EB P>JB @>K ?B QORB CLO
.3?=NIL. 7E>Q JFDEQ PBBJ IFHB JLOB TLOH KLT TFII M>V LCC I>QBO, >KA M>V LCC NRFQB KF@BIV. !KA
VLR ALKfQ E>SB QL T>FQ QLL ILKD, >P VLRO OBT>OA TFII @LJB FK QEB KBUQ @E>MQBO.
&LO KLT, ELTBSBO, TB T>KQ QL CL@RP LK PFJMIF@FQV. 7E>Q ALBP FQ JB>K QL MOLDO>J JLQFLK RPFKD
SB@QLOP? 7BfSB PBBK QEB ?BDFKKFKD LC QEFP FK %U>JMIB 1.2 (PBB M>DB 35): QEB ?LRK@FKD ?>II. !K
L?GB@Q LK P@OBBK E>P > IL@>QFLK (TEBOB FQ FP >Q >KV DFSBK JLJBKQ) >P TBII >P > SBIL@FQV
(FKPQOR@QFLKP CLO ELT FQ PELRIA JLSB COLJ LKB JLJBKQ QL QEB KBUQ). 6BIL@FQV FP >AABA QL
IL@>QFLK:
4EB .>QROB LC #LAB (S005)
45
!KA QEBK TB AO>T QEB L?GB@Q >Q QE>Q IL@>QFLK:
4EFP FP -LQFLK 101.
1. A// A07:.4?D ?: 7:.,?4:9 A// A07:.4?D ?: 7:.,?4:9
2. D=,B :-50.? ,? 7:.,?4:9 D=,B :-50.? ,? 7:.,?4:9
)K QEB ?LRK@FKD ?>II BU>JMIB, >II LC QEFP @LAB E>MMBKBA FK 0OL@BPPFKDfP J>FK Q>?, TFQEFK
M?NOJ() >KA >L;Q(). 7E>Q TB T>KQ QL AL KLT FP JLSB QLT>OAP BK@>MPRI>QFKD >II LC QEB
ILDF@ CLO JLQFLK FKPFAB LC > .7,>> .7,>>. 4EFP T>V, TB @>K @OB>QB > CLRKA>QFLK CLO MOLDO>JJFKD
JLSFKD L?GB@QP FK 0OL@BPPFKD. )K PB@QFLK ).2 LC QEB FKQOLAR@QFLK (PBB M>DB 2), g4EB 2>KALJ
7>IHBO #I>PP,h TB ?OFBCIV OBSFBTBA QEB ?>PF@P LC L?GB@Q-LOFBKQBA-MOLDO>JJFKD (g//0h).
"BVLKA QE>Q PELOQ FKQOLAR@QFLK, QEFP ?LLH >PPRJBP BUMBOFBK@B TFQE L?GB@QP >KA @I>PPBP FK
0OL@BPPFKD. )C VLR KBBA > OBCOBPEBO, ) BK@LRO>DB VLR QL @EB@H LRQ QEB 0OL@BPPFKD L?GB@QP
QRQLOF>I (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/L?GB@QP/).
)K QEFP @>PB, TBfOB DLFKD QL @OB>QB > DBKBOF@ +IP?L @I>PP QE>Q TFII ABP@OF?B > QEFKD JLSFKD
>OLRKA QEB P@OBBK. !KA PL TB JRPQ @LKPFABO QEB CLIILTFKD QTL NRBPQFLKP:
1. +3,? /,?, /:0> , 8:A0= 3,A0? +3,? /,?, /:0> , 8:A0= 3,A0?
2. +3,? 1@9.?4:9,74?D /:0> , 8:A0= 3,A0? +3,? 1@9.?4:9,74?D /:0> , 8:A0= 3,A0?
/RO -LQFLK 101 >IDLOFQEJ QBIIP RP QEB >KPTBOP QL QEBPB NRBPQFLKP. ! +IP?L L?GB@Q E>P QTL
MFB@BP LC A>Q>: FI=;NCIH >KA P?FI=CNS, TEF@E >OB ?LQE .3?=NIL L?GB@QP.
)QP CRK@QFLK>IFQV FP GRPQ >?LRQ >P PFJMIB. 4EB +IP?L KBBAP QL JLSB >KA FQ KBBAP QL ?B PBBK.
7BfII FJMIBJBKQ QEBPB KBBAP >P CRK@QFLKP K>JBA OJ>;N?() >KA >CMJF;S(). 7BfII MRQ >II LC
LRO JLQFLK ILDF@ @LAB FK OJ>;N?() >KA AO>T QEB L?GB@Q FK >CMJF;S().
FI=;NCIH.;>>(P?FI=CNS);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
PIC> OJ>;N?() U
The Mover moves. FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
#E>MQBO 1. 6B@QLOP
46
7BfSB CLODLQQBK LKB @OR@F>I FQBJ, ELTBSBO: QEB L?GB@QfP .:9>?=@.?:= .:9>?=@.?:=. 4EB @LKPQOR@QLO FP >
PMB@F>I CRK@QFLK FKPFAB LC > @I>PP QE>Q @OB>QBP QEB FKPQ>K@B LC QEB L?GB@Q FQPBIC. )Q FP TEBOB VLR
DFSB FKPQOR@QFLKP LK ELT QL PBQ RM QEB L?GB@Q. )Q >IT>VP E>P QEB P>JB K>JB >P QEB @I>PP >KA FP
@>IIBA ?V FKSLHFKD QEB 90B 90B LMBO>QLO:
)K LRO @>PB, IBQfP >O?FQO>OFIV AB@FAB QL FKFQF>IFWB LRO +IP?L L?GB@Q ?V DFSFKD FQ > O>KALJ IL@>QFLK
>KA > O>KALJ SBIL@FQV.
)C L?GB@Q-LOFBKQBA MOLDO>JJFKD FP >Q >II KBT QL VLR, LKB >PMB@Q EBOB J>V PBBJ > ?FQ
@LKCRPFKD. !CQBO >II, TB PMBKQ QEB ?BDFKKFKD LC QEFP @E>MQBO AFP@RPPFKD QEB .3?=NIL @I>PP. 4EB
.3?=NIL @I>PP FP QEB QBJMI>QB CLO J>HFKD QEB FI=;NCIH L?GB@Q >KA QEB P?FI=CNS L?GB@Q. 3L
TE>Q >OB QEBV ALFKD FKPFAB LC VBQ >KLQEBO L?GB@Q, QEB +IP?L L?GB@Q? )K C>@Q, QEFP FP GRPQ >?LRQ
QEB JLPQ KLOJ>I QEFKD BSBO. !K L?GB@Q FP PFJMIV PLJBQEFKD QE>Q ELIAP A>Q> (>KA CRK@QFLK>IFQV).
4E>Q A>Q> @>K ?B KRJ?BOP (FKQBDBOP, CIL>QP, BQ@.) LO LQEBO L?GB@QP! 7BfII PBB QEFP LSBO >KA LSBO
>D>FK FK QEFP ?LLH. &LO BU>JMIB, FK #E>MQBO 4 (PBB M>DB 144) TBfII TOFQB > @I>PP QL ABP@OF?B >
PVPQBJ LC M>OQF@IBP. 4E>Q .;LNC=F?0SMN?G L?GB@Q TFII E>SB >P FQP A>Q> > IFPQ LC .;LNC=F?
L?GB@QPj>KA B>@E .;LNC=F? L?GB@Q TFII E>SB >P FQP A>Q> PBSBO>I .3?=NIL L?GB@QP!
,BQfP CFKFPE LCC QEB +IP?L @I>PP ?V FK@LOMLO>QFKD > CRK@QFLK QL ABQBOJFKB TE>Q QEB L?GB@Q PELRIA
AL TEBK FQ OB>@EBP QEB BADB LC QEB TFKALT. &LO KLT IBQfP AL PLJBQEFKD PFJMIB, >KA GRPQ E>SB
FQ TO>M >OLRKA QEB BADBP.
The Mover is displayed. ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
W
+IP?L G = H?Q +IP?L();
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2));
W
PIC> =B?=E#>A?M() U
When it reaches one edge, set location to
the other.
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
4EB .>QROB LC #LAB (S005)
47
.LT QE>Q QEB +IP?L @I>PP FP CFKFPEBA, TB @>K ILLH >Q TE>Q TB KBBA QL AL FK LRO J>FK
MOLDO>J. 7B CFOPQ AB@I>OB > +IP?L L?GB@Q:
4EBK FKFQF>IFWB QEB JLSBO FK M?NOJ():
>KA @>II QEB >MMOLMOF>QB CRK@QFLKP FK >L;Q():
(BOB FP QEB BKQFOB BU>JMIB CLO OBCBOBK@B:
Example 1.7: Motion 101 (velocity)
W
+IP?L GIP?L;
GIP?L = H?Q +IP?L();
GIP?L.OJ>;N?();
GIP?L.=B?=E#>A?M();
GIP?L.>CMJF;S();
Declare Mover object. +IP?L GIP?L;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
Create Mover object. GIP?L = H?Q +IP?L();
W
PIC> >L;Q() U
<;=EALIOH>(255);
#E>MQBO 1. 6B@QLOP
48
Call functions on Mover object. GIP?L.OJ>;N?();
GIP?L.=B?=E#>A?M();
GIP?L.>CMJF;S();
W
=F;MM +IP?L U
Our object has two PVectors: location and
velocity.
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-2,2),L;H>IG(-2,2));
W
PIC> OJ>;N?() U
Motion 101: Location changes by velocity. FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
PIC> =B?=E#>A?M() U
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
W
W
1.8 Vector Motion: Acceleration 1.8 Vector Motion: Acceleration
/+. !Q QEFP MLFKQ, TB PELRIA CBBI @LJCLOQ>?IB TFQE QTL QEFKDP: (1) TE>Q > .3?=NIL FP >KA (2) ELT
TB RPB .3?=NILP FKPFAB LC >K L?GB@Q QL HBBM QO>@H LC FQP IL@>QFLK >KA JLSBJBKQ. 4EFP FP >K
BU@BIIBKQ CFOPQ PQBM >KA ABPBOSBP > JFIA OLRKA LC >MMI>RPB. "BCLOB PQ>KAFKD LS>QFLKP >KA
P@OB>JFKD C>KP, ELTBSBO, TB KBBA QL J>HB LKB JLOB, PLJBTE>Q ?FDDBO PQBM CLOT>OA. !CQBO
>II, T>Q@EFKD QEB -LQFLK 101 BU>JMIB FP C>FOIV ?LOFKDdQEB @FO@IB KBSBO PMBBAP RM, KBSBO PILTP
4EB .>QROB LC #LAB (S005)
49
ALTK, >KA KBSBO QROKP. &LO JLOB FKQBOBPQFKD JLQFLK, CLO JLQFLK QE>Q >MMB>OP FK QEB OB>I
TLOIA >OLRKA RP, TB KBBA QL >AA LKB JLOB .3?=NIL QL LRO @I>PPd;==?F?L;NCIH.
4EB PQOF@Q ABCFKFQFLK LC ,..070=,?4:9 ,..070=,?4:9 TBfOB RPFKD EBOB FP: H<9 F5H9 C: 7<5B;9 C: J9@C7=HM. ,BQfP
QEFKH >?LRQ QE>Q ABCFKFQFLK CLO > JLJBKQ. )P QEFP > KBT @LK@BMQ? .LQ OB>IIV. 6BIL@FQV FP
ABCFKBA >P H<9 F5H9 C: 7<5B;9 C: @C75H=CB. )K BPPBK@B, TB >OB ABSBILMFKD > gQOF@HIB-ALTKh
BCCB@Q. !@@BIBO>QFLK >CCB@QP SBIL@FQV, TEF@E FK QROK >CCB@QP IL@>QFLK (CLO PLJB ?OFBC
CLOBPE>ALTFKD, QEFP MLFKQ TFII ?B@LJB BSBK JLOB @OR@F>I FK QEB KBUQ @E>MQBO, TEBK TB PBB
ELT CLO@BP >CCB@Q >@@BIBO>QFLK, TEF@E >CCB@QP SBIL@FQV, TEF@E >CCB@QP IL@>QFLK). )K @LAB, QEFP
OB>AP:
!P >K BUBO@FPB, COLJ QEFP MLFKQ CLOT>OA, IBQfP J>HB > ORIB CLO LROPBISBP. ,BQfP TOFQB BSBOV
BU>JMIB FK QEB OBPQ LC QEFP ?LLH TFQELRQ BSBO QLR@EFKD QEB S>IRB LC SBIL@FQV >KA IL@>QFLK
(BU@BMQ QL FKFQF>IFWB QEBJ). )K LQEBO TLOAP, LRO DL>I KLT CLO MOLDO>JJFKD JLQFLK FP: #LJB RM
TFQE >K >IDLOFQEJ CLO ELT TB @>I@RI>QB >@@BIBO>QFLK >KA IBQ QEB QOF@HIB-ALTK BCCB@Q TLOH FQP
J>DF@. ()K QORQE, VLRfII CFKA OB>PLKP QL ?OB>H QEFP ORIB, ?RQ FQfP FJMLOQ>KQ QL FIIRPQO>QB QEB
MOFK@FMIBP ?BEFKA LRO JLQFLK >IDLOFQEJ.) !KA PL TB KBBA QL @LJB RM TFQE PLJB T>VP QL
@>I@RI>QB >@@BIBO>QFLK:
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Acceleration Algorithms! Acceleration Algorithms!
1. A 7CBGH5BH 5779@9F5H=CB
2. A HCH5@@M F5B8CA 5779@9F5H=CB
3. A779@9F5H=CB HCK5F8G H<9 ACIG9
!IDLOFQEJ #1, 5 7CBGH5BH 5779@9F5H=CB, FP KLQ M>OQF@RI>OIV FKQBOBPQFKD, ?RQ FQ FP QEB PFJMIBPQ
>KA TFII EBIM RP ?BDFK FK@LOMLO>QFKD >@@BIBO>QFLK FKQL LRO @LAB. 4EB CFOPQ QEFKD TB KBBA QL
AL FP >AA >KLQEBO .3?=NIL QL QEB +IP?L @I>PP:
!KA FK@LOMLO>QB >@@BIBO>QFLK FKQL QEB OJ>;N?() CRK@QFLK:
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
A new PVector for acceleration +0;9JEH 799;B;H7J?ED;
PIC> OJ>;N?() U
Our motion algorithm is now two lines of
code!
L;BE9?JO.7::(799;B;H7J?ED);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
50
7BfOB >IJLPQ ALKB. 4EB LKIV JFPPFKD MFB@B FP FKFQF>IFW>QFLK FK QEB @LKPQOR@QLO.
,BQfP PQ>OQ QEB +IP?L L?GB@Q FK QEB JFAAIB LC QEB TFKALTj
jTFQE >K FKFQF>I SBIL@FQV LC WBOL.
4EFP JB>KP QE>Q TEBK QEB PHBQ@E PQ>OQP, QEB L?GB@Q FP >Q OBPQ. 7B ALKfQ E>SB QL TLOOV >?LRQ
SBIL@FQV >KVJLOB, >P TB >OB @LKQOLIIFKD QEB L?GB@QfP JLQFLK BKQFOBIV TFQE >@@BIBO>QFLK.
3MB>HFKD LC TEF@E, >@@LOAFKD QL !IDLOFQEJ #1, LRO CFOPQ PHBQ@E FKSLISBP @LKPQ>KQ >@@BIBO>QFLK.
3L IBQfP MF@H > S>IRB.
->V?B VLRfOB QEFKHFKD, g'LPE, QELPB S>IRBP PBBJ >TCRIIV PJ>II!h 4E>QfP OFDEQ, QEBV >OB NRFQB
QFKV. )QfP FJMLOQ>KQ QL OB>IFWB QE>Q LRO >@@BIBO>QFLK S>IRBP (JB>PROBA FK MFUBIP) >@@RJRI>QB LSBO
QFJB FK QEB SBIL@FQV, >?LRQ QEFOQV QFJBP MBO PB@LKA ABMBKAFKD LK LRO PHBQ@EfP CO>JB O>QB. !KA
PL QL HBBM QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO TFQEFK > OB>PLK>?IB O>KDB, LRO >@@BIBO>QFLK
S>IRBP PELRIA OBJ>FK NRFQB PJ>II. 7B @>K >IPL EBIM QEFP @>RPB ?V FK@LOMLO>QFKD QEB .3?=NIL
CRK@QFLK FCGCN().
4EFP QO>KPI>QBP QL QEB CLIILTFKD:
2<5H =G H<9 A5;B=HI89 C: J9@C7=HM? %: =HSG @9GG H<5B 10, BC KCFF=9G; >IGH @95J9 =H 5G =G. %: =HSG ACF9
H<5B 10, <CK9J9F, F98I79 =H HC 10!
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01);
W
The limit() function constrains the magnitude
of a vector.
P?FI=CNS.FCGCN(10);
4EB .>QROB LC #LAB (S005)
51
,BQfP Q>HB > ILLH >Q QEB @E>KDBP QL QEB +IP?L @I>PP, @LJMIBQB TFQE ;==?F?L;NCIH >KA
FCGCN().
Example 1.8: Motion 101 (velocity and constant acceleration)
7OFQB QEB FCGCN() CRK@QFLK CLO QEB .3?=NIL @I>PP.
PIC> FCGCN(@FI;N G;R) U
C@ (::::::: > :::::::) U
:::::::::();
::::(G;R);
W
W
Exercise 1.4 Exercise 1.4
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
Acceleration is the key! .3?=NIL ;==?F?L;NCIH;
The variable topspeed will limit the
magnitude of velocity.
@FI;N NIJMJ??>;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(-0.001,0.01);
NIJMJ??> = 10;
W
PIC> OJ>;N?() U
Velocity changes by acceleration and is
limited by topspeed.
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
#E>MQBO 1. 6B@QLOP
52
.LT LK QL !IDLOFQEJ #2, 5 HCH5@@M F5B8CA 5779@9F5H=CB. )K QEFP @>PB, FKPQB>A LC FKFQF>IFWFKD
>@@BIBO>QFLK FK QEB L?GB@QfP @LKPQOR@QLO, TB T>KQ QL MF@H > KBT >@@BIBO>QFLK B>@E @V@IB, F.B.
B>@E QFJB OJ>;N?() FP @>IIBA.
Example 1.9: Motion 101 (velocity and random acceleration)
"B@>RPB QEB O>KALJ SB@QLO FP > KLOJ>IFWBA LKB, TB @>K QOV P@>IFKD FQ:
(>) P@>IFKD QEB >@@BIBO>QFLK QL > @LKPQ>KQ S>IRB
FI=;NCIH.;>>(P?FI=CNS);
W
display() is the same. PIC> >CMJF;S() UW
checkEdges() is the same. PIC> =B?=E#>A?M() UW
W
#OB>QB > PFJRI>QFLK LC > @>O (LO ORKKBO) QE>Q >@@BIBO>QBP TEBK VLR MOBPP QEB RM HBV >KA
?O>HBP TEBK VLR MOBPP QEB ALTK HBV.
Exercise 1.5 Exercise 1.5
PIC> OJ>;N?() U
The random2D() function will give us a
PVector of length 1 pointing in a random
direction.
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
4EB .>QROB LC #LAB (S005)
53
(?) P@>IFKD QEB >@@BIBO>QFLK QL > O>KALJ S>IRB
7EFIB QEFP J>V PBBJ IFHB >K L?SFLRP MLFKQ, FQfP @OR@F>I QL RKABOPQ>KA QE>Q >@@BIBO>QFLK ALBP
KLQ JBOBIV OBCBO QL QEB GD998=B; ID LO G@CK=B; 8CKB LC > JLSFKD L?GB@Q, ?RQ O>QEBO 5BM
7<5B;9 FK SBIL@FQV FK BFQEBO J>DKFQRAB LO AFOB@QFLK. !@@BIBO>QFLK FP RPBA QL PQBBO >K L?GB@Q,
>KA TBfII PBB QEFP >D>FK >KA >D>FK FK CRQROB @E>MQBOP >P TB ?BDFK QL MOLDO>J L?GB@QP QE>Q
J>HB AB@FPFLKP >?LRQ ELT QL JLSB >?LRQ QEB P@OBBK.
Constant 799;B;H7J?ED.CKBJ(0.5);
;==?F?L;NCIH = .3?=NIL.L;H>IG2"();
Random 799;B;H7J?ED.CKBJ(H7D:EC(2));
2BCBOOFKD ?>@H QL QEB )KQOLAR@QFLK (PBB M>DB 17), FJMIBJBKQ >@@BIBO>QFLK >@@LOAFKD QL
0BOIFK KLFPB.
Exercise 1.6 Exercise 1.6
1.9 Static vs. Non-Static Functions 1.9 Static vs. Non-Static Functions
"BCLOB TB DBQ QL !IDLOFQEJ #3 (5779@9F5H9 HCK5F8G H<9 ACIG9), TB KBBA QL @LSBO LKB JLOB
O>QEBO FJMLOQ>KQ >PMB@Q LC TLOHFKD TFQE SB@QLOP >KA QEB .3?=NIL @I>PP: QEB AFCCBOBK@B
?BQTBBK RPFKD >?,?4. >?,?4. JBQELAP >KA 9:9->?,?4. 9:9->?,?4. JBQELAP.
&LODBQQFKD >?LRQ SB@QLOP CLO > JLJBKQ, Q>HB > ILLH >Q QEB CLIILTFKD @LAB:
0OBQQV PFJMIB, OFDEQ? R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA KLT R FP BNR>I QL 5. 7B @LRIA
TOFQB QEB @LOOBPMLKAFKD @LAB MOBQQV B>PFIV ?>PBA LK TE>Q TBfSB IB>OKBA >?LRQ .3?=NIL.
4EB SB@QLO P E>P QEB S>IRB LC (0,0), TB >AA O QL FQ, >KA KLT P FP BNR>I QL (4,5). %>PV, OFDEQ?
,BQfP Q>HB > ILLH >Q >KLQEBO BU>JMIB LC PLJB PFJMIB CIL>QFKD MLFKQ J>QE:
@FI;N R = 0;
@FI;N S = 5;
R = R + S;
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
P.;>>(O);
#E>MQBO 1. 6B@QLOP
54
R E>P QEB S>IRB LC 0, TB >AA S QL FQ, >KA PQLOB QEB OBPRIQ FK > KBT S>OF>?IB T. 4EB S>IRB LC R
ALBP KLQ @E>KDB FK QEFP BU>JMIB (KBFQEBO ALBP S)! 4EFP J>V PBBJ IFHB > QOFSF>I MLFKQ, >KA LKB
QE>Q FP NRFQB FKQRFQFSB TEBK FQ @LJBP QL J>QEBJ>QF@>I LMBO>QFLKP TFQE CIL>QP. (LTBSBO, FQfP KLQ
PL L?SFLRP TFQE J>QEBJ>QF@>I LMBO>QFLKP FK .3?=NIL. ,BQfP QOV QL TOFQB QEB @LAB ?>PBA LK TE>Q
TB HKLT PL C>O.
4EB >?LSB JFDEQ PBBJ IFHB > DLLA DRBPP, ?RQ FQfP GRPQ KLQ QEB T>V QEB .3?=NIL @I>PP TLOHP. )C
TB ILLH >Q QEB ABCFKFQFLK LC ;>>() . . .
TB PBB QE>Q QEFP @LAB ALBP KLQ >@@LJMIFPE LRO DL>I. &FOPQ, FQ ALBP KLQ OBQROK > KBT .3?=NIL
(QEB OBQROK QVMB FP gSLFAh) >KA PB@LKA, FQ @E>KDBP QEB S>IRB LC QEB .3?=NIL RMLK TEF@E FQ FP
@>IIBA. )K LOABO QL >AA QTL .3?=NIL L?GB@QP QLDBQEBO >KA OBQROK QEB OBPRIQ >P > KBT .3?=NIL,
TB JRPQ RPB QEB PQ>QF@ ;>>() CRK@QFLK.
&RK@QFLKP QE>Q TB @>II COLJ QEB @I>PP K>JB FQPBIC (O>QEBO QE>K COLJ > PMB@Fk@ L?GB@Q FKPQ>K@B)
>OB HKLTK >P >?,?4. 1@9.?4:9> >?,?4. 1@9.?4:9>. (BOB >OB QTL BU>JMIBP LC CRK@QFLK @>IIP QE>Q >PPRJB QTL
.3?=NIL L?GB@QP, P >KA O:
3FK@B VLR @>KfQ TOFQB PQ>QF@ CRK@QFLKP VLROPBIC FK 0OL@BPPFKD, VLR JFDEQ KLQ E>SB BK@LRKQBOBA
QEBJ ?BCLOB. .3?=NIL'P PQ>QF@ CRK@QFLKP >IILT RP QL MBOCLOJ DBKBOF@ J>QEBJ>QF@>I LMBO>QFLKP
LK .3?=NIL L?GB@QP TFQELRQ E>SFKD QL >AGRPQ QEB S>IRB LC LKB LC QEB FKMRQ .3?=NILP. ,BQfP ILLH
>Q ELT TB JFDEQ TOFQB QEB PQ>QF@ SBOPFLK LC ;>>():
@FI;N R = 0;
@FI;N S = 5;
@FI;N T = R + S;
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
Dont be fooled; this is incorrect!!! .3?=NIL Q = P.;>>(O);
PIC> ;>>(.3?=NIL P) U
R = R + P.R;
S = S + P.S;
W
Static: called from the class name. .3?=NIL.;>>(P,O);
Not static: called from an object instance. P.;>>(O);
4EB .>QROB LC #LAB (S005)
55
4EBOB >OB PBSBO>I AFCCBOBK@BP EBOB:
i 4EB CRK@QFLK FP I>?BIBA >P >?,?4. >?,?4..
i 4EB CRK@QFLK ALBP KLQ E>SB > A:4/ A:4/ OBQROK QVMB, ?RQ O>QEBO OBQROKP > .3?=NIL.
i 4EB CRK@QFLK @OB>QBP > KBT .3?=NIL (P3) >KA OBQROKP QEB PRJ LC QEB @LJMLKBKQP
LC P1 >KA P2 FK QE>Q KBT .3?=NIL.
7EBK VLR @>II > PQ>QF@ CRK@QFLK, FKPQB>A LC OBCBOBK@FKD >K >@QR>I L?GB@Q FKPQ>K@B, VLR PFJMIV
OBCBOBK@B QEB K>JB LC QEB @I>PP FQPBIC.
4EB .3?=NIL @I>PP E>P PQ>QF@ SBOPFLKP LC ;>>(), MO<(), GOFN(), >KA >CP().
The static version of add allows us to add
two PVectors together and assign the
result to a new PVector while leaving the
original PVectors (v and u above) intact.
MN;NC= .3?=NIL ;>>(.3?=NIL P1, .3?=NIL P2) U
.3?=NIL P3 = H?Q .3?=NIL(P1.R + P2.R, P1.S + P2.S);
L?NOLH P3;
W
.3?=NIL P = H?Q .3?=NIL(0,0);
.3?=NIL O = H?Q .3?=NIL(4,5);
.3?=NIL Q = P.;>>(O);
+0;9JEH M = +0;9JEH.7::(L,K);
4O>KPI>QB QEB CLIILTFKD MPBRAL@LAB QL @LAB RPFKD PQ>QF@ LO KLK-PQ>QF@ CRK@QFLKP TEBOB
>MMOLMOF>QB.
i 4EB .3?=NIL P BNR>IP (1,5).
i 4EB .3?=NIL O BNR>IP P JRIQFMIFBA ?V 2.
i 4EB .3?=NIL Q BNR>IP P JFKRP O.
i $FSFAB QEB .3?=NIL T ?V 3.
.3?=NIL P = H?Q .3?=NIL(1,5);
.3?=NIL O = ::::::::.:::::(::,::);
.3?=NIL Q = ::::::::.:::::(::,::);
:::::::::::;
Exercise 1.7 Exercise 1.7
#E>MQBO 1. 6B@QLOP
56
1.10 Interactivity with Acceleration 1.10 Interactivity with Acceleration
4L CFKFPE LRQ QEFP @E>MQBO, IBQfP QOV PLJBQEFKD
> ?FQ JLOB @LJMIBU >KA > DOB>Q AB>I JLOB
RPBCRI. 7BfII AVK>JF@>IIV @>I@RI>QB >K
L?GB@QfP >@@BIBO>QFLK >@@LOAFKD QL > ORIB
PQ>QBA FK !IDLOFQEJ #3 d H<9 C6>97H
5779@9F5H9G HCK5F8G H<9 ACIG9.
!KVQFJB TB T>KQ QL @>I@RI>QB > SB@QLO ?>PBA
LK > ORIB LO > CLOJRI>, TB KBBA QL @LJMRQB
QTL QEFKDP: 8,294?@/0 8,294?@/0 >KA /4=0.?4:9 /4=0.?4:9. ,BQfP PQ>OQ TFQE AFOB@QFLK. 7B HKLT QEB >@@BIBO>QFLK
SB@QLO PELRIA MLFKQ COLJ QEB L?GB@QfP IL@>QFLK QLT>OAP QEB JLRPB IL@>QFLK. ,BQfP P>V QEB L?GB@Q
FP IL@>QBA >Q QEB MLFKQ (R,S) >KA QEB JLRPB >Q (GIOM?5,GIOM?6).
)K &FDROB 1.15, TB PBB QE>Q TB @>K DBQ >
SB@QLO (>R,>S) ?V PR?QO>@QFKD QEB L?GB@QfP
IL@>QFLK COLJ QEB JLRPBfP IL@>QFLK.
i :N = CEKI;2 - N
i :O = CEKI;3 - O
,BQfP OBTOFQB QEB >?LSB RPFKD .3?=NIL
PVKQ>U. !PPRJFKD TB >OB FK QEB +IP?L @I>PP
>KA QERP E>SB >@@BPP QL QEB L?GB@QfP .3?=NIL FI=;NCIH, TB QEBK E>SB:
7B KLT E>SB > .3?=NIL QE>Q MLFKQP COLJ QEB JLSBOfP IL@>QFLK >II QEB T>V QL QEB JLRPB. )C QEB
L?GB@Q TBOB QL >@QR>IIV >@@BIBO>QB RPFKD QE>Q SB@QLO, FQ TLRIA >MMB>O FKPQ>KQ>KBLRPIV >Q QEB
JLRPB IL@>QFLK. 4EFP ALBP KLQ J>HB CLO DLLA >KFJ>QFLK, LC @LROPB, >KA TE>Q TB T>KQ QL AL
KLT FP AB@FAB ELT NRF@HIV QE>Q L?GB@Q PELRIA >@@BIBO>QB QLT>OA QEB JLRPB.
)K LOABO QL PBQ QEB J>DKFQRAB (TE>QBSBO FQ J>V ?B) LC LRO >@@BIBO>QFLK .3?=NIL, TB JRPQ CFOPQ
444 QE>Q AFOB@QFLK SB@QLO. 4E>QfP OFDEQ, VLR P>FA FQ. *CFA5@=N9. )C TB @>K PEOFKH QEB SB@QLO ALTK
QL FQP RKFQ SB@QLO (LC IBKDQE LKB) QEBK TB E>SB > SB@QLO QE>Q QBIIP RP QEB AFOB@QFLK >KA @>K B>PFIV
?B P@>IBA QL >KV S>IRB. /KB JRIQFMIFBA ?V >KVQEFKD BNR>IP >KVQEFKD.
F64B?2 1.14
F64B?2 1.15
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
Look! Were using the static reference to
sub() because we want a new PVector
pointing from one point to another.
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
@FI;N ;HSNBCHA = ?????
>CL.HILG;FCT?();
>CL.GOFN(;HSNBCHA);
4EB .>QROB LC #LAB (S005)
57
4L PRJJ>OFWB, TB Q>HB QEB CLIILTFKD PQBMP:
1. #>I@RI>QB > SB@QLO QE>Q MLFKQP COLJ QEB L?GB@Q QL QEB Q>ODBQ IL@>QFLK (JLRPB)
2. .LOJ>IFWB QE>Q SB@QLO (OBAR@FKD FQP IBKDQE QL 1)
3. 3@>IB QE>Q SB@QLO QL >K >MMOLMOF>QB S>IRB (?V JRIQFMIVFKD FQ ?V PLJB S>IRB)
4. !PPFDK QE>Q SB@QLO QL >@@BIBO>QFLK
!KA EBOB >OB QELPB PQBMP FK QEB OJ>;N?() CRK@QFLK FQPBIC:
Example 1.10: Accelerating towards the mouse
9LR J>V ?B TLKABOFKD TEV QEB @FO@IB ALBPKfQ PQLM TEBK FQ OB>@EBP QEB Q>ODBQ. )QfP FJMLOQ>KQ
QL KLQB QE>Q QEB L?GB@Q JLSFKD E>P KL HKLTIBADB >?LRQ QOVFKD QL PQLM >Q > ABPQFK>QFLK; FQ
LKIV HKLTP TEBOB QEB ABPQFK>QFLK FP >KA QOFBP QL DL QEBOB >P NRF@HIV >P MLPPF?IB. 'LFKD >P
PIC> OJ>;N?() U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
Step 1: Compute direction .3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
Step 2: Normalize >CL.HILG;FCT?();
Step 3: Scale >CL.GOFN(0.5);
Step 4: Accelerate ;==?F?L;NCIH = >CL;
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
58
NRF@HIV >P MLPPF?IB JB>KP FQ TFII FKBSFQ>?IV LSBOPELLQ QEB IL@>QFLK >KA E>SB QL QROK >OLRKA,
>D>FK DLFKD >P NRF@HIV >P MLPPF?IB QLT>OAP QEB ABPQFK>QFLK, LSBOPELLQFKD FQ >D>FK, >KA PL LK
>KA PL CLOQE. 3Q>V QRKBA; FK I>QBO @E>MQBOP TBfII IB>OK ELT QL MOLDO>J >K L?GB@Q QL ,==4A0 ,==4A0 >Q >
IL@>QFLK (PILT ALTK LK >MMOL>@E).
4EFP BU>JMIB FP OBJ>OH>?IV @ILPB QL QEB @LK@BMQ LC DO>SFQ>QFLK>I >QQO>@QFLK (FK TEF@E QEB L?GB@Q
FP >QQO>@QBA QL QEB JLRPB IL@>QFLK). 'O>SFQ>QFLK>I >QQO>@QFLK TFII ?B @LSBOBA FK JLOB ABQ>FI FK QEB
KBUQ @E>MQBO. (LTBSBO, LKB QEFKD JFPPFKD EBOB FP QE>Q QEB PQOBKDQE LC DO>SFQV (J>DKFQRAB LC
>@@BIBO>QFLK) FP FKSBOPBIV MOLMLOQFLK>I QL AFPQ>K@B. 4EFP JB>KP QE>Q QEB @ILPBO QEB L?GB@Q FP QL
QEB JLRPB, QEB C>PQBO FQ >@@BIBO>QBP.
,BQfP PBB TE>Q QEFP BU>JMIB TLRIA ILLH IFHB TFQE >K >OO>V LC JLSBOP (O>QEBO QE>K GRPQ LKB).
Example 1.11: Array of movers accelerating towards the mouse
4OV FJMIBJBKQFKD QEB >?LSB BU>JMIB TFQE > S>OF>?IB J>DKFQRAB LC >@@BIBO>QFLK,
PQOLKDBO TEBK FQ FP BFQEBO @ILPBO LO C>OQEBO >T>V.
Exercise 1.8 Exercise 1.8
An array of objects +IP?L89 GIP?LM = H?Q +IP?L8209;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
4EB .>QROB LC #LAB (S005)
59
Initialize each object in the array. GIP?LM8C9 = H?Q +IP?L();
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
Calling functions on all the objects in the
array
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.=B?=E#>A?M();
GIP?LM8C9.>CMJF;S();
W
W
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N NIJMJ??>;
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(0,0);
NIJMJ??> = 4;
W
PIC> OJ>;N?() U
Our algorithm for calculating
acceleration:
Find the vector pointing towards the
mouse.
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
Normalize. >CL.HILG;FCT?();
Scale. >CL.GOFN(0.5);
Set to acceleration. ;==?F?L;NCIH = >CL;
Motion 101! Velocity changes by
acceleration. Location changes by velocity.
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(NIJMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
W
#E>MQBO 1. 6B@QLOP
60
Display the Mover PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
What to do at the edges PIC> =B?=E#>A?M() U
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = 0;
W ?FM? C@ (FI=;NCIH.R < 0) U
FI=;NCIH.R = QC>NB;
W
C@ (FI=;NCIH.S > B?CABN) U
FI=;NCIH.S = 0;
W ?FM? C@ (FI=;NCIH.S < 0) U
FI=;NCIH.S = B?CABN;
W
W
W
4EB .>QROB LC #LAB (S005)
61
The Ecosystem Project The Ecosystem Project
AG A9BH=CB98 =B H<9 DF9:579, CB9 K5M HC IG9 H<=G 6CC? =G HC 6I=@8 5 G=B;@9 DFC>97H
CJ9F H<9 7CIFG9 C: F958=B; =H, =B7CFDCF5H=B; 9@9A9BHG :FCA 957< 7<5DH9F CB9 GH9D
5H 5 H=A9. 29S@@ :C@@CK H<9 89J9@CDA9BH C: 5B 9L5AD@9 DFC>97H H<FCI;<CIH H<=G
6CC?R5 G=AI@5H=CB C: 5B 97CGMGH9A. %A5;=B9 5 DCDI@5H=CB C: 7CADIH5H=CB5@
7F95HIF9G GK=AA=B; 5FCIB8 5 8=;=H5@ DCB8, =BH9F57H=B; K=H< 957< CH<9F 577CF8=B;
HC J5F=CIG FI@9G.
Step 1 Exercise:
Develop a set of rules for simulating the real-world behavior of a creature, such as
a nervous fly, swimming fish, hopping bunny, slithering snake, etc. Can you
control the objects motion by only manipulating the acceleration? Try to give the
creature a personality through its behavior (rather than through its visual design).
F64B?2 1.16: *52 E0<@F@A2: '?<720A
#E>MQBO 1. 6B@QLOP
62
Chapter 2. Forces Chapter 2. Forces
ID<;KA B;12?2@A6:.A2 A52 F<?02.J
Darth Vader
)K QEB CFK>I BU>JMIB LC #E>MQBO 1, TB P>T ELT TB @LRIA @>I@RI>QB > AVK>JF@ >@@BIBO>QFLK
?>PBA LK > SB@QLO MLFKQFKD COLJ > @FO@IB LK QEB P@OBBK QL QEB JLRPB IL@>QFLK. 4EB OBPRIQFKD
JLQFLK OBPBJ?IBA > J>DKBQF@ >QQO>@QFLK ?BQTBBK @FO@IB >KA JLRPB, >P FC PLJB :CF79 TBOB
MRIIFKD QEB @FO@IB FK QLT>OAP QEB JLRPB. )K QEFP @E>MQBO TB TFII CLOJ>IFWB LRO RKABOPQ>KAFKD LC
QEB @LK@BMQ LC > CLO@B >KA FQP OBI>QFLKPEFM QL >@@BIBO>QFLK. /RO DL>I, ?V QEB BKA LC QEFP @E>MQBO,
FP QL RKABOPQ>KA ELT QL J>HB JRIQFMIB L?GB@QP JLSB >OLRKA QEB P@OBBK >KA OBPMLKA QL >
S>OFBQV LC BKSFOLKJBKQ>I CLO@BP.
2.1 Forces and Newtons Laws of Motion 2.1 Forces and Newtons Laws of Motion
"BCLOB TB ?BDFK BU>JFKFKD QEB MO>@QF@>I OB>IFQFBP LC PFJRI>QFKD CLO@BP FK @LAB, IBQfP Q>HB >
@LK@BMQR>I ILLH >Q TE>Q FQ JB>KP QL ?B > CLO@B FK QEB OB>I TLOIA. *RPQ IFHB QEB TLOA gSB@QLO,h
gCLO@Bh FP LCQBK RPBA QL JB>K > S>OFBQV LC QEFKDP. )Q @>K FKAF@>QB > MLTBOCRI FKQBKPFQV, >P FK g3EB
MRPEBA QEB ?LRIABO TFQE DOB>Q CLO@Bh LO g(B PMLHB CLO@BCRIIV.h 4EB ABCFKFQFLK LC 1:=.0 1:=.0 QE>Q TB
@>OB >?LRQ FP JR@E JLOB CLOJ>I >KA @LJBP COLJ )P>>@ .BTQLKfP I>TP LC JLQFLK:
! CLO@B FP > SB@QLO QE>Q @>RPBP >K L?GB@Q TFQE J>PP QL >@@BIBO>QB.
4EB .>QROB LC #LAB (S005)
63
4EB DLLA KBTP EBOB FP QE>Q TB OB@LDKFWB QEB CFOPQ M>OQ LC QEB ABCFKFQFLK: 5 :CF79 =G 5 J97HCF.
4E>KH DLLAKBPP TB GRPQ PMBKQ > TELIB @E>MQBO IB>OKFKD TE>Q > SB@QLO FP >KA ELT QL
MOLDO>J TFQE .3?=NILP!
,BQfP ILLH >Q .BTQLKfP QEOBB I>TP LC JLQFLK FK OBI>QFLK QL QEB @LK@BMQ LC > CLO@B.
Newtons First Law Newtons First Law
.BTQLKfP CFOPQ I>T FP @LJJLKIV PQ>QBA >P:
!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK.
(LTBSBO, QEFP FP JFPPFKD >K FJMLOQ>KQ BIBJBKQ OBI>QBA QL CLO@BP. 7B @LRIA BUM>KA FQ ?V
PQ>QFKD:
!K L?GB@Q >Q OBPQ PQ>VP >Q OBPQ >KA >K L?GB@Q FK JLQFLK PQ>VP FK JLQFLK >Q >
@LKPQ>KQ PMBBA >KA AFOB@QFLK RKIBPP >@QBA RMLK ?V >K RK?>I>K@BA CLO@B.
"V QEB QFJB .BTQLK @>JB >ILKD, QEB MOBS>FIFKD QEBLOV LC JLQFLKdCLOJRI>QBA ?V
!OFPQLQIBdT>P KB>OIV QTL QELRP>KA VB>OP LIA. )Q PQ>QBA QE>Q FC >K L?GB@Q FP JLSFKD, PLJB PLOQ
LC CLO@B FP OBNRFOBA QL HBBM FQ JLSFKD. 5KIBPP QE>Q JLSFKD QEFKD FP ?BFKD MRPEBA LO MRIIBA, FQ
TFII PFJMIV PILT ALTK LO PQLM. 2FDEQ?
4EFP, LC @LROPB, FP KLQ QORB. )K QEB >?PBK@B LC >KV CLO@BP, KL CLO@B FP OBNRFOBA QL HBBM >K
L?GB@Q JLSFKD. !K L?GB@Q (PR@E >P > ?>II) QLPPBA FK QEB B>OQEfP >QJLPMEBOB PILTP ALTK
?B@>RPB LC >FO OBPFPQ>K@B (> CLO@B). !K L?GB@QfP SBIL@FQV TFII LKIV OBJ>FK @LKPQ>KQ FK QEB
>?PBK@B LC >KV CLO@BP LO FC QEB CLO@BP QE>Q >@Q LK FQ @>K@BI B>@E LQEBO LRQ, F.B. QEB KBQ CLO@B
>AAP RM QL WBOL. 4EFP FP LCQBK OBCBOOBA QL >P 0<@474-=4@8 0<@474-=4@8. 4EB C>IIFKD ?>II TFII OB>@E > QBOJFK>I
SBIL@FQV (QE>Q PQ>VP @LKPQ>KQ) LK@B QEB CLO@B LC >FO OBPFPQ>K@B BNR>IP QEB CLO@B LC DO>SFQV.
F64B?2 2.1: *52 =2;1B9B: 1<2@;'A :<C2 /20.B@2 .99 A52 3<?02@ 0.;029 2.05 <A52? <BA (.11 B= A< . ;2A
3<?02 <3 G2?<).
#E>MQBO 2. &LO@BP
64
)K LRO 0OL@BPPFKD TLOIA, TB @LRIA OBPQ>QB .BTQLKfP CFOPQ I>T >P CLIILTP:
!K L?GB@QfP 06B@QLO SBIL@FQV TFII OBJ>FK @LKPQ>KQ FC FQ FP FK > PQ>QB LC
BNRFIF?OFRJ.
3HFMMFKD .BTQLKfP PB@LKA I>T (>ODR>?IV QEB JLPQ FJMLOQ>KQ I>T CLO LRO MROMLPBP) CLO >
JLJBKQ, IBQfP JLSB LK QL QEB QEFOA I>T.
Newtons Third Law Newtons Third Law
4EFP I>T FP LCQBK PQ>QBA >P:
&LO BSBOV >@QFLK QEBOB FP >K BNR>I >KA LMMLPFQB OB>@QFLK.
4EFP I>T COBNRBKQIV @>RPBP PLJB @LKCRPFLK FK QEB T>V QE>Q FQ FP PQ>QBA. &LO LKB, FQ PLRKAP IFHB
LKB CLO@B @>RPBP >KLQEBO. 9BP, FC VLR MRPE PLJBLKB, QE>Q PLJBLKB J>V 57H=J9@M AB@FAB QL
MRPE VLR ?>@H. "RQ QEFP FP KLQ QEB >@QFLK >KA OB>@QFLK TB >OB Q>IHFKD >?LRQ TFQE .BTQLKfP QEFOA
I>T.
,BQfP P>V VLR MRPE >D>FKPQ > T>II. 4EB T>II ALBPKfQ >@QFSBIV AB@FAB QL MRPE ?>@H LK VLR. 4EBOB
FP KL gLOFDFKh CLO@B. 9LRO MRPE PFJMIV FK@IRABP ?LQE CLO@BP, OBCBOOBA QL >P >K g>@QFLK/OB>@QFLK
M>FO.h
! ?BQQBO T>V LC PQ>QFKD QEB I>T JFDEQ ?B:
&LO@BP >IT>VP L@@RO FK M>FOP. 4EB QTL CLO@BP >OB LC BNR>I PQOBKDQE, ?RQ FK
LMMLPFQB AFOB@QFLKP.
.LT, QEFP PQFII @>RPBP @LKCRPFLK ?B@>RPB FQ PLRKAP IFHB QEBPB CLO@BP TLRIA >IT>VP @>K@BI B>@E
LQEBO LRQ. 4EFP FP KLQ QEB @>PB. 2BJBJ?BO, QEB CLO@BP >@Q LK AFCCBOBKQ L?GB@QP. !KA GRPQ
?B@>RPB QEB QTL CLO@BP >OB BNR>I, FQ ALBPKfQ JB>K QE>Q QEB JLSBJBKQP >OB BNR>I (LO QE>Q QEB
L?GB@QP TFII PQLM JLSFKD).
4OV MRPEFKD LK > PQ>QFLK>OV QOR@H. !IQELRDE QEB QOR@H FP C>O JLOB MLTBOCRI QE>K VLR, RKIFHB >
JLSFKD LKB, > PQ>QFLK>OV QOR@H TFII KBSBO LSBOMLTBO VLR >KA PBKA VLR CIVFKD ?>@HT>OAP. 4EB
CLO@B VLR BUBOQ LK FQ FP BNR>I >KA LMMLPFQB QL QEB CLO@B BUBOQBA LK VLRO E>KAP. 4EB LRQ@LJB
ABMBKAP LK > S>OFBQV LC LQEBO C>@QLOP. )C QEB QOR@H FP > PJ>II QOR@H LK >K F@V ALTKEFII, VLRfII
4EB .>QROB LC #LAB (S005)
65
MOL?>?IV ?B >?IB QL DBQ FQ QL JLSB. /K QEB LQEBO E>KA, FC FQfP > SBOV I>ODB QOR@H LK > AFOQ OL>A
>KA VLR MRPE E>OA BKLRDE (J>V?B BSBK Q>HB > ORKKFKD PQ>OQ), VLR @LRIA FKGROB VLRO E>KA.
!KA FC VLR >OB TB>OFKD OLIIBO PH>QBP TEBK VLR MRPE LK QE>Q QOR@H?
9LRfII >@@BIBO>QB >T>V COLJ QEB QOR@H, PIFAFKD >ILKD QEB OL>A TEFIB QEB QOR@H PQ>VP MRQ. 7EV
AL VLR PIFAB ?RQ KLQ QEB QOR@H? &LO LKB, QEB QOR@H E>P > JR@E I>ODBO J>PP (TEF@E TBfII DBQ
FKQL TFQE .BTQLKfP PB@LKA I>T). 4EBOB >OB LQEBO CLO@BP >Q TLOH QLL, K>JBIV QEB COF@QFLK LC
QEB QOR@HfP QFOBP >KA VLRO OLIIBO PH>QBP >D>FKPQ QEB OL>A.
F64B?2 2.2
Newtons Third Law (as seen through the eyes of Processing) Newtons Third Law (as seen through the eyes of Processing)
)C TB @>I@RI>QB > 06B@QLO C QE>Q FP > CLO@B LC L?GB@Q ! LK L?GB@Q ", TB JRPQ
>IPL >MMIV QEB CLO@Bd06B@QLO.JRIQ(C,-1);dQE>Q " BUBOQP LK L?GB@Q !.
7BfII PBB QE>Q FK QEB TLOIA LC 0OL@BPPFKD MOLDO>JJFKD, TB ALKfQ >IT>VP E>SB QL PQ>V QORB QL
QEB >?LSB. 3LJBQFJBP, PR@E >P FK QEB @>PB LC PBB DO>SFQ>QFLK>I >QQO>@QFLK ?BQTBBK ?LAFBP
(PBB M>DB 94), TBfII T>KQ QL JLABI BNR>I >KA LMMLPFQB CLO@BP. /QEBO QFJBP, PR@E >P TEBK
TBfOB PFJMIV P>VFKD, g(BV, QEBOBfP PLJB TFKA FK QEB BKSFOLKJBKQ,h TBfOB KLQ DLFKD QL ?LQEBO
QL JLABI QEB CLO@B QE>Q > ?LAV BUBOQP ?>@H LK QEB >FO. )K C>@Q, TBfOB KLQ JLABIFKD QEB >FO >Q
>II! 2BJBJ?BO, TB >OB PFJMIV Q>HFKD FKPMFO>QFLK COLJ QEB MEVPF@P LC QEB K>QRO>I TLOIA, KLQ
PFJRI>QFKD BSBOVQEFKD TFQE MBOCB@Q MOB@FPFLK.
#E>MQBO 2. &LO@BP
66
2.2 Forces and ProcessingNewtons Second Law 2.2 Forces and ProcessingNewtons Second Law
as a Function as a Function
!KA EBOB TB >OB >Q QEB JLPQ FJMLOQ>KQ I>T CLO QEB 0OL@BPPFKD MOLDO>JJBO.
Newtons Second Law Newtons Second Law
4EFP I>T FP PQ>QBA >P:
&LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.
/O:
F

= M A

7EV FP QEFP QEB JLPQ FJMLOQ>KQ I>T CLO RP? 7BII, IBQfP TOFQB FQ > AFCCBOBKQ T>V.
A

= F

/ M
!@@BIBO>QFLK FP AFOB@QIV MOLMLOQFLK>I QL CLO@B >KA FKSBOPBIV MOLMLOQFLK>I QL J>PP. 4EFP JB>KP
QE>Q FC VLR DBQ MRPEBA, QEB E>OABO VLR >OB MRPEBA, QEB C>PQBO VLRfII JLSB (>@@BIBO>QB). 4EB
?FDDBO VLR >OB, QEB PILTBO VLRfII JLSB.
Weight vs. Mass Weight vs. Mass
i 4EB 8,>> 8,>> LC >K L?GB@Q FP > JB>PROB LC QEB >JLRKQ LC J>QQBO FK QEB L?GB@Q
(JB>PROBA FK HFILDO>JP).
i +0423? +0423?, QELRDE LCQBK JFPQ>HBK CLO J>PP, FP QB@EKF@>IIV QEB CLO@B LC DO>SFQV LK
>K L?GB@Q. &OLJ .BTQLKfP PB@LKA I>T, TB @>K @>I@RI>QB FQ >P J>PP QFJBP QEB
>@@BIBO>QFLK LC DO>SFQV (Q = G * A). 7BFDEQ FP JB>PROBA FK KBTQLKP.
i D09>4?D D09>4?D FP ABCFKBA >P QEB >JLRKQ LC J>PP MBO RKFQ LC SLIRJB (DO>JP MBO @R?F@
@BKQFJBQBO, CLO BU>JMIB).
.LQB QE>Q >K L?GB@Q QE>Q E>P > J>PP LC LKB HFILDO>J LK B>OQE TLRIA E>SB > J>PP LC LKB
HFILDO>J LK QEB JLLK. (LTBSBO, FQ TLRIA TBFDE LKIV LKB-PFUQE >P JR@E.
.LT, FK QEB TLOIA LC 0OL@BPPFKD, TE>Q FP J>PP >KVT>V? !OBKfQ TB AB>IFKD TFQE MFUBIP? 4L PQ>OQ
FK > PFJMIBO MI>@B, IBQfP P>V QE>Q FK LRO MOBQBKA MFUBI TLOIA, >II LC LRO L?GB@QP E>SB > J>PP
BNR>I QL 1. $/ 1 = $. !KA PL:
4EB .>QROB LC #LAB (S005)
67
A

= F

4EB >@@BIBO>QFLK LC >K L?GB@Q FP BNR>I QL CLO@B. 4EFP FP DOB>Q KBTP. !CQBO >II, TB P>T FK
#E>MQBO 1 QE>Q >@@BIBO>QFLK T>P QEB HBV QL @LKQOLIIFKD QEB JLSBJBKQ LC LRO L?GB@QP LK
P@OBBK. ,L@>QFLK FP >AGRPQBA ?V SBIL@FQV, >KA SBIL@FQV ?V >@@BIBO>QFLK. !@@BIBO>QFLK T>P
TEBOB FQ >II ?BD>K. .LT TB IB>OK QE>Q :CF79 FP QORIV TEBOB FQ >II ?BDFKP.
,BQfP Q>HB LRO +IP?L @I>PP, TFQE IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK.
.LT LRO DL>I FP QL ?B >?IB QL >AA CLO@BP QL QEFP L?GB@Q, MBOE>MP P>VFKD:
LO:
TEBOB TFKA >KA DO>SFQV >OB .3?=NILP. !@@LOAFKD QL .BTQLKfP PB@LKA I>T, TB @LRIA
FJMIBJBKQ QEFP CRK@QFLK >P CLIILTP.
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
W
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Newtons second law at its simplest. ;==?F?L;NCIH = @IL=?;
W
2.3 Force Accumulation 2.3 Force Accumulation
4EFP ILLHP MOBQQV DLLA. !CQBO >II, 5779@9F5H=CB = :CF79 FP > IFQBO>I QO>KPI>QFLK LC .BTQLKfP
PB@LKA I>T (TFQELRQ J>PP). .BSBOQEBIBPP, QEBOBfP > MOBQQV ?FD MOL?IBJ EBOB. ,BQfP OBQROK QL
TE>Q TB >OB QOVFKD QL >@@LJMIFPE: @OB>QFKD > JLSFKD L?GB@Q LK QEB P@OBBK QE>Q OBPMLKAP QL
TFKA >KA DO>SFQV.
/H, IBQfP 69 QEB @LJMRQBO CLO > JLJBKQ. &FOPQ, TB @>II ;JJFS$IL=?() TFQE TFKA. !KA PL QEB
+IP?L L?GB@QfP >@@BIBO>QFLK FP KLT >PPFDKBA QEB .3?=NIL QCH>. 3B@LKA, TB @>II
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
GIP?L.OJ>;N?();
GIP?L.>CMJF;S();
#E>MQBO 2. &LO@BP
68
;JJFS$IL=?() TFQE DO>SFQV. .LT QEB +IP?L L?GB@QfP >@@BIBO>QFLK FP PBQ QL QEB DO>SFQV .3?=NIL.
4EFOA, TB @>II OJ>;N?(). 7E>Q E>MMBKP FK OJ>;N?()? !@@BIBO>QFLK FP >AABA QL SBIL@FQV.
7BfOB KLQ DLFKD QL PBB >KV BOOLO FK 0OL@BPPFKD, ?RQ WLFKHP! 7BfSB DLQ > J>GLO MOL?IBJ. 7E>Q
FP QEB S>IRB LC >@@BIBO>QFLK TEBK FQ FP >AABA QL SBIL@FQV? )Q FP BNR>I QL QEB DO>SFQV CLO@B. 7FKA
E>P ?BBK IBCQ LRQ! )C TB @>II ;JJFS$IL=?() JLOB QE>K LK@B, FQ LSBOOFABP B>@E MOBSFLRP @>II.
(LT >OB TB DLFKD QL E>KAIB JLOB QE>K LKB CLO@B?
4EB QORQE LC QEB J>QQBO EBOB FP QE>Q TB PQ>OQBA TFQE > PFJMIFCFBA PQ>QBJBKQ LC .BTQLKfP PB@LKA
I>T. (BOBfP > JLOB >@@RO>QB T>V QL MRQ FQ:
.BQ &LO@B BNR>IP J>PP QFJBP >@@BIBO>QFLK.
/O, >@@BIBO>QFLK FP BNR>I QL QEB GIA C: 5@@ :CF79G AFSFABA ?V J>PP. 4EFP J>HBP MBOCB@Q PBKPB.
!CQBO >II, >P TB P>T FK .BTQLKfP CFOPQ I>T, FC >II QEB CLO@BP >AA RM QL WBOL, >K L?GB@Q
BUMBOFBK@BP >K BNRFIF?OFRJ PQ>QB (F.B. KL >@@BIBO>QFLK). /RO FJMIBJBKQ>QFLK LC QEFP FP QEOLRDE >
MOL@BPP HKLTK >P 1:=.0 ,..@8@7,?4:9 1:=.0 ,..@8@7,?4:9. )QfP >@QR>IIV SBOV PFJMIB; >II TB KBBA QL AL FP >AA >II LC
QEB CLO@BP QLDBQEBO. !Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B 1, 2, 6, 12, LO 303 CLO@BP. !P ILKD >P
LRO L?GB@Q HKLTP ELT QL >@@RJRI>QB QEBJ, FQ ALBPKfQ J>QQBO ELT J>KV CLO@BP >@Q LK FQ.
.LT, TBfOB KLQ CFKFPEBA GRPQ VBQ. &LO@B >@@RJRI>QFLK E>P LKB JLOB MFB@B. 3FK@B TBfOB >AAFKD
>II QEB CLO@BP QLDBQEBO >Q >KV DFSBK JLJBKQ, TB E>SB QL J>HB PROB QE>Q TB @IB>O >@@BIBO>QFLK
(F.B. PBQ FQ QL WBOL) ?BCLOB B>@E QFJB OJ>;N?() FP @>IIBA. ,BQfP QEFKH >?LRQ TFKA CLO > JLJBKQ.
3LJBQFJBP QEB TFKA FP SBOV PQOLKD, PLJBQFJBP FQfP TB>H, >KA PLJBQFJBP QEBOBfP KL TFKA >Q >II.
!Q >KV DFSBK JLJBKQ, QEBOB JFDEQ ?B > ERDB DRPQ LC TFKA, P>V, TEBK QEB RPBO ELIAP ALTK QEB
JLRPB.
7EBK QEB RPBO OBIB>PBP QEB JLRPB, QEB TFKA TFII PQLM, >KA >@@LOAFKD QL .BTQLKfP CFOPQ I>T, QEB
L?GB@Q TFII @LKQFKRB QL JLSB >Q > @LKPQ>KQ SBIL@FQV. (LTBSBO, FC TB E>A CLODLQQBK QL OBPBQ
>@@BIBO>QFLK QL WBOL, QEB DRPQ LC TFKA TLRIA PQFII ?B FK BCCB@Q. %SBK TLOPB, FQ TLRIA >AA LKQL
FQPBIC COLJ QEB MOBSFLRP CO>JB, PFK@B TB >OB >@@RJRI>QFKD CLO@BP! !@@BIBO>QFLK, FK LRO
PFJRI>QFLK, E>P KL JBJLOV; FQ FP PFJMIV @>I@RI>QBA ?>PBA LK QEB BKSFOLKJBKQ>I CLO@BP MOBPBKQ
P?FI=CNS.;>>(;==?F?L;NCIH);
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Newtons second law, but with force
accumulation. We now add each force to
acceleration, one at a time.
;==?F?L;NCIH.;>>(@IL=?);
W
C@ (GIOM?.L?MM?>) U
.3?=NIL QCH> = H?Q .3?=NIL(0.5,0);
GIP?L.;JJFS$IL=?(QCH>);
W
4EB .>QROB LC #LAB (S005)
69
>Q > JLJBKQ FK QFJB. 4EFP FP AFCCBOBKQ QE>K, P>V, IL@>QFLK, TEF@E JRPQ OBJBJ?BO TEBOB QEB
L?GB@Q T>P FK QEB MOBSFLRP CO>JB FK LOABO QL JLSB MOLMBOIV QL QEB KBUQ.
4EB B>PFBPQ T>V QL FJMIBJBKQ @IB>OFKD QEB >@@BIBO>QFLK CLO B>@E CO>JB FP QL JRIQFMIV QEB
.3?=NIL ?V 0 >Q QEB BKA LC OJ>;N?().
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
5PFKD CLO@BP, PFJRI>QB > EBIFRJ-CFIIBA ?>IILLK CIL>QFKD RMT>OA >KA ?LRK@FKD LCC QEB
QLM LC > TFKALT. #>K VLR >AA > TFKA CLO@B QE>Q @E>KDBP LSBO QFJB, MBOE>MP
>@@LOAFKD QL 0BOIFK KLFPB?
Exercise 2.1 Exercise 2.1
2.4 Dealing with Mass 2.4 Dealing with Mass
/+. 7BfSB DLQ LKB QFKV IFQQIB >AAFQFLK QL J>HB ?BCLOB TB >OB ALKB TFQE FKQBDO>QFKD CLO@BP
FKQL LRO +IP?L @I>PP >KA >OB OB>AV QL ILLH >Q BU>JMIBP. !CQBO >II, .BTQLKfP PB@LKA I>T FP
OB>IIV F

= M A

, KLQ A

= F

. )K@LOMLO>QFKD J>PP FP >P B>PV >P >AAFKD >K FKPQ>K@B S>OF>?IB QL


LRO @I>PP, ?RQ TB KBBA QL PMBKA > IFQQIB JLOB QFJB EBOB ?B@>RPB > PIFDEQ @LJMIF@>QFLK TFII
BJBODB.
&FOPQ TB GRPQ KBBA QL >AA J>PP.
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
Adding mass as a float @FI;N G;MM;
#E>MQBO 2. &LO@BP
70
Units of Measurement Units of Measurement
.LT QE>Q TB >OB FKQOLAR@FKD J>PP, FQfP FJMLOQ>KQ QL J>HB > NRF@H KLQB >?LRQ RKFQP LC
JB>PROBJBKQ. )K QEB OB>I TLOIA, QEFKDP >OB JB>PROBA FK PMB@FCF@ RKFQP. 7B P>V QE>Q QTL
L?GB@QP >OB 3 JBQBOP >M>OQ, QEB ?>PB?>II FP JLSFKD >Q > O>QB LC 90 JFIBP MBO ELRO, LO QEFP
?LTIFKD ?>II E>P > J>PP LC 6 HFILDO>JP. !P TBfII PBB I>QBO FK QEFP ?LLH, PLJBQFJBP TB
TFII T>KQ QL Q>HB OB>I-TLOIA RKFQP FKQL @LKPFABO>QFLK. (LTBSBO, FK QEFP @E>MQBO, TBfOB
DLFKD QL FDKLOB QEBJ CLO QEB JLPQ M>OQ. /RO RKFQP LC JB>PROBJBKQ >OB FK MFUBIP (g4EBPB
QTL @FO@IBP >OB 100 MFUBIP >M>OQh) >KA CO>JBP LC >KFJ>QFLK (g4EFP @FO@IB FP JLSFKD >Q >
O>QB LC 2 MFUBIP MBO CO>JBh). )K QEB @>PB LC J>PP, QEBOB FPKfQ >KV RKFQ LC JB>PROBJBKQ CLO
RP QL RPB. 7BfOB GRPQ DLFKD QL J>HB PLJBQEFKD RM. )K QEFP BU>JMIB, TBfOB >O?FQO>OFIV
MF@HFKD QEB KRJ?BO 10. 4EBOB FP KL RKFQ LC JB>PROBJBKQ, QELRDE VLR JFDEQ BKGLV
FKSBKQFKD > RKFQ LC VLRO LTK, IFHB g1 JLLDh LO g1 VROHIB.h )Q PELRIA >IPL ?B KLQBA QE>Q, CLO
ABJLKPQO>QFLK MROMLPBP, TBfII QFB J>PP QL MFUBIP (AO>TFKD, P>V, > @FO@IB TFQE > O>AFRP LC
10). 4EFP TFII >IILT RP QL SFPR>IFWB QEB J>PP LC >K L?GB@Q. )K QEB OB>I TLOIA, ELTBSBO, PFWB
ALBP KLQ ABCFKFQBIV FKAF@>QB J>PP. ! PJ>II JBQ>I ?>II @LRIA E>SB > JR@E EFDEBO J>PP
QE>K > I>ODB ?>IILLK ARB QL FQP EFDEBO ABKPFQV.
->PP FP > P@>I>O (CIL>Q), KLQ > SB@QLO, >P FQfP GRPQ LKB KRJ?BO ABP@OF?FKD QEB >JLRKQ LC J>QQBO FK
>K L?GB@Q. 7B @LRIA ?B C>K@V >?LRQ QEFKDP >KA @LJMRQB QEB >OB> LC > PE>MB >P FQP J>PP, ?RQ
FQfP PFJMIBO QL ?BDFK ?V P>VFKD, g(BV, QEB J>PP LC QEFP L?GB@Q FPjRJ, ) ARKKLjELT >?LRQ 10?h
4EFP FPKfQ PL DOB>Q PFK@B QEFKDP LKIV ?B@LJB FKQBOBPQFKD LK@B TB E>SB L?GB@QP TFQE S>OVFKD
J>PP, ?RQ FQfII DBQ RP PQ>OQBA. 7EBOB ALBP J>PP @LJB FK? 7B RPB FQ TEFIB >MMIVFKD .BTQLKfP
PB@LKA I>T QL LRO L?GB@Q.
9BQ >D>FK, BSBK QELRDE LRO @LAB ILLHP NRFQB OB>PLK>?IB, TB E>SB > C>FOIV J>GLO MOL?IBJ EBOB.
#LKPFABO QEB CLIILTFKD P@BK>OFL TFQE QTL +IP?L L?GB@QP, ?LQE ?BFKD ?ILTK >T>V ?V > TFKA
CLO@B.
+IP?L() U
FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
G;MM = 10.0;
W
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Newtons second law (with force
accumulation and mass)
@IL=?.>CP(G;MM);
;==?F?L;NCIH.;>>(@IL=?);
W
4EB .>QROB LC #LAB (S005)
71
!D>FK, IBQfP 69 QEB @LJMRQBO. /?GB@Q G1 OB@BFSBP QEB TFKA CLO@Bd(1,0)dAFSFABP FQ ?V J>PP
(10), >KA >AAP FQ QL >@@BIBO>QFLK.
C1 ;GK7BI M?D: <EH9;: (1,0)
?L?:;: 8O C7II E< 10: (0.1,0)
/+. -LSFKD LK QL L?GB@Q G2. )Q >IPL OB@BFSBP QEB TFKA CLO@Bd(1,0). 7>FQ. (LIA LK > PB@LKA.
7E>Q FP QEB S>IRB LC QEB TFKA CLO@B? 4>HFKD > @ILPBO ILLH, QEB TFKA CLO@B FP >@QR>IIV
KLTd(0.1,0)!! $L VLR OBJBJ?BO QEFP IFQQIB QFA?FQ >?LRQ TLOHFKD TFQE L?GB@QP? 7EBK VLR M>PP
>K L?GB@Q (FK QEFP @>PB > .3?=NIL) FKQL > CRK@QFLK, VLR >OB M>PPFKD > OBCBOBK@B QL QE>Q L?GB@Q.
)QfP KLQ > @LMV! 3L FC > CRK@QFLK J>HBP > @E>KDB QL QE>Q L?GB@Q (TEF@E, FK QEFP @>PB, FQ ALBP ?V
AFSFAFKD ?V J>PP) QEBK QE>Q L?GB@Q FP MBOJ>KBKQIV @E>KDBA! "RQ TB ALKfQ T>KQ G2 QL OB@BFSB
> CLO@B AFSFABA ?V QEB J>PP LC L?GB@Q G1. 7B T>KQ FQ QL OB@BFSB QE>Q CLO@B FK FQP LOFDFK>I
PQ>QBd(1,0). !KA PL TB JRPQ MOLQB@Q LROPBISBP >KA J>HB > @LMV LC QEB .3?=NIL C ?BCLOB
AFSFAFKD FQ ?V J>PP. &LOQRK>QBIV, QEB .3?=NIL @I>PP E>P > @LKSBKFBKQ JBQELA CLO J>HFKD >
@LMVdA?N(). A?N() OBQROKP > KBT .3?=NIL L?GB@Q TFQE QEB P>JB A>Q>. !KA PL TB @>K
OBSFPB ;JJFS$IL=?() >P CLIILTP:
4EBOBfP >KLQEBO T>V TB @LRIA TOFQB QEB >?LSB CRK@QFLK, RPFKD QEB PQ>QF@ JBQELA >CP(). &LO
EBIM TFQE QEFP BUBO@FPB, OBSFBT PQ>QF@ JBQELAP FK #E>MQBO 1 (PBB M>DB 54).
+IP?L G1 = H?Q +IP?L();
+IP?L G2 = H?Q +IP?L();
.3?=NIL QCH> = H?Q .3?=NIL(1,0);
G1.;JJFS$IL=?(QCH>);
G2.;JJFS$IL=?(QCH>);
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Making a copy of the PVector before using
it!
.3?=NIL @ = @IL=?.A?N();
@.>CP(G;MM);
;==?F?L;NCIH.;>>(@);
W
2BTOFQB QEB ;JJFS$IL=?() JBQELA RPFKD QEB PQ>QF@ JBQELA >CP() FKPQB>A LC A?N().
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
.3?=NIL @ = :::::::.:::(:::::,::::);
;==?F?L;NCIH.;>>(@);
W
Exercise 2.2 Exercise 2.2
#E>MQBO 2. &LO@BP
72
2.5 Creating Forces 2.5 Creating Forces
,BQfP Q>HB > JLJBKQ QL OBJFKA LROPBISBP TEBOB TB >OB. 7B HKLT TE>Q > CLO@B FP (> SB@QLO),
>KA TB HKLT ELT QL >MMIV > CLO@B QL >K L?GB@Q (AFSFAB FQ ?V J>PP >KA >AA FQ QL QEB L?GB@QfP
>@@BIBO>QFLK SB@QLO). 7E>Q >OB TB JFPPFKD? 7BII, TB E>SB VBQ QL CFDROB LRQ ELT TB DBQ > CLO@B
FK QEB CFOPQ MI>@B. 7EBOB AL CLO@BP @LJB COLJ?
)K QEFP @E>MQBO, TBfII ILLH >Q QTL JBQELAP CLO @OB>QFKD CLO@BP FK LRO 0OL@BPPFKD TLOIA.
1. Make up a force! Make up a force! !CQBO >II, VLR >OB QEB MOLDO>JJBO, QEB @OB>QLO LC VLRO TLOIA.
4EBOBfP KL OB>PLK TEV VLR @>KfQ GRPQ J>HB RM > CLO@B >KA >MMIV FQ.
2. Model a force! Model a force! 9BP, CLO@BP BUFPQ FK QEB OB>I TLOIA. !KA MEVPF@P QBUQ?LLHP LCQBK
@LKQ>FK CLOJRI>P CLO QEBPB CLO@BP. 7B @>K Q>HB QEBPB CLOJRI>P, QO>KPI>QB QEBJ FKQL
PLRO@B @LAB, >KA JLABI OB>I-TLOIA CLO@BP FK 0OL@BPPFKD.
4EB B>PFBPQ T>V QL J>HB RM > CLO@B FP QL GRPQ MF@H > KRJ?BO. ,BQfP PQ>OQ TFQE QEB FAB> LC
PFJRI>QFKD TFKA. (LT >?LRQ > TFKA CLO@B QE>Q MLFKQP QL QEB OFDEQ >KA FP C>FOIV TB>H? !PPRJFKD
> +IP?L L?GB@Q J, LRO @LAB TLRIA ILLH IFHB:
4EB OBPRIQ FPKfQ QBOOF?IV FKQBOBPQFKD, ?RQ FQ FP > DLLA MI>@B QL PQ>OQ. 7B @OB>QB > .3?=NIL L?GB@Q,
FKFQF>IFWB FQ, >KA M>PP FQ FKQL >K L?GB@Q (TEF@E FK QROK TFII >MMIV FQ QL FQP LTK >@@BIBO>QFLK). )C TB
T>KQBA QL E>SB QTL CLO@BP, MBOE>MP TFKA >KA DO>SFQV (> ?FQ PQOLKDBO, MLFKQFKD ALTK), TB JFDEQ
TOFQB QEB CLIILTFKD:
Example 2.1
.3?=NIL QCH> = H?Q .3?=NIL(0.01,0);
G.;JJFS$IL=?(QCH>);
.3?=NIL QCH> = H?Q .3?=NIL(0.01,0);
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
G.;JJFS$IL=?(QCH>);
G.;JJFS$IL=?(AL;PCNS);
4EB .>QROB LC #LAB (S005)
73
.LT TB E>SB QTL CLO@BP, MLFKQFKD FK AFCCBOBKQ AFOB@QFLKP TFQE AFCCBOBKQ J>DKFQRABP, ?LQE
>MMIFBA QL L?GB@Q G. 7BfOB ?BDFKKFKD QL DBQ PLJBTEBOB. 7BfSB KLT ?RFIQ > TLOIA CLO LRO
L?GB@QP FK 0OL@BPPFKD, >K BKSFOLKJBKQ QL TEF@E QEBV @>K >@QR>IIV OBPMLKA.
,BQfP ILLH >Q ELT TB @LRIA J>HB QEFP BU>JMIB > ?FQ JLOB BU@FQFKD TFQE J>KV L?GB@QP LC
S>OVFKD J>PP. 4L AL QEFP, TBfII KBBA > NRF@H OBSFBT LC L?GB@Q-LOFBKQBA MOLDO>JJFKD. !D>FK,
TBfOB KLQ @LSBOFKD >II QEB ?>PF@P LC MOLDO>JJFKD EBOB (CLO QE>Q VLR @>K @EB@H LRQ >KV LC QEB
FKQOL 0OL@BPPFKD ?LLHP IFPQBA FK QEB FKQOLAR@QFLK). (LTBSBO, PFK@B QEB FAB> LC @OB>QFKD >
TLOIA CFIIBA TFQE L?GB@QP FP MOBQQV CRKA>JBKQ>I QL >II QEB BU>JMIBP FK QEFP ?LLH, FQfP TLOQE
Q>HFKD > JLJBKQ QL T>IH QEOLRDE QEB PQBMP LC DLFKD COLJ LKB L?GB@Q QL J>KV.
4EFP FP TEBOB TB >OB TFQE QEB +IP?L @I>PP >P > TELIB. .LQF@B ELT FQ FP FABKQF@>I QL QEB +IP?L
@I>PP @OB>QBA FK #E>MQBO 1, TFQE QTL >AAFQFLKPdG;MM >KA > KBT ;JJFS$IL=?() CRK@QFLK.
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
The object now has mass! @FI;N G;MM;
+IP?L() U
And for now, well just set the mass equal
to 1 for simplicity.
G;MM = 1;
FI=;NCIH = H?Q .3?=NIL(30,30);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
W
Newtons second law. PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
Receive a force, divide by mass, and add
to acceleration.
.3?=NIL @ = .3?=NIL.>CP(@IL=?,G;MM);
;==?F?L;NCIH.;>>(@);
W
PIC> OJ>;N?() U
Motion 101 from Chapter 1 P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Now add clearing the acceleration each
time!
;==?F?L;NCIH.GOFN(0);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*16,G;MM*16);
W Scaling the size according to mass.
#E>MQBO 2. &LO@BP
74
.LT QE>Q LRO @I>PP FP PBQ, TB @>K @ELLPB QL @OB>QB, P>V, LKB ERKAOBA +IP?L L?GB@QP TFQE >K
>OO>V.
!KA QEBK TB @>K FKFQF>IFWB >II LC QELPB +IP?L L?GB@QP FK M?NOJ() TFQE > ILLM.
"RQ KLT TB E>SB > PJ>II FPPRB. )C TB OBCBO ?>@H QL QEB +IP?L L?GB@QfP @LKPQOR@QLOj
jTB AFP@LSBO QE>Q BSBOV +IP?L L?GB@Q FP J>AB BU>@QIV QEB P>JB T>V. 7E>Q TB T>KQ >OB +IP?L
L?GB@QP LC S>OVFKD J>PP QE>Q PQ>OQ >Q S>OVFKD IL@>QFLKP. (BOB FP TEBOB TB KBBA QL FK@OB>PB QEB
PLMEFPQF@>QFLK LC LRO @LKPQOR@QLO ?V >AAFKD >ODRJBKQP.
Somewhat arbitrarily, we are deciding that
an object bounces when it hits the edges of
a window.
PIC> =B?=E#>A?M() U
C@ (FI=;NCIH.R > QC>NB) U
FI=;NCIH.R = QC>NB;
P?FI=CNS.R *= -1;
W ?FM? C@ (FI=;NCIH.R < 0) U
P?FI=CNS.R *= -1;
FI=;NCIH.R = 0;
W
C@ (FI=;NCIH.S > B?CABN) U
Even though we said we shouldn't touch
location and velocity directly, there are some
exceptions. Here we are doing so as a quick
and easy way to reverse the direction of our
object when it reaches the edge.
P?FI=CNS.S *= -1;
FI=;NCIH.S = B?CABN;
W
W
W
+IP?L89 GIP?LM = H?Q +IP?L81009;
PIC> M?NOJ() U
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L();
W
W
+IP?L() U
Every object has a mass of 1 and a location
of (30,30).
G;MM = 1;
FI=;NCIH = H?Q .3?=NIL(30,30);
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
W
+IP?L(@FI;N G, @FI;N R , @FI;N S) U
Now setting these variables with arguments G;MM = G;
FI=;NCIH = H?Q .3?=NIL(R,S);
4EB .>QROB LC #LAB (S005)
75
.LQF@B ELT QEB J>PP >KA IL@>QFLK >OB KL ILKDBO PBQ QL E>OA@LABA KRJ?BOP, ?RQ O>QEBO
FKFQF>IFWBA SF> >ODRJBKQP M>PPBA QEOLRDE QEB @LKPQOR@QLO. 4EFP JB>KP TB @>K @OB>QB >
S>OFBQV LC +IP?L L?GB@QP: ?FD LKBP, PJ>II LKBP, LKBP QE>Q PQ>OQ LK QEB IBCQ PFAB LC QEB P@OBBK,
LKBP QE>Q PQ>OQ LK QEB OFDEQ, BQ@.
7FQE >K >OO>V, ELTBSBO, TB T>KQ QL FKFQF>IFWB >II LC QEB L?GB@QP TFQE > ILLM.
&LO B>@E JLSBO @OB>QBA, QEB J>PP FP PBQ QL > O>KALJ S>IRB ?BQTBBK 0.1 >KA 5, QEB PQ>OQFKD
U-IL@>QFLK FP PBQ QL 0, >KA QEB PQ>OQFKD V-IL@>QFLK FP PBQ QL 0. #BOQ>FKIV, QEBOB >OB >II PLOQP LC
T>VP TB JFDEQ @ELLPB QL FKFQF>IFWB QEB L?GB@QP; QEFP FP GRPQ > ABJLKPQO>QFLK LC LKB MLPPF?FIFQV.
/K@B QEB >OO>V LC L?GB@QP FP AB@I>OBA, @OB>QBA, >KA FKFQF>IFWBA, QEB OBPQ LC QEB @LAB FP PFJMIB.
7B ORK QEOLRDE BSBOV L?GB@Q, E>KA QEBJ B>@E QEB CLO@BP FK QEB BKSFOLKJBKQ, >KA BKGLV QEB
PELT.
P?FI=CNS = H?Q .3?=NIL(0,0);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
W
A big Mover on the left side of the window +IP?L G1 = H?Q +IP?L(10,0,B?CABN/2);
A small Mover on the right side of the
window
+IP?L G1 = H?Q +IP?L(0.1,QC>NB,B?CABN/2);
PIC> M?NOJ() U
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
Initializing many Mover objects, all with
random mass (and all starting at 0,0)
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,5),0,0);
W
W
#E>MQBO 2. &LO@BP
76
Example 2.2
.LQB ELT FK QEB >?LSB FJ>DB, QEB PJ>IIBO @FO@IBP OB>@E QEB OFDEQ LC QEB TFKALT C>PQBO QE>K QEB
I>ODBO LKBP. 4EFP FP ?B@>RPB LC LRO CLOJRI>: 5779@9F5H=CB = :CF79 8=J=898 6M A5GG. 4EB I>ODBO
QEB J>PP, QEB PJ>IIBO QEB >@@BIBO>QFLK.
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL QCH> = H?Q .3?=NIL(0.01,0);
Make up two forces. .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
Loop through all objects and apply both
forces to each object.
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9.;JJFS$IL=?(QCH>);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
W
)KPQB>A LC L?GB@QP ?LRK@FKD LCC QEB BADB LC QEB T>II, @OB>QB >K BU>JMIB FK TEF@E >K
FKSFPF?IB CLO@B MRPEBP ?>@H LK QEB L?GB@QP QL HBBM QEBJ FK QEB TFKALT. #>K VLR TBFDEQ
QEB CLO@B >@@LOAFKD QL ELT C>O QEB L?GB@Q FP COLJ >K BADBdF.B., QEB @ILPBO FQ FP, QEB
PQOLKDBO QEB CLO@B?
Exercise 2.3 Exercise 2.3
2.6 Gravity on Earth and Modeling a Force 2.6 Gravity on Earth and Modeling a Force
9LR J>V E>SB KLQF@BA PLJBQEFKD TLBCRIIV FK>@@RO>QB >?LRQ QEFP I>PQ BU>JMIB. 4EB PJ>IIBO QEB
@FO@IB, QEB C>PQBO FQ C>IIP. 4EBOB FP > ILDF@ QL QEFP; >CQBO >II, TB GRPQ PQ>QBA (>@@LOAFKD QL .BTQLKfP
PB@LKA I>T) QE>Q QEB PJ>IIBO QEB J>PP, QEB EFDEBO QEB >@@BIBO>QFLK. "RQ QEFP FP KLQ TE>Q
E>MMBKP FK QEB OB>I TLOIA. )C VLR TBOB QL @IFJ? QL QEB QLM LC QEB ,B>KFKD 4LTBO LC 0FP> >KA
AOLM QTL ?>IIP LC AFCCBOBKQ J>PPBP, TEF@E LKB TFII EFQ QEB DOLRKA CFOPQ? !@@LOAFKD QL IBDBKA,
'>IFIBL MBOCLOJBA QEFP BU>@Q QBPQ FK 1589, AFP@LSBOFKD QE>Q QEBV CBII TFQE QEB P>JB >@@BIBO>QFLK,
EFQQFKD QEB DOLRKA >Q QEB P>JB QFJB. 7EV FP QEFP? !P TB TFII PBB I>QBO FK QEFP @E>MQBO, QEB CLO@B
LC DO>SFQV FP @>I@RI>QBA OBI>QFSB QL >K L?GB@QfP J>PP. 4EB ?FDDBO QEB L?GB@Q, QEB PQOLKDBO QEB
CLO@B. 3L FC QEB CLO@B FP P@>IBA >@@LOAFKD QL J>PP, FQ FP @>K@BIBA LRQ TEBK >@@BIBO>QFLK FP
AFSFABA ?V J>PP. 7B @>K FJMIBJBKQ QEFP FK LRO PHBQ@E O>QEBO B>PFIV ?V JRIQFMIVFKD LRO J>AB-RM
DO>SFQV CLO@B ?V J>PP.
4EB .>QROB LC #LAB (S005)
77
Example 2.3
7EFIB QEB L?GB@QP KLT C>II >Q QEB P>JB O>QB, ?B@>RPB QEB PQOBKDQE LC QEB TFKA CLO@B FP
FKABMBKABKQ LC J>PP, QEB PJ>IIBO L?GB@QP PQFII >@@BIBO>QB QL QEB OFDEQ JLOB NRF@HIV.
->HFKD RM CLO@BP TFII >@QR>IIV DBQ RP NRFQB C>O. 4EB TLOIA LC 0OL@BPPFKD FP > MOBQBKA TLOIA LC
MFUBIP >KA VLR >OB FQP J>PQBO. 3L TE>QBSBO VLR ABBJ >MMOLMOF>QB QL ?B > CLO@B, TBII ?V
DLIIV, QE>QfP QEB CLO@B FQ PELRIA ?B. .BSBOQEBIBPP, QEBOB J>V @LJB > QFJB TEBOB VLR CFKA
VLROPBIC TLKABOFKD: g"RQ ELT ALBP FQ OB>IIV >II TLOH?h
/MBK RM >KV EFDE P@ELLI MEVPF@P QBUQ?LLH >KA VLR TFII CFKA PLJB AF>DO>JP >KA CLOJRI>P
ABP@OF?FKD J>KV AFCCBOBKQ CLO@BPdDO>SFQV, BIB@QOLJ>DKBQFPJ, COF@QFLK, QBKPFLK, BI>PQF@FQV, >KA
JLOB. )K QEFP @E>MQBO TBfOB DLFKD QL ILLH >Q QTL CLO@BPdCOF@QFLK >KA DO>SFQV. 4EB MLFKQ TBfOB
J>HFKD EBOB FP KLQ QE>Q COF@QFLK >KA DO>SFQV >OB CRKA>JBKQ>I CLO@BP QE>Q VLR >IT>VP KBBA QL
E>SB FK VLRO 0OL@BPPFKD PHBQ@EBP. 2>QEBO, TB T>KQ QL BS>IR>QB QEBPB QTL CLO@BP >P @>PB
PQRAFBP CLO QEB CLIILTFKD MOL@BPP:
i 5KABOPQ>KAFKD QEB @LK@BMQ ?BEFKA > CLO@B
i $B@LKPQOR@QFKD QEB CLO@BfP CLOJRI> FKQL QTL M>OQP:
(LT AL TB @LJMRQB QEB CLO@BfP AFOB@QFLK?
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0);
@FI;N G = GIP?LM8C9.G;MM;
Scaling gravity by mass to be more
accurate
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1*G);
GIP?LM8C9.;JJFS$IL=?(QCH>);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
#E>MQBO 2. &LO@BP
78
(LT AL TB @LJMRQB QEB CLO@BfP J>DKFQRAB?
i 4O>KPI>QFKD QE>Q CLOJRI> FKQL 0OL@BPPFKD @LAB QE>Q @>I@RI>QBP > .3?=NIL QL ?B PBKQ
QEOLRDE LRO +IP?L'P ;JJFS$IL=?() CRK@QFLK
)C TB @>K CLIILT QEB >?LSB PQBMP TFQE QTL CLO@BP, QEBK ELMBCRIIV FC VLR BSBO CFKA VLROPBIC
'LLDIFKD g>QLJF@ KR@IBF TB>H KR@IB>O CLO@Bh >Q 3 >.J., VLR TFII E>SB QEB PHFIIP QL Q>HB TE>Q VLR
CFKA >KA >A>MQ FQ CLO 0OL@BPPFKD.
Dealing with formulae Dealing with formulae
/+, FK > JLJBKQ TBfOB DLFKD QL TOFQB LRQ QEB CLOJRI> CLO COF@QFLK. 4EFP FPKfQ QEB CFOPQ QFJB
TBfSB PBBK > CLOJRI> FK QEFP ?LLH; TB GRPQ CFKFPEBA RM LRO AFP@RPPFLK LC .BTQLKfP
PB@LKA I>T, F

= M A

(LO CLO@B = J>PP * >@@BIBO>QFLK). 7B AFAKfQ PMBKA > ILQ LC QFJB


TLOOVFKD >?LRQ QEFP CLOJRI> ?B@>RPB FQfP > KF@B >KA PFJMIB LKB. .BSBOQEBIBPP, FQfP >
P@>OV TLOIA LRQ QEBOB. *RPQ Q>HB > ILLH >Q QEB BNR>QFLK CLO > gKLOJ>Ih AFPQOF?RQFLK, TEF@E
TB @LSBOBA (TFQELRQ ILLHFKD >Q QEB CLOJRI>) FK QEB )KQOLAR@QFLK (PBB M>DB 10).
f (x; ,
2
) =
1
2
e
(x)
2
2
2
7E>Q TBfOB PBBFKD EBOB FP QE>Q CLOJRI>P IFHB QL RPB > ILQ LC PVJ?LIP (NRFQB LCQBK IBQQBOP
COLJ QEB 'OBBH >IME>?BQ). ,BQfP Q>HB > ILLH >Q QEB CLOJRI> CLO COF@QFLK.
Friction

= Nv

)C FQfP ?BBK > TEFIB PFK@B VLRfSB ILLHBA >Q > CLOJRI> COLJ > J>QE LO MEVPF@P QBUQ?LLH,
QEBOB >OB QEOBB HBV MLFKQP QE>Q >OB FJMLOQ>KQ QL @LSBO ?BCLOB TB JLSB LK.
i EA,7@,?0 ?30 =423? >4/0, ,>>429 ?: ?30 701? >4/0. EA,7@,?0 ?30 =423? >4/0, ,>>429 ?: ?30 701? >4/0. 4EFP FP GRPQ IFHB FK @LAB! 7E>Q
TBfOB ALFKD EBOB FP BS>IR>QFKD QEB OFDEQ PFAB LC QEB BNR>QFLK >KA >PPFDKFKD FQ
QL QEB IBCQ. )K QEB @>PB >?LSB, TB T>KQ QL @>I@RI>QB QEB CLO@B LC COF@QFLKdQEB
IBCQ PFAB QBIIP RP TE>Q TB T>KQ QL @>I@RI>QB >KA QEB OFDEQ PFAB QBIIP RP ELT QL
AL FQ.
i A=0 B0 ?,76492 ,-:@? , A0.?:= := , >.,7,=? A=0 B0 ?,76492 ,-:@? , A0.?:= := , >.,7,=? )QfP FJMLOQ>KQ CLO RP QL OB>IFWB QE>Q
FK PLJB @>PBP, TBfII ?B ILLHFKD >Q > SB@QLO; FK LQEBOP, > P@>I>O. &LO BU>JMIB, FK
QEFP @>PB QEB CLO@B LC COF@QFLK FP > SB@QLO. 7B @>K PBB QE>Q ?V QEB >OOLT >?LSB
QEB TLOA gCOF@QFLK.h )Q E>P > J>DKFQRAB >KA AFOB@QFLK. 4EB OFDEQ PFAB LC QEB
BNR>QFLK >IPL E>P > SB@QLO, >P FKAF@>QBA ?V QEB PVJ?LI v

, TEF@E FK QEFP @>PB


PQ>KAP CLO QEB SBIL@FQV RKFQ SB@QLO.
i +309 >D8-:7> ,=0 ;7,.0/ 90C? ?: 0,.3 :?30=, B0 80,9 1:= ?308 ?: -0 +309 >D8-:7> ,=0 ;7,.0/ 90C? ?: 0,.3 :?30=, B0 80,9 1:= ?308 ?: -0
8@7?4;740/. 8@7?4;740/. 4EB CLOJRI> >?LSB >@QR>IIV E>P CLRO BIBJBKQP: -1, P, *, >KA v

. 7B
T>KQ QL JRIQFMIV QEBJ QLDBQEBO >KA OB>A QEB CLOJRI> >P: Friction

= 1 * * N * v

4EB .>QROB LC #LAB (S005)


79
2.7 Friction 2.7 Friction
,BQfP ?BDFK TFQE COF@QFLK >KA CLIILT LRO PQBMP.
&OF@QFLK FP > /4>>4;,?4A0 1:=.0 /4>>4;,?4A0 1:=.0. ! AFPPFM>QFSB CLO@B FP LKB FK TEF@E QEB QLQ>I BKBODV LC >
PVPQBJ AB@OB>PBP TEBK >K L?GB@Q FP FK JLQFLK. ,BQfP P>V VLR >OB AOFSFKD > @>O. 7EBK VLR
MOBPP VLRO CLLQ ALTK LK QEB ?O>HB MBA>I, QEB @>OfP ?O>HBP RPB COF@QFLK QL PILT ALTK QEB
JLQFLK LC QEB QFOBP. +FKBQF@ BKBODV (JLQFLK) FP @LKSBOQBA FKQL QEBOJ>I BKBODV (EB>Q).
7EBKBSBO QTL PROC>@BP @LJB FKQL @LKQ>@Q, QEBV BUMBOFBK@B COF@QFLK. ! @LJMIBQB JLABI LC
COF@QFLK TLRIA FK@IRAB PBM>O>QB @>PBP CLO PQ>QF@ COF@QFLK (> ?LAV >Q OBPQ >D>FKPQ > PROC>@B) >KA
HFKBQF@ COF@QFLK (> ?LAV FK JLQFLK >D>FKPQ > PROC>@B), ?RQ CLO LRO MROMLPBP, TB >OB LKIV DLFKD
QL ILLH >Q QEB HFKBQF@ @>PB.
(BOBfP QEB CLOJRI> CLO COF@QFLK:
)QfP KLT RM QL RP QL PBM>O>QB QEFP CLOJRI> FKQL QTL @LJMLKBKQP QE>Q ABQBOJFKB QEB AFOB@QFLK
LC COF@QFLK >P TBII >P QEB J>DKFQRAB. ">PBA LK QEB AF>DO>J >?LSB, TB @>K PBB QE>Q :F=7H=CB
DC=BHG =B H<9 CDDCG=H9 8=F97H=CB C: J9@C7=HM. )K C>@Q, QE>QfP QEB M>OQ LC QEB CLOJRI> QE>Q P>VP -1 *
v

, LO -1 QFJBP QEB SBIL@FQV RKFQ SB@QLO. )K 0OL@BPPFKD, QEFP TLRIA JB>K Q>HFKD QEB SBIL@FQV
SB@QLO, KLOJ>IFWFKD FQ, >KA JRIQFMIVFKD ?V -1.
.LQF@B QTL >AAFQFLK>I PQBMP EBOB. &FOPQ, FQfP FJMLOQ>KQ QL J>HB > @LMV LC QEB SBIL@FQV SB@QLO,
>P TB ALKfQ T>KQ QL OBSBOPB QEB L?GB@QfP AFOB@QFLK ?V >@@FABKQ. 3B@LKA, TB KLOJ>IFWB QEB
SB@QLO. 4EFP FP ?B@>RPB QEB J>DKFQRAB LC COF@QFLK FP KLQ >PPL@F>QBA TFQE ELT C>PQ FQ FP JLSFKD,
>KA TB T>KQ QL PQ>OQ TFQE > COF@QFLK SB@QLO LC J>DKFQRAB 1 PL QE>Q FQ @>K B>PFIV ?B P@>IBA.
F64B?2 2.3
.3?=NIL @LC=NCIH = P?FI=CNS.A?N();
@LC=NCIH.HILG;FCT?();
Lets figure out the direction of the friction
force (a unit vector in the opposite direction
of velocity).
@LC=NCIH.GOFN(-1);
#E>MQBO 2. &LO@BP
80
!@@LOAFKD QL QEB CLOJRI>, QEB J>DKFQRAB FP X * ,. X, QEB 'OBBH IBQQBO AI (MOLKLRK@BA gJBTh),
FP RPBA EBOB QL ABP@OF?B QEB .:0114.409? :1 1=4.?4:9 .:0114.409? :1 1=4.?4:9. 4EB @LBCCF@FBKQ LC COF@QFLK BPQ>?IFPEBP QEB
PQOBKDQE LC > COF@QFLK CLO@B CLO > M>OQF@RI>O PROC>@B. 4EB EFDEBO FQ FP, QEB PQOLKDBO QEB COF@QFLK; QEB
ILTBO, QEB TB>HBO. ! ?IL@H LC F@B, CLO BU>JMIB, TFII E>SB > JR@E ILTBO @LBCCF@FBKQ LC COF@QFLK
QE>K, P>V, P>KAM>MBO. 3FK@B TBfOB FK > MOBQBKA 0OL@BPPFKD TLOIA, TB @>K >O?FQO>OFIV PBQ QEB
@LBCCF@FBKQ ?>PBA LK ELT JR@E COF@QFLK TB T>KQ QL PFJRI>QB.
.LT CLO QEB PB@LKA M>OQ: ,. , OBCBOP QL QEB 9:=8,7 1:=.0 9:=8,7 1:=.0, QEB CLO@B MBOMBKAF@RI>O QL QEB
L?GB@QfP JLQFLK >ILKD > PROC>@B. 4EFKH LC > SBEF@IB AOFSFKD >ILKD > OL>A. 4EB SBEF@IB MRPEBP
ALTK >D>FKPQ QEB OL>A TFQE DO>SFQV, >KA .BTQLKfP QEFOA I>T QBIIP RP QE>Q QEB OL>A FK QROK
MRPEBP ?>@H >D>FKPQ QEB SBEF@IB. 4E>QfP QEB KLOJ>I CLO@B. 4EB DOB>QBO QEB DO>SFQ>QFLK>I CLO@B,
QEB DOB>QBO QEB KLOJ>I CLO@B. !P TBfII PBB FK QEB KBUQ PB@QFLK, DO>SFQV FP >PPL@F>QBA TFQE J>PP,
>KA PL > IFDEQTBFDEQ PMLOQP @>O TLRIA BUMBOFBK@B IBPP COF@QFLK QE>K > J>PPFSB QO>@QLO QO>FIBO
QOR@H. 7FQE QEB AF>DO>J >?LSB, ELTBSBO, TEBOB QEB L?GB@Q FP JLSFKD >ILKD > PROC>@B >Q >K
>KDIB, @LJMRQFKD QEB KLOJ>I CLO@B FP > ?FQ JLOB @LJMIF@>QBA ?B@>RPB FQ ALBPKfQ MLFKQ FK QEB
P>JB AFOB@QFLK >P DO>SFQV. 7BfII KBBA QL HKLT PLJBQEFKD >?LRQ >KDIBP >KA QOFDLKLJBQOV.
!II LC QEBPB PMB@FCF@P >OB FJMLOQ>KQ; ELTBSBO, FK 0OL@BPPFKD, > gDLLA BKLRDEh PFJRI>QFLK @>K
?B >@EFBSBA TFQELRQ QEBJ. 7B @>K, CLO BU>JMIB, J>HB COF@QFLK TLOH TFQE QEB >PPRJMQFLK QE>Q
QEB KLOJ>I CLO@B TFII >IT>VP E>SB > J>DKFQRAB LC 1. 7EBK TB DBQ FKQL QOFDLKLJBQOV FK QEB KBUQ
@E>MQBO, TBfII OBJBJ?BO QL OBQROK QL QEFP NRBPQFLK >KA J>HB LRO COF@QFLK BU>JMIB > ?FQ JLOB
PLMEFPQF@>QBA. 4EBOBCLOB:
.LT QE>Q TB E>SB ?LQE QEB J>DKFQRAB >KA AFOB@QFLK CLO COF@QFLK, TB @>K MRQ FQ >II QLDBQEBOj
j>KA >AA FQ QL LRO gCLO@BPh BU>JMIB, TEBOB J>KV L?GB@QP BUMBOFBK@B TFKA, DO>SFQV, >KA KLT
COF@QFLK:
@FI;N = = 0.01;
@FI;N HILG;F = 1;
@FI;N = = 0.01;
@FI;N HILG;F = 1;
Lets figure out the magnitude of friction
(really just an arbitrary constant).
@FI;N @LC=NCIH+;A = =*HILG;F;
.3?=NIL @LC=NCIH = P?FI=CNS.A?N();
@LC=NCIH.GOFN(-1);
@LC=NCIH.HILG;FCT?();
Take the unit vector and multiply it by
magnitude and we have our force vector!
@LC=NCIH.GOFN(@LC=NCIH+;A);
4EB .>QROB LC #LAB (S005)
81
Example 2.4: Including friction
2RKKFKD QEFP BU>JMIB, VLRfII KLQF@B QE>Q QEB @FO@IBP ALKfQ BSBK J>HB FQ QL QEB OFDEQ PFAB LC QEB
TFKALT. 3FK@B COF@QFLK @LKQFKRLRPIV MRPEBP >D>FKPQ QEB L?GB@Q FK QEB LMMLPFQB AFOB@QFLK LC FQP
JLSBJBKQ, QEB L?GB@Q @LKQFKRLRPIV PILTP ALTK. 4EFP @>K ?B > RPBCRI QB@EKFNRB LO >
MOL?IBJ ABMBKAFKD LK QEB DL>IP LC VLRO SFPR>IFW>QFLK.
%< 3?60A6<; ,6A5 3?60A6<;
PIC> >L;Q() U
<;=EALIOH>(255);
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0);
We could scale by mass to be more
accurate.
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
<BE7J 9 = 0.01;
+0;9JEH <H?9J?ED = CEL;HI4?5.L;BE9?JO.=;J();
<H?9J?ED.CKBJ(-1);
<H?9J?ED.DEHC7B?P;();
<H?9J?ED.CKBJ(9);
Apply the friction force vector to the object. CEL;HI4?5.7FFBO"EH9;(<H?9J?ED);
GIP?LM8C9.;JJFS$IL=?(QCH>);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
W
#E>MQBO 2. &LO@BP
82
#OB>QB ML@HBQP LC COF@QFLK FK > 0OL@BPPFKD PHBQ@E PL QE>Q L?GB@QP LKIV BUMBOFBK@B COF@QFLK
TEBK @OLPPFKD LSBO QELPB ML@HBQP. 7E>Q FC VLR S>OV QEB PQOBKDQE (COF@QFLK @LBCCF@FBKQ) LC
B>@E >OB>? 7E>Q FC VLR J>HB PLJB ML@HBQP CB>QROB QEB LMMLPFQB LC COF@QFLKdF.B., TEBK
VLR BKQBO > DFSBK ML@HBQ VLR >@QR>IIV PMBBA RM FKPQB>A LC PILTFKD ALTK?
Exercise 2.4 Exercise 2.4
2.8 Air and Fluid Resistance 2.8 Air and Fluid Resistance
&OF@QFLK >IPL L@@ROP TEBK > ?LAV M>PPBP QEOLRDE > IFNRFA LO D>P. 4EFP CLO@B E>P J>KV AFCCBOBKQ
K>JBP, >II OB>IIV JB>KFKD QEB P>JB QEFKD: J=G7CIG :CF79, 8F5; :CF79, :@I=8 F9G=GH5B79. 7EFIB QEB
OBPRIQ FP RIQFJ>QBIV QEB P>JB >P LRO MOBSFLRP COF@QFLK BU>JMIBP (QEB L?GB@Q PILTP ALTK), QEB
T>V FK TEF@E TB @>I@RI>QB > AO>D CLO@B TFII ?B PIFDEQIV AFCCBOBKQ. ,BQfP ILLH >Q QEB CLOJRI>:
F
d
=
1
2
v
2
AC
d
v

.LT IBQfP ?OB>H QEFP ALTK >KA PBB TE>Q TB OB>IIV KBBA CLO >K BCCB@QFSB PFJRI>QFLK FK
0OL@BPPFKD, J>HFKD LROPBISBP > JR@E PFJMIBO CLOJRI> FK QEB MOL@BPP.
i
F
d OBCBOP QL 8F5; :CF79, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP FKQL LRO
;JJFS$IL=?() CRK@QFLK.
i - 1/2 FP > @LKPQ>KQ: -0.5. 4EFP FP C>FOIV FOOBIBS>KQ FK QBOJP LC LRO 0OL@BPPFKD TLOIA, >P
TB TFII ?B J>HFKD RM S>IRBP CLO LQEBO @LKPQ>KQP >KVT>V. (LTBSBO, QEB C>@Q QE>Q FQ FP
KBD>QFSB FP FJMLOQ>KQ, >P FQ QBIIP RP QE>Q QEB CLO@B FP FK QEB LMMLPFQB AFOB@QFLK LC
SBIL@FQV (GRPQ >P TFQE COF@QFLK).
F64B?2 2.4
4EB .>QROB LC #LAB (S005)
83
i FP QEB 'OBBH IBQQBO F<C, >KA OBCBOP QL QEB ABKPFQV LC QEB IFNRFA, PLJBQEFKD TB
ALKfQ KBBA QL TLOOV >?LRQ. 7B @>K PFJMIFCV QEB MOL?IBJ >KA @LKPFABO QEFP QL E>SB
> @LKPQ>KQ S>IRB LC 1.
i v OBCBOP QL QEB PMBBA LC QEB L?GB@Q JLSFKD. /+, TBfSB DLQ QEFP LKB! 4EB L?GB@QfP
PMBBA FP QEB J>DKFQRAB LC QEB SBIL@FQV SB@QLO: P?FI=CNS.G;AHCNO>?(). !KA v
2
GRPQ
JB>KP v PNR>OBA LO v * v.
i A OBCBOP QL QEB COLKQ>I >OB> LC QEB L?GB@Q QE>Q FP MRPEFKD QEOLRDE QEB IFNRFA (LO D>P).
!K >BOLAVK>JF@ ,>J?LODEFKF, CLO BU>JMIB, TFII BUMBOFBK@B IBPP >FO OBPFPQ>K@B QE>K
> ?LUV 6LISL. .BSBOQEBIBPP, CLO > ?>PF@ PFJRI>QFLK, TB @>K @LKPFABO LRO L?GB@Q QL
?B PMEBOF@>I >KA FDKLOB QEFP BIBJBKQ.
i
C
d FP QEB @LBCCF@FBKQ LC AO>D, BU>@QIV QEB P>JB >P QEB @LBCCF@FBKQ LC COF@QFLK ( ). 4EFP
FP > @LKPQ>KQ TBfII ABQBOJFKB ?>PBA LK TEBQEBO TB T>KQ QEB AO>D CLO@B QL ?B
PQOLKD LO TB>H.
i v

,LLH C>JFIF>O? )Q PELRIA. 4EFP OBCBOP QL QEB SBIL@FQV RKFQ SB@QLO, F.B.
P?FI=CNS.HILG;FCT?(). *RPQ IFHB TFQE COF@QFLK, AO>D FP > CLO@B QE>Q MLFKQP FK QEB
LMMLPFQB AFOB@QFLK LC SBIL@FQV.
.LT QE>Q TBfSB >K>IVWBA B>@E LC QEBPB @LJMLKBKQP >KA ABQBOJFKBA TE>Q TB KBBA CLO >
PFJMIB PFJRI>QFLK, TB @>K OBAR@B LRO CLOJRI> QL:
LO:
,BQfP FJMIBJBKQ QEFP CLO@B FK LRO +IP?L @I>PP BU>JMIB TFQE LKB >AAFQFLK. 7EBK TB TOLQB LRO
COF@QFLK BU>JMIB, QEB CLO@B LC COF@QFLK T>P >IT>VP MOBPBKQ. 7EBKBSBO >K L?GB@Q T>P JLSFKD,
COF@QFLK TLRIA PILT FQ ALTK. (BOB, IBQfP FKQOLAR@B >K BIBJBKQ QL QEB BKSFOLKJBKQd> gIFNRFAh
QE>Q QEB +IP?L L?GB@QP M>PP QEOLRDE. 4EB *CKOC> L?GB@Q TFII ?B > OB@Q>KDIB >KA TFII HKLT
F64B?2 2.5: &B? @6:=963621 1?.4 3<?02 3<?:B9.
@FI;N = = 0.1;
@FI;N MJ??> = P.G;A();
Part 1 of our formula (magnitude): Cd * v2 @FI;N >L;A+;AHCNO>? = = * MJ??> * MJ??>;
.3?=NIL >L;A = P?FI=CNS.A?N();
Part 2 of our formula (direction): -1 *
velocity
>L;A.GOFN(-1);
>L;A.HILG;FCT?();
Magnitude and direction together! >L;A.GOFN(>L;A+;AHCNO>?);
#E>MQBO 2. &LO@BP
84
>?LRQ FQP IL@>QFLK, TFAQE, EBFDEQ, >KA g@LBCCF@FBKQ LC AO>DhdF.B., FP FQ B>PV CLO L?GB@QP QL JLSB
QEOLRDE FQ (IFHB >FO) LO AFCCF@RIQ (IFHB JLI>PPBP)? )K >AAFQFLK, FQ PELRIA FK@IRAB > CRK@QFLK QL AO>T
FQPBIC LK QEB P@OBBK (>KA QTL JLOB CRK@QFLKP, TEF@E TBfII PBB FK > JLJBKQ).
4EB J>FK MOLDO>J TFII KLT FK@IRAB > *CKOC> L?GB@Q OBCBOBK@B >P TBII >P > IFKB LC @LAB QE>Q
FKFQF>IFWBP QE>Q L?GB@Q.
.LT @LJBP >K FKQBOBPQFKD NRBPQFLK: ELT AL TB DBQ QEB +IP?L L?GB@Q QL Q>IH QL QEB *CKOC>
L?GB@Q? )K LQEBO TLOAP, TB T>KQ QL BUB@RQB QEB CLIILTFKD:
2<9B 5 ACJ9F D5GG9G H<FCI;< 5 @=EI=8 =H 9LD9F=9B79G 5 8F5; :CF79.
jLO FK L?GB@Q-LOFBKQBA PMB>H (>PPRJFKD TB >OB ILLMFKD QEOLRDE >K >OO>V LC +IP?L L?GB@QP TFQE
FKABU F):
=F;MM *CKOC> U
The liquid object includes a variable defining
its coefficient of drag.
@FI;N R,S,Q,B;
@FI;N =;
*CKOC>(@FI;N R:, @FI;N S:, @FI;N Q:, @FI;N B:, @FI;N =:) U
R = R:;
S = S:;
Q = Q:;
B = B:;
= = =:;
W
PIC> >CMJF;S() U
HI0NLIE?();
@CFF(175);
L?=N(R,S,Q,B);
W
W
*CKOC> FCKOC>;
PIC> M?NOJ() U
FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1);
W
Initialize a Liquid object. Note the coefficient
is low (0.1), otherwise the object would
come to a halt fairly quickly (which may
someday be the effect you want).
C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U
If a Mover is inside a Liquid, apply the drag
force.
GIP?LM8C9.>L;A(FCKOC>);
W
4EB .>QROB LC #LAB (S005)
85
4EB >?LSB @LAB QBIIP RP QE>Q TB KBBA QL >AA QTL CRK@QFLKP QL QEB +IP?L @I>PP: (1) > CRK@QFLK
QE>Q ABQBOJFKBP FC > +IP?L L?GB@Q FP FKPFAB QEB *CKOC> L?GB@Q, >KA (2) > CRK@QFLK QE>Q
@LJMRQBP >KA >MMIFBP > AO>D CLO@B LK QEB +IP?L L?GB@Q.
4EB CFOPQ FP B>PV; TB @>K PFJMIV RPB > @LKAFQFLK>I PQ>QBJBKQ QL ABQBOJFKB FC QEB IL@>QFLK
SB@QLO OBPQP FKPFAB QEB OB@Q>KDIB ABCFKBA ?V QEB IFNRFA.
4EB >L;A() CRK@QFLK FP > ?FQ JLOB @LJMIF@>QBA; ELTBSBO, TBfSB TOFQQBK QEB @LAB CLO FQ
>IOB>AV. 4EFP FP PFJMIV >K FJMIBJBKQ>QFLK LC LRO CLOJRI>. 4EB AO>D CLO@B FP BNR>I QL H<9
7C9::=7=9BH C: 8F5; AI@H=D@=98 6M H<9 GD998 C: H<9 +IP?L GEI5F98 =B H<9 CDDCG=H9 8=F97H=CB C:
J9@C7=HM!
!KA TFQE QEBPB QTL CRK@QFLKP >AABA QL QEB +IP?L @I>PP, TBfOB OB>AV QL MRQ FQ >II QLDBQEBO FK
QEB J>FK Q>?:
<IIF?;H CM'HMC>?(*CKOC> F) U
C@ (FI=;NCIH.R>F.R && FI=;NCIH.R<F.R+F.Q && FI=;NCIH.S>F.S && FI=;NCIH.S<F.S+F.B)
U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
This conditional statement determines if
the PVector location is inside the rectangle
defined by the Liquid class.
PIC> >L;A(*CKOC> F) U
@FI;N MJ??> = P?FI=CNS.G;A();
The forces magnitude: Cd * v~2~ @FI;N >L;A+;AHCNO>? = F.= * MJ??> * MJ??>;
.3?=NIL >L;A = P?FI=CNS.A?N();
>L;A.GOFN(-1);
The force's direction: -1 * velocity >L;A.HILG;FCT?();
Finalize the force: magnitude and direction
together.
>L;A.GOFN(>L;A+;AHCNO>?);
Apply the force. ;JJFS$IL=?(>L;A);
W
#E>MQBO 2. &LO@BP
86
Example 2.5: Fluid Resistance
2RKKFKD QEB BU>JMIB, VLR PELRIA KLQF@B QE>Q TB >OB PFJRI>QFKD ?>IIP C>IIFKD FKQL T>QBO. 4EB
L?GB@QP LKIV PILT ALTK TEBK @OLPPFKD QEOLRDE QEB DO>V >OB> >Q QEB ?LQQLJ LC QEB TFKALT
(OBMOBPBKQFKD QEB IFNRFA). 9LRfII >IPL KLQF@B QE>Q QEB PJ>IIBO L?GB@QP PILT ALTK > DOB>Q AB>I
+IP?L89 GIP?LM = H?Q +IP?L81009;
*CKOC> FCKOC>;
PIC> M?NOJ() U
MCT?(360, 640);
MGIINB();
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,5),0,0);
W
FCKOC> = H?Q *CKOC>(0, B?CABN/2, QC>NB, B?CABN/2, 0.1);
W
PIC> >L;Q() U
<;=EALIOH>(255);
FCKOC>.>CMJF;S();
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
C@ (GIP?LM8C9.CM'HMC>?(FCKOC>)) U
GIP?LM8C9.>L;A(FCKOC>);
W
@FI;N G = 0.1*GIP?LM8C9.G;MM;
Note that we are scaling gravity according to
mass.
.3?=NIL AL;PCNS = H?Q .3?=NIL(0, G);
GIP?LM8C9.;JJFS$IL=?(AL;PCNS);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
GIP?LM8C9.=B?=E#>A?M();
W
W
4EB .>QROB LC #LAB (S005)
87
JLOB QE>K QEB I>ODBO L?GB@QP. 2BJBJ?BO .BTQLKfP PB@LKA I>T? A = $ / +. !@@BIBO>QFLK
BNR>IP CLO@B 8=J=898 ?V J>PP. ! J>PPFSB L?GB@Q TFII >@@BIBO>QB IBPP. ! PJ>IIBO L?GB@Q TFII
>@@BIBO>QB JLOB. )K QEFP @>PB, QEB >@@BIBO>QFLK TBfOB Q>IHFKD >?LRQ FP QEB gPILTFKD ALTKh
ARB QL AO>D. 4EB PJ>IIBO L?GB@QP TFII PILT ALTK >Q > DOB>QBO O>QB QE>K QEB I>ODBO LKBP.
4>HB > ILLH >Q LRO CLOJRI> CLO AO>D >D>FK: /=,2 1:=.0 = .:0114.409? * >;00/ * >;00/ /=,2 1:=.0 = .:0114.409? * >;00/ * >;00/.
4EB C>PQBO >K L?GB@Q JLSBP, QEB DOB>QBO QEB AO>D CLO@B >D>FKPQ FQ. )K C>@Q, >K L?GB@Q KLQ
JLSFKD FK T>QBO BUMBOFBK@BP KL AO>D >Q >II. %UM>KA QEB BU>JMIB QL AOLM QEB ?>IIP
COLJ AFCCBOBKQ EBFDEQP. (LT ALBP QEFP >CCB@Q QEB AO>D >P QEBV EFQ QEB T>QBO?
Exercise 2.5 Exercise 2.5
4EB CLOJRI> CLO AO>D >IPL FK@IRABA PROC>@B >OB>. #>K VLR @OB>QB > PFJRI>QFLK LC ?LUBP
C>IIFKD FKQL T>QBO TFQE > AO>D CLO@B ABMBKABKQ LK QEB IBKDQE LC QEB PFAB EFQQFKD QEB
T>QBO?
Exercise 2.6 Exercise 2.6
&IRFA OBPFPQ>K@B ALBP KLQ LKIV TLOH LMMLPFQB QL QEB SBIL@FQV SB@QLO, ?RQ >IPL
MBOMBKAF@RI>O QL FQ. 4EFP FP HKLTK >P gIFCQ-FKAR@BA AO>Dh >KA TFII @>RPB >K >FOMI>KB
TFQE >K >KDIBA TFKD QL OFPB FK >IQFQRAB. 4OV @OB>QFKD > PFJRI>QFLK LC IFCQ.
Exercise 2.7 Exercise 2.7
2.9 Gravitational Attraction 2.9 Gravitational Attraction
0OL?>?IV QEB JLPQ C>JLRP CLO@B LC >II FP
DO>SFQV. 7B ERJ>KP LK B>OQE QEFKH LC
DO>SFQV >P >K >MMIB EFQQFKD )P>>@ .BTQLK LK
QEB EB>A. 'O>SFQV JB>KP QE>Q PQRCC C>IIP
ALTK. "RQ QEFP FP LKIV CIF BUMBOFBK@B LC
DO>SFQV. )K QORQE, GRPQ >P QEB B>OQE MRIIP QEB
>MMIB QLT>OAP FQ ARB QL > DO>SFQ>QFLK>I
CLO@B, QEB >MMIB MRIIP QEB B>OQE >P TBII. 4EB
QEFKD FP, QEB B>OQE FP GRPQ PL COB>HFKD ?FD
QE>Q FQ LSBOTEBIJP >II QEB LQEBO DO>SFQV
FKQBO>@QFLKP. %SBOV L?GB@Q TFQE J>PP BUBOQP
> DO>SFQ>QFLK>I CLO@B LK BSBOV LQEBO L?GB@Q.
F64B?2 2.6
#E>MQBO 2. &LO@BP
88
!KA QEBOB FP > CLOJRI> CLO @>I@RI>QFKD QEB PQOBKDQEP LC QEBPB CLO@BP, >P ABMF@QBA FK &FDROB 2.6.
,BQfP BU>JFKB QEFP CLOJRI> > ?FQ JLOB @ILPBIV.
i $ OBCBOP QL QEB DO>SFQ>QFLK>I CLO@B, QEB SB@QLO TB RIQFJ>QBIV T>KQ QL @LJMRQB >KA M>PP
FKQL LRO ;JJFS$IL=?() CRK@QFLK.
i % FP QEB IB=J9FG5@ ;F5J=H5H=CB5@ 7CBGH5BH, TEF@E FK LRO TLOIA BNR>IP 6.67428 U 10
-11
JBQBOP @R?BA MBO HFILDO>J MBO PB@LKA PNR>OBA. 4EFP FP > MOBQQV FJMLOQ>KQ KRJ?BO FC
VLRO K>JB FP )P>>@ .BTQLK LO !I?BOQ %FKPQBFK. )QfP KLQ >K FJMLOQ>KQ KRJ?BO FC VLR >OB
> 0OL@BPPFKD MOLDO>JJBO. !D>FK, FQfP > @LKPQ>KQ QE>Q TB @>K RPB QL J>HB QEB CLO@BP
FK LRO TLOIA TB>HBO LO PQOLKDBO. *RPQ J>HFKD FQ BNR>I QL LKB >KA FDKLOFKD FQ FPKfQ PR@E
> QBOOF?IB @ELF@B BFQEBO.
i G
1
>KA G
2
>OB QEB J>PPBP LC L?GB@QP 1 >KA 2. !P TB P>T TFQE .BTQLKfP PB@LKA I>T (
F

= M A

), J>PP FP >IPL PLJBQEFKD TB @LRIA @ELLPB QL FDKLOB. !CQBO >II, PE>MBP AO>TK
LK QEB P@OBBK ALKfQ >@QR>IIV E>SB > MEVPF@>I J>PP. (LTBSBO, FC TB HBBM QEBPB
S>IRBP, TB @>K @OB>QB JLOB FKQBOBPQFKD PFJRI>QFLKP FK TEF@E g?FDDBOh L?GB@QP BUBOQ >
PQOLKDBO DO>SFQ>QFLK>I CLO@B QE>K PJ>IIBO LKBP.
i r

OBCBOP QL QEB RKFQ SB@QLO MLFKQFKD COLJ L?GB@Q 1 QL L?GB@Q 2. !P TBfII PBB FK >
JLJBKQ, TB @>K @LJMRQB QEFP AFOB@QFLK SB@QLO ?V PR?QO>@QFKD QEB IL@>QFLK LC LKB
L?GB@Q COLJ QEB LQEBO.
i L
2
OBCBOP QL QEB AFPQ>K@B ?BQTBBK QEB QTL L?GB@QP PNR>OBA. ,BQfP Q>HB > JLJBKQ QL
QEFKH >?LRQ QEFP > ?FQ JLOB. 7FQE BSBOVQEFKD LK QEB QLM LC QEB CLOJRI>d%, G
1
, G
2
dQEB
?FDDBO FQP S>IRB, QEB PQOLKDBO QEB CLO@B. "FD J>PP, ?FD CLO@B. "FD %, ?FD CLO@B. .LT,
TEBK TB AFSFAB ?V PLJBQEFKD, TB E>SB QEB LMMLPFQB. 4EB PQOBKDQE LC QEB CLO@B FP
FKSBOPBIV MOLMLOQFLK>I QL QEB AFPQ>K@B PNR>OBA. 4EB :5FH<9F 5K5M >K L?GB@Q FP, QEB
K95?9F QEB CLO@B; QEB 7@CG9F, QEB GHFCB;9F.
(LMBCRIIV ?V KLT QEB CLOJRI> J>HBP PLJB PBKPB QL RP. 7BfSB ILLHBA >Q > AF>DO>J >KA
AFPPB@QBA QEB FKAFSFAR>I @LJMLKBKQP LC QEB CLOJRI>. .LT FQfP QFJB QL CFDROB LRQ ELT TB
QO>KPI>QB QEB J>QE FKQL 0OL@BPPFKD @LAB. ,BQfP J>HB QEB CLIILTFKD >PPRJMQFLKP.
7B E>SB QTL L?GB@QP, >KA:
1. %>@E L?GB@Q E>P > IL@>QFLK: .3?=NIL FI=;NCIH1 >KA .3?=NIL FI=;NCIH2.
2. %>@E L?GB@Q E>P > J>PP: @FI;N G;MM1 >KA @FI;N G;MM2.
3. 4EBOB FP > S>OF>?IB @FI;N % CLO QEB RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ.
'FSBK QEBPB >PPRJMQFLKP, TB T>KQ QL @LJMRQB .3?=NIL @IL=?, QEB CLO@B LC DO>SFQV. 7BfII AL FQ
FK QTL M>OQP. &FOPQ, TBfII @LJMRQB QEB AFOB@QFLK LC QEB CLO@B r

FK QEB CLOJRI> >?LSB. 3B@LKA,


TBfII @>I@RI>QB QEB PQOBKDQE LC QEB CLO@B >@@LOAFKD QL QEB J>PPBP >KA AFPQ>K@B.
4EB .>QROB LC #LAB (S005)
89
2BJBJ?BO FK #E>MQBO 1 (PBB M>DB 56),
TEBK TB CFDROBA LRQ ELT QL E>SB >K L?GB@Q
>@@BIBO>QB QLT>OAP QEB JLRPB? (3BB &FDROB
2.7.)
! SB@QLO FP QEB AFCCBOBK@B ?BQTBBK QTL
MLFKQP. 4L J>HB > SB@QLO QE>Q MLFKQP COLJ
QEB @FO@IB QL QEB JLRPB, TB PFJMIV PR?QO>@Q
LKB MLFKQ COLJ >KLQEBO:
)K LRO @>PB, QEB AFOB@QFLK LC QEB >QQO>@QFLK CLO@B QE>Q L?GB@Q 1 BUBOQP LK L?GB@Q 2 FP BNR>I QL:
$LKfQ CLODBQ QE>Q PFK@B TB T>KQ > RKFQ SB@QLO, > SB@QLO QE>Q QBIIP RP >?LRQ AFOB@QFLK LKIV, TBfII
KBBA QL BCFA5@=N9 QEB SB@QLO >CQBO PR?QO>@QFKD QEB IL@>QFLKP.
/+, TBfSB DLQ QEB AFOB@QFLK LC QEB CLO@B. .LT TB GRPQ KBBA QL @LJMRQB QEB J>DKFQRAB >KA
P@>IB QEB SB@QLO >@@LOAFKDIV.
4EB LKIV MOL?IBJ FP QE>Q TB ALKfQ HKLT QEB
AFPQ>K@B. %, G;MM1, >KA G;MM2 TBOB >II
DFSBKP, ?RQ TBfII KBBA QL >@QR>IIV @LJMRQB
AFPQ>K@B ?BCLOB QEB >?LSB @LAB TFII TLOH.
$FAKfQ TB GRPQ J>HB > SB@QLO QE>Q MLFKQP >II
QEB T>V COLJ LKB IL@>QFLK QL >KLQEBO?
7LRIAKfQ QEB IBKDQE LC QE>Q SB@QLO ?B QEB
AFPQ>K@B ?BQTBBK QTL L?GB@QP?
7BII, FC TB >AA GRPQ LKB IFKB LC @LAB >KA
DO>? QEB J>DKFQRAB LC QE>Q SB@QLO ?BCLOB
KLOJ>IFWFKD FQ, QEBK TBfII E>SB QEB AFPQ>K@B.
F64B?2 2.7
.3?=NIL >CL = .3?=NIL.MO<(GIOM?,FI=;NCIH);
.3?=NIL >CL = .3?=NIL.MO<(FI=;NCIH1,FI=;NCIH2);
>CL.HILG;FCT?();
@FI;N G = (% * G;MM1 * G;MM2) / (>CMN;H=? * >CMN;H=?);
>CL.GOFN(G);
F64B?2 2.8
The vector that points from one object to
another
.3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH1,FI=;NCIH2);
The length (magnitude) of that vector is the
distance between the two objects.
@FI;N >CMN;H=? = @IL=?.G;AHCNO>?();
Use the formula for gravity to compute the
strength of the force.
@FI;N G = (% * G;MM1 * G;MM2) / (>CMN;H=? *
>CMN;H=?);
#E>MQBO 2. &LO@BP
90
.LQB QE>Q ) >IPL OBK>JBA QEB .3?=NIL gAFOh >P gCLO@B.h !CQBO >II, TEBK TBfOB CFKFPEBA TFQE QEB
@>I@RI>QFLKP, QEB .3?=NIL TB PQ>OQBA TFQE BKAP RM ?BFKD QEB >@QR>I CLO@B SB@QLO TB T>KQBA >II
>ILKD.
.LT QE>Q TBfSB TLOHBA LRQ QEB J>QE >KA QEB @LAB CLO @>I@RI>QFKD >K >QQO>@QFSB CLO@B
(BJRI>QFKD DO>SFQV), TB KBBA QL QROK LRO >QQBKQFLK QL >MMIVFKD QEFP QB@EKFNRB FK QEB @LKQBUQ LC
>K >@QR>I 0OL@BPPFKD PHBQ@E. )K %U>JMIB 2.1, VLR J>V OB@>II ELT TB @OB>QBA > PFJMIB +IP?L
L?GB@Qd> @I>PP TFQE .3?=NILfP IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK >P TBII >P >K
;JJFS$IL=?(). ,BQfP Q>HB QEFP BU>@Q @I>PP >KA MRQ FQ FK > PHBQ@E TFQE:
i ! PFKDIB +IP?L L?GB@Q.
i ! PFKDIB ANNL;=NIL L?GB@Q (> KBT
@I>PP QE>Q TFII E>SB > CFUBA
IL@>QFLK).
4EB +IP?L L?GB@Q TFII BUMBOFBK@B >
DO>SFQ>QFLK>I MRII QLT>OAP QEB ANNL;=NIL
L?GB@Q, >P FIIRPQO>QBA FK &FDROB 2.9.
7B @>K PQ>OQ ?V J>HFKD QEB KBT ANNL;=NIL
@I>PP SBOV PFJMIBdDFSFKD FQ > IL@>QFLK >KA >
J>PP, >ILKD TFQE > CRK@QFLK QL AFPMI>V FQPBIC
(QVFKD J>PP QL PFWB).
!KA FK LRO J>FK MOLDO>J, TB @>K >AA >K FKPQ>K@B LC QEB ANNL;=NIL @I>PP.
Normalize and scale the force vector to the
appropriate magnitude.
@IL=?.HILG;FCT?();
@IL=?.GOFN(G);
F64B?2 2.9
=F;MM ANNL;=NIL U
Our Attractor is a simple object that doesnt
move. We just need a mass and a location.
@FI;N G;MM;
.3?=NIL FI=;NCIH;
ANNL;=NIL() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
G;MM = 20;
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175,200);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*2,G;MM*2);
W
W
4EB .>QROB LC #LAB (S005)
91
4EFP FP > DLLA PQOR@QROB: > J>FK MOLDO>J TFQE > +IP?L >KA >K ANNL;=NIL L?GB@Q, >KA > @I>PP
QL E>KAIB QEB S>OF>?IBP >KA ?BE>SFLOP LC JLSBOP >KA >QQO>@QLOP. 4EB I>PQ MFB@B LC QEB MRWWIB
FP ELT QL DBQ LKB L?GB@Q QL >QQO>@Q QEB LQEBO. (LT AL TB DBQ QEBPB QTL L?GB@QP QL Q>IH QL
B>@E LQEBO?
4EBOB >OB > KRJ?BO LC T>VP TB @LRIA AL QEFP. (BOB >OB GRPQ > CBT MLPPF?FIFQFBP.
Task Task Function Function
1. ! CRK@QFLK QE>Q OB@BFSBP ?LQE >K !QQO>@QLO >KA > -LSBO: ;NNL;=NCIH(;,G);
2. ! CRK@QFLK FK QEB !QQO>@QLO @I>PP QE>Q OB@BFSBP > -LSBO: ;.;NNL;=N(G);
3. ! CRK@QFLK FK QEB -LSBO @I>PP QE>Q OB@BFSBP >K !QQO>@QLO: G.;NNL;=N?>1I(;);
4. ! CRK@QFLK FK QEB !QQO>@QLO @I>PP QE>Q OB@BFSBP > -LSBO
>KA OBQROKP > 06B@QLO, TEF@E FP QEB >QQO>@QFLK CLO@B. 4E>Q
>QQO>@QFLK CLO@B FP QEBK M>PPBA FKQL QEB -LSBO'P
>MMIV&LO@B() CRK@QFLK:
.3?=NIL @ = ;.;NNL;=N(G);
G.;JJFS$IL=?(@);
>KA PL LK. . .
)QfP DLLA QL ILLH >Q > O>KDB LC LMQFLKP CLO J>HFKD L?GB@QP Q>IH QL B>@E LQEBO, >KA VLR @LRIA
MOL?>?IV J>HB >ODRJBKQP CLO B>@E LC QEB >?LSB MLPPF?FIFQFBP. )fA IFHB QL >Q IB>PQ AFP@>OA QEB
CFOPQ LKB, PFK@B >K L?GB@Q-LOFBKQBA >MMOL>@E FP OB>IIV > JR@E ?BQQBO @ELF@B LSBO >K >O?FQO>OV
CRK@QFLK KLQ QFBA QL BFQEBO QEB +IP?L LO ANNL;=NIL @I>PP. 7EBQEBO VLR MF@H LMQFLK 2 LO
LMQFLK 3 FP QEB AFCCBOBK@B ?BQTBBK P>VFKD g4EB >QQO>@QLO >QQO>@QP QEB JLSBOh LO g4EB JLSBO
FP >QQO>@QBA QL QEB >QQO>@QLO.h .RJ?BO 4 FP OB>IIV JV C>SLOFQB, >Q IB>PQ FK QBOJP LC TEBOB TB
+IP?L G;
ANNL;=NIL ;;
PIC> M?NOJ() U
MCT?(200,200);
G = H?Q +IP?L();
Initialize Attractor object. ; = H?Q ANNL;=NIL();
W
PIC> >L;Q() U
<;=EALIOH>(255);
Display Attractor object. ;.>CMJF;S();
G.OJ>;N?();
G.>CMJF;S();
W
#E>MQBO 2. &LO@BP
92
>OB FK QEFP ?LLH. !CQBO >II, TB PMBKQ > ILQ LC QFJB TLOHFKD LRQ QEB ;JJFS$IL=?() CRK@QFLK, >KA )
QEFKH LRO BU>JMIBP TFII ?B @IB>OBO FC TB @LKQFKRB TFQE QEB P>JB JBQELALILDV.
)K LQEBO TLOAP, TEBOB TB LK@B E>A:
7B KLT E>SB:
!KA PL LRO >L;Q() CRK@QFLK @>K KLT ?B TOFQQBK >P:
7BfOB >IJLPQ QEBOB. 3FK@B TB AB@FABA QL MRQ QEB ;NNL;=N() CRK@QFLK FKPFAB LC QEB ANNL;=NIL
@I>PP, TBfII KBBA QL >@QR>IIV TOFQB QE>Q CRK@QFLK. 4EB CRK@QFLK KBBAP QL OB@BFSB > +IP?L L?GB@Q
>KA OBQROK > .3?=NIL, F.B.:
!KA TE>Q DLBP FKPFAB QE>Q CRK@QFLK? !II LC QE>Q KF@B J>QE TB TLOHBA LRQ CLO DO>SFQ>QFLK>I
>QQO>@QFLK!
Made-up force .3?=NIL @ = H?Q .3?=NIL(0.1,0);
G.;JJFS$IL=?(@);
Attraction force between two objects +0;9JEH < = 7.7JJH79J(C);
G.;JJFS$IL=?(@);
PIC> >L;Q() U
<;=EALIOH>(255);
Calculate attraction force and apply it. +0;9JEH < = 7.7JJH79J(C);
C.7FFBO"EH9;(<);
G.OJ>;N?();
;.>CMJF;S();
G.>CMJF;S();
W
.3?=NIL ;NNL;=N(+IP?L G) U
W
.3?=NIL ;NNL;=N(+IP?L G) U
Whats the forces direction? .3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH);
@FI;N >CMN;H=? = @IL=?.G;A();
@IL=?.HILG;FCT?();
4EB .>QROB LC #LAB (S005)
93
!KA TBfOB ALKB. 3LOQ LC. !IJLPQ. 4EBOBfP LKB PJ>II HFKH TB KBBA QL TLOH LRQ. ,BQfP ILLH >Q
QEB >?LSB @LAB >D>FK. 3BB QE>Q PVJ?LI CLO AFSFAB, QEB PI>PE? 7EBKBSBO TB E>SB LKB LC
QEBPB, TB KBBA QL >PH LROPBISBP QEB NRBPQFLK: 7E>Q TLRIA E>MMBK FC QEB AFPQ>K@B E>MMBKBA
QL ?B > OB>IIV, OB>IIV PJ>II KRJ?BO LO (BSBK TLOPB!) WBOL??! 7BII, TB HKLT TB @>KfQ AFSFAB >
KRJ?BO ?V 0, >KA FC TB TBOB QL AFSFAB > KRJ?BO ?V PLJBQEFKD IFHB 0.0001, QE>Q FP QEB
BNRFS>IBKQ LC JRIQFMIVFKD QE>Q KRJ?BO ?V 10,000! 9BP, QEFP FP QEB OB>I-TLOIA CLOJRI> CLO QEB
PQOBKDQE LC DO>SFQV, ?RQ TB ALKfQ IFSB FK QEB OB>I TLOIA. 7B IFSB FK QEB ,FC79GG=B; TLOIA. !KA
FK QEB 0OL@BPPFKD TLOIA, QEB JLSBO @LRIA BKA RM ?BFKD SBOV, SBOV @ILPB QL QEB >QQO>@QLO >KA
QEB CLO@B @LRIA ?B@LJB PL PQOLKD QEB JLSBO TLRIA GRPQ CIV T>V LCC QEB P@OBBK. !KA PL TFQE
QEFP CLOJRI>, FQfP DLLA CLO RP QL ?B MO>@QF@>I >KA @LKPQO>FK QEB O>KDB LC TE>Q >CMN;H=? @>K
>@QR>IIV ?B. ->V?B, KL J>QQBO TEBOB QEB +IP?L >@QR>IIV FP, TB PELRIA KBSBO @LKPFABO FQ IBPP
QE>K 5 MFUBIP LO JLOB QE>K 25 MFUBIP >T>V COLJ QEB >QQO>@QLO.
&LO QEB P>JB OB>PLK QE>Q TB KBBA QL @LKPQO>FK QEB JFKFJRJ AFPQ>K@B, FQfP RPBCRI CLO RP QL AL
QEB P>JB TFQE QEB J>UFJRJ. !CQBO >II, FC QEB JLSBO TBOB QL ?B, P>V, 500 MFUBIP COLJ QEB
>QQO>@QLO (KLQ RKOB>PLK>?IB), TBfA ?B AFSFAFKD QEB CLO@B ?V 250,000. 4E>Q CLO@B JFDEQ BKA
RM ?BFKD PL TB>H QE>Q FQfP >IJLPQ >P FC TBfOB KLQ >MMIVFKD FQ >Q >II.
.LT, FQfP OB>IIV RM QL VLR QL AB@FAB TE>Q ?BE>SFLOP VLR T>KQ. "RQ FK QEB @>PB LC, g) T>KQ
OB>PLK>?IB-ILLHFKD >QQO>@QFLK QE>Q FP KBSBO >?PROAIV TB>H LO PQOLKD,h QEBK @LKPQO>FKFKD QEB
AFPQ>K@B FP > DLLA QB@EKFNRB.
/RO +IP?L @I>PP E>PKfQ @E>KDBA >Q >II, PL IBQfP GRPQ ILLH >Q QEB J>FK MOLDO>J >KA QEB
ANNL;=NIL @I>PP >P > TELIB, >AAFKD > S>OF>?IB % CLO QEB RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ. (/K
QEB TB?PFQB, VLRfII CFKA QE>Q QEFP BU>JMIB >IPL E>P @LAB QE>Q >IILTP VLR QL JLSB QEB
ANNL;=NIL L?GB@Q TFQE QEB JLRPB.)
@FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?);
@IL=?.GOFN(MNL?HANB); Whats the forces magnitude?
Return the force so that it can be applied! L?NOLH @IL=?;
W
>CMN;H=? = =IHMNL;CH(>CMN;H=?,5,25);
#E>MQBO 2. &LO@BP
94
Example 2.6: Attraction
A Mover and an Attractor +IP?L G;
ANNL;=NIL ;;
PIC> M?NOJ() U
MCT?(200,200);
G = H?Q +IP?L();
; = H?Q ANNL;=NIL();
W
PIC> >L;Q() U
<;=EALIOH>(255);
Apply the attraction force from the Attractor
on the Mover.
.3?=NIL @IL=? = ;.;NNL;=N(G);
G.;JJFS$IL=?(@IL=?);
G.OJ>;N?();
;.>CMJF;S();
G.>CMJF;S();
W
=F;MM ANNL;=NIL U
@FI;N G;MM;
.3?=NIL FI=;NCIH;
@FI;N %;
ANNL;=NIL() U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
G;MM = 20;
% = 0.4;
W
.3?=NIL ;NNL;=N(+IP?L G) U
.3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH);
@FI;N >CMN;H=? = @IL=?.G;A();
Remember, we need to constrain the
distance so that our circle doesnt spin out of
control.
>CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0);
@IL=?.HILG;FCT?();
@FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?);
@IL=?.GOFN(MNL?HANB);
L?NOLH @IL=?;
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175,200);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,G;MM*2,G;MM*2);
W
W
4EB .>QROB LC #LAB (S005)
95
!KA TB @LRIA, LC @LROPB, BUM>KA QEFP BU>JMIB RPFKD >K >OO>V QL FK@IRAB J>KV +IP?L
L?GB@QP, GRPQ >P TB AFA TFQE COF@QFLK >KA AO>D:
Example 2.7: Attraction with many Movers
Now we have 10 Movers! +IP?L89 GIP?LM = H?Q +IP?L8109;
ANNL;=NIL ;;
PIC> M?NOJ() U
MCT?(400,400);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN));
W
; = H?Q ANNL;=NIL();
W
PIC> >L;Q() U
<;=EALIOH>(255);
;.>CMJF;S();
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
Each Mover is initialized randomly.
We calculate an attraction force for each
Mover object.
.3?=NIL @IL=? = ;.;NNL;=N(GIP?LM8C9);
GIP?LM8C9.;JJFS$IL=?(@IL=?);
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
W
W
#E>MQBO 2. &LO@BP
96
)K QEB BU>JMIB >?LSB, TB E>SB > PVPQBJ (F.B. >OO>V) LC +IP?L L?GB@QP >KA LKB
ANNL;=NIL L?GB@Q. "RFIA >K BU>JMIB QE>Q E>P PVPQBJP LC ?LQE JLSBOP >KA >QQO>@QLOP.
7E>Q FC VLR J>HB QEB >QQO>@QLOP FKSFPF?IB? #>K VLR @OB>QB > M>QQBOK/ABPFDK COLJ QEB
QO>FIP LC L?GB@QP JLSFKD >OLRKA >QQO>@QLOP? 3BB QEB -BQOLMLM $BKFJ MOLGB@Q ?V #I>VQLK
#R?FQQ >KA 4LJ #>OABK (EQQM://MOL@BPPFKD.LOD/BUEF?FQFLK/TLOHP/JBQOLMLM/) CLO >K
BU>JMIB.
Exercise 2.8 Exercise 2.8
)QfP TLOQE KLQFKD QE>Q DO>SFQ>QFLK>I >QQO>@QFLK FP > JLABI TB @>K CLIILT QL ABSBILM LRO
LTK CLO@BP. 4EFP @E>MQBO FPKfQ PRDDBPQFKD QE>Q VLR PELRIA BU@IRPFSBIV @OB>QB PHBQ@EBP
QE>Q RPB DO>SFQ>QFLK>I >QQO>@QFLK. 2>QEBO, VLR PELRIA ?B QEFKHFKD @OB>QFSBIV >?LRQ ELT QL
ABPFDK VLRO LTK ORIBP QL AOFSB QEB ?BE>SFLO LC L?GB@QP. &LO BU>JMIB, TE>Q E>MMBKP FC
VLR ABPFDK > CLO@B QE>Q FP TB>HBO QEB @ILPBO FQ DBQP >KA PQOLKDBO QEB C>OQEBO FQ DBQP? /O
TE>Q FC VLR ABPFDK VLRO >QQO>@QLO QL >QQO>@Q C>O>T>V L?GB@QP, ?RQ OBMBI @ILPB LKBP?
Exercise 2.9 Exercise 2.9
2.10 Everything Attracts (or Repels) Everything 2.10 Everything Attracts (or Repels) Everything
(LMBCRIIV, VLR CLRKA FQ EBIMCRI QE>Q TB PQ>OQBA TFQE > PFJMIB P@BK>OFLdCB9 C6>97H 5HHF57HG
5BCH<9F C6>97Hd>KA JLSBA LK QL CB9 C6>97H 5HHF57HG A5BM C6>97HG. (LTBSBO, FQfP IFHBIV QE>Q
VLR >OB DLFKD QL CFKA VLROPBIC FK > PIFDEQIV JLOB @LJMIBU PFQR>QFLK: A5BM C6>97HG 5HHF57H 957<
CH<9F. )K LQEBO TLOAP, BSBOV L?GB@Q FK > DFSBK PVPQBJ >QQO>@QP BSBOV LQEBO L?GB@Q FK QE>Q PVPQBJ
(BU@BMQ CLO FQPBIC).
7BfSB OB>IIV ALKB >IJLPQ >II LC QEB TLOH CLO QEFP >IOB>AV. ,BQfP @LKPFABO > 0OL@BPPFKD PHBQ@E
TFQE >K >OO>V LC +IP?L L?GB@QP:
4EB .>QROB LC #LAB (S005)
97
4EB >L;Q() CRK@QFLK FP TEBOB TB KBBA QL TLOH PLJB J>DF@. #ROOBKQIV, TBfOB P>VFKD: gCLO
BSBOV JLSBO C, RMA>QB >KA AFPMI>V VLROPBIC.h .LT TE>Q TB KBBA QL P>V FP: gCLO BSBOV JLSBO
C, ?B >QQO>@QBA QL BSBOV LQEBO JLSBO D, >KA RMA>QB >KA AFPMI>V VLROPBIC.h
4L AL QEFP, TB KBBA QL KBPQ > PB@LKA ILLM.
)K QEB MOBSFLRP BU>JMIB, TB E>A >K ANNL;=NIL L?GB@Q TFQE > CRK@QFLK K>JBA ;NNL;=N().
.LT, PFK@B TB E>SB JLSBOP >QQO>@QFKD JLSBOP, >II TB KBBA QL AL FP @LMV QEB ;NNL;=N()
CRK@QFLK FKQL QEB +IP?L @I>PP.
+IP?L89 GIP?LM = H?Q +IP?L8109;
PIC> M?NOJ() U
MCT?(400,400);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN));
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
W
W
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
For every Mover, check every Mover! @IL (CHN D = 0; D < GIP?LM.F?HANB; D++) U
.3?=NIL @IL=? = GIP?LM8D9.;NNL;=N(GIP?LM8C9);
GIP?LM8C9.;JJFS$IL=?(@IL=?);
W
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
W
=F;MM +IP?L U
// AFF NB? INB?L MNO@@ Q? B;> <?@IL? JFOM. . .
#E>MQBO 2. &LO@BP
98
/C @LROPB, QEBOBfP LKB PJ>II MOL?IBJ. 7EBK TB >OB ILLHFKD >Q BSBOV JLSBO C >KA BSBOV JLSBO
D, >OB TB /+ TFQE QEB QFJBP QE>Q C BNR>IP D? &LO BU>JMIB, PELRIA JLSBO #3 >QQO>@Q JLSBO #3?
4EB >KPTBO, LC @LROPB, FP KL. )C QEBOB >OB CFSB L?GB@QP, TB LKIV T>KQ JLSBO #3 QL >QQO>@Q 0, 1, 2,
>KA 4, PHFMMFKD FQPBIC. !KA PL, TB CFKFPE QEFP BU>JMIB ?V >AAFKD > PFJMIB @LKAFQFLK>I PQ>QBJBKQ
QL PHFM >MMIVFKD QEB CLO@B TEBK F BNR>IP G.
Example 2.8: Mutual attraction
The Mover now knows how to attract
another Mover.
.3?=NIL ;NNL;=N(+IP?L G) U
.3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH);
@FI;N >CMN;H=? = @IL=?.G;A();
>CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0);
@IL=?.HILG;FCT?();
@FI;N MNL?HANB = (% * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?);
@IL=?.GOFN(MNL?HANB);
L?NOLH @IL=?;
W
W
+IP?L89 GIP?LM = H?Q +IP?L8209;
@FI;N A = 0.4;
PIC> M?NOJ() U
MCT?(400,400);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
GIP?LM8C9 = H?Q +IP?L(L;H>IG(0.1,2),L;H>IG(QC>NB),L;H>IG(B?CABN));
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (CHN C = 0; C < GIP?LM.F?HANB; C++) U
@IL (CHN D = 0; D < GIP?LM.F?HANB; D++) U
4EB .>QROB LC #LAB (S005)
99
The Ecosystem Project The Ecosystem Project
Step 2 Exercise:
Incorporate the concept of forces into your ecosystem. Try introducing other
elements into the environment (food, a predator) for the creature to interact with.
Does the creature experience attraction or repulsion to things in its world? Can
you think more abstractly and design forces based on the creatures desires or
goals?
Dont attract yourself! C@ (C != D) U
.3?=NIL @IL=? = GIP?LM8D9.;NNL;=N(GIP?LM8C9);
GIP?LM8C9.;JJFS$IL=?(@IL=?);
W
W
GIP?LM8C9.OJ>;N?();
GIP?LM8C9.>CMJF;S();
W
W
#E>KDB QEB >QQO>@QFLK CLO@B FK %U>JMIB 2.8 QL > OBMRIPFLK CLO@B. #>K VLR @OB>QB >K
BU>JMIB FK TEF@E >II LC QEB +IP?L L?GB@QP >OB >QQO>@QBA QL QEB JLRPB, ?RQ OBMBI B>@E
LQEBO? 4EFKH >?LRQ ELT VLR KBBA QL ?>I>K@B QEB OBI>QFSB PQOBKDQE LC QEB CLO@BP >KA
ELT QL JLPQ BCCB@QFSBIV RPB AFPQ>K@B FK VLRO CLO@B @>I@RI>QFLKP.
Exercise 2.10 Exercise 2.10
#E>MQBO 2. &LO@BP
100
Chapter 3. Oscillation Chapter 3. Oscillation
I*?64<;<:2A?F 6@ . @6;2 <3 A52 A6:2@.J
Anonymous
)K #E>MQBOP 1 >KA 2, TB @>OBCRIIV TLOHBA LRQ >K L?GB@Q-LOFBKQBA PQOR@QROB QL J>HB PLJBQEFKD
JLSB LK QEB P@OBBK, RPFKD QEB @LK@BMQ LC > SB@QLO QL OBMOBPBKQ IL@>QFLK, SBIL@FQV, >KA
>@@BIBO>QFLK AOFSBK ?V CLO@BP FK QEB BKSFOLKJBKQ. 7B @LRIA JLSB PQO>FDEQ COLJ EBOB FKQL
QLMF@P PR@E >P M>OQF@IB PVPQBJP, PQBBOFKD CLO@BP, DOLRM ?BE>SFLOP, BQ@. )C TB AFA QE>Q, ELTBSBO,
TBfA PHFM >K FJMLOQ>KQ >OB> LC J>QEBJ>QF@P QE>Q TBfOB DLFKD QL KBBA: ?=42:9:80?=D ?=42:9:80?=D, LO QEB
J>QEBJ>QF@P LC QOF>KDIBP, PMB@FCF@>IIV OFDEQ QOF>KDIBP.
4OFDLKLJBQOV FP DLFKD QL DFSB RP > ILQ LC QLLIP. 7BfII DBQ QL QEFKH >?LRQ >KDIBP >KA >KDRI>O
SBIL@FQV >KA >@@BIBO>QFLK. 4OFD TFII QB>@E RP >?LRQ QEB PFKB >KA @LPFKB CRK@QFLKP, TEF@E TEBK
RPBA MOLMBOIV @>K VFBIA >K KF@B B>PB-FK, B>PB-LRQ T>SB M>QQBOK. )QfP DLFKD QL >IILT RP QL
@>I@RI>QB JLOB @LJMIBU CLO@BP FK >K BKSFOLKJBKQ QE>Q FKSLISBP >KDIBP, PR@E >P > MBKARIRJ
PTFKDFKD LO > ?LU PIFAFKD ALTK >K FK@IFKB.
3L QEFP @E>MQBO FP > ?FQ LC > JFPEJ>PE. 7BfII PQ>OQ TFQE QEB ?>PF@P LC >KDIBP FK 0OL@BPPFKD >KA
@LSBO J>KV QOFDLKLJBQOF@ QLMF@P, QVFKD FQ >II FKQL CLO@BP >Q QEB BKA. !KA ?V Q>HFKD QEFP ?OB>H
KLT, TBfII >IPL M>SB QEB T>V CLO JLOB >AS>K@BA BU>JMIBP QE>Q OBNRFOB QOFD I>QBO FK QEFP ?LLH.
3.1 Angles 3.1 Angles
/+. "BCLOB TB @>K AL >KV LC QEFP PQRCC, TB KBBA QL J>HB PROB TB RKABOPQ>KA TE>Q FQ JB>KP QL
?B >K >KDIB FK 0OL@BPPFKD. )C VLR E>SB BUMBOFBK@B TFQE 0OL@BPPFKD, VLRfSB RKALR?QBAIV
BK@LRKQBOBA QEFP FPPRB TEFIB RPFKD QEB LIN;N?() CRK@QFLK QL OLQ>QB >KA PMFK L?GB@QP.
4EB .>QROB LC #LAB (S005)
101
4EB CFOPQ LOABO LC ?RPFKBPP FP QL @LSBO =,/4,9> =,/4,9> >KA /02=00> /02=00>. 9LRfOB MOL?>?IV C>JFIF>O TFQE
QEB @LK@BMQ LC >K >KDIB FK /02=00> /02=00>. ! CRII OLQ>QFLK DLBP COLJ 0 QL 360 ABDOBBP. 90 ABDOBBP
(> OFDEQ >KDIB) FP 1/4QE LC 360, PELTK ?BILT >P QTL MBOMBKAF@RI>O IFKBP.
)QfP C>FOIV FKQRFQFSB CLO RP QL QEFKH LC >KDIBP FK QBOJP LC ABDOBBP. &LO BU>JMIB, QEB PNR>OB FK
&FDROB 3.2 FP OLQ>QBA 45 ABDOBBP >OLRKA FQP @BKQBO.
0OL@BPPFKD, ELTBSBO, OBNRFOBP >KDIBP QL ?B PMB@FCFBA FK =,/4,9> =,/4,9>. ! O>AF>K FP > RKFQ LC
JB>PROBJBKQ CLO >KDIBP ABCFKBA ?V QEB O>QFL LC QEB IBKDQE LC QEB >O@ LC > @FO@IB QL QEB O>AFRP
LC QE>Q @FO@IB. /KB O>AF>K FP QEB >KDIB >Q TEF@E QE>Q O>QFL BNR>IP LKB (PBB &FDROB 3.1). 180
ABDOBBP = 0) O>AF>KP, 360 ABDOBBP = 2*0) O>AF>KP, 90 ABDOBBP = 0)/2 O>AF>KP, BQ@.
F64B?2 3.1
F64B?2 3.3
#E>MQBO 3. /P@FII>QFLK
102
4EB CLOJRI> QL @LKSBOQ COLJ ABDOBBP QL O>AF>KP FP:
O>AF>KP = 2 * 0) * (ABDOBBP / 360)
4E>KHCRIIV, FC TB MOBCBO QL QEFKH FK ABDOBBP ?RQ @LAB TFQE O>AF>KP, 0OL@BPPFKD J>HBP QEFP B>PV.
4EB L;>C;HM() CRK@QFLK TFII >RQLJ>QF@>IIV @LKSBOQ S>IRBP COLJ ABDOBBP QL O>AF>KP, >KA QEB
@LKPQ>KQP .' >KA 14-:.' MOLSFAB @LKSBKFBKQ >@@BPP QL QEBPB @LJJLKIV RPBA KRJ?BOP
(BNRFS>IBKQ QL 180 >KA 360 ABDOBBP, OBPMB@QFSBIV). 4EB CLIILTFKD @LAB, CLO BU>JMIB, TFII OLQ>QB
PE>MBP ?V 60 ABDOBBP.
)C VLR >OB KLQ C>JFIF>O TFQE ELT OLQ>QFLK FP FJMIBJBKQBA FK 0OL@BPPFKD, ) TLRIA PRDDBPQ QEFP
QRQLOF>I: 0OL@BPPFKD - 4O>KPCLOJ 2$ (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/QO>KPCLOJ2A/).
What is PI? What is PI?
4EB J>QEBJ>QF@>I @LKPQ>KQ MF (LO `) FP > OB>I KRJ?BO ABCFKBA >P QEB O>QFL LC > @FO@IBfP
@FO@RJCBOBK@B (QEB AFPQ>K@B >OLRKA QEB MBOFJBQBO) QL FQP AF>JBQBO (> PQO>FDEQ IFKB QE>Q
M>PPBP QEOLRDE QEB @FO@IBfP @BKQBO). )Q FP BNR>I QL >MMOLUFJ>QBIV 3.14159 >KA @>K ?B
>@@BPPBA FK 0OL@BPPFKD TFQE QEB ?RFIQ-FK S>OF>?IB .'.
F64B?2 3.3
@FI;N ;HAF? = L;>C;HM(60);
LIN;N?(;HAF?);
4EB .>QROB LC #LAB (S005)
103
2LQ>QB > ?>QLK-IFHB L?GB@Q (PBB ?BILT) >OLRKA FQP @BKQBO RPFKD NL;HMF;N?() >KA
LIN;N?().
Exercise 3.1 Exercise 3.1
3.2 Angular Motion 3.2 Angular Motion
2BJBJ?BO >II QEFP PQRCC?
BE97J?ED = BE97J?ED + L;BE9?JO
L;BE9?JO = L;BE9?JO + 799;B;H7J?ED
4EB PQRCC TB ABAF@>QBA >IJLPQ >II LC #E>MQBOP 1 >KA 2 QL? 7BII, TB @>K >MMIV BU>@QIV QEB
P>JB ILDF@ QL > OLQ>QFKD L?GB@Q.
7D=B; = 7D=B; + 7D=KB7H L;BE9?JO
7D=KB7H L;BE9?JO = 7D=KB7H L;BE9?JO + 7D=KB7H 799;B;H7J?ED
)K C>@Q, QEB >?LSB FP >@QR>IIV PFJMIBO QE>K TE>Q TB PQ>OQBA TFQE ?B@>RPB >K >KDIB FP > G75@5F
NR>KQFQVd> PFKDIB KRJ?BO, KLQ > SB@QLO!
5PFKD QEB >KPTBO COLJ %UBO@FPB 3.1 >?LSB, IBQfP P>V TB T>KQBA QL OLQ>QB > ?>QLK FK
0OL@BPPFKD ?V PLJB >KDIB. 7B TLRIA E>SB @LAB IFHB:
!AAFKD FK LRO MOFK@FMIBP LC JLQFLK ?OFKDP RP QL QEB CLIILTFKD BU>JMIB.
NL;HMF;N?(QC>NB/2,B?CABN/2);
LIN;N?(;HAF?);
FCH?(-50,0,50,0);
?FFCJM?(50,0,8,8);
?FFCJM?(-50,0,8,8);
#E>MQBO 3. /P@FII>QFLK
104
Example 3.1: Angular motion using rotate()
4EB ?>QLK PQ>OQP LKP@OBBK TFQE KL OLQ>QFLK >KA QEBK PMFKP C>PQBO >KA C>PQBO >P QEB >KDIB LC
OLQ>QFLK >@@BIBO>QBP.
4EFP FAB> @>K ?B FK@LOMLO>QBA FKQL LRO +IP?L L?GB@Q. &LO BU>JMIB, TB @>K >AA QEB S>OF>?IBP
OBI>QBA QL >KDRI>O JLQFLK QL LRO +IP?L.
Location @FI;N ;HAF? = 0;
Velocity @FI;N ;3?FI=CNS = 0;
Acceleration @FI;N ;A==?F?L;NCIH = 0.001;
PIC> M?NOJ() U
MCT?(200,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
@CFF(175);
MNLIE?(0);
L?=N+I>?(!#,1#/);
NL;HMF;N?(QC>NB/2,B?CABN/2);
LIN;N?(;HAF?);
FCH?(-50,0,50,0);
?FFCJM?(50,0,8,8);
?FFCJM?(-50,0,8,8);
Angular equivalent of
velocity.add(acceleration);
;3?FI=CNS += ;A==?F?L;NCIH;
Angular equivalent of location.add(velocity); ;HAF? += ;3?FI=CNS;
W
4EB .>QROB LC #LAB (S005)
105
!KA QEBK FK OJ>;N?(), TB RMA>QB ?LQE IL@>QFLK >KA >KDIB >@@LOAFKD QL QEB P>JB >IDLOFQEJ!
/C @LROPB, CLO >KV LC QEFP QL J>QQBO, TB >IPL TLRIA KBBA QL OLQ>QB QEB L?GB@Q TEBK AFPMI>VFKD
FQ.
.LT, FC TB TBOB QL >@QR>IIV DL >EB>A >KA ORK QEB >?LSB @LAB, TB TLRIAKfQ PBB >KVQEFKD
KBT. 4EFP FP ?B@>RPB QEB >KDRI>O >@@BIBO>QFLK (@FI;N ;A==?F?L;NCIH = 0;) FP FKFQF>IFWBA QL
WBOL. &LO QEB L?GB@Q QL OLQ>QB, TB KBBA QL DFSB FQ >K >@@BIBO>QFLK! #BOQ>FKIV, TB @LRIA E>OA-
@LAB FK > AFCCBOBKQ KRJ?BO.
=F;MM +IP?L U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N G;MM;
@FI;N ;HAF? = 0;
@FI;N ;3?FI=CNS = 0;
@FI;N ;A==?F?L;NCIH = 0;
PIC> OJ>;N?() U
Regular old-fashioned motion P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Newfangled angular motion ;3?FI=CNS += ;A==?F?L;NCIH;
;HAF? += ;3?FI=CNS;
;==?F?L;NCIH.GOFN(0);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175,200);
L?=N+I>?(!#,1#/);
pushMatrix() and popMatrix() are
necessary so that the rotation of this shape
doesnt affect the rest of our world.
JOMB+;NLCR();
Set the origin at the shapes location. NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S);
Rotate by the angle. LIN;N?(;HAF?);
L?=N(0,0,G;MM*16,G;MM*16);
JIJ+;NLCR();
W
@FI;N ;A==?F?L;NCIH = 0.01;
#E>MQBO 3. /P@FII>QFLK
106
(LTBSBO, TB @>K MOLAR@B > JLOB FKQBOBPQFKD OBPRIQ ?V AVK>JF@>IIV >PPFDKFKD >K >KDRI>O
>@@BIBO>QFLK >@@LOAFKD QL CLO@BP FK QEB BKSFOLKJBKQ. .LT, TB @LRIA EB>A C>O ALTK QEFP OL>A,
QOVFKD QL JLABI QEB MEVPF@P LC >KDRI>O >@@BIBO>QFLK RPFKD QEB @LK@BMQP LC QLONRB
(EQQM://BK.TFHFMBAF>.LOD/TFHF/4LONRB) >KA JLJBKQ LC FKBOQF> (EQQM://BK.TFHFMBAF>.LOD/TFHF/
-LJBKQ=LC=FKBOQF>). .BSBOQEBIBPP, QEFP IBSBI LC PFJRI>QFLK FP ?BVLKA QEB P@LMB LC QEFP ?LLH.
(7B TFII PBB JLOB >?LRQ JLABIFKD >KDRI>O >@@BIBO>QFLK TFQE > MBKARIRJ I>QBO FK QEFP @E>MQBO,
>P TBII >P ILLH >Q ELT "LU2$ OB>IFPQF@>IIV JLABIP OLQ>QFLK>I JLQFLK FK #E>MQBO 5.)
&LO KLT, > NRF@H >KA AFOQV PLIRQFLK TFII AL. 7B @>K MOLAR@B OB>PLK>?IB OBPRIQP ?V PFJMIV
@>I@RI>QFKD >KDRI>O >@@BIBO>QFLK >P > CRK@QFLK LC QEB L?GB@QfP >@@BIBO>QFLK SB@QLO. (BOBfP LKB
PR@E BU>JMIB:
9BP, QEFP FP @LJMIBQBIV >O?FQO>OV. "RQ FQ ALBP AL PLJBQEFKD. )C QEB L?GB@Q FP >@@BIBO>QFKD QL QEB
OFDEQ, FQP >KDRI>O OLQ>QFLK >@@BIBO>QBP FK > @IL@HTFPB AFOB@QFLK; >@@BIBO>QFLK QL QEB IBCQ OBPRIQP FK
> @LRKQBO@IL@HTFPB OLQ>QFLK. /C @LROPB, FQfP FJMLOQ>KQ QL QEFKH >?LRQ P@>IB FK QEFP @>PB. 4EB L
@LJMLKBKQ LC QEB >@@BIBO>QFLK SB@QLO JFDEQ ?B > NR>KQFQV QE>QfP QLL I>ODB, @>RPFKD QEB L?GB@Q
QL PMFK FK > T>V QE>Q ILLHP OFAF@RILRP LO RKOB>IFPQF@. 3L AFSFAFKD QEB L @LJMLKBKQ ?V PLJB
S>IRB, LO MBOE>MP @LKPQO>FKFKD QEB >KDRI>O SBIL@FQV QL > OB>PLK>?IB O>KDB, @LRIA OB>IIV EBIM.
(BOBfP QEB BKQFOB OJ>;N?() CRK@QFLK TFQE QEBPB QTB>HP >AABA.
Example 3.2: Forces with (arbitrary) angular motion
;A==?F?L;NCIH = ;==?F?L;NCIH.R;
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Calculate angular acceleration according to
accelerations horizontal direction and
magnitude.
;A==?F?L;NCIH = ;==?F?L;NCIH.R / 10.0;
;3?FI=CNS += ;A==?F?L;NCIH;
4EB .>QROB LC #LAB (S005)
107
Use constrain() to ensure that angular
velocity doesnt spin out of control.
;3?FI=CNS = =IHMNL;CH(;3?FI=CNS,-0.1,0.1);
;HAF? += ;3?FI=CNS;
;==?F?L;NCIH.GOFN(0);
W
3QBM 1: #OB>QB > PFJRI>QFLK TEBOB L?GB@QP >OB PELQ LRQ LC > @>KKLK. %>@E L?GB@Q
PELRIA BUMBOFBK@B > PRAABK CLO@B TEBK PELQ (GRPQ LK@B) >P TBII >P DO>SFQV (>IT>VP
MOBPBKQ).
3QBM 2: !AA OLQ>QFLK QL QEB L?GB@Q QL JLABI FQP PMFK >P FQ FP PELQ COLJ QEB @>KKLK. (LT
OB>IFPQF@ @>K VLR J>HB FQ ILLH?
Exercise 3.2 Exercise 3.2
3.3 Trigonometry 3.3 Trigonometry
) QEFKH FQ J>V ?B QFJB. 7BfSB ILLHBA >Q >KDIBP, TBfSB PMRK >K L?GB@Q. )QfP QFJB CLO:
GC<75<HC5. 9BP, GC<75<HC5. 4EFP PBBJFKDIV KLKPBKPF@>I TLOA FP >@QR>IIV QEB CLRKA>QFLK CLO >
ILQ LC @LJMRQBO DO>MEF@P TLOH. ! ?>PF@ RKABOPQ>KAFKD LC QOFDLKLJBQOV FP BPPBKQF>I FC VLR
T>KQ QL @>I@RI>QB >K >KDIB, CFDROB LRQ QEB AFPQ>K@B ?BQTBBK MLFKQP, TLOH TFQE @FO@IBP, >O@P,
LO IFKBP. !KA GC<75<HC5 FP > JKBJLKF@ ABSF@B (>I?BFQ > PLJBTE>Q >?PROA LKB) CLO TE>Q QEB
QOFDLKLJBQOF@ CRK@QFLKP PFKB, @LPFKB, >KA Q>KDBKQ JB>K.
i >:3 >:3: PFKB = LMMLPFQB / EVMLQBKRPB
i .,3 .,3: @LPFKB = >AG>@BKQ / EVMLQBKRPB
i ?:, ?:,: Q>KDBKQ = LMMLPFQB / >AG>@BKQ
F64B?2 3.4
#E>MQBO 3. /P@FII>QFLK
108
4>HB > ILLH >Q &FDROB 3.4 >D>FK. 4EBOBfP KL
KBBA QL JBJLOFWB FQ, ?RQ J>HB PROB VLR CBBI
@LJCLOQ>?IB TFQE FQ. $O>T FQ >D>FK VLROPBIC.
.LT IBQfP AO>T FQ > PIFDEQIV AFCCBOBKQ T>V
(&FDROB 3.5).
3BB ELT TB @OB>QB > OFDEQ QOF>KDIB LRQ LC >
SB@QLO? 4EB SB@QLO >OOLT FQPBIC FP QEB
EVMLQBKRPB >KA QEB @LJMLKBKQP LC QEB
SB@QLO (R >KA S) >OB QEB PFABP LC QEB QOF>KDIB.
4EB >KDIB FP >K >AAFQFLK>I JB>KP CLO
PMB@FCVFKD QEB SB@QLOfP AFOB@QFLK (LO
gEB>AFKDh).
"B@>RPB QEB QOFDLKLJBQOF@ CRK@QFLKP >IILT RP QL BPQ>?IFPE > OBI>QFLKPEFM ?BQTBBK QEB
@LJMLKBKQP LC > SB@QLO >KA FQP AFOB@QFLK + J>DKFQRAB, QEBV TFII MOLSB SBOV RPBCRI QEOLRDELRQ
QEFP ?LLH. 7BfII ?BDFK ?V ILLHFKD >Q >K BU>JMIB QE>Q OBNRFOBP QEB Q>KDBKQ CRK@QFLK.
F64B?2 3.5
3.4 Pointing in the Direction of Movement 3.4 Pointing in the Direction of Movement
,BQfP DL >II QEB T>V ?>@H QL %U>JMIB 1.10, TEF@E CB>QROBP > +IP?L L?GB@Q >@@BIBO>QFKD QLT>OAP
QEB JLRPB.
9LR JFDEQ KLQF@B QE>Q >IJLPQ >II LC QEB PE>MBP TBfSB ?BBK AO>TFKD PL C>O >OB @FO@IBP. 4EFP FP
@LKSBKFBKQ CLO > KRJ?BO LC OB>PLKP, LKB LC TEF@E FP QE>Q TB ALKfQ E>SB QL @LKPFABO QEB
NRBPQFLK LC OLQ>QFLK. 2LQ>QB > @FO@IB >KA, TBII, FQ ILLHP BU>@QIV QEB P>JB. (LTBSBO, QEBOB @LJBP
> QFJB FK >II JLQFLK MOLDO>JJBOPf IFSBP TEBK QEBV T>KQ QL AO>T PLJBQEFKD LK QEB P@OBBK QE>Q
MLFKQP FK QEB AFOB@QFLK LC JLSBJBKQ. 0BOE>MP VLR >OB AO>TFKD >K >KQ, LO > @>O, LO > PM>@BPEFM.
!KA TEBK TB P>V "MLFKQ FK QEB AFOB@QFLK LC JLSBJBKQ," TE>Q TB >OB OB>IIV P>VFKD FP gOLQ>QB
>@@LOAFKD QL QEB SBIL@FQV SB@QLO.h 6BIL@FQV FP > SB@QLO, TFQE >K R >KA > S @LJMLKBKQ, ?RQ QL
OLQ>QB FK 0OL@BPPFKD TB KBBA >K >KDIB, FK O>AF>KP. ,BQfP AO>T LRO QOFDLKLJBQOV AF>DO>J LKB
JLOB QFJB, TFQE >K L?GB@QfP SBIL@FQV SB@QLO (&FDROB 3.6).
4EB .>QROB LC #LAB (S005)
109
=: =: H5B;9BH(5) = 6
H<9B H<9B 5 = 5F7H5B;9BH(6)
=: =: H5B;9BH(5B;@9) = J9@C7=HM
M
/ J9@C7=HM
L
H<9B H<9B 5B;@9 = 5F7H5B;9BH(J9@C7=HM
M
/ J9@C7=HM
L
)
/+. 7B HKLT QE>Q QEB ABCFKFQFLK LC Q>KDBKQ
FP:
tangent(angle) =
velocit y
y
velocit y
x
4EB MOL?IBJ TFQE QEB >?LSB FP QE>Q TB
HKLT SBIL@FQV, ?RQ TB ALKfQ HKLT QEB
>KDIB. 7B E>SB QL PLISB CLO QEB >KDIB. 4EFP
FP TEBOB > PMB@F>I CRK@QFLK HKLTK >P
=BJ9FG9 H5B;9BH @LJBP FK, PLJBQFJBP
OBCBOOBA QL >P 5F7H5B;9BH LO H5B
-1
. (4EBOB FP
>IPL >K =BJ9FG9 G=B9 >KA >K =BJ9FG9 7CG=B9.)
)C QEB Q>KDBKQ LC PLJB S>IRB ; BNR>IP PLJB S>IRB <, QEBK QEB FKSBOPB Q>KDBKQ LC < BNR>IP ;.
&LO BU>JMIB:
3BB ELT QE>Q FP QEB FKSBOPB? 4EB >?LSB KLT >IILTP RP QL PLISB CLO QEB >KDIB:
.LT QE>Q TB E>SB QEB CLOJRI>, IBQfP PBB TEBOB FQ PELRIA DL FK LRO JLSBOfP >CMJF;S()
CRK@QFLK. .LQF@B QE>Q FK 0OL@BPPFKD, QEB CRK@QFLK CLO >O@Q>KDBKQ FP @>IIBA ;N;H().
.LT QEB >?LSB @LAB FP MOBQQV A>OK @ILPB, >KA >IJLPQ TLOHP. 7B PQFII E>SB > ?FD MOL?IBJ,
QELRDE. ,BQfP @LKPFABO QEB QTL SBIL@FQV SB@QLOP ABMF@QBA ?BILT.
F64B?2 3.6
PIC> >CMJF;S() U
Solve for angle by using atan(). @FI;N ;HAF? = ;N;H(P?FI=CNS.S/P?FI=CNS.R);
MNLIE?(0);
@CFF(175);
JOMB+;NLCR();
L?=N+I>?(!#,1#/);
NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S);
Rotate according to that angle. LIN;N?(;HAF?);
L?=N(0,0,30,10);
JIJ+;NLCR();
W
#E>MQBO 3. /P@FII>QFLK
110
4ELRDE PRMBOCF@F>IIV PFJFI>O, QEB QTL SB@QLOP MLFKQ FK NRFQB AFCCBOBKQ AFOB@QFLKPdLMMLPFQB
AFOB@QFLKP, FK C>@Q! (LTBSBO, FC TB TBOB QL >MMIV LRO CLOJRI> QL PLISB CLO QEB >KDIB QL B>@E
SB@QLOj
01 Y 7D=B; = 7J7D(-4/3) = 7J7D(-1.25) = -0.9272952 H7:?7DI = -53 :;=H;;I
02 Y 7D=B; = 7J7D(4/-3) = 7J7D(-1.25) = -0.9272952 H7:?7DI = -53 :;=H;;I
jTB DBQ QEB P>JB >KDIB CLO B>@E SB@QLO. 4EFP @>KfQ ?B OFDEQ CLO ?LQE; QEB SB@QLOP MLFKQ FK
LMMLPFQB AFOB@QFLKP! 4EB QEFKD FP, QEFP FP > MOBQQV @LJJLK MOL?IBJ FK @LJMRQBO DO>MEF@P.
2>QEBO QE>K PFJMIV RPFKD ;N;H() >ILKD TFQE > ?RK@E LC @LKAFQFLK>I PQ>QBJBKQP QL >@@LRKQ CLO
MLPFQFSB/KBD>QFSB P@BK>OFLP, 0OL@BPPFKD (>ILKD TFQE MOBQQV JR@E >II MOLDO>JJFKD
BKSFOLKJBKQP) E>P > KF@B CRK@QFLK @>IIBA ;N;H2() QE>Q ALBP FQ CLO VLR.
Example 3.3: Pointing in the direction of motion
F64B?2 3.7
PIC> >CMJF;S() U
Using atan2() to account for all possible
directions
@FI;N ;HAF? = ;N;H2(P?FI=CNS.S,P?FI=CNS.R);
MNLIE?(0);
@CFF(175);
JOMB+;NLCR();
L?=N+I>?(!#,1#/);
NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S);
4EB .>QROB LC #LAB (S005)
111
4L PFJMIFCV QEFP BSBK CROQEBO, QEB .3?=NIL @I>PP FQPBIC MOLSFABP > CRK@QFLK @>IIBA
B?;>CHA2"(), TEF@E Q>HBP @>OB LC @>IIFKD ;N;H2() CLO VLR PL VLR @>K DBQ QEB AFOB@QFLK
>KDIB, FK O>AF>KP, CLO >KV 0OL@BPPFKD .3?=NIL.
Rotate according to that angle. LIN;N?(;HAF?);
L?=N(0,0,30,10);
JIJ+;NLCR();
W
The easiest way to do this! @FI;N ;HAF? = P?FI=CNS.B?;>CHA2"();
#OB>QB > PFJRI>QFLK LC > SBEF@IB QE>Q VLR @>K AOFSB >OLRKA QEB P@OBBK RPFKD QEB >OOLT
HBVP: IBCQ >OOLT >@@BIBO>QBP QEB @>O QL QEB IBCQ, OFDEQ QL QEB OFDEQ. 4EB @>O PELRIA MLFKQ
FK QEB AFOB@QFLK FK TEF@E FQ FP @ROOBKQIV JLSFKD.
Exercise 3.3 Exercise 3.3
3.5 Polar vs. Cartesian Coordinates 3.5 Polar vs. Cartesian Coordinates
!KV QFJB TB AFPMI>V > PE>MB FK 0OL@BPPFKD, TB E>SB QL PMB@FCV > MFUBI IL@>QFLK, > PBQ LC R
>KA S @LLOAFK>QBP. 4EBPB @LLOAFK>QBP >OB HKLTK >P C,=?0>4,9 .::=/49,?0> C,=?0>4,9 .::=/49,?0>, K>JBA CLO
2BKZ $BP@>OQBP, QEB &OBK@E J>QEBJ>QF@F>K TEL ABSBILMBA QEB FAB>P ?BEFKA #>OQBPF>K
PM>@B.
!KLQEBO RPBCRI @LLOAFK>QB PVPQBJ HKLTK >P ;:7,= .::=/49,?0> ;:7,= .::=/49,?0> ABP@OF?BP > MLFKQ FK PM>@B
>P >K >KDIB LC OLQ>QFLK >OLRKA QEB LOFDFK >KA > O>AFRP COLJ QEB LOFDFK. 4EFKHFKD >?LRQ QEFP FK
QBOJP LC > SB@QLO:
#>OQBPF>K @LLOAFK>QBdQEB L,M @LJMLKBKQP LC > SB@QLO
0LI>O @LLOAFK>QBdQEB J>DKFQRAB (IBKDQE) >KA AFOB@QFLK (>KDIB) LC > SB@QLO
0OL@BPPFKDfP AO>TFKD CRK@QFLKP, ELTBSBO, ALKfQ RKABOPQ>KA MLI>O @LLOAFK>QBP. 7EBKBSBO TB
T>KQ QL AFPMI>V PLJBQEFKD FK 0OL@BPPFKD, TB E>SB QL PMB@FCV IL@>QFLKP >P (L,M) #>OQBPF>K
@LLOAFK>QBP. (LTBSBO, PLJBQFJBP FQ FP > DOB>Q AB>I JLOB @LKSBKFBKQ CLO RP QL QEFKH FK MLI>O
@LLOAFK>QBP TEBK ABPFDKFKD. (>MMFIV CLO RP, TFQE QOFDLKLJBQOV TB @>K @LKSBOQ ?>@H >KA
CLOQE ?BQTBBK MLI>O >KA #>OQBPF>K, TEF@E >IILTP RP QL ABPFDK TFQE TE>QBSBO @LLOAFK>QB
PVPQBJ TB E>SB FK JFKA ?RQ >IT>VP AO>T TFQE #>OQBPF>K @LLOAFK>QBP.
#E>MQBO 3. /P@FII>QFLK
112
I?D;(J>;J7) = O/H X O = H * I?D;(J>;J7)
9EI?D;(J>;J7) = N/H X N = H * 9EI?D;(J>;J7)
&LO BU>JMIB, FC L FP 75 >KA NB?N; FP 45 ABDOBBP (LO 0)/4 O>AF>KP), TB @>K @>I@RI>QB R >KA S >P
?BILT. 4EB CRK@QFLKP CLO PFKB >KA @LPFKB FK 0OL@BPPFKD >OB MCH() >KA =IM(), OBPMB@QFSBIV.
4EBV B>@E Q>HB LKB >ODRJBKQ, >K >KDIB JB>PROBA FK O>AF>KP.
4EFP QVMB LC @LKSBOPFLK @>K ?B RPBCRI FK @BOQ>FK >MMIF@>QFLKP. &LO BU>JMIB, QL JLSB > PE>MB
>ILKD > @FO@RI>O M>QE RPFKD #>OQBPF>K @LLOAFK>QBP FP KLQ PL B>PV. 7FQE MLI>O @LLOAFK>QBP, LK
QEB LQEBO E>KA, FQfP PFJMIB: FK@OBJBKQ QEB >KDIB!
(BOBfP ELT FQ FP ALKB TFQE DIL?>I S>OF>?IBP L >KA NB?N;.
F64B?2 3.8: *52 G?228 92AA2? L (A52A.) 6@ <3A2; B@21 A< 12;<A2 .; .;492. )6;02 . =<9.? 0<<?16;.A2 6@
0<;C2;A6<;.99F ?232??21 A< .@ (?, L), D2K99 B@2 A52A. .@ . C.?6./92 ;.:2 D52; ?232??6;4 A< .; .;492.
@FI;N L = 75;
@FI;N NB?N; = .' / 4;
Converting from polar (r,theta) to Cartesian
(x,y)
@FI;N R = L * =IM(NB?N;);
@FI;N S = L * MCH(NB?N;);
4EB .>QROB LC #LAB (S005)
113
Example 3.4: Polar to Cartesian
@FI;N L = 75;
@FI;N NB?N; = 0;
PIC> M?NOJ() U
MCT?(200,200);
<;=EALIOH>(255);
MGIINB();
W
PIC> >L;Q() U
Polar coordinates (r,theta) are converted
to Cartesian (x,y) for use in the ellipse()
function.
@FI;N R = L * =IM(NB?N;);
@FI;N S = L * MCH(NB?N;);
HI0NLIE?();
@CFF(0);
?FFCJM?(R+QC>NB/2, S+B?CABN/2, 16, 16);
NB?N; += 0.01;
W
#E>MQBO 3. /P@FII>QFLK
114
5PFKD %U>JMIB 3.4 >P > ?>PFP, AO>T > PMFO>I M>QE. 3Q>OQ FK QEB @BKQBO >KA JLSB LRQT>OAP.
.LQB QE>Q QEFP @>K ?B ALKB ?V LKIV @E>KDFKD LKB IFKB LC @LAB >KA >AAFKD LKB IFKB LC
@LAB!
Exercise 3.4 Exercise 3.4
3FJRI>QB QEB PM>@BPEFM FK QEB D>JB !PQBOLFAP. )K @>PB VLR >OBKfQ C>JFIF>O TFQE !PQBOLFAP,
EBOB FP > ?OFBC ABP@OFMQFLK: ! PM>@BPEFM (OBMOBPBKQBA >P > QOF>KDIB) CIL>QP FK QTL
AFJBKPFLK>I PM>@B. 4EB IBCQ >OOLT HBV QROKP QEB PM>@BPEFM @LRKQBO@IL@HTFPB, QEB OFDEQ
>OOLT HBV, @IL@HTFPB. 4EB N HBV >MMIFBP > gQEORPQh CLO@B FK QEB AFOB@QFLK QEB PM>@BPEFM FP
MLFKQFKD.
Exercise 3.5 Exercise 3.5
4EB .>QROB LC #LAB (S005)
115
3.6 Oscillation Amplitude and Period 3.6 Oscillation Amplitude and Period
!OB VLR >J>WBA VBQ? 7BfSB PBBK PLJB MOBQQV DOB>Q RPBP LC Q>KDBKQ (CLO CFKAFKD QEB >KDIB LC
> SB@QLO) >KA PFKB >KA @LPFKB (CLO @LKSBOQFKD COLJ MLI>O QL #>OQBPF>K @LLOAFK>QBP). 7B @LRIA
PQLM OFDEQ EBOB >KA ?B P>QFPCFBA. "RQ TBfOB KLQ DLFKD QL. 4EFP FP LKIV QEB ?BDFKKFKD. 7E>Q
PFKB >KA @LPFKB @>K AL CLO VLR DLBP ?BVLKA J>QEBJ>QF@>I CLOJRI>P >KA OFDEQ QOF>KDIBP.
,BQfP Q>HB > ILLH >Q > DO>ME LC QEB PFKB CRK@QFLK, TEBOB M = G=B9(L).
9LRfII KLQF@B QE>Q QEB LRQMRQ LC QEB PFKB CRK@QFLK FP > PJLLQE @ROSB >IQBOK>QFKD ?BQTBBK c1
>KA 1. 4EFP QVMB LC > ?BE>SFLO FP HKLTK >P :>.477,?4:9 :>.477,?4:9, > MBOFLAF@ JLSBJBKQ ?BQTBBK QTL
MLFKQP. 0IR@HFKD > DRFQ>O PQOFKD, PTFKDFKD > MBKARIRJ, ?LRK@FKD LK > MLDL PQF@HdQEBPB >OB
>II BU>JMIBP LC LP@FII>QFKD JLQFLK.
!KA PL TB E>MMFIV AFP@LSBO QE>Q TB @>K PFJRI>QB LP@FII>QFLK FK > 0OL@BPPFKD PHBQ@E ?V
>PPFDKFKD QEB LRQMRQ LC QEB PFKB CRK@QFLK QL >K L?GB@QfP IL@>QFLK. .LQB QE>Q QEFP TFII CLIILT QEB
P>JB JBQELALILDV TB >MMIFBA QL 0BOIFK KLFPB FK QEB )KQOLAR@QFLK (PBB M>DB 17).
,BQfP ?BDFK TFQE > OB>IIV ?>PF@ P@BK>OFL. 7B T>KQ > @FO@IB QL LP@FII>QB COLJ QEB IBCQ PFAB QL QEB
OFDEQ PFAB LC > 0OL@BPPFKD TFKALT.
F64B?2 3.9: F = @6;2(E)
#E>MQBO 3. /P@FII>QFLK
116
4EFP FP TE>Q FP HKLTK >P >48;70 3,=8:94. 8:?4:9 >48;70 3,=8:94. 8:?4:9 (LO, QL ?B C>K@FBO, gQEB MBOFLAF@ PFKRPLFA>I
LP@FII>QFLK LC >K L?GB@Qh). )QfP DLFKD QL ?B > PFJMIB MOLDO>J QL TOFQB, ?RQ ?BCLOB TB DBQ FKQL QEB
@LAB, IBQfP C>JFIF>OFWB LROPBISBP TFQE PLJB LC QEB QBOJFKLILDV LC LP@FII>QFLK (>KA T>SBP).
3FJMIB E>OJLKF@ JLQFLK @>K ?B BUMOBPPBA >P >KV IL@>QFLK (FK LRO @>PB, QEB R IL@>QFLK) >P >
CRK@QFLK LC QFJB, TFQE QEB CLIILTFKD QTL BIBJBKQP:
i A8;74?@/0 A8;74?@/0: 4EB AFPQ>K@B COLJ QEB @BKQBO LC JLQFLK QL BFQEBO BUQOBJB
i %0=4:/ %0=4:/: 4EB >JLRKQ LC QFJB FQ Q>HBP CLO LKB @LJMIBQB @V@IB LC JLQFLK
,LLHFKD >Q QEB DO>ME LC PFKB (&FDROB 3.9), TB @>K PBB QE>Q QEB >JMIFQRAB FP 1 >KA QEB MBOFLA FP
14-:.'; QEB LRQMRQ LC PFKB KBSBO OFPBP >?LSB 1 LO ?BILT -1; >KA BSBOV 14-:.' O>AF>KP (LO 360
ABDOBBP) QEB T>SB M>QQBOK OBMB>QP.
.LT, FK QEB 0OL@BPPFKD TLOIA TB IFSB FK, TE>Q FP >JMIFQRAB >KA TE>Q FP MBOFLA? !JMIFQRAB @>K
?B JB>PROBA O>QEBO B>PFIV FK MFUBIP. )K QEB @>PB LC > TFKALT 200 MFUBIP TFAB, TB TLRIA
LP@FII>QB COLJ QEB @BKQBO 100 MFUBIP QL QEB OFDEQ >KA 100 MFUBIP QL QEB IBCQ. 4EBOBCLOB:
,9F=C8 FP QEB >JLRKQ LC QFJB FQ Q>HBP CLO LKB @V@IB, ?RQ TE>Q FP QFJB FK LRO 0OL@BPPFKD TLOIA? )
JB>K, @BOQ>FKIV TB @LRIA P>V TB T>KQ QEB @FO@IB QL LP@FII>QB BSBOV QEOBB PB@LKAP. !KA TB
@LRIA QO>@H QEB JFIIFPB@LKAPdRPFKD GCFFCM() dFK 0OL@BPPFKD >KA @LJB RM TFQE >K BI>?LO>QB
>IDLOFQEJ CLO LP@FII>QFKD >K L?GB@Q >@@LOAFKD QL OB>I-TLOIA QFJB. "RQ CLO RP, OB>I-TLOIA QFJB
ALBPKfQ OB>IIV J>QQBO. 4EB OB>I JB>PROB LC QFJB FK 0OL@BPPFKD FP FK CO>JBP. 4EB LP@FII>QFKD
JLQFLK PELRIA OBMB>Q BSBOV 30 CO>JBP, LO 50 CO>JBP, LO 1000 CO>JBP, BQ@.
/K@B TB E>SB QEB >JMIFQRAB >KA MBOFLA, FQfP QFJB QL TOFQB > CLOJRI> QL @>I@RI>QB U >P >
CRK@QFLK LC QFJB, TEF@E TB KLT HKLT FP QEB @ROOBKQ CO>JB @LRKQ.
Our amplitude is measured in pixels. @FI;N ;GJFCNO>? = 100;
Our period is measured in frames (our unit
of time for animation).
@FI;N J?LCI> = 120;
@FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>);
4EB .>QROB LC #LAB (S005)
117
,BQfP AFPPB@Q QEB CLOJRI> > ?FQ JLOB >KA QOV QL RKABOPQ>KA B>@E @LJMLKBKQ. 4EB CFOPQ FP
MOL?>?IV QEB B>PFBPQ. 7E>QBSBO @LJBP LRQ LC QEB @LPFKB CRK@QFLK TB JRIQFMIV ?V >JMIFQRAB.
7B HKLT QE>Q @LPFKB TFII LP@FII>QB ?BQTBBK -1 >KA 1. )C TB Q>HB QE>Q S>IRB >KA JRIQFMIV FQ ?V
>JMIFQRAB QEBK TBfII DBQ QEB ABPFOBA OBPRIQ: > S>IRB LP@FII>QFKD ?BQTBBK ->JMIFQRAB >KA
>JMIFQRAB. (.LQB: QEFP FP >IPL > MI>@B TEBOB TB @LRIA RPB 0OL@BPPFKDfP G;J() CRK@QFLK QL
J>M QEB LRQMRQ LC @LPFKB QL > @RPQLJ O>KDB.)
.LT, IBQfP ILLH >Q TE>Q FP FKPFAB QEB @LPFKB CRK@QFLK:
.1*6+% * <H7C;CEKDJ / F;H?E:
7E>QfP DLFKD LK EBOB? ,BQfP PQ>OQ TFQE TE>Q TB HKLT. 7B HKLT QE>Q @LPFKB TFII OBMB>Q BSBOV
2*0) O>AF>KPdF.B. FQ TFII PQ>OQ >Q 0 >KA OBMB>Q >Q 2*0), 4*0), 6*0), BQ@. )C QEB MBOFLA FP 120, QEBK
TB T>KQ QEB LP@FII>QFKD JLQFLK QL OBMB>Q TEBK QEB @L;G?!IOHN FP >Q 120 CO>JBP, 240 CO>JBP,
360 CO>JBP, BQ@. @L;G?!IOHN FP OB>IIV QEB LKIV S>OF>?IB; FQ PQ>OQP >Q 0 >KA @LRKQP RMT>OA.
,BQfP Q>HB > ILLH >Q TE>Q QEB CLOJRI> VFBIAP TFQE QELPB S>IRBP.
frameCount frameCount frameCount / period frameCount / period
TWO_PI * frameCount / TWO_PI * frameCount /
period period
0 0 0
60 0.5 0)
120 1 47/=0)
240 2 2 * 47/=0) (LO 4* 0))
BQ@.
@L;G?!IOHN AFSFABA ?V J?LCI> QBIIP RP ELT J>KV @V@IBP TBfSB @LJMIBQBAd>OB TB E>ICT>V
QEOLRDE QEB CFOPQ @V@IB? (>SB TB @LJMIBQBA QTL @V@IBP? "V JRIQFMIVFKD QE>Q KRJ?BO ?V
14-:.', TB DBQ QEB OBPRIQ TB T>KQ, PFK@B 14-:.' FP QEB KRJ?BO LC O>AF>KP OBNRFOBA CLO LKB
@LPFKB (LO PFKB) QL @LJMIBQB LKB @V@IB.
7O>MMFKD QEFP >II RM, EBOBfP QEB 0OL@BPPFKD BU>JMIB QE>Q LP@FII>QBP QEB R IL@>QFLK LC > @FO@IB
TFQE >K >JMIFQRAB LC 100 MFUBIP >KA > MBOFLA LC 120 CO>JBP.
#E>MQBO 3. /P@FII>QFLK
118
Example 3.5 Simple Harmonic Motion
)QfP >IPL TLOQE JBKQFLKFKD QEB QBOJ 1=0<@09.D 1=0<@09.D: QEB KRJ?BO LC @V@IBP MBO QFJB RKFQ. &OBNRBK@V
FP BNR>I QL 1 AFSFABA ?V J?LCI>. )C QEB MBOFLA FP 120 CO>JBP, QEBK LKIV 1/120QE LC > @V@IB FP
@LJMIBQBA FK LKB CO>JB, >KA PL COBNRBK@V = 1/120. )K QEB >?LSB BU>JMIB, TB PFJMIV @ELPB QL
ABCFKB QEB O>QB LC LP@FII>QFLK FK QBOJP LC MBOFLA >KA QEBOBCLOB AFA KLQ KBBA > S>OF>?IB CLO
COBNRBK@V.
PIC> M?NOJ() U
MCT?(200,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
@FI;N J?LCI> = 120;
@FI;N ;GJFCNO>? = 100;
@FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>);
MNLIE?(0);
@CFF(175);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,R,0);
?FFCJM?(R,0,20,20);
W
Calculating horizontal location according to
the formula for simple harmonic motion
5PFKD QEB PFKB CRK@QFLK, @OB>QB > PFJRI>QFLK LC > TBFDEQ (PLJBQFJBP OBCBOOBA QL >P >
g?L?h) QE>Q E>KDP COLJ > PMOFKD COLJ QEB QLM LC QEB TFKALT. 5PB QEB G;J() CRK@QFLK QL
@>I@RI>QB QEB SBOQF@>I IL@>QFLK LC QEB ?L?. ,>QBO FK QEFP @E>MQBO, TBfII PBB ELT QL OB@OB>QB
QEFP P>JB PFJRI>QFLK ?V JLABIFKD QEB CLO@BP LC > PMOFKD >@@LOAFKD QL (LLHBfP I>T.
Exercise 3.6 Exercise 3.6
3.7 Oscillation with Angular Velocity 3.7 Oscillation with Angular Velocity
!K RKABOPQ>KAFKD LC QEB @LK@BMQP LC LP@FII>QFLK, >JMIFQRAB, >KA COBNRBK@V/MBOFLA FP LCQBK
OBNRFOBA FK QEB @LROPB LC PFJRI>QFKD OB>I-TLOIA ?BE>SFLOP. (LTBSBO, QEBOB FP > PIFDEQIV B>PFBO
T>V QL OBTOFQB QEB >?LSB BU>JMIB TFQE QEB P>JB OBPRIQ. ,BQfP Q>HB LKB JLOB ILLH >Q LRO
LP@FII>QFLK CLOJRI>:
!KA IBQfP OBTOFQB FQ > PIFDEQIV AFCCBOBKQ T>V:
@FI;N R = ;GJFCNO>? * =IM(14-:.' * @L;G?!IOHN / J?LCI>);
4EB .>QROB LC #LAB (S005)
119
)C TB @>OB >?LRQ MOB@FPBIV ABCFKFKD QEB MBOFLA LC LP@FII>QFLK FK QBOJP LC CO>JBP LC >KFJ>QFLK,
TB JFDEQ KBBA QEB CLOJRI> QEB T>V TB CFOPQ TOLQB FQ, ?RQ TB @>K GRPQ >P B>PFIV OBTOFQB LRO
BU>JMIB RPFKD QEB @LK@BMQ LC >KDRI>O SBIL@FQV (>KA >@@BIBO>QFLK) COLJ PB@QFLK 3.2 (PBB M>DB
104). !PPRJFKD:
FK >L;Q(), TB @>K PFJMIV P>V:
;HAF? FP LRO gPLJB S>IRB QE>Q FK@OBJBKQP PILTIV.h
Example 3.6 Simple Harmonic Motion II
*RPQ ?B@>RPB TBfOB KLQ OBCBOBK@FKD FQ AFOB@QIV ALBPKfQ JB>K QE>Q TBfSB BIFJFK>QBA QEB
@LK@BMQ LC J?LCI>. !CQBO >II, QEB DOB>QBO QEB >KDRI>O SBIL@FQV, QEB C>PQBO QEB @FO@IB TFII
LP@FII>QB (QEBOBCLOB ILTBOFKD QEB MBOFLA). )K C>@Q, QEB KRJ?BO LC QFJBP FQ Q>HBP QL >AA RM QEB
>KDRI>O SBIL@FQV QL DBQ QL 14-:.' FP QEB MBOFLA LO:
F;H?E: = .1*6+% / 7D=KB7H L;BE9?JO
@FI;N R = ;GJFCNO>? * =IM ( MIG? P;FO? NB;N CH=L?G?HNM MFIQFS );
@FI;N ;HAF? = 0;
@FI;N ;3?FI=CNS = 0.05;
;HAF? += ;3?FI=CNS;
@FI;N R = ;GJFCNO>? * =IM(;HAF?);
@FI;N ;HAF? = 0;
@FI;N ;3?FI=CNS = 0.05;
PIC> M?NOJ() U
MCT?(200,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
@FI;N ;GJFCNO>? = 100;
@FI;N R = ;GJFCNO>? * =IM(;HAF?);
Using the concept of angular velocity to
increment an angle variable
;HAF? += ;3?FI=CNS;
?FFCJM?+I>?(!#,1#/);
MNLIE?(0);
@CFF(175);
NL;HMF;N?(QC>NB/2,B?CABN/2);
FCH?(0,0,R,0);
?FFCJM?(R,0,20,20);
W
#E>MQBO 3. /P@FII>QFLK
120
,BQfP BUM>KA QEFP BU>JMIB > ?FQ JLOB >KA @OB>QB >K -M=CFF;NIL @I>PP. !KA IBQfP >PPRJB TB
T>KQ QEB LP@FII>QFLK QL E>MMBK >ILKD ?LQE QEB U->UFP (>P >?LSB) >KA QEB V->UFP. 4L AL QEFP, TBfII
KBBA QTL >KDIBP, QTL >KDRI>O SBIL@FQFBP, >KA QTL >JMIFQRABP (LKB CLO B>@E >UFP). !KLQEBO
MBOCB@Q LMMLOQRKFQV CLO .3?=NIL!
Example 3.7: Oscillator objects
=F;MM -M=CFF;NIL U
Using a PVector to track two angles! .3?=NIL ;HAF?;
.3?=NIL P?FI=CNS;
.3?=NIL ;GJFCNO>?;
-M=CFF;NIL() U
;HAF? = H?Q .3?=NIL();
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-0.05,0.05),L;H>IG(-0.05,0.05));
;GJFCNO>? = H?Q .3?=NIL(L;H>IG(QC>NB/2),L;H>IG(B?CABN/2));
W
PIC> IM=CFF;N?() U
;HAF?.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
Random velocities and amplitudes
Oscillating on the x-axis @FI;N R = MCH(;HAF?.R)*;GJFCNO>?.R;
Oscillating on the y-axis @FI;N S = MCH(;HAF?.S)*;GJFCNO>?.S;
JOMB+;NLCR();
NL;HMF;N?(QC>NB/2,B?CABN/2);
MNLIE?(0);
@CFF(175);
4EB .>QROB LC #LAB (S005)
121
Drawing the Oscillator as a line connecting
a circle
FCH?(0,0,R,S);
?FFCJM?(R,S,16,16);
JIJ+;NLCR();
W
W
4OV FKFQF>IFWFKD B>@E -M=CFF;NIL L?GB@Q TFQE SBIL@FQFBP >KA >JMIFQRABP QE>Q >OB KLQ
O>KALJ QL @OB>QB PLJB PLOQ LC OBDRI>O M>QQBOK. #>K VLR J>HB QEB LP@FII>QLOP >MMB>O
QL ?B QEB IBDP LC > FKPB@Q-IFHB @OB>QROB?
Exercise 3.7 Exercise 3.7
)K@LOMLO>QB >KDRI>O >@@BIBO>QFLK FKQL QEB -M=CFF;NIL L?GB@Q.
Exercise 3.8 Exercise 3.8
3.8 Waves 3.8 Waves
)C VLRfOB P>VFKD QL VLROPBIC, g5J, QEFP FP >II DOB>Q >KA BSBOVQEFKD, ?RQ TE>Q ) OB>IIV T>KQ FP QL
AO>T > T>SB LKP@OBBK,h TBII, QEBK, QEB QFJB E>P @LJB. 4EB QEFKD FP, TBfOB >?LRQ 90% QEBOB.
7EBK TB LP@FII>QB > PFKDIB @FO@IB RM >KA ALTK >@@LOAFKD QL QEB PFKB CRK@QFLK, TE>Q TB >OB
ALFKD FP ILLHFKD >Q > PFKDIB MLFKQ >ILKD QEB U->UFP LC > T>SB M>QQBOK. 7FQE > IFQQIB M>K>@EB
>KA > @IL ILLM, TB @>K MI>@B > TELIB ?RK@E LC QEBPB LP@FII>QFKD @FO@IBP KBUQ QL B>@E LQEBO.
4EFP T>SV M>QQBOK @LRIA ?B RPBA FK QEB ABPFDK LC QEB ?LAV LO >MMBKA>DBP LC > @OB>QROB, >P
TBII >P QL PFJRI>QB > PLCQ PROC>@B (PR@E >P T>QBO).
(BOB, TBfOB DLFKD QL BK@LRKQBO QEB P>JB NRBPQFLKP LC >JMIFQRAB (EBFDEQ LC M>QQBOK) >KA
MBOFLA. )KPQB>A LC MBOFLA OBCBOOFKD QL QFJB, ELTBSBO, PFK@B TBfOB ILLHFKD >Q QEB CRII T>SB, TB
#E>MQBO 3. /P@FII>QFLK
122
@>K Q>IH >?LRQ MBOFLA >P QEB TFAQE (FK MFUBIP) LC > CRII T>SB @V@IB. !KA GRPQ >P TFQE PFJMIB
LP@FII>QFLK, TB E>SB QEB LMQFLK LC @LJMRQFKD QEB T>SB M>QQBOK >@@LOAFKD QL > MOB@FPB MBOFLA LO
PFJMIV CLIILTFKD QEB JLABI LC >KDRI>O SBIL@FQV.
,BQfP DL TFQE QEB PFJMIBO @>PB, >KDRI>O SBIL@FQV. 7B HKLT TB KBBA QL PQ>OQ TFQE >K >KDIB, >K
>KDRI>O SBIL@FQV, >KA >K >JMIFQRAB:
4EBK TBfOB DLFKD QL ILLM QEOLRDE >II LC QEB R S>IRBP TEBOB TB T>KQ QL AO>T > MLFKQ LC QEB
T>SB. ,BQfP P>V BSBOV 24 MFUBIP CLO KLT. )K QE>Q ILLM, TBfOB DLFKD QL T>KQ QL AL QEOBB QEFKDP:
1. #>I@RI>QB QEB V IL@>QFLK >@@LOAFKD QL >JMIFQRAB >KA PFKB LC QEB >KDIB.
2. $O>T > @FO@IB >Q QEB (L,M) IL@>QFLK.
3. )K@OBJBKQ QEB >KDIB >@@LOAFKD QL >KDRI>O SBIL@FQV.
,BQfP ILLH >Q QEB OBPRIQP TFQE AFCCBOBKQ S>IRBP CLO ;HAF?3?F:
@FI;N ;HAF? = 0;
@FI;N ;HAF?3?F = 0.2;
@FI;N ;GJFCNO>? = 100;
@IL (CHN R = 0; R <= QC>NB; R += 24) U
1) Calculate the y location according to
amplitude and sine of the angle.
@FI;N S = ;GJFCNO>?*MCH(;HAF?);
2) Draw a circle at the (x,y) location. ?FFCJM?(R,S+B?CABN/2,48,48);
3) Increment the angle according to angular
velocity.
;HAF? += ;HAF?3?F;
W
.;492+29 = 0.05 .;492+29 = 0.2 .;492+29 = 0.4
4EB .>QROB LC #LAB (S005)
123
.LQF@B ELT, >IQELRDE TBfOB KLQ MOB@FPBIV @LJMRQFKD QEB MBOFLA LC QEB T>SB, QEB EFDEBO QEB
>KDRI>O SBIL@FQV, QEB PELOQBO QEB MBOFLA. )QfP >IPL TLOQE KLQFKD QE>Q >P QEB MBOFLA ?B@LJBP
PELOQBO, FQ ?B@LJBP JLOB >KA JLOB AFCCF@RIQ QL J>HB LRQ QEB T>SB FQPBIC >P QEB AFPQ>K@B
?BQTBBK QEB FKAFSFAR>I MLFKQP FK@OB>PBP. /KB LMQFLK TB E>SB FP QL RPB <?ACH0B;J?() >KA
?H>0B;J?() QL @LKKB@Q QEB MLFKQP TFQE > IFKB.
Example 3.8: Static wave drawn as a continuous line
9LR J>V E>SB KLQF@BA QE>Q QEB >?LSB BU>JMIB FP PQ>QF@. 4EB T>SB KBSBO @E>KDBP, KBSBO
RKARI>QBP. 4EFP >AAFQFLK>I PQBM FP > ?FQ QOF@HV. 9LRO CFOPQ FKPQFK@Q JFDEQ ?B QL P>V: g(BV, KL
MOL?IBJ, TBfII GRPQ IBQ QEBQ> ?B > DIL?>I S>OF>?IB >KA IBQ FQ FK@OBJBKQ COLJ LKB @V@IB QEOLRDE
>L;Q() QL >KLQEBO.h
7EFIB FQfP > KF@B QELRDEQ, FQ ALBPKfQ TLOH. )C VLR ILLH >Q QEB T>SB, QEB OFDEQE>KA BADB ALBPKfQ
J>Q@E QEB IBCQE>KA; TEBOB FQ BKAP FK LKB @V@IB LC >L;Q() @>KfQ ?B TEBOB FQ PQ>OQP FK QEB KBUQ.
@FI;N ;HAF? = 0;
@FI;N ;HAF?3?F = 0.2;
@FI;N ;GJFCNO>? = 100;
MCT?(400,200);
<;=EALIOH>(255);
MGIINB();
MNLIE?(0);
MNLIE?4?CABN(2);
HI$CFF();
<?ACH0B;J?();
@IL (CHN R = 0; R <= QC>NB; R += 5) U
Heres an example of using the map()
function instead.
@FI;N S = G;J(MCH(;HAF?),-1,1,0,B?CABN);
With beginShape() and endShape(), you
call vertex() to set all the vertices of your
shape.
P?LN?R(R,S);
;HAF? +=;HAF?3?F;
W
?H>0B;J?();
#E>MQBO 3. /P@FII>QFLK
124
)KPQB>A, TE>Q TB KBBA QL AL FP E>SB > S>OF>?IB ABAF@>QBA BKQFOBIV QL QO>@HFKD TE>Q S>IRB LC
>KDIB QEB T>SB PELRIA PQ>OQ TFQE. 4EFP >KDIB (TEF@E TBfII @>II MN;LNAHAF?) FK@OBJBKQP TFQE FQP
LTK >KDRI>O SBIL@FQV.
Example 3.9: The Wave
@FI;N MN;LNAHAF? = 0;
@FI;N ;HAF?3?F = 0.1;
PIC> M?NOJ() U
MCT?(400,200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
In order to move the wave, we start at a
different theta value each frame. startAngle
+= 0.02;
@FI;N ;HAF? = MN;LNAHAF?;
@IL (CHN R = 0; R <= QC>NB; R += 24) U
@FI;N S = G;J(MCH(;HAF?),-1,1,0,B?CABN);
MNLIE?(0);
@CFF(0,50);
?FFCJM?(R,S,48,48);
;HAF? += ;HAF?3?F;
W
W
4EB .>QROB LC #LAB (S005)
125
4OV RPFKD QEB 0BOIFK KLFPB CRK@QFLK FKPQB>A LC PFKB LO @LPFKB TFQE QEB >?LSB BU>JMIB.
Exercise 3.9 Exercise 3.9
%K@>MPRI>QB QEB >?LSB BU>JMIBP FKQL > 4;P? @I>PP >KA @OB>QB > PHBQ@E QE>Q AFPMI>VP
QTL T>SBP (TFQE AFCCBOBKQ >JMIFQRABP/MBOFLAP) >P FK QEB P@OBBKPELQ ?BILT. -LSB
?BVLKA MI>FK @FO@IBP >KA IFKBP >KA QOV SFPR>IFWFKD QEB T>SB FK > JLOB @OB>QFSB T>V.
Exercise 3.10 Exercise 3.10
-LOB @LJMIBU T>SBP @>K ?B MOLAR@BA ?V QEB S>IRBP LC JRIQFMIB T>SBP QLDBQEBO.
#OB>QB > PHBQ@E QE>Q FJMIBJBKQP QEFP, >P FK QEB P@OBBKPELQ ?BILT.
Exercise 3.11 Exercise 3.11
#E>MQBO 3. /P@FII>QFLK
126
3.9 Trigonometry and Forces: The Pendulum 3.9 Trigonometry and Forces: The Pendulum
$L VLR JFPP .BTQLKfP I>TP LC JLQFLK? ) HKLT ) PROB AL. 7BII, IR@HV CLO VLR, FQfP QFJB QL ?OFKD FQ
>II ?>@H ELJB. !CQBO >II, FQfP ?BBK KF@B IB>OKFKD >?LRQ QOF>KDIBP >KA Q>KDBKQP >KA T>SBP, ?RQ
OB>IIV, QEB @LOB LC QEFP ?LLH FP >?LRQ PFJRI>QFKD QEB MEVPF@P LC JLSFKD ?LAFBP. ,BQfP Q>HB > ILLH
>Q ELT QOFDLKLJBQOV @>K EBIM RP TFQE QEFP MROPRFQ.
! MBKARIRJ FP > ?L? PRPMBKABA COLJ > MFSLQ. /?SFLRPIV > OB>I-TLOIA MBKARIRJ TLRIA IFSB FK >
3$ PM>@B, ?RQ TBfOB DLFKD QL ILLH >Q > PFJMIBO P@BK>OFL, > MBKARIRJ FK > 2$ PM>@Bd>
0OL@BPPFKD TFKALT (PBB &FDROB 3.10).
)K #E>MQBO 2, TB IB>OKBA ELT > CLO@B (PR@E >P QEB CLO@B LC DO>SFQV FK &FDROB 3.11) @>RPBP >K
L?GB@Q QL >@@BIBO>QB. " = ( * A LO A = " / (. )K QEFP @>PB, ELTBSBO, QEB MBKARIRJ ?L?
ALBPKfQ PFJMIV C>II QL QEB DOLRKA ?B@>RPB FQ FP >QQ>@EBA ?V >K >OJ QL QEB MFSLQ MLFKQ. !KA PL, FK
LOABO QL ABQBOJFKB FQP 5B;I@5F >@@BIBO>QFLK, TB KLQ LKIV KBBA QL ILLH >Q QEB CLO@B LC DO>SFQV,
?RQ >IPL QEB CLO@B >Q QEB >KDIB LC QEB MBKARIRJfP >OJ (OBI>QFSB QL > MBKARIRJ >Q OBPQ TFQE >K
>KDIB LC 0).
)K QEB >?LSB @>PB, PFK@B QEB MBKARIRJfP >OJ FP LC CFUBA IBKDQE, QEB LKIV S>OF>?IB FK QEB
P@BK>OFL FP QEB >KDIB. 7B >OB DLFKD QL PFJRI>QB QEB MBKARIRJfP JLQFLK QEOLRDE QEB RPB LC
>KDRI>O SBIL@FQV >KA >@@BIBO>QFLK. 4EB >KDRI>O >@@BIBO>QFLK TFII ?B @>I@RI>QBA RPFKD .BTQLKfP
PB@LKA I>T TFQE > IFQQIB QOFDLKLJBQOV QTFPQ.
,BQfP WLLJ FK LK QEB OFDEQ QOF>KDIB COLJ QEB MBKARIRJ AF>DO>J.
F64B?2 3.10 F64B?2 3.11
4EB .>QROB LC #LAB (S005)
127
7B @>K PBB QE>Q QEB CLO@B LC QEB MBKARIRJ
($
J
) PELRIA MLFKQ MBOMBKAF@RI>O QL QEB >OJ
LC QEB MBKARIRJ FK QEB AFOB@QFLK QE>Q QEB
MBKARIRJ FP PTFKDFKD. !CQBO >II, FC QEBOB
TBOB KL >OJ, QEB ?L? TLRIA GRPQ C>II
PQO>FDEQ ALTK. )QfP QEB QBKPFLK CLO@B LC QEB
>OJ QE>Q HBBMP QEB ?L? >@@BIBO>QFKD
QLT>OAP QEB MBKARIRJfP OBPQ PQ>QB. 3FK@B
QEB CLO@B LC DO>SFQV ($
J
) MLFKQP ALTKT>OA,
?V J>HFKD > OFDEQ QOF>KDIB LRQ LC QEBPB QTL
SB@QLOP, TBfSB >@@LJMIFPEBA PLJBQEFKD
NRFQB J>DKFCF@BKQ. 7BfSB J>AB QEB CLO@B LC
DO>SFQV QEB EVMLQBKRPB LC > OFDEQ QOF>KDIB
>KA PBM>O>QBA QEB SB@QLO FKQL QTL
@LJMLKBKQP, LKB LC TEF@E OBMOBPBKQP QEB
CLO@B LC QEB MBKARIRJ. 3FK@B PFKB BNR>IP
LMMLPFQB LSBO EVMLQBKRPB, TB E>SB:
I?D;(V) = "
F
/ "
=
4EBOBCLOB:
"
F
= "
=
* I?D;(V)
,BPQ TB CLODBQ, TBfSB ?BBK ALFKD >II LC QEFP TFQE > PFKDIB NRBPQFLK FK JFKA: 7E>Q FP QEB
>KDRI>O >@@BIBO>QFLK LC QEB MBKARIRJ? /K@B TB E>SB QEB >KDRI>O >@@BIBO>QFLK, TBfII ?B >?IB
QL >MMIV LRO ORIBP LC JLQFLK QL CFKA QEB KBT >KDIB CLO QEB MBKARIRJ.
7D=KB7H L;BE9?JO = 7D=KB7H L;BE9?JO + 7D=KB7H 799;B;H7J?ED
7D=B; = 7D=B; + 7D=KB7H L;BE9?JO
4EB DLLA KBTP FP QE>Q TFQE .BTQLKfP PB@LKA I>T, TB HKLT QE>Q QEBOB FP > OBI>QFLKPEFM
?BQTBBK CLO@B >KA >@@BIBO>QFLK, K>JBIV & = - * !, LO ! = & / -. 3L FC QEB CLO@B LC QEB
MBKARIRJ FP BNR>I QL QEB CLO@B LC DO>SFQV QFJBP PFKB LC QEB >KDIB, QEBK:
F;D:KBKC 7D=KB7H 799;B;H7J?ED = 799;B;H7J?ED :K; JE =H7L?JO * I?D; (V)
4EFP FP > DLLA QFJB QL OBJFKA LROPBISBP QE>Q TBfOB 0OL@BPPFKD MOLDO>JJBOP >KA KLQ
MEVPF@FPQP. 9BP, TB HKLT QE>Q QEB >@@BIBO>QFLK ARB QL DO>SFQV LK B>OQE FP 9.8 JBQBOP MBO
PB@LKA PNR>OBA. "RQ QEFP KRJ?BO FPKfQ OBIBS>KQ QL RP. 7E>Q TB E>SB EBOB FP GRPQ >K >O?FQO>OV
@LKPQ>KQ (TBfII @>II FQ AL;PCNS), LKB QE>Q TB @>K RPB QL P@>IB QEB >@@BIBO>QFLK QL PLJBQEFKD
QE>Q CBBIP OFDEQ.
7D=KB7H 799;B;H7J?ED = =H7L?JO * I?D;(V)
!J>WFKD. !CQBO >II QE>Q, QEB CLOJRI> FP PL PFJMIB. 9LR JFDEQ ?B TLKABOFKD, TEV ?LQEBO DLFKD
QEOLRDE QEB ABOFS>QFLK >Q >II? ) JB>K, IB>OKFKD FP DOB>Q >KA >II, ?RQ TB @LRIA E>SB B>PFIV GRPQ
F64B?2 3.12
#E>MQBO 3. /P@FII>QFLK
128
P>FA, "(BV, QEB >KDRI>O >@@BIBO>QFLK LC > MBKARIRJ FP PLJB @LKPQ>KQ QFJBP QEB PFKB LC QEB
>KDIB." 4EFP FP GRPQ >KLQEBO JLJBKQ FK TEF@E TB OBJFKA LROPBISBP QE>Q QEB MROMLPB LC QEB
?LLH FP KLQ QL IB>OK ELT MBKARIRJP PTFKD LO DO>SFQV TLOHP. 4EB MLFKQ FP QL QEFKH @OB>QFSBIV
>?LRQ ELT QEFKDP @>K JLSB >?LRQ QEB P@OBBK FK > @LJMRQ>QFLK>IIV ?>PBA DO>MEF@P PVPQBJ. 4EB
MBKARIRJ FP GRPQ > @>PB PQRAV. )C VLR @>K RKABOPQ>KA QEB >MMOL>@E QL MOLDO>JJFKD >
MBKARIRJ, QEBK ELTBSBO VLR @ELLPB QL ABPFDK VLRO LKP@OBBK TLOIA, VLR @>K >MMIV QEB P>JB
QB@EKFNRBP.
/C @LROPB, TBfOB KLQ CFKFPEBA VBQ. 7B J>V ?B E>MMV TFQE LRO PFJMIB, BIBD>KQ CLOJRI>, ?RQ TB
PQFII E>SB QL >MMIV FQ FK @LAB. 4EFP FP JLPQ ABCFKFQBIV > DLLA QFJB QL MO>@QF@B LRO L?GB@Q-LOFBKQBA
MOLDO>JJFKD PHFIIP >KA @OB>QB > .?H>OFOG @I>PP. ,BQfP QEFKH >?LRQ >II QEB MOLMBOQFBP TBfSB
BK@LRKQBOBA FK LRO MBKARIRJ AFP@RPPFLK QE>Q QEB @I>PP TFII KBBA:
i >OJ IBKDQE
i >KDIB
i >KDRI>O SBIL@FQV
i >KDRI>O >@@BIBO>QFLK
7BfII >IPL KBBA QL TOFQB > CRK@QFLK OJ>;N?() QL RMA>QB QEB MBKARIRJfP >KDIB >@@LOAFKD QL LRO
CLOJRI>j
=F;MM .?H>OFOG U
Length of arm @FI;N L;
Pendulum arm angle @FI;N ;HAF?;
Angular velocity @FI;N ;3?FI=CNS;
Angular acceleration @FI;N ;A==?F?L;NCIH;
PIC> OJ>;N?() U
Arbitrary constant @FI;N AL;PCNS = 0.4;
Calculate acceleration according to our
formula.
;A==?F?L;NCIH = -1 * AL;PCNS * MCH(;HAF?);
Increment velocity. ;3?FI=CNS += ;A==?F?L;NCIH;
Increment angle. ;HAF? += ;3?FI=CNS;
W
4EB .>QROB LC #LAB (S005)
129
j>P TBII >P > CRK@QFLK >CMJF;S() QL AO>T
QEB MBKARIRJ FK QEB TFKALT. 4EFP ?BDP QEB
NRBPQFLK: g5J, TEBOB AL TB AO>T QEB
MBKARIRJ?h 7B HKLT QEB >KDIB >KA QEB
>OJ IBKDQE, ?RQ ELT AL TB HKLT QEB L,M
(#>OQBPF>K!) @LLOAFK>QBP CLO ?LQE QEB
MBKARIRJfP MFSLQ MLFKQ (IBQfP @>II FQ LOFDFK)
>KA ?L? IL@>QFLK (IBQfP @>II FQ IL@>QFLK)? 4EFP
J>V ?B DBQQFKD > IFQQIB QFOFKD, ?RQ QEB
>KPTBO, VBQ >D>FK, FP QOFDLKLJBQOV.
4EB LOFDFK FP GRPQ PLJBQEFKD TB J>HB RM,
>P FP QEB >OJ IBKDQE. ,BQfP P>V:
7BfSB DLQ QEB @ROOBKQ >KDIB PQLOBA FK LRO S>OF>?IB ;HAF?. 3L OBI>QFSB QL QEB LOFDFK, QEB
MBKARIRJfP IL@>QFLK FP > MLI>O @LLOAFK>QB: (F,5B;@9). !KA TB KBBA FQ QL ?B #>OQBPF>K. ,R@HFIV
CLO RP, TB GRPQ PMBKQ PLJB QFJB (PB@QFLK 3.5) ABOFSFKD QEB CLOJRI> CLO @LKSBOQFKD COLJ MLI>O
QL #>OQBPF>K. !KA PL:
3FK@B QEB IL@>QFLK FP OBI>QFSB QL TEBOBSBO QEB LOFDFK E>MMBKP QL ?B, TB @>K GRPQ >AA LOFDFK QL
QEB IL@>QFLK .3?=NIL:
!KA >II QE>Q OBJ>FKP FP QEB IFQQIB J>QQBO LC AO>TFKD > IFKB >KA BIIFMPB (VLR PELRIA ?B JLOB
@OB>QFSB, LC @LROPB).
"BCLOB TB MRQ BSBOVQEFKD QLDBQEBO, QEBOBfP LKB I>PQ IFQQIB ABQ>FI ) KBDIB@QBA QL JBKQFLK. ,BQfP
QEFKH >?LRQ QEB MBKARIRJ >OJ CLO > JLJBKQ. )P FQ > JBQ>I OLA? ! PQOFKD? ! OR??BO ?>KA? (LT
FP FQ >QQ>@EBA QL QEB MFSLQ MLFKQ? (LT ILKD FP FQ? 7E>Q FP FQP J>PP? )P FQ > TFKAV A>V? 4EBOB
>OB > ILQ LC NRBPQFLKP QE>Q TB @LRIA @LKQFKRB QL >PH QE>Q TLRIA >CCB@Q QEB PFJRI>QFLK. 7BfOB
F64B?2 3.13
.3?=NIL ILCACH = H?Q .3?=NIL(100,10);
@FI;N L = 125;
.3?=NIL FI=;NCIH = H?Q .3?=NIL(L*MCH(;HAF?),L*=IM(;HAF?));
FI=;NCIH.;>>(ILCACH);
MNLIE?(0);
@CFF(175);
FCH?(ILCACH.R,ILCACH.S,FI=;NCIH.R,FI=;NCIH.S);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
#E>MQBO 3. /P@FII>QFLK
130
IFSFKD, LC @LROPB, FK > C>KQ>PV TLOIA, LKB TEBOB QEB MBKARIRJfP >OJ FP PLJB FAB>IFWBA OLA QE>Q
KBSBO ?BKAP >KA QEB J>PP LC QEB ?L? FP @LK@BKQO>QBA FK > PFKDIB, FKCFKFQBPFJ>IIV PJ>II MLFKQ.
.BSBOQEBIBPP, BSBK QELRDE TB ALKfQ T>KQ QL TLOOV LROPBISBP TFQE >II LC QEB NRBPQFLKP, TB
PELRIA >AA LKB JLOB S>OF>?IB QL LRO @>I@RI>QFLK LC >KDRI>O >@@BIBO>QFLK. 4L HBBM QEFKDP
PFJMIB, FK LRO ABOFS>QFLK LC QEB MBKARIRJfP >@@BIBO>QFLK, TB >PPRJBA QE>Q QEB IBKDQE LC QEB
MBKARIRJfP >OJ FP 1. )K C>@Q, QEB IBKDQE LC QEB MBKARIRJfP >OJ >CCB@QP QEB >@@BIBO>QFLK DOB>QIV:
QEB ILKDBO QEB >OJ, QEB PILTBO QEB >@@BIBO>QFLK. 4L PFJRI>QB > MBKARIRJ JLOB >@@RO>QBIV, TB
AFSFAB ?V QE>Q IBKDQE, FK QEFP @>PB L. &LO > JLOB FKSLISBA BUMI>K>QFLK, SFPFQ 4EB 3FJMIB
0BKARIRJ TB?PFQB (EQQM://@>I@RIRPI>?.ABIQ>@LIIBDB.BAR//$%/7-!-2/7-!-2-E.EQJI).
&FK>IIV, > OB>I-TLOIA MBKARIRJ FP DLFKD QL BUMBOFBK@B PLJB >JLRKQ LC COF@QFLK (>Q QEB MFSLQ
MLFKQ) >KA >FO OBPFPQ>K@B. 7FQE LRO @LAB >P FP, QEB MBKARIRJ TLRIA PTFKD CLOBSBO, PL QL J>HB FQ
JLOB OB>IFPQF@ TB @>K RPB > gA>JMFKDh QOF@H. ) P>V HF=7? ?B@>RPB O>QEBO QE>K JLABI QEB
OBPFPQ>K@B CLO@BP TFQE PLJB ABDOBB LC >@@RO>@V (>P TB AFA FK #E>MQBO 2), TB @>K >@EFBSB >
PFJFI>O OBPRIQ ?V PFJMIV OBAR@FKD QEB >KDRI>O SBIL@FQV AROFKD B>@E @V@IB. 4EB CLIILTFKD @LAB
OBAR@BP QEB SBIL@FQV ?V 1% (LO JRIQFMIFBP FQ ?V 99%) AROFKD B>@E CO>JB LC >KFJ>QFLK:
0RQQFKD BSBOVQEFKD QLDBQEBO, TB E>SB QEB CLIILTFKD BU>JMIB (TFQE QEB MBKARIRJ ?BDFKKFKD >Q >
45-ABDOBB >KDIB).
Example 3.10: Swinging pendulum
;A==?F?L;NCIH = (-1 * % * MCH(;HAF?)) / L;
;3?FI=CNS *= 0.99;
.?H>OFOG J;
PIC> M?NOJ() U
MCT?(200,200);
4EB .>QROB LC #LAB (S005)
131
We make a new Pendulum object with an
origin location and arm length.
J = H?Q .?H>OFOG(H?Q .3?=NIL(QC>NB/2,10),125);
W
PIC> >L;Q() U
<;=EALIOH>(255);
J.AI();
W
=F;MM .?H>OFOG U
Many, many variables to keep track of the
Pendulums various properties
.3?=NIL FI=;NCIH; // *I=;NCIH I@ <I<
.3?=NIL ILCACH; // *I=;NCIH I@ ;LG ILCACH
@FI;N L; // *?HANB I@ ;LG
@FI;N ;HAF?; // .?H>OFOG ;LG ;HAF?
@FI;N ;3?FI=CNS; // AHAF? P?FI=CNS
@FI;N ;A==?F?L;NCIH; // AHAF? ;==?F?L;NCIH
@FI;N >;GJCHA; // AL<CNL;LS >;GJCHA ;GIOHN
.?H>OFOG(.3?=NIL ILCACH:, @FI;N L:) U
ILCACH = ILCACH:.A?N();
FI=;NCIH = H?Q .3?=NIL();
L = L:;
;HAF? = .'/4;
;3?FI=CNS = 0.0;
;A==?F?L;NCIH = 0.0;
An arbitrary damping so that the Pendulum
slows over time
>;GJCHA = 0.995;
W
PIC> AI() U
OJ>;N?();
>CMJF;S();
W
PIC> OJ>;N?() U
@FI;N AL;PCNS = 0.4;
;A==?F?L;NCIH = (-1 * AL;PCNS / L) * MCH(;HAF?);
Formula we worked out for angular
acceleration
Standard angular motion algorithm ;3?FI=CNS += ;A==?F?L;NCIH;
;HAF? += ;3?FI=CNS;
Apply some damping. ;3?FI=CNS *= >;GJCHA;
W
PIC> >CMJF;S() U
Where is the bob relative to the origin?
Polar to Cartesian coordinates will tell us!
FI=;NCIH.M?N(L*MCH(;HAF?),L*=IM(;HAF?),0);
FI=;NCIH.;>>(ILCACH);
MNLIE?(0);
#E>MQBO 3. /P@FII>QFLK
132
(*CH9 H<5H H<9 J9FG=CB C: H<9 9L5AD@9 DCGH98 CB H<9 K96G=H9 <5G 588=H=CB5@ 7C89 HC 5@@CK H<9
IG9F HC ;F56 H<9 D9B8I@IA 5B8 GK=B; =H K=H< H<9 ACIG9.)
The arm FCH?(ILCACH.R,ILCACH.S,FI=;NCIH.R,FI=;NCIH.S);
@CFF(175);
The bob ?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,16,16);
W
W
3QOFKD QLDBQEBO > PBOFBP LC MBKARIRJP PL QE>Q QEB BKAMLFKQ LC LKB FP QEB LOFDFK MLFKQ LC
>KLQEBO. .LQB QE>Q ALFKD QEFP J>V MOLAR@B FKQOFDRFKD OBPRIQP ?RQ TFII ?B TFIAIV FK>@@RO>QB
MEVPF@>IIV. 3FJRI>QFKD >K >@QR>I ALR?IB MBKARIRJ FKSLISBP PLMEFPQF@>QBA BNR>QFLKP,
TEF@E VLR @>K OB>A >?LRQ EBOB: EQQM://P@FBK@BTLOIA.TLICO>J.@LJ/MEVPF@P/
$LR?IB0BKARIRJ.EQJI (EQQM://P@FBK@BTLOIA.TLICO>J.@LJ/MEVPF@P/$LR?IB0BKARIRJ.EQJI).
Exercise 3.12 Exercise 3.12
5PFKD QOFDLKLJBQOV, TE>Q FP QEB
J>DKFQRAB LC QEB KLOJ>I CLO@B FK QEB
FIIRPQO>QFLK LK QEB OFDEQ (QEB CLO@B
MBOMBKAF@RI>O QL QEB FK@IFKB LK TEF@E QEB
PIBA OBPQP)? .LQB QE>Q, >P FKAF@>QBA, QEB
gKLOJ>Ih CLO@B FP > @LJMLKBKQ LC QEB
CLO@B LC DO>SFQV.
Exercise 3.13 Exercise 3.13
#OB>QB >K BU>JMIB QE>Q PFJRI>QBP > ?LU PIFAFKD ALTK QEB FK@IFKB TFQE COF@QFLK. .LQB QE>Q
QEB J>DKFQRAB LC QEB COF@QFLK CLO@B FP BNR>I QL QEB KLOJ>I CLO@B.
Exercise 3.14 Exercise 3.14
4EB .>QROB LC #LAB (S005)
133
3.10 Spring Forces 3.10 Spring Forces
)K PB@QFLK 3.6 (PBB M>DB 115), TB ILLHBA >Q JLABIFKD PFJMIB E>OJLKF@ JLQFLK ?V J>MMFKD
QEB PFKB T>SB QL > MFUBI O>KDB. %UBO@FPB 3.6 (PBB M>DB 119) >PHBA VLR QL RPB QEFP QB@EKFNRB
QL @OB>QB > PFJRI>QFLK LC > ?L? E>KDFKD COLJ > PMOFKD. 7EFIB RPFKD QEB MCH() CRK@QFLK FP >
NRF@H->KA-AFOQV, LKB-IFKB-LC-@LAB T>V LC DBQQFKD PLJBQEFKD RM >KA ORKKFKD, FQ TLKfQ AL FC
TE>Q TB OB>IIV T>KQ FP QL E>SB > ?L? E>KDFKD COLJ > PMOFKD FK > QTL-AFJBKPFLK>I PM>@B QE>Q
OBPMLKAP QL LQEBO CLO@BP FK QEB BKSFOLKJBKQ (TFKA, DO>SFQV, BQ@.) 4L >@@LJMIFPE > PFJRI>QFLK
IFHB QEFP (LKB QE>Q FP FABKQF@>I QL QEB MBKARIRJ BU>JMIB, LKIV KLT QEB >OJ FP > PMOFKDV
@LKKB@QFLK), TB KBBA QL JLABI QEB CLO@BP LC > PMOFKD RPFKD .3?=NIL.
4EB CLO@B LC > PMOFKD FP @>I@RI>QBA >@@LOAFKD QL (LLHBfP I>T, K>JBA CLO 2L?BOQ (LLHB, >
"OFQFPE MEVPF@FPQ TEL ABSBILMBA QEB CLOJRI> FK 1660. (LLHB LOFDFK>IIV PQ>QBA QEB I>T FK ,>QFK:
"0H H9BG=C, G=7 J=G," LO g!P QEB BUQBKPFLK, PL QEB CLO@B.h ,BQfP QEFKH LC FQ QEFP T>V:
4EB CLO@B LC QEB PMOFKD FP AFOB@QIV MOLMLOQFLK>I QL QEB BUQBKPFLK LC QEB
PMOFKD.
F64B?2 3.14
#E>MQBO 3. /P@FII>QFLK
134
)K LQEBO TLOAP, FC VLR MRII LK QEB ?L? > ILQ,
QEB CLO@B TFII ?B PQOLKD; FC VLR MRII LK QEB
?L? > IFQQIB, QEB CLO@B TFII ?B TB>H.
->QEBJ>QF@>IIV, QEB I>T FP PQ>QBA >P CLIILTP:
"
IFH?D=
= - A * N
i E FP @LKPQ>KQ >KA FQP S>IRB TFII
RIQFJ>QBIV P@>IB QEB CLO@B. )P QEB
PMOFKD EFDEIV BI>PQF@ LO NRFQB OFDFA?
i R OBCBOP QL QEB AFPMI>@BJBKQ LC QEB
PMOFKD, F.B. QEB AFCCBOBK@B ?BQTBBK
QEB @ROOBKQ IBKDQE >KA QEB OBPQ
IBKDQE. 4EB OBPQ IBKDQE FP ABCFKBA
>P QEB IBKDQE LC QEB PMOFKD FK >
PQ>QB LC BNRFIF?OFRJ.
.LT OBJBJ?BO, CLO@B FP > SB@QLO, PL TB
KBBA QL @>I@RI>QB ?LQE J>DKFQRAB >KA
AFOB@QFLK. ,BQfP ILLH >Q LKB JLOB AF>DO>J LC
QEB PMOFKD >KA I>?BI >II QEB DFSBKP TB JFDEQ E>SB FK > 0OL@BPPFKD PHBQ@E.
,BQfP BPQ>?IFPE QEB CLIILTFKD QEOBB S>OF>?IBP >P PELTK FK &FDROB 3.16.
F64B?2 3.15: E = 0B??2;A 92;4A5 - ?2@A 92;4A5
F64B?2 3.16
.3?=NIL ;H=BIL;
.3?=NIL FI=;NCIH;
@FI;N L?MN*?HANB;
4EB .>QROB LC #LAB (S005)
135
&FOPQ, IBQfP RPB (LLHBfP I>T QL @>I@RI>QB QEB J>DKFQRAB LC QEB CLO@B. 7B KBBA QL HKLT E >KA
R. E FP B>PV; FQfP GRPQ > @LKPQ>KQ, PL IBQfP J>HB PLJBQEFKD RM.
R FP MBOE>MP > ?FQ JLOB AFCCF@RIQ. 7B KBBA QL HKLT QEB gAFCCBOBK@B ?BQTBBK QEB @ROOBKQ
IBKDQE >KA QEB OBPQ IBKDQE.h 4EB OBPQ IBKDQE FP ABCFKBA >P QEB S>OF>?IB L?MN*?HANB. 7E>QfP
QEB @ROOBKQ IBKDQE? 4EB AFPQ>K@B ?BQTBBK QEB >K@ELO >KA QEB ?L?. !KA ELT @>K TB
@>I@RI>QB QE>Q AFPQ>K@B? (LT >?LRQ QEB J>DKFQRAB LC > SB@QLO QE>Q MLFKQP COLJ QEB >K@ELO QL
QEB ?L?? (.LQB QE>Q QEFP FP BU>@QIV QEB P>JB MOL@BPP TB BJMILVBA TEBK @>I@RI>QFKD AFPQ>K@B
FK %U>JMIB 2.9: DO>SFQ>QFLK>I >QQO>@QFLK.)
.LT QE>Q TBfSB PLOQBA LRQ QEB BIBJBKQP KB@BPP>OV CLO QEB J>DKFQRAB LC QEB CLO@B (-1 * E * R),
TB KBBA QL CFDROB LRQ QEB AFOB@QFLK, > RKFQ SB@QLO MLFKQFKD FK QEB AFOB@QFLK LC QEB CLO@B. 4EB
DLLA KBTP FP QE>Q TB >IOB>AV E>SB QEFP SB@QLO. 2FDEQ? *RPQ > JLJBKQ >DL TB QELRDEQ QL
LROPBISBP: g(LT TB @>K @>I@RI>QB QE>Q AFPQ>K@B? (LT >?LRQ QEB J>DKFQRAB LC > SB@QLO QE>Q
MLFKQP COLJ QEB >K@ELO QL QEB ?L??h 7BII, QE>Q FP QEB AFOB@QFLK LC QEB CLO@B!
@FI;N E = 0.1;
A vector pointing from anchor to bob gives
us the current length of the spring.
.3?=NIL >CL = .3?=NIL.MO<(<I<,;H=BIL);
@FI;N =OLL?HN*?HANB = >CL.G;A();
@FI;N R = L?MN*?HANB - =OLL?HN*?HANB;
F64B?2 3.17
#E>MQBO 3. /P@FII>QFLK
136
)K &FDROB 3.17, TB @>K PBB QE>Q FC TB PQOBQ@E QEB PMOFKD ?BVLKA FQP OBPQ IBKDQE, QEBOB PELRIA ?B
> CLO@B MRIIFKD FQ ?>@H QLT>OAP QEB >K@ELO. !KA FC FQ PEOFKHP ?BILT FQP OBPQ IBKDQE, QEB CLO@B
PELRIA MRPE FQ >T>V COLJ QEB >K@ELO. 4EFP OBSBOP>I LC AFOB@QFLK FP >@@LRKQBA CLO FK QEB CLOJRI>
TFQE QEB -1. !KA PL >II TB KBBA QL AL FP KLOJ>IFWB QEB .3?=NIL TB RPBA CLO QEB AFPQ>K@B
@>I@RI>QFLK! ,BQfP Q>HB > ILLH >Q QEB @LAB >KA OBK>JB QE>Q .3?=NIL S>OF>?IB >P gCLO@B.h
.LT QE>Q TB E>SB QEB >IDLOFQEJ TLOHBA LRQ CLO @LJMRQFKD QEB PMOFKD CLO@B SB@QLO, QEB
NRBPQFLK OBJ>FKP: TE>Q L?GB@Q-LOFBKQBA MOLDO>JJFKD PQOR@QROB PELRIA TB RPB? 4EFP, >D>FK, FP
LKB LC QELPB PFQR>QFLKP FK TEF@E QEBOB FP KL g@LOOB@Qh >KPTBO. 4EBOB >OB PBSBO>I MLPPF?FIFQFBP;
TEF@E LKB TB @ELLPB ABMBKAP LK QEB MOLDO>JfP DL>IP >KA LKBfP LTK MBOPLK>I @LAFKD PQVIB.
3QFII, PFK@B TBfSB ?BBK TLOHFKD >II >ILKD TFQE > +IP?L @I>PP, IBQfP HBBM DLFKD TFQE QEFP P>JB
CO>JBTLOH. ,BQfP QEFKH LC LRO +IP?L @I>PP >P QEB PMOFKDfP g?L?.h 4EB ?L? KBBAP FI=;NCIH,
P?FI=CNS, >KA ;==?F?L;NCIH SB@QLOP QL JLSB >?LRQ QEB P@OBBK. 0BOCB@QdTBfSB DLQ QE>Q
>IOB>AV! !KA MBOE>MP QEB ?L? BUMBOFBK@BP > DO>SFQV CLO@B SF> QEB ;JJFS$IL=?() CRK@QFLK. *RPQ
LKB JLOB PQBMdTB KBBA QL >MMIV QEB PMOFKD CLO@B:
Magnitude of spring force according to
Hookes law
@FI;N E = 0.1;
.3?=NIL @IL=? = .3?=NIL.MO<(<I<,;H=BIL);
@FI;N =OLL?HN*?HANB = >CL.G;A();
@FI;N R = L?MN*?HANB - =OLL?HN*?HANB;
Direction of spring force (unit vector) @IL=?.HILG;FCT?();
Putting it together: direction and magnitude! @IL=?.GOFN(-1 * E * R);
I< <I<;
PIC> M?NOJ() U
<I< = H?Q I<();
W
PIC> >L;Q() U
Our Chapter 2 make-up-a-gravity force .3?=NIL AL;PCNS = H?Q .3?=NIL(0,1);
<I<.;JJFS$IL=?(AL;PCNS);
We need to also calculate and apply a
spring force!
+0;9JEH IFH?D="EH9; = 666666666666666????
8E8.7FFBO"EH9;(IFH?D=);
Our standard update() and display()
functions
<I<.OJ>;N?();
<I<.>CMJF;S();
W
4EB .>QROB LC #LAB (S005)
137
/KB LMQFLK TLRIA ?B QL TOFQB LRQ >II LC QEB PMOFKD CLO@B @LAB FK QEB J>FK >L;Q() ILLM. "RQ
QEFKHFKD >EB>A QL TEBK VLR JFDEQ E>SB JRIQFMIB ?L?P >KA JRIQFMIB PMOFKD @LKKB@QFLKP, FQ
J>HBP > DLLA AB>I LC PBKPB QL TOFQB >K >AAFQFLK>I @I>PP, > 0JLCHA @I>PP. !P PELTK FK &FDROB
3.18, QEB I< @I>PP HBBMP QO>@H LC QEB JLSBJBKQP LC QEB ?L?; QEB 0JLCHA @I>PP HBBMP QO>@H
LC QEB PMOFKDfP >K@ELO >KA FQP OBPQ IBKDQE >KA @>I@RI>QBP QEB PMOFKD CLO@B LK QEB ?L?.
4EFP >IILTP RP QL TOFQB > ILSBIV J>FK MOLDO>J >P CLIILTP:
F64B?2 3.18
I< <I<;
Adding a Spring object -FH?D= IFH?D=;
PIC> M?NOJ() U
<I< = H?Q I<();
MJLCHA = H?Q 0JLCHA();
W
PIC> >L;Q() U
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,1);
<I<.;JJFS$IL=?(AL;PCNS);
This new function in the Spring class will
take care of computing the force of the
spring on the bob.
IFH?D=.9EDD;9J(8E8);
<I<.OJ>;N?();
<I<.>CMJF;S();
MJLCHA.>CMJF;S();
W
#E>MQBO 3. /P@FII>QFLK
138
9LR J>V KLQF@B EBOB QE>Q QEFP FP NRFQB PFJFI>O QL TE>Q TB AFA FK %U>JMIB 2.6 (PBB M>DB 94) TFQE
>K >QQO>@QLO. 4EBOB, TB P>FA PLJBQEFKD IFHB:
4EB >K>ILDLRP PFQR>QFLK EBOB TFQE > PMOFKD TLRIA ?B:
.BSBOQEBIBPP, FK QEFP BU>JMIB >II TB P>FA T>P:
7E>Q DFSBP? 7EV ALKfQ TB KBBA QL @>II ;JJFS$IL=?() LK QEB ?L?? 4EB >KPTBO FP, LC @LROPB,
QE>Q TB AL KBBA QL @>II ;JJFS$IL=?() LK QEB ?L?. /KIV FKPQB>A LC ALFKD FQ FK >L;Q(), TBfOB
GRPQ ABJLKPQO>QFKD QE>Q > MBOCB@QIV OB>PLK>?IB (>KA PLJBQFJBP MOBCBO>?IB) >IQBOK>QFSB FP QL >PH
QEB =IHH?=N() CRK@QFLK QL FKQBOK>IIV E>KAIB @>IIFKD ;JJFS$IL=?() LK QEB ?L?.
7EV AL FQ LKB T>V TFQE QEB ANNL;=NIL @I>PP >KA >KLQEBO T>V TFQE QEB 0JLCHA @I>PP? 7EBK TB
TBOB CFOPQ IB>OKFKD >?LRQ CLO@BP, FQ T>P > ?FQ @IB>OBO QL PELT >II QEB CLO@BP ?BFKD >MMIFBA FK QEB
J>FK >L;Q() ILLM, >KA ELMBCRIIV QEFP EBIMBA VLR IB>OK >?LRQ CLO@B >@@RJRI>QFLK. .LT QE>Q
TBfOB JLOB @LJCLOQ>?IB TFQE QE>Q, MBOE>MP FQfP PFJMIBO QL BJ?BA PLJB LC QEB ABQ>FIP FKPFAB QEB
L?GB@QP QEBJPBISBP.
,BQfP Q>HB > ILLH >Q QEB OBPQ LC QEB BIBJBKQP FK QEB 0JLCHA @I>PP.
.3?=NIL @IL=? = ;NNL;=NIL.;NNL;=N(GIP?L);
GIP?L.;JJFS$IL=?(@IL=?);
.3?=NIL @IL=? = MJLCHA.=IHH?=N(<I<);
<I<.;JJFS$IL=?(@IL=?);
MJLCHA.=IHH?=N(<I<);
PIC> =IHH?=N(I< <) U
.3?=NIL @IL=? = MIG? @;H=S =;F=OF;NCIHM
The function connect() takes care of calling
applyForce() and therefore doesnt have to
return a vector to the calling area.
<.;JJFS$IL=?(@IL=?);
W
4EB .>QROB LC #LAB (S005)
139
Example 3.11: A Spring connection
4EB CRII @LAB CLO QEFP BU>JMIB FP FK@IRABA LK QEB ?LLH TB?PFQB, >KA QEB 7B? SBOPFLK >IPL
FK@LOMLO>QBP QTL >AAFQFLK>I CB>QROBP: (1) QEB I< @I>PP FK@IRABP CRK@QFLKP CLO JLRPB
=F;MM 0JLCHA U
We need to keep track of the springs
anchor location.
.3?=NIL ;H=BIL;
Rest length and spring constant variables @FI;N F?H;
@FI;N E = 0.1;
The constructor initializes the anchor point
and rest length.
0JLCHA(@FI;N R, @FI;N S, CHN F) U
;H=BIL = H?Q .3?=NIL(R,S);
F?H = F;
W
Calculate spring forceour implementation
of Hookes Law.
PIC> =IHH?=N(I< <) U
Get a vector pointing from anchor to Bob
location.
+0;9JEH <EH9; =
+0;9JEH.IK8(8.BE97J?ED,7D9>EH);
<BE7J : = <EH9;.C7=();
Calculate the displacement between
distance and rest length.
<BE7J IJH;J9> = : - B;D;
Direction and magnitude together! <EH9;.DEHC7B?P;();
<EH9;.CKBJ(-1 * A * IJH;J9>);
Call applyForce() right here! <.;JJFS$IL=?(@IL=?);
W
Draw the anchor. PIC> >CMJF;S() U
@CFF(100);
L?=N+I>?(!#,1#/);
L?=N(;H=BIL.R,;H=BIL.S,10,10);
W
Draw the spring connection between Bob
location and anchor.
PIC> >CMJF;S*CH?(I< <) U
MNLIE?(255);
FCH?(<.FI=;NCIH.R,<.FI=;NCIH.S,;H=BIL.R,;H=BIL.S);
W
W
#E>MQBO 3. /P@FII>QFLK
140
FKQBO>@QFSFQV PL QE>Q QEB ?L? @>K ?B AO>DDBA >OLRKA QEB TFKALT, >KA (2) QEB 0JLCHA L?GB@Q
FK@IRABP > CRK@QFLK QL @LKPQO>FK QEB @LKKB@QFLKfP IBKDQE ?BQTBBK > JFKFJRJ >KA > J>UFJRJ.
"BCLOB ORKKFKD QL PBB QEB BU>JMIB LKIFKB, Q>HB > ILLH >Q QEFP @LKPQO>FK CRK@QFLK >KA PBB
FC VLR @>K CFII FK QEB ?I>KHP.
PIC> =IHMNL;CH*?HANB(I< <, @FI;N GCHF?H, @FI;N G;RF?H) U
Vector pointing from Bob to Anchor .3?=NIL >CL = .3?=NIL.MO<(::::::,::::::);
@FI;N > = >CL.G;A();
Is it too short? C@ (> < GCHF?H) U
>CL.HILG;FCT?();
>CL.GOFN(::::::::);
Keep location within constraint. <.FI=;NCIH = .3?=NIL.;>>(::::::,::::::);
<.P?FI=CNS.GOFN(0);
Is it too long? W ?FM? C@ (::::::::::::) U
>CL.HILG;FCT?();
>CL.GOFN(:::::::::);
Keep location within constraint. <.FI=;NCIH = .3?=NIL.;>>(::::::,::::::);
<.P?FI=CNS.GOFN(0);
W
W
Exercise 3.15 Exercise 3.15
#OB>QB > PVPQBJ LC JRIQFMIB ?L?P >KA PMOFKD @LKKB@QFLKP. (LT TLRIA VLR E>SB > ?L?
@LKKB@QBA QL > ?L? TFQE KL CFUBA >K@ELO?
Exercise 3.16 Exercise 3.16
4EB .>QROB LC #LAB (S005)
141
The Ecosystem Project The Ecosystem Project
Step 3 Exercise:
Take one of your creatures and incorporate oscillation into its motion. You can
use the -M=CFF;NIL class from Example 3.7 as a model. The -M=CFF;NIL object,
however, oscillates around a single point (the middle of the window). Try
oscillating around a moving point. In other words, design a creature that moves
around the screen according to location, velocity, and acceleration. But that
creature isnt just a static shape, its an oscillating body. Consider tying the speed
of oscillation to the speed of motion. Think of a butterflys flapping wings or the
legs of an insect. Can you make it appear that the creatures internal mechanics
(oscillation) drive its locomotion? For a sample, check out the
AttractionArrayWithOscillation example with the code download.
#E>MQBO 3. /P@FII>QFLK
142
Chapter 4. Particle Chapter 4. Particle
Systems Systems
I*5.A 6@ D6@2. ,2?2 ! A< 6;C<82 9<460, 5<D2C2?, 9<460 092.?9F 160A.A2@ A5.A
A52 ;221@ <3 A52 :.;F <BAD2645 A52 ;221@ <3 A52 32D.J
Spock
)K 1982, 7FIIF>J 4. 2BBSBP, > OBPB>O@EBO >Q ,R@>PCFIJ ,QA., T>P TLOHFKD LK QEB CFIJ .H5F /F9? %%:
/<9 2F5H< C: '<5B. -R@E LC QEB JLSFB OBSLISBP >OLRKA QEB 'BKBPFP $BSF@B, > QLOMBAL QE>Q
TEBK PELQ >Q > ?>OOBK, IFCBIBPP MI>KBQ E>P QEB >?FIFQV QL OBLOD>KFWB J>QQBO >KA @OB>QB >
E>?FQ>?IB TLOIA CLO @LILKFW>QFLK. $ROFKD QEB PBNRBK@B, > T>II LC CFOB OFMMIBP LSBO QEB MI>KBQ
TEFIB FQ FP ?BFKD gQBOO>CLOJBA.h 4EB QBOJ ;,=?4.70 >D>?08 ;,=?4.70 >D>?08, >K FK@OBAF?IV @LJJLK >KA RPBCRI
QB@EKFNRB FK @LJMRQBO DO>MEF@P, T>P @LFKBA FK QEB @OB>QFLK LC QEFP M>OQF@RI>O BCCB@Q.
IA =.?A6092 @F@A2: 6@ . 0<9920A6<; <3 :.;F :.;F :6;BA2 =.?A6092@ A5.A A<42A52? ?2=?2@2;A .
3BGGF </720A. &C2? . =2?6<1 <3 A6:2, =.?A6092@ .?2 42;2?.A21 6;A< . @F@A2:, :<C2 .;1 05.;42
3?<: D6A56; A52 @F@A2:, .;1 162 3?<: A52 @F@A2:.J
H,6996.: (22C2@, "'.?A6092 )F@A2:@HA *205;6>B2 3<? $<1296;4 . C9.@@ <3 FBGGF
&/720A@," ACM Transactions on Graphics 2:2 (A=?69 1983), 92.
3FK@B QEB B>OIV 1980P, M>OQF@IB PVPQBJP E>SB ?BBK RPBA FK @LRKQIBPP SFABL D>JBP, >KFJ>QFLKP,
AFDFQ>I >OQ MFB@BP, >KA FKPQ>II>QFLKP QL JLABI S>OFLRP FOOBDRI>O QVMBP LC K>QRO>I MEBKLJBK>,
PR@E >P CFOB, PJLHB, T>QBOC>IIP, CLD, DO>PP, ?R??IBP, >KA PL LK.
4EFP @E>MQBO TFII ?B ABAF@>QBA QL ILLHFKD >Q FJMIBJBKQ>QFLK PQO>QBDFBP CLO @LAFKD > M>OQF@IB
PVPQBJ. (LT AL TB LOD>KFWB LRO @LAB? 7EBOB AL TB PQLOB FKCLOJ>QFLK OBI>QBA QL FKAFSFAR>I
M>OQF@IBP SBOPRP FKCLOJ>QFLK OBI>QBA QL QEB PVPQBJ >P > TELIB? 4EB BU>JMIBP TBfII ILLH >Q TFII
4EB .>QROB LC #LAB (S005)
143
CL@RP LK J>K>DFKD QEB A>Q> >PPL@F>QBA TFQE > M>OQF@IB PVPQBJ. 4EBVfII RPB PFJMIB PE>MBP CLO
QEB M>OQF@IBP >KA >MMIV LKIV QEB JLPQ ?>PF@ ?BE>SFLOP (PR@E >P DO>SFQV). (LTBSBO, ?V RPFKD
QEFP CO>JBTLOH >KA ?RFIAFKD FK JLOB FKQBOBPQFKD T>VP QL OBKABO QEB M>OQF@IBP >KA @LJMRQB
?BE>SFLOP, VLR @>K >@EFBSB > S>OFBQV LC BCCB@QP.
4.1 Why We Need Particle Systems 4.1 Why We Need Particle Systems
7BfSB ABCFKBA > M>OQF@IB PVPQBJ QL ?B > @LIIB@QFLK LC FKABMBKABKQ L?GB@QP, LCQBK
OBMOBPBKQBA ?V > PFJMIB PE>MB LO ALQ. 7EV ALBP QEFP J>QQBO? #BOQ>FKIV, QEB MOLPMB@Q LC
JLABIFKD PLJB LC QEB MEBKLJBK> TB IFPQBA (BUMILPFLKP!) FP >QQO>@QFSB >KA MLQBKQF>IIV RPBCRI.
"RQ OB>IIV, QEBOBfP >K BSBK ?BQQBO OB>PLK CLO RP QL @LK@BOK LROPBISBP TFQE M>OQF@IB PVPQBJP. )C
TB T>KQ QL DBQ >KVTEBOB FK QEFP K>QROB LC @LAB IFCB, TBfOB DLFKD QL KBBA QL TLOH TFQE
PVPQBJP LC A5BM QEFKDP. 7BfOB DLFKD QL T>KQ QL ILLH >Q ?>IIP ?LRK@FKD, ?FOAP CIL@HFKD,
B@LPVPQBJP BSLISFKD, >II PLOQP LC QEFKDP FK MIRO>I.
*RPQ >?LRQ BSBOV @E>MQBO >CQBO QEFP LKB FP DLFKD QL KBBA QL AB>I TFQE > IFPQ LC L?GB@QP. 9BP,
TBfSB ALKB QEFP TFQE >K >OO>V FK PLJB LC LRO CFOPQ SB@QLO >KA CLO@BP BU>JMIBP. "RQ TB KBBA
QL DL TEBOB KL >OO>V E>P DLKB ?BCLOB.
&FOPQ, TBfOB DLFKD QL T>KQ QL AB>I TFQE CIBUF?IB NR>KQFQFBP LC BIBJBKQP. 3LJBQFJBP TBfII E>SB
WBOL QEFKDP, PLJBQFJBP LKB QEFKD, PLJBQFJBP QBK QEFKDP, >KA PLJBQFJBP QBK QELRP>KA
QEFKDP. 3B@LKA, TBfOB DLFKD QL T>KQ QL Q>HB > JLOB PLMEFPQF@>QBA L?GB@Q-LOFBKQBA >MMOL>@E.
)KPQB>A LC PFJMIV TOFQFKD > @I>PP QL ABP@OF?B > PFKDIB M>OQF@IB, TBfOB >IPL DLFKD QL T>KQ QL
TOFQB > @I>PP QE>Q ABP@OF?BP QEB @LIIB@QFLK LC M>OQF@IBPdQEB M>OQF@IB PVPQBJ FQPBIC. 4EB DL>I
EBOB FP QL ?B >?IB QL TOFQB > J>FK MOLDO>J QE>Q ILLHP IFHB QEB CLIILTFKD:
.L PFKDIB M>OQF@IB FP BSBO OBCBOBK@BA FK QEB >?LSB @LAB, VBQ QEB OBPRIQ TFII ?B CRII LC M>OQF@IBP
CIVFKD >II LSBO QEB P@OBBK. 'BQQFKD RPBA QL TOFQFKD 0OL@BPPFKD PHBQ@EBP TFQE JRIQFMIB @I>PPBP,
>KA @I>PPBP QE>Q HBBM IFPQP LC FKPQ>K@BP LC LQEBO @I>PPBP, TFII MOLSB SBOV RPBCRI >P TB DBQ QL
JLOB >AS>K@BA @E>MQBOP FK QEFP ?LLH.
&FK>IIV, TLOHFKD TFQE M>OQF@IB PVPQBJP FP >IPL > DLLA BU@RPB CLO RP QL Q>@HIB QTL LQEBO
>AS>K@BA L?GB@Q-LOFBKQBA MOLDO>JJFKD QB@EKFNRBP: FKEBOFQ>K@B >KA MLIVJLOMEFPJ. 7FQE QEB
Ah, isnt this main program so simple and
lovely?
.;LNC=F?0SMN?G JM;
PIC> M?NOJ() U
MCT?(200,200);
JM = H?Q .;LNC=F?0SMN?G();
W
PIC> >L;Q() U
<;=EALIOH>(255);
JM.LOH();
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
144
BU>JMIBP TBfSB PBBK RM RKQFI KLT, TBfSB >IT>VP E>A >K >OO>V LC > PFKDIB QVMB LC L?GB@Q, IFHB
"JLSBOP" LO gLP@FII>QLOP.h 7FQE FKEBOFQ>K@B (>KA MLIVJLOMEFPJ), TBfII IB>OK > @LKSBKFBKQ T>V QL
PQLOB > PFKDIB IFPQ QE>Q @LKQ>FKP L?GB@QP LC AFCCBOBKQ QVMBP. 4EFP T>V, > M>OQF@IB PVPQBJ KBBA KLQ
LKIV ?B > PVPQBJ LC > PFKDIB QVMB LC M>OQF@IB.
4ELRDE FQ J>V PBBJ L?SFLRP QL VLR, )fA >IPL IFHB QL MLFKQ LRQ QE>Q QEBOB >OB QVMF@>I
FJMIBJBKQ>QFLKP LC M>OQF@IB PVPQBJP, >KA QE>QfP TEBOB TB TFII ?BDFK FK QEFP @E>MQBO. (LTBSBO,
QEB C>@Q QE>Q QEB M>OQF@IBP FK QEFP @E>MQBO ILLH LO ?BE>SB > @BOQ>FK T>V PELRIA KLQ IFJFQ VLRO
FJ>DFK>QFLK. *RPQ ?B@>RPB M>OQF@IB PVPQBJP QBKA QL ILLH PM>OHIV, CIV CLOT>OA, >KA C>II TFQE
DO>SFQV ALBPKfQ JB>K QE>Q QELPB >OB QEB @E>O>@QBOFPQF@P VLROP PELRIA E>SB.
4EB CL@RP EBOB FP OB>IIV GRPQ ELT QL HBBM QO>@H LC > PVPQBJ LC J>KV BIBJBKQP. 7E>Q QELPB
BIBJBKQP AL >KA ELT QELPB BIBJBKQP ILLH FP RM QL VLR.
4.2 A Single Particle 4.2 A Single Particle
"BCLOB TB @>K DBQ OLIIFKD LK QEB PVPQBJ FQPBIC, TB E>SB QL TOFQB QEB @I>PP QE>Q TFII ABP@OF?B >
PFKDIB M>OQF@IB. 4EB DLLA KBTP: TBfSB ALKB QEFP >IOB>AV. /RO +IP?L @I>PP COLJ #E>MQBO 2
PBOSBP >P QEB MBOCB@Q QBJMI>QB. &LO RP, > M>OQF@IB FP >K FKABMBKABKQ ?LAV QE>Q JLSBP >?LRQ QEB
P@OBBK. )Q E>P FI=;NCIH, P?FI=CNS, >KA ;==?F?L;NCIH, > @LKPQOR@QLO QL FKFQF>IFWB QELPB
S>OF>?IBP, >KA CRK@QFLKP QL >CMJF;S() FQPBIC >KA OJ>;N?() FQP IL@>QFLK.
4EFP FP >?LRQ >P PFJMIB >P > M>OQF@IB @>K DBQ. &OLJ EBOB, TB @LRIA Q>HB LRO M>OQF@IB FK PBSBO>I
AFOB@QFLKP. 7B @LRIA >AA >K ;JJFS$IL=?() CRK@QFLK QL >CCB@Q QEB M>OQF@IBfP ?BE>SFLO (TBfII AL
=F;MM .;LNC=F? U
A Particle object is just another name for
our Mover. It has location, velocity, and
acceleration.
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
.;LNC=F?(.3?=NIL F) U
FI=;NCIH = F.A?N();
;==?F?L;NCIH = H?Q .3?=NIL();
P?FI=CNS = H?Q .3?=NIL();
W
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(175);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
W
4EB .>QROB LC #LAB (S005)
145
MOB@FPBIV QEFP FK > CRQROB BU>JMIB). 7B @LRIA >AA S>OF>?IBP QL ABP@OF?B @LILO >KA PE>MB, LO
OBCBOBK@B > .'G;A? QL AO>T QEB M>OQF@IB. &LO KLT, ELTBSBO, IBQfP CL@RP LK >AAFKD GRPQ LKB
>AAFQFLK>I ABQ>FI: 7410>;,9 7410>;,9.
4VMF@>I M>OQF@IB PVPQBJP FKSLISB PLJBQEFKD @>IIBA >K 084??0= 084??0=. 4EB BJFQQBO FP QEB PLRO@B LC
QEB M>OQF@IBP >KA @LKQOLIP QEB FKFQF>I PBQQFKDP CLO QEB M>OQF@IBP, IL@>QFLK, SBIL@FQV, BQ@. !K
BJFQQBO JFDEQ BJFQ > PFKDIB ?ROPQ LC M>OQF@IBP, LO > @LKQFKRLRP PQOB>J LC M>OQF@IBP, LO ?LQE.
4EB MLFKQ FP QE>Q CLO > QVMF@>I FJMIBJBKQ>QFLK PR@E >P QEFP, > M>OQF@IB FP ?LOK >Q QEB BJFQQBO
?RQ ALBP KLQ IFSB CLOBSBO. )C FQ TBOB QL IFSB CLOBSBO, LRO 0OL@BPPFKD PHBQ@E TLRIA BSBKQR>IIV
DOFKA QL > E>IQ >P QEB KRJ?BO LC M>OQF@IBP FK@OB>PBP QL >K RKTFBIAV KRJ?BO LSBO QFJB. !P
KBT M>OQF@IBP >OB ?LOK, TB KBBA LIA M>OQF@IBP QL AFB. 4EFP @OB>QBP QEB FIIRPFLK LC >K FKCFKFQB
PQOB>J LC M>OQF@IBP, >KA QEB MBOCLOJ>K@B LC LRO MOLDO>J ALBP KLQ PRCCBO. 4EBOB >OB J>KV
AFCCBOBKQ T>VP QL AB@FAB TEBK > M>OQF@IB AFBP. &LO BU>JMIB, FQ @LRIA @LJB FKQL @LKQ>@Q TFQE
>KLQEBO L?GB@Q, LO FQ @LRIA PFJMIV IB>SB QEB P@OBBK. &LO LRO CFOPQ .;LNC=F? @I>PP, ELTBSBO,
TBfOB PFJMIV DLFKD QL >AA > FC@?MJ;H S>OF>?IB. 4EB QFJBO TFII PQ>OQ >Q 255 >KA @LRKQ ALTK
QL 0, TEBK QEB M>OQF@IB TFII ?B @LKPFABOBA gAB>A.h !KA PL TB BUM>KA QEB .;LNC=F? @I>PP >P
CLIILTP:
4EB OB>PLK TB @ELPB QL PQ>OQ QEB IFCBPM>K >Q 255 >KA @LRKQ ALTK QL 0 FP CLO @LKSBKFBK@B.
7FQE QELPB S>IRBP, TB @>K >PPFDK FC@?MJ;H QL >@Q >P QEB >IME> QO>KPM>OBK@V CLO QEB BIIFMPB
>P TBII. 7EBK QEB M>OQF@IB FP gAB>Ah FQ TFII >IPL E>SB C>ABA >T>V LKP@OBBK.
=F;MM .;LNC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
A new variable to keep track of how long
the particle has been alive
<BE7J B?<;IF7D;
.;LNC=F?(.3?=NIL F) U
FI=;NCIH = F.A?N();
;==?F?L;NCIH = H?Q .3?=NIL();
P?FI=CNS = H?Q .3?=NIL();
We start at 255 and count down for
convenience
B?<;IF7D = 255;
W
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
Lifespan decreases B?<;IF7D -= 2.0;
W
PIC> >CMJF;S() U
Since our life ranges from 255 to 0 we can
use it for alpha
IJHEA;(0,B?<;IF7D);
<?BB(175,B?<;IF7D);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
146
7FQE QEB >AAFQFLK LC QEB FC@?MJ;H S>OF>?IB, TBfII >IPL KBBA LKB >AAFQFLK>I CRK@QFLKd> CRK@QFLK
QE>Q @>K ?B NRBOFBA (CLO > QORB LO C>IPB >KPTBO) >P QL TEBQEBO QEB M>OQF@IB FP >IFSB LO AB>A. 4EFP
TFII @LJB FK E>KAV TEBK TB >OB TOFQFKD QEB .;LNC=F?0SMN?G @I>PP, TELPB Q>PH TFII ?B QL
J>K>DB QEB IFPQ LC M>OQF@IBP QEBJPBISBP. 7OFQFKD QEFP CRK@QFLK FP MOBQQV B>PV; TB GRPQ KBBA QL
@EB@H >KA PBB FC QEB S>IRB LC FC@?MJ;H FP IBPP QE>K 0. )C FQ FP TB L?NOLH NLO?, FC KLQ TB
L?NOLH @;FM?.
"BCLOB TB DBQ QL QEB KBUQ PQBM LC J>HFKD J>KV M>OQF@IBP, FQfP TLOQE Q>HFKD > JLJBKQ QL J>HB
PROB LRO M>OQF@IB TLOHP @LOOB@QIV >KA @OB>QB > PHBQ@E TFQE LKB PFKDIB .;LNC=F? L?GB@Q. (BOB FP
QEB CRII @LAB ?BILT, TFQE QTL PJ>II >AAFQFLKP. 7B >AA > @LKSBKFBK@B CRK@QFLK @>IIBA LOH()
QE>Q PFJMIV @>IIP ?LQE OJ>;N?() >KA >CMJF;S() CLO RP. )K >AAFQFLK, TB DFSB QEB M>OQF@IB >
O>KALJ FKFQF>I SBIL@FQV >P TBII >P > ALTKT>OA >@@BIBO>QFLK (QL PFJRI>QB DO>SFQV).
Example 4.1: A single particle
<IIF?;H CM"?;>() U
Is the particle still alive? C@ (FC@?MJ;H < 0.0) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
.;LNC=F? J;
PIC> M?NOJ() U
MCT?(200,200);
J = H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,10));
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
4EB .>QROB LC #LAB (S005)
147
Operating the single Particle J.LOH();
C@ (J.CM"?;>()) U
JLCHNFH(".;LNC=F? >?;>!");
W
W
=F;MM .;LNC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N FC@?MJ;H;
.;LNC=F?(.3?=NIL F) U
;==?F?L;NCIH = H?Q .3?=NIL(0,0.05);
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0));
FI=;NCIH = F.A?N();
FC@?MJ;H = 255.0;
W
For demonstration purposes we assign the
Particle an initial velocity and constant
acceleration.
Sometimes its convenient to have a run
function that calls all the other functions we
need.
PIC> LOH() U
OJ>;N?();
>CMJF;S();
W
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
FC@?MJ;H -= 2.0;
W
PIC> >CMJF;S() U
MNLIE?(0,FC@?MJ;H);
@CFF(0,FC@?MJ;H);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
Is the Particle alive or dead? <IIF?;H CM"?;>() U
C@ (FC@?MJ;H < 0.0) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
148
.LT QE>Q TB E>SB > @I>PP QL ABP@OF?B > PFKDIB M>OQF@IB, TBfOB OB>AV CLO QEB KBUQ ?FD PQBM. (LT
AL TB HBBM QO>@H LC J>KV M>OQF@IBP, TEBK TB @>KfQ BKPROB BU>@QIV ELT J>KV M>OQF@IBP TB
JFDEQ E>SB >Q >KV DFSBK QFJB?
2BTOFQB QEB BU>JMIB PL QE>Q QEB M>OQF@IB @>K OBPMLKA QL CLO@B SB@QLOP SF> >K
;JJFS$IL=?() CRK@QFLK.
Exercise 4.1 Exercise 4.1
!AA >KDRI>O SBIL@FQV (OLQ>QFLK) QL QEB M>OQF@IB. #OB>QB VLRO LTK KLK-@FO@IB M>OQF@IB
ABPFDK.
Exercise 4.2 Exercise 4.2
4.3 The ArrayList 4.3 The ArrayList
)K QORQE, TB @LRIA RPB > PFJMIB >OO>V QL J>K>DB LRO .;LNC=F? L?GB@QP. 3LJB M>OQF@IB PVPQBJP
JFDEQ E>SB > CFUBA KRJ?BO LC M>OQF@IBP, >KA >OO>VP >OB J>DKFCF@BKQIV BCCF@FBKQ FK QELPB
FKPQ>K@BP. 0OL@BPPFKD >IPL LCCBOP ?RJ;H>(), =IHNL;=N(), MO<M?N(), MJFC=?(), >KA LQEBO
JBQELAP CLO OBPFWFKD >OO>VP. (LTBSBO, CLO QEBPB BU>JMIBP, TBfOB DLFKD QL Q>HB > JLOB
PLMEFPQF@>QBA >MMOL>@E >KA RPB QEB *>S> @I>PP ALL;S*CMN, CLRKA FK QEB G>S>.RQFI M>@H>DB
!OO>V,FPQ $L@RJBKQ>QFLK (EQQM://ALTKIL>A.LO>@IB.@LJ/G>S>PB/6/AL@P/>MF/G>S>/RQFI/
!OO>V,FPQ.EQJI).
5PFKD >K ALL;S*CMN CLIILTP QEB P>JB FAB> >P RPFKD > PQ>KA>OA >OO>V, ?RQ TFQE AFCCBOBKQ PVKQ>U.
4EB CLIILTFKD QTL @LAB BU>JMIBP (TEF@E >PPRJB QEB BUFPQBK@B LC > DBKBOF@ .;LNC=F? @I>PP)
MOLAR@B QEB P>JB OBPRIQ: CFOPQ TFQE >K >OO>V, >KA PB@LKA TFQE >K ALL;S*CMN.
4EB PQ>KA>OA >OO>V T>V:
CHN NIN;F = 10;
.;LNC=F?89 J;LL;S = H?Q .;LNC=F?8NIN;F9;
PIC> M?NOJ() U
This is what were used to, accessing
elements on the array via an index and
brackets[ ].
@IL (CHN C = 0; C < J;LL;S.F?HANB; C++) U
J;LL;S8C9 = H?Q .;LNC=F?();
W
4EB .>QROB LC #LAB (S005)
149
4EB KBT ALL;S*CMN T>V:
4EFP I>PQ @IL ILLM ILLHP MOBQQV PFJFI>O QL LRO @LAB QE>Q ILLMBA QEOLRDE > OBDRI>O >OO>V ?V
>@@BPPFKD B>@E FKABU. 7B FKFQF>IFWB > S>OF>?IB @>IIBA C QL 0 >KA @LRKQ RM ?V 1, >@@BPPFKD
B>@E BIBJBKQ LC QEB ALL;S*CMN RKQFI TB DBQ QL QEB BKA. (LTBSBO, QEFP FP > KF@B JLJBKQ QL
JBKQFLK QEB gBKE>K@BA @IL ILLMh >S>FI>?IB FK *>S> (>KA 0OL@BPPFKD) TEF@E FP > ?FQ JLOB
@LK@FPB. 4EB BKE>K@BA ILLM TLOHP TFQE ?LQE ALL;S*CMNP >KA OBDRI>O >OO>VP >KA ILLHP IFHB
QEFP:
,BQfP QO>KPI>QB QE>Q. 3>V gCLO B>@Eh FKPQB>A LC gCLOh >KA P>V gFKh FKPQB>A LC g:h. .LT VLR E>SB:
g&LO B>@E 0>OQF@IB M FK M>OQF@IBP, ORK QE>Q 0>OQF@IB M!h
) HKLT. 9LR @>KKLQ @LKQ>FK VLRO BU@FQBJBKQ. ) @>KfQ. ) HKLT FQfP KLQ KB@BPP>OV, ?RQ ) GRPQ E>SB
QL QVMB QE>Q >D>FK.
W
PIC> >L;Q() U
@IL (CHN C = 0; C < J;LL;S.F?HANB; C++) U
.;LNC=F? J = J;LL;S8C9;
J.LOH();
W
W
CHN NIN;F = 10;
ALL;S*CMN<.;LNC=F?> JFCMN = H?Q ALL;S*CMN<.;LNC=F?>();
PIC> M?NOJ() U
@IL (CHN C = 0; C < NIN;F; C++) U
Have you ever seen this syntax before?
This is a new feature in Java 1.6 (called
"generics") that Processing now supports.
It allows us to specify in advance what type
of object we intend to put in the ArrayList.
An object is added to an ArrayList with
add().
JFCMN.;>>(H?Q .;LNC=F?());
W
W
PIC> >L;Q() U
The size of the ArrayList is returned by
size().
@IL (CHN C = 0; C < JFCMN.MCT?(); C++) U
An object is accessed from the ArrayList
with get(). Because we are using generics,
we do not need to specify a type when we
pull objects out of the ArrayList.
.;LNC=F? J = JFCMN.A?N(C);
J.LOH();
W
W
ALL;S*CMN<.;LNC=F?> JFCMN = H?Q ALL;S*CMN<.;LNC=F?>();
@IL (.;LNC=F? J: J;LNC=F?M) U
J.LOH();
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
150
3FJMIB, BIBD>KQ, @LK@FPB, ILSBIV. 4>HB > JLJBKQ. "OB>QEB. ) E>SB PLJB ?>A KBTP. 9BP, TB
ILSB QE>Q BKE>K@BA ILLM >KA TB TFII DBQ QL RPB FQ. "RQ KLQ OFDEQ KLT. /RO M>OQF@IB PVPQBJ
BU>JMIBP TFII OBNRFOB > CB>QROB QE>Q J>HBP RPFKD QE>Q ILLM FJMLPPF?IB. ,BQfP @LKQFKRB.
4EB @LAB TBfSB TOFQQBK >?LSB ALBPKfQ Q>HB >AS>KQ>DB LC QEB ALL;S*CMNfP OBPFW>?FIFQV, >KA FQ
RPBP > CFUBA PFWB LC 10. 7B KBBA QL ABPFDK >K BU>JMIB QE>Q CFQP TFQE LRO M>OQF@IB PVPQBJ
P@BK>OFL, TEBOB TB BJFQ > @LKQFKRLRP PQOB>J LC .;LNC=F? L?GB@QP, >AAFKD LKB KBT M>OQF@IB
TFQE B>@E @V@IB QEOLRDE >L;Q(). 7BfII PHFM OBE>PEFKD QEB .;LNC=F? @I>PP @LAB EBOB, >P FQ
ALBPKfQ KBBA QL @E>KDB.
2RK QEB >?LSB @LAB CLO > CBT JFKRQBP >KA VLRfII PQ>OQ QL PBB QEB CO>JB O>QB PILT ALTK CROQEBO
>KA CROQEBO RKQFI QEB MOLDO>J DOFKAP QL > E>IQ (JV QBPQP VFBIABA ELOOFCF@ MBOCLOJ>K@B >CQBO
CFCQBBK JFKRQBP). 4EB FPPRB LC @LROPB FP QE>Q TB >OB @OB>QFKD JLOB >KA JLOB M>OQF@IBP TFQELRQ
OBJLSFKD >KV.
&LOQRK>QBIV, QEB ALL;S*CMN @I>PP E>P > @LKSBKFBKQ L?GIP?() CRK@QFLK QE>Q >IILTP RP QL ABIBQB
> 0>OQF@IB (?V OBCBOBK@FKD FQP FKABU). 4EFP FP TEV TB @>KKLQ RPB QEB KBT BKE>K@BA @IL ILLM TB
GRPQ IB>OKBA; QEB BKE>K@BA ILLM MOLSFABP KL JB>KP CLO ABIBQFKD BIBJBKQP TEFIB FQBO>QFKD.
(BOB, TB T>KQ QL @>II L?GIP?() TEBK QEB 0>OQF@IBfP CM"?;>() CRK@QFLK OBQROKP QORB.
This enhanced loop also works for regular
arrays!
@IL (.;LNC=F? J : J;LNC=F?M) U
J.LOH();
W
ALL;S*CMN<.;LNC=F?> J;LNC=F?M;
PIC> M?NOJ() U
MCT?(200,200);
J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
W
PIC> >L;Q() U
<;=EALIOH>(255);
J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50)));
@IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U
.;LNC=F? J = J;LNC=F?M.A?N(C);
J.LOH();
W
W
A new Particle object is added to the
ArrayList every cycle through draw().
@IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U
.;LNC=F? J = J;LNC=F?M.A?N(C);
J.LOH();
If the Particle is dead, we can go ahead
and delete it from the list.
C@ (J.CM"?;>()) U
J;LNC=F?M.L?GIP?(C);
W
W
4EB .>QROB LC #LAB (S005)
151
!IQELRDE QEB >?LSB @LAB TFII ORK GRPQ CFKB (>KA QEB MOLDO>J TFII KBSBO DOFKA QL > E>IQ), TB
E>SB LMBKBA RM > JBAFRJ-PFWBA @>K LC TLOJP. 7EBKBSBO TB J>KFMRI>QB QEB @LKQBKQP LC >
IFPQ TEFIB FQBO>QFKD QEOLRDE QE>Q SBOV IFPQ, TB @>K DBQ LROPBISBP FKQL QOLR?IB. 4>HB, CLO
BU>JMIB, QEB CLIILTFKD @LAB.
4EFP FP > PLJBTE>Q BUQOBJB BU>JMIB (TFQE CI>TBA ILDF@), ?RQ FQ MOLSBP QEB MLFKQ. )K QEB
>?LSB @>PB, CLO B>@E M>OQF@IB FK QEB IFPQ, TB >AA > KBT M>OQF@IB QL QEB IFPQ (J>KFMRI>QFKD QEB
MCT?() LC QEB ALL;S*CMN). 4EFP TFII OBPRIQ FK >K FKCFKFQB ILLM, >P C @>K KBSBO FK@OBJBKQ M>PQ
QEB PFWB LC QEB ALL;S*CMN.
7EFIB OBJLSFKD BIBJBKQP COLJ QEB ALL;S*CMN AROFKD > ILLM ALBPKfQ @>RPB QEB MOLDO>J QL
@O>PE (>P FQ ALBP TFQE >AAFKD), QEB MOL?IBJ FP >IJLPQ JLOB FKPFAFLRP FK QE>Q FQ IB>SBP KL
BSFABK@B. 4L AFP@LSBO QEB MOL?IBJ TB JRPQ CFOPQ BPQ>?IFPE >K FJMLOQ>KQ C>@Q. 7EBK >K L?GB@Q
FP OBJLSBA COLJ QEB ALL;S*CMN, >II BIBJBKQP >OB PEFCQBA LKB PMLQ QL QEB IBCQ. .LQB QEB
AF>DO>J ?BILT TEBOB M>OQF@IB # (FKABU 2) FP OBJLSBA. 0>OQF@IBP ! >KA " HBBM QEB P>JB
FKABU, TEFIB M>OQF@IBP $ >KA % PEFCQ COLJ 3 >KA 4 QL 2 >KA 3, OBPMB@QFSBIV.
,BQfP MOBQBKA TB >OB C ILLMFKD QEOLRDE QEB ALL;S*CMN.
M>;D ? = 0 X C>;9A F7HJ?9B; A X E DEJ :;B;J;
M>;D ? = 1 X C>;9A F7HJ?9B; B X E DEJ :;B;J;
M>;D ? = 2 X C>;9A F7HJ?9B; C X ;B;J;!
-B?:; F7HJ?9B;I 7D: ! 879A <HEC IBEJI 3 7D: 4 JE 2 7D: 3
M>;D ? = 3 X C>;9A F7HJ?9B; ! X E DEJ :;B;J;
.LQF@B QEB MOL?IBJ? 7B KBSBO @EB@HBA M>OQF@IB $! 7EBK # T>P ABIBQBA COLJ PILQ #2, $
JLSBA FKQL PILQ #2, ?RQ F E>P >IOB>AV JLSBA LK QL PILQ # 3. 4EFP FP KLQ > AFP>PQBO, PFK@B
@IL (CHN C = 0; C < J;LNC=F?M.MCT?(); C++) U
.;LNC=F? J = J;LNC=F?M.A?N(C);
J.LOH();
J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50)));
W Adding a new Particle to the list while
iterating?
F64B?2 4.1
#E>MQBO 4. 0>OQF@IB 3VPQBJP
152
M>OQF@IB $ TFII DBQ @EB@HBA QEB KBUQ QFJB >OLRKA. 3QFII, QEB BUMB@Q>QFLK FP QE>Q TB >OB TOFQFKD
@LAB QL FQBO>QB QEOLRDE BSBOV PFKDIB BIBJBKQ LC QEB ALL;S*CMN. 3HFMMFKD >K BIBJBKQ FP
RK>@@BMQ>?IB.
4EBOB >OB QTL PLIRQFLKP QL QEFP MOL?IBJ. 4EB CFOPQ PLIRQFLK FP QL PFJMIV FQBO>QB QEOLRDE QEB
ALL;S*CMN ?>@HT>OAP. )C VLR >OB PIFAFKD BIBJBKQP COLJ OFDEQ QL IBCQ >P BIBJBKQP >OB OBJLSBA,
FQfP FJMLPPF?IB QL PHFM >K BIBJBKQ ?V >@@FABKQ. (BOBfP ELT QEB @LAB TLRIA ILLH:
4EFP FP > MBOCB@QIV CFKB PLIRQFLK FK KFKBQV-KFKB @>PBP LRQ LC > ERKAOBA. "RQ PLJBQFJBP, QEB
LOABO FK TEF@E QEB BIBJBKQP >OB AO>TK @LRIA ?B FJMLOQ>KQ >KA VLR J>V KLQ T>KQ QL FQBO>QB
?>@HT>OAP. *>S> MOLSFABP > PMB@F>I @I>PPd'N?L;NILdQE>Q Q>HBP @>OB LC >II LC QEB ABQ>FIP LC
FQBO>QFLK CLO VLR. 9LR DBQ QL P>V:
$9M, %S8 @=?9 HC =H9F5H9 H<FCI;< H<=G ALL;S*CMN. CCI@8 MCI 7CBH=BI9 HC ;=J9 A9 H<9 B9LH
9@9A9BH =B H<9 @=GH CB9 5H 5 H=A9 IBH=@ K9 ;9H HC H<9 9B8? AB8 =: % F9ACJ9 9@9A9BHG CF ACJ9
H<9A 5FCIB8 =B H<9 @=GH K<=@9 K9SF9 =H9F5H=B;, K=@@ MCI A5?9 GIF9 % 8CBSH @CC? 5H 5BM 9@9A9BHG
HK=79 CF G?=D 5BM 6M 577=89BH?
!K ALL;S*CMN @>K MOLAR@B >K 'N?L;NIL L?GB@Q CLO VLR.
/K@B VLRfSB DLQ QEB FQBO>QLO, QEB B;M,?RN() CRK@QFLK TFII QBII RP TEBQEBO QEBOB FP > .;LNC=F?
CLO RP QL ORK >KA QEB H?RN() CRK@QFLK TFII DO>? QE>Q .;LNC=F? L?GB@Q FQPBIC.
!KA FC VLR @>II QEB L?GIP?() CRK@QFLK LK QEB 'N?L;NIL L?GB@Q AROFKD QEB ILLM, FQ TFII ABIBQB QEB
@ROOBKQ .;LNC=F? L?GB@Q (>KA KLQ PHFM >EB>A M>PQ QEB KBUQ LKB, >P TB P>T TFQE @LRKQFKD
CLOT>OA QEOLRDE QEB ALL;S*CMN).
Looping through the list backwards <EH (?DJ ? = F7HJ?9B;I.I?P;()-1; ? >= 0; ?--) Q
.;LNC=F? J = (.;LNC=F?) J;LNC=F?M.A?N(C);
J.LOH();
C@ (J.CM"?;>()) U
J;LNC=F?M.L?GIP?(C);
W
W
Note that with the Iterator object, we can
also use the new <ClassName> generics
syntax and specify the type that the Iterator
will reference.
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL();
An Iterator object doing the iterating for you QBCF? (CN.B;M,?RN()) U
.;LNC=F? J = CN.H?RN();
J.LOH();
C@ (J.CM"?;>()) U
4EB .>QROB LC #LAB (S005)
153
0RQQFKD FQ >II QLDBQEBO, TB E>SB:
Example 4.2: ArrayList of particles with Iterator
An Iterator object doing the deleting for you CN.L?GIP?();
W
W
ALL;S*CMN<.;LNC=F?> J;LNC=F?M;
PIC> M?NOJ() U
MCT?(200,200);
J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
W
PIC> >L;Q() U
<;=EALIOH>(255);
J;LNC=F?M.;>>(H?Q .;LNC=F?(H?Q .3?=NIL(QC>NB/2,50)));
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL();
Using an Iterator object instead of
counting with int i
QBCF? (CN.B;M,?RN()) U
.;LNC=F? J = CN.H?RN();
J.LOH();
C@ (J.CM"?;>()) U
CN.L?GIP?();
W
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
154
4.4 The Particle System Class 4.4 The Particle System Class
/+. .LT TBfSB ALKB QTL QEFKDP. 7BfSB TOFQQBK > @I>PP QL ABP@OF?B >K FKAFSFAR>I .;LNC=F?
L?GB@Q. 7BfSB @LKNRBOBA QEB ALL;S*CMN >KA RPBA FQ QL J>K>DB > IFPQ LC J>KV .;LNC=F?
L?GB@QP (TFQE QEB >?FIFQV QL >AA >KA ABIBQB >Q TFII).
7B @LRIA PQLM EBOB. (LTBSBO, LKB >AAFQFLK>I PQBM TB @>K >KA PELRIA Q>HB FP QL TOFQB > @I>PP
QL ABP@OF?B QEB IFPQ LC .;LNC=F? L?GB@QP FQPBICdQEB .;LNC=F?0SMN?G @I>PP. 4EFP TFII >IILT RP QL
OBJLSB QEB ?RIHV ILDF@ LC ILLMFKD QEOLRDE >II M>OQF@IBP COLJ QEB J>FK Q>?, >P TBII >P LMBK RM
QEB MLPPF?FIFQV LC E>SFKD JLOB QE>K LKB M>OQF@IB PVPQBJ.
)C VLR OB@>II QEB DL>I TB PBQ >Q QEB ?BDFKKFKD LC QEFP @E>MQBO, TB T>KQBA LRO J>FK Q>? QL ILLH
IFHB QEFP:
,BQfP Q>HB QEB @LAB COLJ %U>JMIB 4.2 >KA OBSFBT > ?FQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKD,
ILLHFKD >Q ELT B>@E MFB@B COLJ QEB J>FK Q>? @>K CFQ FKQL QEB .;LNC=F?0SMN?G @I>PP.
Just one wee ParticleSystem! .;LNC=F?0SMN?G JM;
PIC> M?NOJ() U
MCT?(200,200);
JM = H?Q .;LNC=F?0SMN?G();
W
PIC> >L;Q() U
<;=EALIOH>(255);
JM.LOH();
W
4EB .>QROB LC #LAB (S005)
155
ArrayList in the main tab ArrayList in the main tab ArrayList in the ParticleSystem class ArrayList in the ParticleSystem class
AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I;
PIC> M?NOJ() U
MCT?(200,200);
F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>();
W
PIC> >L;Q() U
<;=EALIOH>(255);
F7HJ?9B;I.7::(D;M +7HJ?9B;());
%J;H7JEH<+7HJ?9B;> ?J =
F7HJ?9B;I.?J;H7JEH();
M>?B; (?J.>7I);NJ()) Q
+7HJ?9B; F = ?J.D;NJ();
F.HKD();
?< (F.?I;7:()) Q
?J.H;CEL;();
S
S
W
=F;MM .;LNC=F?0SMN?G U
AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I;
.;LNC=F?0SMN?G() U
F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>();
W
PIC> ;>>.;LNC=F?() U
F7HJ?9B;I.7::(D;M +7HJ?9B;());
W
PIC> LOH() U
%J;H7JEH<+7HJ?9B;> ?J =
F7HJ?9B;I.?J;H7JEH();
M>?B; (?J.>7I);NJ()) Q
+7HJ?9B; F = ?J.D;NJ();
F.HKD();
?< (F.?I;7:()) Q
?J.H;CEL;();
S
S
W
W
7B @LRIA >IPL >AA PLJB KBT CB>QROBP QL QEB M>OQF@IB PVPQBJ FQPBIC. &LO BU>JMIB, FQ JFDEQ ?B
RPBCRI CLO QEB .;LNC=F?0SMN?G @I>PP QL HBBM QO>@H LC >K LOFDFK MLFKQ TEBOB M>OQF@IBP >OB
J>AB. 4EFP CFQP FK TFQE QEB FAB> LC > M>OQF@IB PVPQBJ ?BFKD >K gBJFQQBO,h > MI>@B TEBOB
M>OQF@IBP >OB ?LOK >KA PBKQ LRQ FKQL QEB TLOIA. 4EB LOFDFK MLFKQ PELRIA ?B FKFQF>IFWBA FK QEB
@LKPQOR@QLO.
Example 4.3: Simple Single Particle System
=F;MM .;LNC=F?0SMN?G U
ALL;S*CMN J;LNC=F?M;
This particular ParticleSystem
implementation includes an origin point
where each Particle begins.
+0;9JEH EH?=?D;
.;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U
ILCACH = FI=;NCIH.A?N();
J;LNC=F?M = H?Q ALL;S*CMN();
W
PIC> ;>>.;LNC=F?() U
The origin is passed to each Particle when
it is added.
F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D));
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
156
->HB QEB LOFDFK MLFKQ JLSB AVK>JF@>IIV. (>SB QEB M>OQF@IBP BJFQ COLJ QEB JLRPB
IL@>QFLK LO RPB QEB @LK@BMQP LC SBIL@FQV >KA >@@BIBO>QFLK QL J>HB QEB PVPQBJ JLSB
>RQLKLJLRPIV.
Exercise 4.3 Exercise 4.3
"RFIAFKD LCC #E>MQBO 3fP g!PQBOLFAPh BU>JMIB, RPB > M>OQF@IB PVPQBJ QL BJFQ M>OQF@IBP
COLJ QEB PEFMfP gQEORPQBOPh TEBKBSBO > QEORPQ CLO@B FP >MMIFBA. 4EB M>OQF@IBPf FKFQF>I
SBIL@FQV PELRIA ?B OBI>QBA QL QEB PEFMfP @ROOBKQ AFOB@QFLK.
Exercise 4.4 Exercise 4.4
4.5 A System of Systems 4.5 A System of Systems
,BQfP OBSFBT CLO > JLJBKQ TEBOB TB >OB. 7B HKLT ELT QL Q>IH >?LRQ >K FKAFSFAR>I .;LNC=F?
L?GB@Q. 7B >IPL HKLT ELT QL Q>IH >?LRQ > PVPQBJ LC .;LNC=F? L?GB@QP, >KA QEFP TB @>II >
gM>OQF@IB PVPQBJ.h !KA TBfSB ABCFKBA > M>OQF@IB PVPQBJ >P > @LIIB@QFLK LC FKABMBKABKQ L?GB@QP.
"RQ FPKfQ > M>OQF@IB PVPQBJ FQPBIC >K L?GB@Q? )C QE>QfP QEB @>PB (TEF@E FQ FP), QEBOBfP KL OB>PLK TEV
TB @LRIAKfQ >IPL E>SB > @LIIB@QFLK LC J>KV M>OQF@IB PVPQBJP, F.B. > PVPQBJ LC PVPQBJP.
4EFP IFKB LC QEFKHFKD @LRIA LC @LROPB Q>HB RP BSBK CROQEBO, >KA VLR JFDEQ IL@H VLROPBIC FK >
?>PBJBKQ CLO A>VP PHBQ@EFKD LRQ > AF>DO>J LC > PVPQBJ LC PVPQBJP LC PVPQBJP LC PVPQBJP LC
PVPQBJP LC PVPQBJP. /C PVPQBJP. !CQBO >II, QEFP FP ELT QEB TLOIA TLOHP. !K LOD>K FP > PVPQBJ LC
@BIIP, > ERJ>K ?LAV FP > PVPQBJ LC LOD>KP, > KBFDE?LOELLA FP > PVPQBJ LC ERJ>K ?LAFBP, > @FQV
FP > PVPQBJ LC KBFDE?LOELLAP, >KA PL LK >KA PL CLOQE. 7EFIB QEFP FP >K FKQBOBPQFKD OL>A QL
QO>SBI ALTK, FQfP > ?FQ ?BVLKA TEBOB TB KBBA QL ?B OFDEQ KLT. )Q FP, ELTBSBO, NRFQB RPBCRI QL
HKLT ELT QL TOFQB > 0OL@BPPFKD PHBQ@E QE>Q HBBMP QO>@H LC J>KV M>OQF@IB PVPQBJP, B>@E LC
TEF@E HBBM QO>@H LC J>KV M>OQF@IBP. ,BQfP Q>HB QEB CLIILTFKD P@BK>OFL.
9LR PQ>OQ TFQE > ?I>KH P@OBBK.
4EB .>QROB LC #LAB (S005)
157
9LR @IF@H QEB JLRPB >KA DBKBO>QB > M>OQF@IB PVPQBJ >Q QEB JLRPBfP IL@>QFLK.
%>@E QFJB VLR @IF@H QEB JLRPB, > KBT M>OQF@IB PVPQBJ FP @OB>QBA >Q QEB JLRPBfP IL@>QFLK.
)K %U>JMIB 4.3 (PBB M>DB 156), TB PQLOBA > PFKDIB OBCBOBK@B QL > M>OQF@IB PVPQBJ L?GB@Q FK
QEB S>OF>?IB JM.
&LO QEFP KBT BU>JMIB, TE>Q TB T>KQ QL AL FKPQB>A FP @OB>QB >K ALL;S*CMN QL HBBM QO>@H LC
JRIQFMIB FKPQ>K@BP LC M>OQF@IB PVPQBJP. 7EBK QEB MOLDO>J PQ>OQP, F.B. FK M?NOJ(), QEB
ALL;S*CMN FP BJMQV.
.;LNC=F?0SMN?G JM;
PIC> M?NOJ() U
MCT?(200,200);
JM = H?Q .;LNC=F?0SMN?G(1,H?Q .3?=NIL(QC>NB/2,50));
W
PIC> >L;Q() U
<;=EALIOH>(255);
JM.LOH();
JM.;>>.;LNC=F?();
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
158
Example 4.4: System of systems
7EBKBSBO QEB JLRPB FP MOBPPBA, > KBT .;LNC=F?0SMN?G L?GB@Q FP @OB>QBA >KA MI>@BA FKQL QEB
ALL;S*CMN.
!KA FK >L;Q(), FKPQB>A LC OBCBOBK@FKD > PFKDIB .;LNC=F?0SMN?G L?GB@Q, TB KLT ILLH QEOLRDE
>II QEB PVPQBJP FK QEB ALL;S*CMN >KA @>II LOH() LK B>@E LC QEBJ.
This time, the type of thing we are putting in
the ArrayList is a ParticleSystem itself!
ALL;S*CMN<.;LNC=F?0SMN?G> MSMN?GM;
PIC> M?NOJ() U
MCT?(600,200);
MSMN?GM = H?Q ALL;S*CMN<.;LNC=F?0SMN?G>();
W
PIC> GIOM?.L?MM?>() U
MSMN?GM.;>>(H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(GIOM?5,GIOM?6)));
W
PIC> >L;Q() U
<;=EALIOH>(255);
Since we arent deleting elements, we can
use our enhanced loop!
@IL (.;LNC=F?0SMN?G JM: MSMN?GM) U
JM.LOH();
JM.;>>.;LNC=F?();
W
W
2BTOFQB %U>JMIB 4.4 PL QE>Q B>@E M>OQF@IB PVPQBJ ALBPKfQ IFSB CLOBSBO. 7EBK > M>OQF@IB
PVPQBJ FP BJMQV (F.B. E>P KL M>OQF@IBP IBCQ FK FQP ALL;S*CMN), OBJLSB FQ COLJ QEB
ALL;S*CMN MSMN?GM.
Exercise 4.5 Exercise 4.5
#OB>QB > PFJRI>QFLK LC >K L?GB@Q PE>QQBOFKD FKQL J>KV MFB@BP. (LT @>K VLR QROK LKB
I>ODB PE>MB FKQL J>KV PJ>II M>OQF@IBP? 7E>Q FC QEBOB >OB PBSBO>I I>ODB PE>MBP LK QEB
P@OBBK >KA QEBV PE>QQBO TEBK VLR @IF@H LK QEBJ?
Exercise 4.6 Exercise 4.6
4EB .>QROB LC #LAB (S005)
159
4.6 Inheritance and Polymorphism: An Introduction 4.6 Inheritance and Polymorphism: An Introduction
9LR J>V E>SB BK@LRKQBOBA QEB QBOJP =B<9F=H5B79 >KA DC@MACFD<=GA FK VLRO MOLDO>JJFKD
IFCB ?BCLOB QEFP ?LLH. !CQBO >II, QEBV >OB QTL LC QEB QEOBB CRKA>JBKQ>I MOFK@FMIBP ?BEFKA QEB
QEBLOV LC L?GB@Q-LOFBKQBA MOLDO>JJFKD (QEB LQEBO ?BFKD 9B75DGI@5H=CB). )C VLRfSB OB>A LQEBO
0OL@BPPFKD LO *>S> MOLDO>JJFKD ?LLHP, @E>K@BP >OB FQfP ?BBK @LSBOBA. -V ?BDFKKBO QBUQ,
(95FB=B; ,FC79GG=B;, E>P @ILPB QL >K BKQFOB @E>MQBO (#22) ABAF@>QBA QL QEBPB QTL QLMF@P.
3QFII, MBOE>MP VLRfSB LKIV IB>OKBA >?LRQ FQ FK QEB >?PQO>@Q PBKPB >KA KBSBO E>A > OB>PLK QL
OB>IIV RPB FKEBOFQ>K@B >KA MLIVJLOMEFPJ. )C QEFP FP QORB, VLRfSB @LJB QL QEB OFDEQ MI>@B.
7FQELRQ QEBPB QTL QLMF@P, VLRO >?FIFQV QL MOLDO>J > S>OFBQV LC M>OQF@IBP >KA M>OQF@IB PVPQBJP
FP BUQOBJBIV IFJFQBA. ()K QEB KBUQ @E>MQBO, TBfII >IPL PBB ELT RKABOPQ>KAFKD QEBPB QLMF@P TFII
EBIM RP QL RPB MEVPF@P IF?O>OFBP.)
)J>DFKB QEB CLIILTFKD. )QfP > 3>QROA>V JLOKFKD, VLRfSB GRPQ DLKB LRQ CLO > ILSBIV GLD, E>A >
ABIF@FLRP ?LTI LC @BOB>I, >KA >OB PFQQFKD NRFBQIV >Q VLRO @LJMRQBO TFQE > @RM LC T>OJ
@E>JLJFIB QB>. )QfP VLRO LIA COFBKA 3L >KA 3LfP ?FOQEA>V >KA VLRfSB AB@FABA VLRfA IFHB QL
J>HB > DOBBQFKD @>OA FK 0OL@BPPFKD. (LT >?LRQ PLJB @LKCBQQF CLO > ?FOQEA>V? 0ROMIB
@LKCBQQF, MFKH @LKCBQQF, PQ>O-PE>MBA @LKCBQQF, PNR>OB @LKCBQQF, C>PQ @LKCBQQF, CIRQQBOV @LKCBQQF,
BQ@. !II LC QEBPB MFB@BP LC @LKCBQQF TFQE AFCCBOBKQ >MMB>O>K@BP >KA AFCCBOBKQ ?BE>SFLOP
BUMILAB LKQL QEB P@OBBK >Q LK@B.
7E>Q TBfSB DLQ EBOB FP @IB>OIV > M>OQF@IB PVPQBJd> @LIIB@QFLK LC FKAFSFAR>I MFB@BP LC @LKCBQQF
(F.B. M>OQF@IBP). 7B JFDEQ ?B >?IB QL @IBSBOIV ABPFDK LRO .;LNC=F? @I>PP QL E>SB S>OF>?IBP
QE>Q PQLOB FQP @LILO, PE>MB, ?BE>SFLO, BQ@. !KA MBOE>MP TB FKFQF>IFWB QEB S>IRBP LC QEBPB
S>OF>?IBP O>KALJIV. "RQ TE>Q FC VLRO M>OQF@IBP >OB AO>PQF@>IIV AFCCBOBKQ? 4EFP @LRIA ?B@LJB
SBOV JBPPV, E>SFKD >II PLOQP LC @LAB CLO AFCCBOBKQ T>VP LC ?BFKD > M>OQF@IB FK QEB P>JB @I>PP.
7BII, VLR JFDEQ @LKPFABO ALFKD QEB CLIILTFKD:
4EFP FP > KF@B PLIRQFLK: TB E>SB QEOBB AFCCBOBKQ @I>PPBP QL ABP@OF?B QEB AFCCBOBKQ HFKAP LC
MFB@BP LC @LKCBQQF QE>Q @LRIA ?B M>OQ LC LRO M>OQF@IB PVPQBJ. 4EB .;LNC=F?0SMN?G
@LKPQOR@QLO @LRIA QEBK E>SB PLJB @LAB QL MF@H O>KALJIV COLJ QEB QEOBB @I>PPBP TEBK CFIIFKD
QEB ALL;S*CMN. .LQB QE>Q QEFP MOL?>?FIFPQF@ JBQELA FP QEB P>JB LKB TB BJMILVBA FK LRO
O>KALJ T>IH BU>JMIBP FK QEB )KQOLAR@QFLK (PBB M>DB 2).
=F;MM &;JJS!IH@?NNC U
W
=F;MM $OH!IH@?NNC U
W
=F;MM 4;=ES!IH@?NNC U
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
160
/+, TB KLT KBBA QL M>RPB CLO > JLJBKQ. 7BfSB ALKB KLQEFKD TOLKD. !II TB T>KQBA QL AL
T>P TFPE LRO COFBKA > E>MMV ?FOQEA>V >KA BKGLV TOFQFKD PLJB @LAB. "RQ TEFIB QEB OB>PLKFKD
?BEFKA QEB >?LSB >MMOL>@E FP NRFQB PLRKA, TBfSB LMBKBA RM QTL J>GLO MOL?IBJP.
0OL?IBJ #1: !OBKfQ TB DLFKD QL ?B @LMVFKD/M>PQFKD > ILQ LC @LAB ?BQTBBK QEB
AFCCBOBKQ g@LKCBQQFh @I>PPBP?
9BP. %SBK QELRDE LRO HFKAP LC M>OQF@IBP >OB AFCCBOBKQ BKLRDE QL JBOFQ LRO ?OB>HFKD QEBJ LRQ
FKQL PBM>O>QB @I>PPBP, QEBOB FP PQFII > QLK LC @LAB QE>Q QEBV TFII IFHBIV PE>OB. 4EBVfII >II E>SB
.3?=NILP QL HBBM QO>@H LC IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK; >K OJ>;N?() CRK@QFLK QE>Q
FJMIBJBKQP LRO JLQFLK >IDLOFQEJ; BQ@.
4EFP FP TEBOB 4930=4?,9.0 4930=4?,9.0 @LJBP FK. )KEBOFQ>K@B >IILTP RP QL TOFQB > @I>PP QE>Q =B<9F=HG
S>OF>?IBP >KA CRK@QFLKP COLJ >KLQEBO @I>PP, >II QEB TEFIB FJMIBJBKQFKD FQP LTK @RPQLJ
CB>QROBP.
0OL?IBJ #2: (LT TFII QEB !OO>V,FPQ HKLT TEF@E L?GB@QP >OB TEF@E QVMB?
4EFP FP > MOBQQV PBOFLRP MOL?IBJ. 2BJBJ?BO, TB TBOB RPFKD DBKBOF@P QL QBII QEB ALL;S*CMN
TE>Q QVMB LC L?GB@QP TBfOB DLFKD QL MRQ FKPFAB FQ. !OB TB PRAABKIV DLFKD QL KBBA QEOBB
AFCCBOBKQ ALL;S*CMNP?
4EFP PBBJP >TCRIIV FK@LKSBKFBKQ, DFSBK QE>Q TB OB>IIV GRPQ T>KQ LKB IFPQ QL HBBM QO>@H LC >II QEB
PQRCC FK QEB M>OQF@IB PVPQBJ. 4E>Q @>K ?B J>AB MLPPF?IB TFQE MLIVJLOMEFPJ. 0LIVJLOMEFPJ TFII
>IILT RP QL @LKPFABO L?GB@QP LC AFCCBOBKQ QVMBP >P QEB P>JB QVMB >KA PQLOB QEBJ FK > PFKDIB
ALL;S*CMN.
=F;MM .;LNC=F?0SMN?G U
.;LNC=F?0SMN?G(CHN HOG) U
J;LNC=F?M = H?Q ALL;S*CMN();
@IL (CHN C = 0; C < HOG; C++) U
@FI;N L = L;H>IG(1);
Randomly picking a "kind" of particle
C@ (L < 0.33) U J;LNC=F?M.;>>(H?Q &;JJS!IH@?NNC()); W
?FM? C@ (L < 0.67) U J;LNC=F?M.;>>(H?Q $OH!IH@?NNC()); W
?FM? U J;LNC=F?M.;>>(H?Q 4;=ES!IH@?NNC()); W
W
W
ALL;S*CMN<&;JJS!IH@?NNC> ;1 = H?Q ALL;S*CMN<&;JJS!IH@?NNC>();
ALL;S*CMN<$OH!IH@?NNC> ;2 = H?Q ALL;S*CMN<$OH!IH@?NNC>();
ALL;S*CMN<4;=ES!IH@?NNC> ;3 = H?Q ALL;S*CMN<4;=ES!IH@?NNC>();
4EB .>QROB LC #LAB (S005)
161
.LT QE>Q TB RKABOPQ>KA QEB MOL?IBJ, IBQfP ILLH >Q QEBPB QTL @LK@BMQP FK > ?FQ JLOB ABQ>FI
>KA QEBK @OB>QB > M>OQF@IB PVPQBJ BU>JMIB QE>Q FJMIBJBKQP ?LQE FKEBOFQ>K@B >KA
MLIVJLOMEFPJ.
4.7 Inheritance Basics 4.7 Inheritance Basics
,BQfP Q>HB > AFCCBOBKQ BU>JMIB, QEB TLOIA LC >KFJ>IP: ALDP, @>QP, JLKHBVP, M>KA>P, TLJ?>QP,
>KA PB> KBQQIBP. 7BfII PQ>OQ ?V MOLDO>JJFKD > "IA @I>PP. ! "IA L?GB@Q TFII E>SB >K >DB
S>OF>?IB (>K FKQBDBO), >P TBII >P ?;N(), MF??J(), >KA <;LE() CRK@QFLKP.
.LT, IBQfP JLSB LK QL @>QP.
=F;MM "IA U
CHN ;A?;
Dogs and cats have the same variables
(age) and functions (eat, sleep).
"IA() U
;A? = 0;
W
PIC> ?;N() U
JLCHNFH("6OG!");
W
PIC> MF??J() U
JLCHNFH("7TTTTT");
W
A unique function for barking. PIC> <;LE() U
JLCHNFH("4--$!");
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
162
!P TB OBTOFQB QEB P>JB @LAB CLO CFPE, ELOPBP, HL>I>P, >KA IBJROP, QEFP MOL@BPP TFII ?B@LJB
O>QEBO QBAFLRP. )KPQB>A, IBQfP ABSBILM > DBKBOF@ AHCG;F @I>PP QE>Q @>K ABP@OF?B >KV QVMB LC
>KFJ>I. !II >KFJ>IP B>Q >KA PIBBM, >CQBO >II. 7B @LRIA QEBK P>V:
i ! ALD FP >K >KFJ>I >KA E>P >II QEB MOLMBOQFBP LC >KFJ>IP >KA @>K AL >II QEB QEFKDP
>KFJ>IP AL. !IPL, > ALD @>K ?>OH.
i ! @>Q FP >K >KFJ>I >KA E>P >II QEB MOLMBOQFBP LC >KFJ>IP >KA @>K AL >II QEB QEFKDP
>KFJ>IP AL. !IPL, > @>Q @>K JBLT.
)KEBOFQ>K@B J>HBP QEFP >II MLPPF?IB. 7FQE FKEBOFQ>K@B, @I>PPBP @>K FKEBOFQ MOLMBOQFBP (S>OF>?IBP)
>KA CRK@QFLK>IFQV (JBQELAP) COLJ LQEBO @I>PPBP. ! "IA @I>PP FP > @EFIA (>@-.7,>> >@-.7,>>) LC >K AHCG;F
@I>PP. #EFIAOBK TFII >RQLJ>QF@>IIV FKEBOFQ >II S>OF>?IBP >KA CRK@QFLKP COLJ QEB M>OBKQ
(>@;0=.7,>> >@;0=.7,>>), ?RQ @>K >IPL FK@IRAB CRK@QFLKP >KA S>OF>?IBP KLQ CLRKA FK QEB M>OBKQ. ,FHB >
MEVILDBKBQF@ "QOBB LC IFCB," FKEBOFQ>K@B CLIILTP > QOBB PQOR@QROB. $LDP FKEBOFQ COLJ @>KFKBP,
TEF@E FKEBOFQ COLJ J>JJ>IP, TEF@E FKEBOFQ COLJ >KFJ>IP, BQ@.
=F;MM !;N U
CHN ;A?;
!;N() U
;A? = 0;
W
PIC> ?;N() U
JLCHNFH("6OG!");
W
PIC> MF??J() U
JLCHNFH("7TTTTT");
W
PIC> G?IQ() U
JLCHNFH("+#-4!");
W
W
F64B?2 4.2
4EB .>QROB LC #LAB (S005)
163
(BOB FP ELT QEB PVKQ>U TLOHP TFQE FKEBOFQ>K@B.
4EFP ?OFKDP RM QTL KBT QBOJP:
i 0C?09/> 0C?09/> c 4EFP HBVTLOA FP RPBA QL FKAF@>QB > M>OBKQ CLO QEB @I>PP ?BFKD ABCFKBA.
.LQB QE>Q @I>PPBP @>K LKIV BUQBKA CB9 @I>PP. (LTBSBO, @I>PPBP @>K BUQBKA @I>PPBP
QE>Q BUQBKA LQEBO @I>PPBP, F.B. "IA ?RN?H>M AHCG;F, 1?LLC?L ?RN?H>M "IA.
%SBOVQEFKD FP FKEBOFQBA >II QEB T>V ALTK QEB IFKB.
i >@;0=() >@;0=() c 4EFP @>IIP QEB @LKPQOR@QLO FK QEB M>OBKQ @I>PP. )K LQEBO TLOAP, TE>QBSBO
VLR AL FK QEB M>OBKQ @LKPQOR@QLO, AL PL FK QEB @EFIA @LKPQOR@QLO >P TBII. /QEBO @LAB
@>K ?B TOFQQBK FKQL QEB @LKPQOR@QLO FK >AAFQFLK QL MOJ?L(). MOJ?L() @>K >IPL
The Animal class is the parent (or super)
class.
=F;MM AHCG;F U
Dog and Cat inherit the variable age. CHN ;A?;
AHCG;F() U
;A? = 0;
W
Dog and Cat inherit the functions eat() and
sleep().
PIC> ?;N() U
JLCHNFH("6OG!");
W
PIC> MF??J() U
JLCHNFH("7TTTTT");
W
W
The Dog class is the child (or sub) class,
indicated by the code "extends Animal".
9B7II E= ;NJ;D:I AD?C7B Q
"IA() U
super() executes code found in the parent
class.
IKF;H();
W
We define bark() in the child class, since it
isn't part of the parent class.
PIC> <;LE() U
JLCHNFH("4--$!");
W
W
=F;MM !;N ?RN?H>M AHCG;F U
!;N() U
MOJ?L();
W
PIC> G?IQ() U
JLCHNFH("+#-4!");
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
164
OB@BFSB >ODRJBKQP FC QEBOB FP > M>OBKQ @LKPQOR@QLO ABCFKBA TFQE J>Q@EFKD >ODRJBKQP.
! PR?@I>PP @>K ?B BUM>KABA QL FK@IRAB >AAFQFLK>I CRK@QFLKP >KA MOLMBOQFBP ?BVLKA TE>Q FP
@LKQ>FKBA FK QEB PRMBO@I>PP. &LO BU>JMIB, IBQfP >PPRJB QE>Q > "IA L?GB@Q E>P > E>FO@LILO
S>OF>?IB FK >AAFQFLK QL >DB, TEF@E FP PBQ O>KALJIV FK QEB @LKPQOR@QLO. 4EB @I>PP TLRIA KLT ILLH
IFHB QEFP:
.LQB ELT QEB M>OBKQ @LKPQOR@QLO FP @>IIBA SF> MOJ?L(), TEF@E PBQP QEB >DB QL 0, ?RQ QEB
E>FO@LILO FP PBQ FKPFAB QEB "IA @LKPQOR@QLO FQPBIC. )C > "IA L?GB@Q B>QP AFCCBOBKQIV QE>K > DBKBOF@
AHCG;F L?GB@Q, M>OBKQ CRK@QFLKP @>K ?B CJ9FF=889B ?V OBTOFQFKD QEB CRK@QFLK FKPFAB QEB
PR?@I>PP.
"RQ TE>Q FC > ALD B>QP QEB P>JB T>V >P > DBKBOF@ >KFJ>I, GRPQ TFQE PLJB BUQO> CRK@QFLK>IFQV? !
PR?@I>PP @>K ?LQE ORK QEB @LAB COLJ > M>OBKQ @I>PP >KA FK@LOMLO>QB @RPQLJ @LAB.
=F;MM "IA ?RN?H>M AHCG;F U
A child class can introduce new variables
not included in the parent.
=IFIL B;CL=IFIL;
"IA() U
MOJ?L();
B;CL=IFIL = =IFIL(L;H>IG(255));
W
PIC> <;LE() U
JLCHNFH("4--$!");
W
W
=F;MM "IA ?RN?H>M AHCG;F U
=IFIL B;CL=IFIL;
"IA() U
MOJ?L();
B;CL=IFIL = =IFIL(L;H>IG(255));
W
A child can override a parent function if
necessary.
PIC> ?;N() U
A Dog's specific eating characteristics JLCHNFH("4II@! 4II@! 0FOLJ.")
W
PIC> <;LE() U
JLCHNFH("4--$!");
W
W
4EB .>QROB LC #LAB (S005)
165
=F;MM "IA ?RN?H>M AHCG;F U
=IFIL B;CL=IFIL;
"IA() U
MOJ?L();
B;CL=IFIL = =IFIL(L;H>IG(255));
W
PIC> ?;N() U
Call eat() from Animal. A child can execute
a function from the parent while adding its
own code.
IKF;H.;7J();
Add some additional code for a Dog's
specific eating characteristics.
JLCHNFH("4II@!!!");
W
PIC> <;LE() U
JLCHNFH("4--$!");
W
W
4.8 Particles with Inheritance 4.8 Particles with Inheritance
.LT QE>Q TBfSB E>A >K FKQOLAR@QFLK QL QEB QEBLOV LC FKEBOFQ>K@B >KA FQP PVKQ>U, TB @>K
ABSBILM > TLOHFKD BU>JMIB FK 0OL@BPPFKD ?>PBA LK LRO .;LNC=F? @I>PP.
,BQfP OBSFBT > PFJMIB .;LNC=F? FJMIBJBKQ>QFLK, CROQEBO PFJMIFCFBA COLJ %U>JMIB 4.1 (PBB
M>DB 147):
#E>MQBO 4. 0>OQF@IB 3VPQBJP
166
.BUQ, TB @OB>QB > PR?@I>PP COLJ .;LNC=F? (IBQfP @>II FQ !IH@?NNC). )Q TFII FKEBOFQ >II QEB FKPQ>K@B
S>OF>?IBP >KA JBQELAP COLJ .;LNC=F?. 7B TOFQB > KBT @LKPQOR@QLO TFQE QEB K>JB !IH@?NNC
>KA BUB@RQB QEB @LAB COLJ QEB M>OBKQ @I>PP ?V @>IIFKD MOJ?L().
=F;MM .;LNC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
.;LNC=F?(.3?=NIL F) U
;==?F?L;NCIH = H?Q .3?=NIL(0,0.05);
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0));
FI=;NCIH = F.A?N();
W
PIC> LOH() U
OJ>;N?();
>CMJF;S();
W
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
W
PIC> >CMJF;S() U
@CFF(0);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
W
=F;MM !IH@?NNC ?RN?H>M .;LNC=F? U
We could add variables for only Confetti
here.
!IH@?NNC(.3?=NIL F) U
MOJ?L(F);
W
There is no code here because we inherit
update() from parent.
Override the display method. PIC> >CMJF;S() U
L?=N+I>?(!#,1#/);
@CFF(175);
MNLIE?(0);
L?=N(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
W
4EB .>QROB LC #LAB (S005)
167
,BQfP J>HB QEFP > ?FQ JLOB PLMEFPQF@>QBA. ,BQfP P>V TB T>KQ QL E>SB QEB !IH@?NNC M>OQF@IB
OLQ>QB >P FQ CIFBP QEOLRDE QEB >FO. 7B @LRIA, LC @LROPB, JLABI >KDRI>O SBIL@FQV >KA
>@@BIBO>QFLK >P TB AFA FK #E>MQBO 3. )KPQB>A, TBfII QOV > NRF@H >KA AFOQV PLIRQFLK.
7B HKLT > M>OQF@IB E>P >K L IL@>QFLK PLJBTEBOB ?BQTBBK 0 >KA QEB TFAQE LC QEB TFKALT.
7E>Q FC TB P>FA: TEBK QEB M>OQF@IBfP L IL@>QFLK FP 0, FQP OLQ>QFLK PELRIA ?B 0; TEBK FQP L
IL@>QFLK FP BNR>I QL QEB TFAQE, FQP OLQ>QFLK PELRIA ?B BNR>I QL 14-:.'? $LBP QEFP OFKD > ?BII?
7EBKBSBO TB E>SB > S>IRB TFQE LKB O>KDB QE>Q TB T>KQ QL J>M QL >KLQEBO O>KDB, TB @>K
RPB 0OL@BPPFKDfP G;J() CRK@QFLK, TEF@E TB IB>OKBA >?LRQ FK QEB )KQOLAR@QFLK (PBB M>DB 17)!
!KA GRPQ QL DFSB FQ > ?FQ JLOB PMFK, TB @>K >@QR>IIV J>M QEB >KDIBfP O>KDB COLJ 0 QL
14-:.'*2. ,BQfP ILLH >Q ELT QEFP @LAB CFQP FKQL QEB >CMJF;S() CRK@QFLK.
.LT QE>Q TB E>SB > !IH@?NNC @I>PP QE>Q BUQBKAP LRO ?>PB .;LNC=F? @I>PP, TB KBBA QL
CFDROB LRQ ELT LRO .;LNC=F?0SMN?G @I>PP @>K J>K>DB M>OQF@IBP LC AFCCBOBKQ QVMBP TFQEFK QEB
P>JB PVPQBJ. 4L >@@LJMIFPE QEFP DL>I, IBQfP OBQROK QL QEB >KFJ>I HFKDALJ FKEBOFQ>K@B
BU>JMIB >KA PBB ELT QEB @LK@BMQ BUQBKAP FKQL QEB TLOIA LC MLIVJLOMEFPJ.
@FI;N ;HAF? = G;J(FI=;NCIH.R,0,QC>NB,0,14-:.');
PIC> >CMJF;S() U
@FI;N NB?N; = G;J(FI=;NCIH.R,0,QC>NB,0,14-:.'*2);
L?=N+I>?(!#,1#/);
@CFF(0,FC@?MJ;H);
MNLIE?(0,FC@?MJ;H);
If we rotate() a shape in Processing, we
need to familiarize ourselves with
transformations. For more, visit:
http://processing.org/learning/transform2d/
JOMB+;NLCR();
NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S);
LIN;N?(NB?N;);
L?=N(0,0,8,8);
JIJ+;NLCR();
W
)KPQB>A LC RPFKD G;J() QL @>I@RI>QB QEBQ>, ELT TLRIA VLR JLABI >KDRI>O SBIL@FQV >KA
>@@BIBO>QFLK?
Exercise 4.7 Exercise 4.7
4.9 Polymorphism Basics 4.9 Polymorphism Basics
7FQE QEB @LK@BMQ LC FKEBOFQ>K@B RKABO LRO ?BIQP, TB @>K FJ>DFKB ELT TB TLRIA MOLDO>J >
AFSBOPB >KFJ>I HFKDALJ RPFKD ALL;S*CMNPd>K >OO>V LC ALDP, >K >OO>V LC @>QP, LC QROQIBP, LC
HFTFP, BQ@. COLIF@HFKD >?LRQ.
#E>MQBO 4. 0>OQF@IB 3VPQBJP
168
!P QEB A>V ?BDFKP, QEB >KFJ>IP >OB >II MOBQQV ERKDOV >KA >OB ILLHFKD QL B>Q. 3L FQfP LCC QL
ILLMFKD QFJB (BKE>K@BA ILLMFKD QFJB!)j
4EFP TLOHP TBII, ?RQ >P LRO TLOIA BUM>KAP QL FK@IRAB J>KV JLOB >KFJ>I PMB@FBP, TBfOB DLFKD
QL DBQ PQR@H TOFQFKD > ILQ LC FKAFSFAR>I ILLMP. )P QEFP OB>IIV KB@BPP>OV? !CQBO >II, QEB @OB>QROBP
>OB >II >KFJ>IP, >KA QEBV >II IFHB QL B>Q. 7EV KLQ GRPQ E>SB LKB ALL;S*CMN LC AHCG;F L?GB@QP
>KA CFII FQ TFQE >II AFCCBOBKQ ?=B8G LC >KFJ>IP?
Separate ArrayLists for each animal ALL;S*CMN<"IA> >IAM = H?Q ALL;S*CMN<"IA>();
ALL;S*CMN<!;N> =;NM = H?Q ALL;S*CMN<!;N>();
ALL;S*CMN<1OLNF?> NOLNF?M = H?Q ALL;S*CMN<1OLNF?>();
ALL;S*CMN<)CQC> ECQCM = H?Q ALL;S*CMN<)CQC>();
@IL (CHN C = 0; C < 10; C++) U
>IAM.;>>(H?Q "IA());
W
@IL (CHN C = 0; C < 15; C++) U
=;NM.;>>(H?Q !;N());
W
@IL (CHN C = 0; C < 6; C++) U
NOLNF?M.;>>(H?Q 1OLNF?());
W
@IL (CHN C = 0; C < 98; C++) U
ECQCM.;>>(H?Q )CQC());
W
Separate loops for each animal @IL ("IA >: >IAM) U
>.?;N();
W
@IL (!;N =: =;NM) U
=.?;N();
W
@IL (1OLNF? N: NOLNF?M) U
N.?;N();
W
@IL ()CQC E: ECQCM) U
E.?;N();
W
4EB .>QROB LC #LAB (S005)
169
4EB >?FIFQV QL QOB>Q > "IA L?GB@Q >P BFQEBO > JBJ?BO LC QEB "IA @I>PP LO QEB AHCG;F @I>PP (FQP
M>OBKQ) FP >K BU>JMIB LC MLIVJLOMEFPJ. %:7D8:=;34>8 %:7D8:=;34>8 (COLJ QEB 'OBBH DC@MACFD<CG,
JB>KFKD J>KV CLOJP) OBCBOP QL QEB QOB>QJBKQ LC > PFKDIB FKPQ>K@B LC >K L?GB@Q FK JRIQFMIB
CLOJP. ! ALD FP @BOQ>FKIV > ALD, ?RQ PFK@B "IA ?RN?H>M AHCG;F, FQ @>K >IPL ?B @LKPFABOBA
>K >KFJ>I. )K @LAB, TB @>K OBCBO QL FQ ?LQE T>VP.
!IQELRDE QEB PB@LKA IFKB LC @LAB JFDEQ FKFQF>IIV PBBJ QL SFLI>QB PVKQ>U ORIBP, ?LQE T>VP LC
AB@I>OFKD > "IA L?GB@Q >OB IBD>I. %SBK QELRDE TB AB@I>OB MJIN >P >K AHCG;F L?GB@Q, TBfOB
OB>IIV J>HFKD > "IA L?GB@Q >KA PQLOFKD FQ FK QEB MJIN S>OF>?IB. !KA TB @>K P>CBIV @>II >II LC
QEB AHCG;F @I>PP JBQELAP LK PMLQ ?B@>RPB QEB ORIBP LC FKEBOFQ>K@B AF@Q>QB QE>Q > ALD @>K
AL >KVQEFKD >K >KFJ>I @>K.
7E>Q FC QEB "IA @I>PP, ELTBSBO, LSBOOFABP QEB ?;N() CRK@QFLK FK QEB AHCG;F @I>PP? %SBK FC
PMLQ FP AB@I>OBA >P >K AHCG;F, *>S> TFII ABQBOJFKB QE>Q FQP QORB FABKQFQV FP QE>Q LC > "IA >KA
ORK QEB >MMOLMOF>QB SBOPFLK LC QEB ?;N() CRK@QFLK.
4EFP FP M>OQF@RI>OIV RPBCRI TEBK TB E>SB >K >OO>V LO ALL;S*CMN.
ALL;S*CMN<AHCG;F> ECHA>IG = H?Q ALL;S*CMN<AHCG;F>();
@IL (CHN C = 0; C < 1000; C++) U
C@ (C < 100) ECHA>IG.;>>(H?Q "IA());
?FM? C@ (C < 400) ECHA>IG.;>>(H?Q !;N());
?FM? C@ (C < 900) ECHA>IG.;>>(H?Q 1OLNF?());
?FM? ECHA>IG.;>>(H?Q )CQC());
W
@IL (AHCG;F ;: ECHA>IG) U
;.?;N();
W
Just one ArrayList for all the animals!
"IA LIP?L = H?Q "IA();
AHCG;F MJIN = H?Q "IA();
4.10 Particle Systems with Polymorphism 4.10 Particle Systems with Polymorphism
,BQfP MOBQBKA CLO > JLJBKQ QE>Q MLIVJLOMEFPJ ALBPKfQ BUFPQ >KA OBTOFQB > .;LNC=F?0SMN?G
@I>PP QL FK@IRAB J>KV .;LNC=F? L?GB@QP >KA J>KV !IH@?NNC L?GB@QP.
=F;MM .;LNC=F?0SMN?G U
Were stuck doing everything twice with
two lists!
AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I;
AHH7O'?IJ<CED<;JJ?> 9ED<;JJ?;
#E>MQBO 4. 0>OQF@IB 3VPQBJP
170
.LQF@B ELT TB E>SB QTL PBM>O>QB IFPQP, LKB CLO M>OQF@IBP >KA LKB CLO @LKCBQQF. %SBOV >@QFLK TB
T>KQ QL MBOCLOJ TB E>SB QL AL QTF@B! 0LIVJLOMEFPJ >IILTP RP QL PFJMIFCV QEB >?LSB ?V GRPQ
J>HFKD LKB ALL;S*CMN LC M>OQF@IBP QE>Q @LKQ>FKP ?LQE PQ>KA>OA .;LNC=F? L?GB@QP >P TBII >P
!IH@?NNC L?GB@QP. 7B ALKfQ E>SB QL TLOOV >?LRQ TEF@E >OB TEF@E; QEFP TFII >II ?B Q>HBK @>OB LC
CLO RP! (!IPL, KLQB QE>Q QEB @LAB CLO QEB J>FK MOLDO>J >KA QEB @I>PPBP E>P KLQ @E>KDBA, PL TB
>OBKfQ FK@IRAFKD FQ EBOB. 3BB QEB TB?PFQB CLO QEB CRII BU>JMIB.)
.3?=NIL ILCACH;
.;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U
ILCACH = FI=;NCIH.A?N();
Were stuck doing everything twice with two
lists!
F7HJ?9B;I = D;M AHH7O'?IJ<+7HJ?9B;>();
9ED<;JJ? = D;M AHH7O'?IJ<CED<;JJ?>();
W
PIC> ;>>.;LNC=F?() U
Were stuck doing everything twice with two
lists!
F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D));
F7HJ?9B;I.7::(D;M CED<;JJ?(EH?=?D));
W
PIC> LOH() U
Were stuck doing everything twice with two
lists!
%J;H7JEH<+7HJ?9B;> ?J = F7HJ?9B;I.?J;H7JEH();
QBCF? (CN.B;M,?RN()) U
.;LNC=F? J = CN.H?RN();
J.LOH();
C@ (J.CM"?;>()) U
CN.L?GIP?();
W
W
?J = 9ED<;JJ?.?J;H7JEH();
QBCF? (CN.B;M,?RN()) U
!IH@?NNC = = CN.H?RN();
=.LOH();
C@ (=.CM"?;>()) U
CN.L?GIP?();
W
W
W
W
4EB .>QROB LC #LAB (S005)
171
Example 4.5: Particle system inheritance and polymorphism
=F;MM .;LNC=F?0SMN?G U
One list, for anything that is a Particle or
extends Particle
AHH7O'?IJ<+7HJ?9B;> F7HJ?9B;I;
.3?=NIL ILCACH;
.;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U
ILCACH = FI=;NCIH.A?N();
J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
W
PIC> ;>>.;LNC=F?() U
@FI;N L = L;H>IG(1);
We have a 50% chance of adding each
kind of Particle.
C@ (L < 0.5) U
F7HJ?9B;I.7::(D;M +7HJ?9B;(EH?=?D));
W ?FM? U
F7HJ?9B;I.7::(D;M CED<;JJ?(EH?=?D));
W
W
PIC> LOH() U
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL();
QBCF? (CN.B;M,?RN()) U
Polymorphism allows us to treat everything
as a Particle, whether it is a Particle or a
Confetti.
+7HJ?9B; F = ?J.D;NJ();
J.LOH();
C@ (J.CM"?;>()) U
CN.L?GIP?();
W
W
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
172
#OB>QB > M>OQF@IB PVPQBJ TFQE AFCCBOBKQ gHFKAPh LC M>OQF@IBP FK QEB P>JB PVPQBJ. 4OV
S>OVFKD JLOB QE>K GRPQ QEB ILLH LC QEB M>OQF@IBP. (LT AL VLR AB>I TFQE AFCCBOBKQ
?BE>SFLOP RPFKD FKEBOFQ>K@B?
Exercise 4.8 Exercise 4.8
4.11 Particle Systems with Forces 4.11 Particle Systems with Forces
3L C>O FK QEFP @E>MQBO, TBfSB ?BBK CL@RPFKD LK PQOR@QROFKD LRO @LAB FK >K L?GB@Q-LOFBKQBA T>V
QL J>K>DB > @LIIB@QFLK LC M>OQF@IBP. ->V?B VLR KLQF@BA, LO J>V?B VLR AFAKfQ, ?RQ AROFKD QEFP
MOL@BPP TB RKTFQQFKDIV QLLH > @LRMIB PQBMP ?>@HT>OA COLJ TEBOB TB TBOB FK MOBSFLRP
@E>MQBOP. ,BQfP BU>JFKB QEB @LKPQOR@QLO LC LRO PFJMIB .;LNC=F? @I>PP.
!KA KLT IBQfP ILLH >Q QEB OJ>;N?() CRK@QFLK.
/RO .;LNC=F? @I>PP FP PQOR@QROBA QL E>SB > @LKPQ>KQ >@@BIBO>QFLK, LKB QE>Q KBSBO @E>KDBP. !
JR@E ?BQQBO CO>JBTLOH TLRIA ?B QL CLIILT .BTQLKfP PB@LKA I>T (& = -* !) >KA FK@LOMLO>QB
QEB CLO@B >@@RJRI>QFLK >IDLOFQEJ TB TLOHBA PL E>OA LK FK #E>MQBO 2 (PBB M>DB 68).
3QBM 1 TLRIA ?B QL >AA FK QEB ;JJFS$IL=?() CRK@QFLK. (2BJBJ?BO, TB KBBA QL J>HB > @LMV LC
QEB .3?=NIL ?BCLOB TB AFSFAB FQ ?V J>PP.)
.;LNC=F?(.3?=NIL F) U
Were setting acceleration to a constant
value!
799;B;H7J?ED = D;M +0;9JEH(0,0.05);
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0));
FI=;NCIH = F.A?N();
FC@?MJ;H = 255.0;
W
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
// 4B?L? CM NB? FCH? I@ =I>? NI =F?;L ;==?F?L;NCIH?
FC@?MJ;H -= 2.0;
W
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
.3?=NIL @ = @IL=?.A?N();
@.>CP(G;MM);
;==?F?L;NCIH.;>>(@);
W
4EB .>QROB LC #LAB (S005)
173
/K@B TB E>SB QEFP, TB @>K >AA FK LKB JLOB IFKB LC @LAB QL @IB>O QEB >@@BIBO>QFLK >Q QEB BKA
LC OJ>;N?().
!KA LRO .;LNC=F? @I>PP FP @LJMIBQB!
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
There it is! ;==?F?L;NCIH.GOFN(0);
FC@?MJ;H -= 2.0;
W
=F;MM .;LNC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N FC@?MJ;H;
We could vary mass for more interesting
results.
@FI;N G;MM = 1;
.;LNC=F?(.3?=NIL F) U
We now start with acceleration of 0. ;==?F?L;NCIH = H?Q .3?=NIL(0,0);
P?FI=CNS = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-2,0));
FI=;NCIH = F.A?N();
FC@?MJ;H = 255.0;
W
PIC> LOH() U
OJ>;N?();
>CMJF;S();
W
Newtons second law & force
accumulation
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
.3?=NIL @ = @IL=?.A?N();
@.>CP(G;MM);
;==?F?L;NCIH.;>>(@);
W
Standard update PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
FC@?MJ;H -= 2.0;
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
174
.LT QE>Q QEB .;LNC=F? @I>PP FP @LJMIBQBA, TB E>SB > SBOV FJMLOQ>KQ NRBPQFLK QL >PH. 7EBOB
AL TB @>II QEB ;JJFS$IL=?() CRK@QFLK? 7EBOB FK QEB @LAB FP FQ >MMOLMOF>QB QL >MMIV > CLO@B QL
> M>OQF@IB? 4EB QORQE LC QEB J>QQBO FP QE>Q QEBOBfP KL OFDEQ LO TOLKD >KPTBO; FQ OB>IIV ABMBKAP LK
QEB BU>@Q CRK@QFLK>IFQV >KA DL>IP LC > M>OQF@RI>O 0OL@BPPFKD PHBQ@E. 3QFII, TB @>K @OB>QB >
DBKBOF@ PFQR>QFLK QE>Q TLRIA IFHBIV >MMIV QL JLPQ @>PBP >KA @O>CQ > JLABI CLO >MMIVFKD CLO@BP QL
FKAFSFAR>I M>OQF@IBP FK > PVPQBJ.
,BQfP @LKPFABO QEB CLIILTFKD DL>I: !MMIV > CLO@B DIL?>IIV BSBOV QFJB QEOLRDE >L;Q() QL >II
M>OQF@IBP. 7BfII MF@H >K B>PV LKB CLO KLT: > CLO@B MLFKQFKD ALTK, IFHB DO>SFQV.
7B P>FA FQ PELRIA >IT>VP ?B >MMIFBA, F.B. FK >L;Q(), PL IBQfP Q>HB > ILLH >Q LRO >L;Q() CRK@QFLK
>P FQ PQ>KAP.
7BII, FQ PBBJP QE>Q TB E>SB > PJ>II MOL?IBJ. ;JJFS$IL=?() FP > JBQELA TOFQQBK FKPFAB QEB
.;LNC=F? @I>PP, ?RQ TB ALKfQ E>SB >KV OBCBOBK@B QL QEB FKAFSFAR>I M>OQF@IBP QEBJPBISBP, LKIV
QEB .;LNC=F?0SMN?G L?GB@Q: QEB S>OF>?IB JM.
3FK@B TB T>KQ >II M>OQF@IBP QL OB@BFSB QEB CLO@B, ELTBSBO, TB @>K AB@FAB QL >MMIV QEB CLO@B QL
QEB M>OQF@IB PVPQBJ >KA IBQ FQ J>K>DB >MMIVFKD QEB CLO@B QL >II QEB FKAFSFAR>I M>OQF@IBP:
Our Particle is a circle. PIC> >CMJF;S() U
MNLIE?(255,FC@?MJ;H);
@CFF(255,FC@?MJ;H);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,8,8);
W
Should the Particle be deleted? <IIF?;H CM"?;>() U
C@ (FC@?MJ;H < 0.0) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
W
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
PIC> >L;Q() U
<;=EALIOH>(100);
JM.;>>.;LNC=F?();
JM.LOH();
W
PIC> >L;Q() U
<;=EALIOH>(100);
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
4EB .>QROB LC #LAB (S005)
175
/C @LROPB, FC TB @>II > KBT CRK@QFLK LK QEB .;LNC=F?0SMN?G L?GB@Q FK >L;Q(), TBII, TB E>SB
QL TOFQB QE>Q CRK@QFLK FK QEB .;LNC=F?0SMN?G @I>PP. ,BQfP ABP@OF?B QEB GL? QE>Q CRK@QFLK
KBBAP QL MBOCLOJ: OB@BFSB > CLO@B >P > .3?=NIL >KA >MMIV QE>Q CLO@B QL >II QEB M>OQF@IBP.
.LT FK @LAB:
)Q >IJLPQ PBBJP PFIIV QL TOFQB QEFP CRK@QFLK. 7E>Q TBfOB P>VFKD FP g>MMIV > CLO@B QL > M>OQF@IB
PVPQBJ PL QE>Q QEB PVPQBJ @>K >MMIV QE>Q CLO@B QL >II LC QEB FKAFSFAR>I M>OQF@IBP.h
.BSBOQEBIBPP, FQfP OB>IIV NRFQB OB>PLK>?IB. !CQBO >II, QEB .;LNC=F?0SMN?G L?GB@Q FP FK @E>ODB
LC J>K>DFKD QEB M>OQF@IBP, PL FC TB T>KQ QL Q>IH QL QEB M>OQF@IBP, TBfSB DLQ QL Q>IH QL QEBJ
QEOLRDE QEBFO J>K>DBO. (!IPL, EBOBfP > @E>K@B CLO QEB BKE>K@BA ILLM PFK@B TB >OBKfQ
ABIBQFKD M>OQF@IBP!)
(BOB FP QEB CRII BU>JMIB (>PPRJFKD QEB BUFPQBK@B LC QEB .;LNC=F? @I>PP TOFQQBK >?LSB; KL
KBBA QL FK@IRAB FQ >D>FK PFK@B KLQEFKD E>P @E>KDBA):
Applying a force to the system as a whole JM.;JJFS$IL=?(AL;PCNS);
JM.;>>.;LNC=F?();
JM.LOH();
W
PIC> ;JJFS$IL=?(.3?=NIL @) U
@IL (.;LNC=F? J: J;LNC=F?M) U
J.;JJFS$IL=?(@);
W
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
176
Example 4.6: Particle system with forces
.;LNC=F?0SMN?G JM;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50));
W
PIC> >L;Q() U
<;=EALIOH>(100);
Apply a force to all particles. +0;9JEH =H7L?JO = D;M +0;9JEH(0,0.1);
FI.7FFBO"EH9;(=H7L?JO);
JM.;>>.;LNC=F?();
JM.LOH();
W
=F;MM .;LNC=F?0SMN?G U
ALL;S*CMN<.;LNC=F?> J;LNC=F?M;
.3?=NIL ILCACH;
.;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U
ILCACH = FI=;NCIH.A?N();
J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
W
PIC> ;>>.;LNC=F?() U
J;LNC=F?M.;>>(H?Q .;LNC=F?(ILCACH));
W
PIC> ;JJFS$IL=?(.3?=NIL @) U
Using an enhanced loop to apply the force
to all particles
@IL (.;LNC=F? J: J;LNC=F?M) U
J.;JJFS$IL=?(@);
W
W
PIC> LOH() U
Cant use the enhanced loop because we
want to check for particles to delete.
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL();
QBCF? (CN.B;M,?RN()) U
.;LNC=F? J = (.;LNC=F?) CN.H?RN();
J.LOH();
C@ (J.CM"?;>()) U
CN.L?GIP?();
W
W
W
W
4EB .>QROB LC #LAB (S005)
177
4.12 Particle Systems with Repellers 4.12 Particle Systems with Repellers
7E>Q FC TB T>KQBA QL Q>HB QEFP BU>JMIB LKB PQBM CROQEBO >KA >AA > /?J?FF?L L?GB@QdQEB
FKSBOPB LC QEB ANNL;=NIL L?GB@Q TB @LSBOBA FK #E>MQBO 2 (PBB M>DB 88) QE>Q MRPEBP >KV
M>OQF@IBP >T>V QE>Q DBQ @ILPB? 4EFP OBNRFOBP > ?FQ JLOB PLMEFPQF@>QFLK ?B@>RPB, RKIFHB QEB
DO>SFQV CLO@B, B>@E CLO@B >K >QQO>@QLO LO OBMBIIBO BUBOQP LK > M>OQF@IB JRPQ ?B @>I@RI>QBA CLO
B>@E M>OQF@IB.
,BQfP PQ>OQ PLISFKD QEFP MOL?IBJ ?V BU>JFKFKD ELT TB TLRIA FK@LOMLO>QB > KBT /?J?FF?L
L?GB@Q FKQL LRO PFJMIB M>OQF@IB PVPQBJ MIRP CLO@BP BU>JMIB. 7BfOB DLFKD QL KBBA QTL J>GLO
>AAFQFLKP QL LRO @LAB:
1. ! /?J?FF?L L?GB@Q (AB@I>OBA, FKFQF>IFWBA, >KA AFPMI>VBA).
2. ! CRK@QFLK QE>Q M>PPBP QEB /?J?FF?L L?GB@Q FKQL QEB .;LNC=F?0SMN?G PL QE>Q FQ @>K
>MMIV > CLO@B QL B>@E M>OQF@IB L?GB@Q.
F64B?2 4.3: G?.C6AF 3<?02HC20A<?@ .?2 .99
612;A60.9
F64B?2 4.4: AAA?.0A<? 3<?02HC20A<?@ .?2 .99
16332?2;A
.;LNC=F?0SMN?G JM;
New thing: we declare a Repeller object. ,;F;BB;H H;F;BB;H;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50));
#E>MQBO 4. 0>OQF@IB 3VPQBJP
178
->HFKD > /?J?FF?L L?GB@Q FP NRFQB B>PV; FQfP > ARMIF@>QB LC QEB ANNL;=NIL @I>PP COLJ #E>MQBO
2, %U>JMIB 2.6 .
4EB JLOB AFCCF@RIQ NRBPQFLK FP, ELT AL TB TOFQB QEB ;JJFS/?J?FF?L() CRK@QFLK? )KPQB>A LC
M>PPFKD > .3?=NIL FKQL > CRK@QFLK IFHB TB AL TFQE ;JJFS$IL=?(), TBfOB DLFKD QL FKPQB>A M>PP
> /?J?FF?L L?GB@Q FKQL ;JJFS/?J?FF?L() >KA >PH QE>Q CRK@QFLK QL AL QEB TLOH LC @>I@RI>QFKD
QEB CLO@B ?BQTBBK QEB OBMBIIBO >KA >II M>OQF@IBP. ,BQfP ILLH >Q ?LQE LC QEBPB CRK@QFLKP PFAB ?V
PFAB.
New thing: we initialize a Repeller object. H;F;BB;H = D;M ,;F;BB;H(M?:J>/2-20,>;?=>J/2);
W
PIC> >L;Q() U
<;=EALIOH>(100);
JM.;>>.;LNC=F?();
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
JM.;JJFS$IL=?(AL;PCNS);
New thing: we need a function to apply a
force from a repeller.
FI.7FFBO,;F;BB;H(H;F;BB;H);
JM.LOH();
New thing: we display the Repeller object. H;F;BB;H.:?IFB7O();
W
=F;MM /?J?FF?L U
A Repeller doesnt move, so you just need
location.
.3?=NIL FI=;NCIH;
@FI;N L = 10;
/?J?FF?L(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R,S);
W
PIC> >CMJF;S() U
MNLIE?(255);
@CFF(255);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,L*2,L*2);
W
W
4EB .>QROB LC #LAB (S005)
179
applyForce() applyForce() applyRepeller applyRepeller
PIC> ;JJFS$IL=?(.3?=NIL @) U
@IL (.;LNC=F? J: J;LNC=F?M) U
J.;JJFS$IL=?(@);
W
W
PIC> ;JJFS/?J?FF?L(/?J?FF?L L) U
@IL (.;LNC=F? J: J;LNC=F?M) U
.3?=NIL @IL=? = L.L?J?F(J);
J.;JJFS$IL=?(@IL=?);
W
W
4EB CRK@QFLKP >OB >IJLPQ FABKQF@>I. 4EBOB >OB LKIV QTL AFCCBOBK@BP. /KB TB JBKQFLKBA
?BCLOBd> /?J?FF?L L?GB@Q FP QEB >ODRJBKQ, KLQ > .3?=NIL. 4EB PB@LKA AFCCBOBK@B FP QEB
FJMLOQ>KQ LKB. 7B JRPQ @>I@RI>QB > @RPQLJ .3?=NIL CLO@B CLO B>@E >KA BSBOV M>OQF@IB >KA
>MMIV QE>Q CLO@B. (LT FP QE>Q CLO@B @>I@RI>QBA? )K > CRK@QFLK @>IIBA L?J?F(), TEF@E FP QEB
FKSBOPB LC QEB ;NNL;=N() CRK@QFLK TB TOLQB CLO QEB ANNL;=NIL @I>PP.
.LQF@B ELT QEOLRDELRQ QEFP BKQFOB MOL@BPP LC >AAFKD > OBMBIIBO QL QEB BKSFOLKJBKQ, TBfSB
KBSBO LK@B @LKPFABOBA BAFQFKD QEB .;LNC=F? @I>PP FQPBIC. ! M>OQF@IB ALBPKfQ >@QR>IIV E>SB QL
HKLT >KVQEFKD >?LRQ QEB ABQ>FIP LC FQP BKSFOLKJBKQ; FQ PFJMIV KBBAP QL J>K>DB FQP IL@>QFLK,
SBIL@FQV, >KA >@@BIBO>QFLK, >P TBII >P E>SB QEB >?FIFQV QL OB@BFSB >K BUQBOK>I CLO@B >KA >@Q LK
FQ.
3L TB @>K KLT ILLH >Q QEFP BU>JMIB FK FQP BKQFOBQV, >D>FK IB>SFKD LRQ QEB .;LNC=F? @I>PP,
TEF@E E>PKfQ @E>KDBA.
All the same steps we had to calculate an
attractive force, only pointing in the
opposite direction.
.3?=NIL L?J?F(.;LNC=F? J) U
1) Get force direction. .3?=NIL >CL =
.3?=NIL.MO<(FI=;NCIH,J.FI=;NCIH);
2) Get distance (constrain distance). @FI;N > = >CL.G;A();
> = =IHMNL;CH(>,5,100);
>CL.HILG;FCT?();
3) Calculate magnitude. @FI;N @IL=? = -1 * % / (> * >);
4) Make a vector out of direction and
magnitude.
>CL.GOFN(@IL=?);
L?NOLH >CL;
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
180
Example 4.7: ParticleSystem with repeller
One ParticleSystem .;LNC=F?0SMN?G JM;
One repeller /?J?FF?L L?J?FF?L;
PIC> M?NOJ() U
MCT?(200,200);
MGIINB();
JM = H?Q .;LNC=F?0SMN?G(H?Q .3?=NIL(QC>NB/2,50));
L?J?FF?L = H?Q /?J?FF?L(QC>NB/2-20,B?CABN/2);
W
PIC> >L;Q() U
<;=EALIOH>(100);
JM.;>>.;LNC=F?();
Were applying a universal gravity. .3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
JM.;JJFS$IL=?(AL;PCNS);
Applying the repeller JM.;JJFS/?J?FF?L(L?J?FF?L);
JM.LOH();
L?J?FF?L.>CMJF;S();
W
4EB .>QROB LC #LAB (S005)
181
The ParticleSystem manages all the
Particles.
=F;MM .;LNC=F?0SMN?G U
ALL;S*CMN<.;LNC=F?> J;LNC=F?M;
.3?=NIL ILCACH;
.;LNC=F?0SMN?G(.3?=NIL FI=;NCIH) U
ILCACH = FI=;NCIH.A?N();
J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
W
PIC> ;>>.;LNC=F?() U
J;LNC=F?M.;>>(H?Q .;LNC=F?(ILCACH));
W
Applying a force as a PVector PIC> ;JJFS$IL=?(.3?=NIL @) U
@IL (.;LNC=F? J: J;LNC=F?M) U
J.;JJFS$IL=?(@);
W
W
PIC> ;JJFS/?J?FF?L(/?J?FF?L L) U
Calculating a force for each Particle based
on a Repeller
@IL (.;LNC=F? J: J;LNC=F?M) U
.3?=NIL @IL=? = L.L?J?F(J);
J.;JJFS$IL=?(@IL=?);
W
W
PIC> LOH() U
'N?L;NIL<.;LNC=F?> CN = J;LNC=F?M.CN?L;NIL();
QBCF? (CN.B;M,?RN()) U
.;LNC=F? J = (.;LNC=F?) CN.H?RN();
J.LOH();
C@ (J.CM"?;>()) U
CN.L?GIP?();
W
W
W
W
=F;MM /?J?FF?L U
#E>MQBO 4. 0>OQF@IB 3VPQBJP
182
How strong is the repeller? @FI;N MNL?HANB = 100;
.3?=NIL FI=;NCIH;
@FI;N L = 10;
/?J?FF?L(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R,S);
W
PIC> >CMJF;S() U
MNLIE?(255);
@CFF(255);
?FFCJM?(FI=;NCIH.R,FI=;NCIH.S,L*2,L*2);
W
.3?=NIL L?J?F(.;LNC=F? J) U
This is the same repel algorithm we used in
Chapter 2: forces based on gravitational
attraction.
.3?=NIL >CL = .3?=NIL.MO<(FI=;NCIH,J.FI=;NCIH);
@FI;N > = >CL.G;A();
>CL.HILG;FCT?();
> = =IHMNL;CH(>,5,100);
@FI;N @IL=? = -1 * MNL?HANB / (> * >);
>CL.GOFN(@IL=?);
L?NOLH >CL;
W
W
%UM>KA QEB >?LSB BU>JMIB QL FK@IRAB J>KV OBMBIIBOP (RPFKD >K >OO>V LO ALL;S*CMN).
Exercise 4.9 Exercise 4.9
#OB>QB > M>OQF@IB PVPQBJ FK TEF@E B>@E M>OQF@IB OBPMLKAP QL BSBOV LQEBO M>OQF@IB. (.LQB
QE>Q TBfII ?B DLFKD QEOLRDE QEFP FK ABQ>FI FK #E>MQBO 6.)
Exercise 4.10 Exercise 4.10
4.13 Image Textures and Additive Blending 4.13 Image Textures and Additive Blending
%SBK QELRDE QEFP ?LLH FP OB>IIV >?LRQ ?BE>SFLOP >KA >IDLOFQEJP O>QEBO QE>K @LJMRQBO DO>MEF@P
>KA ABPFDK, ) ALKfQ QEFKH TB TLRIA ?B >?IB QL IFSB TFQE LROPBISBP FC TB TBKQ QEOLRDE >
AFP@RPPFLK LC M>OQF@IB PVPQBJP >KA KBSBO LK@B ILLHBA >Q >K BU>JMIB QE>Q FKSLISBP QBUQROFKD
B>@E M>OQF@IB TFQE >K FJ>DB. 4EB T>V VLR @ELLPB QL AO>T > M>OQF@IB FP > ?FD M>OQ LC QEB MRWWIB
FK QBOJP LC ABPFDKFKD @BOQ>FK QVMBP LC SFPR>I BCCB@QP.
,BQfP QOV QL @OB>QB > PJLHB PFJRI>QFLK FK 0OL@BPPFKD. 4>HB > ILLH >Q QEB CLIILTFKD QTL FJ>DBP:
4EB .>QROB LC #LAB (S005)
183
"LQE LC QEBPB FJ>DBP TBOB DBKBO>QBA COLJ FABKQF@>I >IDLOFQEJP. 4EB LKIV AFCCBOBK@B FP QE>Q >
TEFQB @FO@IB FP AO>TK FK FJ>DB ! CLO B>@E M>OQF@IB >KA > gCRWWVh ?IL? FP AO>TK CLO B>@E FK ".
4EB DLLA KBTP EBOB FP QE>Q VLR DBQ > ILQ LC ?>KD CLO SBOV IFQQIB ?R@H. "BCLOB VLR TOFQB >KV
@LAB, ELTBSBO, VLRfSB DLQ QL J>HB VLRO FJ>DB QBUQROB! ) OB@LJJBKA RPFKD 0.' CLOJ>Q, >P
0OL@BPPFKD TFII OBQ>FK QEB >IME> @E>KKBI (F.B. QO>KPM>OBK@V) TEBK AO>TFKD QEB FJ>DB, TEF@E
FP KBBABA CLO ?IBKAFKD QEB QBUQROB >P M>OQF@IBP I>VBO LK QLM LC B>@E LQEBO. /K@B VLRfSB
J>AB VLRO 0.' >KA ABMLPFQBA FQ FK VLRO PHBQ@EfP gA>Q>h CLIABO, VLR >OB LK VLRO T>V TFQE
GRPQ > CBT IFKBP LC @LAB.
&FOPQ, TBfII KBBA QL AB@I>OB > .'G;A? L?GB@Q.
Example 4.8: Image texture particle system
,L>A QEB FJ>DB FK M?NOJ().
,56A2 06?092@ FBGGF 6:.42@ D6A5 A?.;@=.?2;0F
F64B?2 4.5
.'G;A? CGA;
PIC> M?NOJ() U
Loading the PNG CGA = FI;>'G;A?("N?RNOL?.JHA");
W
#E>MQBO 4. 0>OQF@IB 3VPQBJP
184
!KA TEBK FQ @LJBP QFJB QL AO>T QEB M>OQF@IB, TBfII RPB QEB FJ>DB OBCBOBK@B FKPQB>A LC AO>TFKD
>K BIIFMPB LO OB@Q>KDIB.
)K@FABKQ>IIV, QEFP PJLHB BU>JMIB FP > KF@B BU@RPB QL OBSFPFQ QEB '>RPPF>K KRJ?BO AFPQOF?RQFLKP
COLJ QEB )KQOLAR@QFLK (PBB M>DB 10). 4L J>HB QEB PJLHB >MMB>O > ?FQ JLOB OB>IFPQF@, TB ALKfQ
T>KQ QL I>RK@E >II QEB M>OQF@IBP FK > MROBIV O>KALJ AFOB@QFLK. )KPQB>A, ?V @OB>QFKD FKFQF>I SBIL@FQV
SB@QLOP JLPQIV >OLRKA > JB>K S>IRB (TFQE > ILTBO MOL?>?FIFQV LC LRQIFBOP), TBfII DBQ >K BCCB@Q
QE>Q >MMB>OP IBPP CLRKQ>FK-IFHB >KA JLOB IFHB PJLHB (LO CFOB).
!PPRJFKD > /;H>IG L?GB@Q @>IIBA gDBKBO>QLOh, TB @LRIA @OB>QB FKFQF>I SBIL@FQFBP >P CLIILTP:
&FK>IIV, FK QEFP BU>JMIB, > TFKA CLO@B FP >MMIFBA QL QEB PJLHB J>MMBA COLJ QEB JLRPBfP
ELOFWLKQ>I IL@>QFLK.
PIC> L?H>?L() U
CG;A?+I>?(!#,1#/);
Note how tint() is the image equivalent of
shapes fill().
NCHN(255,FC@?MJ;H);
CG;A?(CGA,FI=.R,FI=.S);
W
@FI;N PR = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H()*0.3;
@FI;N PS = (@FI;N) A?H?L;NIL.H?RN%;OMMC;H()*0.3 - 1.0;
P?F = H?Q .3?=NIL(PR,PS);
PIC> >L;Q() U
<;=EALIOH>(0);
@FI;N >R = G;J(GIOM?5,0,QC>NB,-0.2,0.2);
Wind force points towards mouseX. .3?=NIL QCH> = H?Q .3?=NIL(>R,0);
JM.;JJFS$IL=?(QCH>);
JM.LOH();
Two particles are added each cycle through
draw().
@IL (CHN C = 0; C < 2; C++) U
JM.;>>.;LNC=F?();
W
W
4EB .>QROB LC #LAB (S005)
185
&FK>IIV, FQfP TLOQE KLQFKD QE>Q QEBOB >OB J>KV AFCCBOBKQ >IDLOFQEJP CLO ?IBKAFKD @LILOP FK
@LJMRQBO DO>MEF@P. 4EBPB >OB LCQBK OBCBOOBA QL >P g?IBKA JLABP.h "V ABC>RIQ, TEBK TB AO>T
PLJBQEFKD LK QLM LC PLJBQEFKD BIPB FK 0OL@BPPFKD, TB LKIV PBB QEB QLM I>VBOdQEFP FP
@LJJLKIV OBCBOOBA QL >P > gKLOJ>Ih ?IBKA JLAB. 7EBK QEB MFUBIP E>SB >IME> QO>KPM>OBK@V
(>P QEBV AL FK QEB PJLHB BU>JMIB), 0OL@BPPFKD RPBP >K >IME> @LJMLPFQFKD >IDLOFQEJ QE>Q
@LJ?FKBP > MBO@BKQ>DB LC QEB ?>@HDOLRKA MFUBIP TFQE QEB KBT CLOBDOLRKA MFUBIP ?>PBA LK
QEB >IME> S>IRBP.
(LTBSBO, FQfP MLPPF?IB QL AO>T RPFKD LQEBO ?IBKA JLABP, >KA > JR@E ILSBA ?IBKA JLAB CLO
M>OQF@IB PVPQBJP FP g>AAFQFSB.h !AAFQFSB ?IBKAFKD FK 0OL@BPPFKD T>P MFLKBBOBA ?V 2L?BOQ
(LADFK (EQQM://OL?BOQELADFK.@LJ/) FK EFP C>JLRP M>OQF@IB PVPQBJ >KA CLO@BP BUMILO>QFLK,
->DKBQLPMEBOB, TEF@E I>QBO ?B@>JB QEB F4RKBP SFPR>IFWBO. &LO JLOB PBB: ->DKBQLPMEBOB
(EQQM://OL?BOQELADFK.@LJ/J>DKBQLPMEBOB-M>OQ-2/).
!AAFQFSB ?IBKAFKD FP FK C>@Q LKB LC QEB PFJMIBPQ ?IBKA >IDLOFQEJP >KA FKSLISBP >AAFKD QEB
MFUBI S>IRBP LC LKB I>VBO QL >KLQEBO (@>MMFKD >II S>IRBP >Q 255 LC @LROPB). 4EFP OBPRIQP FK >
PM>@B->DB DILT BCCB@Q ARB QL QEB @LILOP DBQQFKD ?OFDEQBO >KA ?OFDEQBO TFQE JLOB I>VBOP.
4L >@EFBSB >AAFQFSB ?IBKAFKD FK 0OL@BPPFKD, VLRfII KBBA QL RPB QEB .2" LO .3" OBKABOBO.
4OV @OB>QFKD VLRO LTK QBUQROBP CLO AFCCBOBKQ QVMBP LC BCCB@QP. #>K VLR J>HB FQ ILLH IFHB
CFOB, FKPQB>A LC PJLHB?
Exercise 4.11 Exercise 4.11
5PB >K >OO>V LC FJ>DBP >KA >PPFDK B>@E .;LNC=F? L?GB@Q > AFCCBOBKQ FJ>DB. %SBK
QELRDE PFKDIB FJ>DBP >OB AO>TK ?V JRIQFMIB M>OQF@IBP, J>HB PROB VLR ALKfQ @>II
FI;>'G;A?() >KV JLOB QE>K VLR KBBA QL, F.B. LK@B CLO B>@E FJ>DB CFIB.
Exercise 4.12 Exercise 4.12
#E>MQBO 4. 0>OQF@IB 3VPQBJP
186
Example 4.9: Additive blending
4EBK, ?BCLOB VLR DL QL AO>T >KVQEFKD, VLR PBQ QEB ?IBKA JLAB RPFKD <F?H>+I>?():
PIC> M?NOJ() U
Using the P2D renderer MCT?(200,200,.2");
W
PIC> >L;Q() U
Additive blending <F?H>+I>?(A"");
Note that the glowing effect of additive
blending will not work with a white (or very
bright) background.
<;=EALIOH>(0);
All your other particle stuff would go here.
W
5PB NCHN() FK @LJ?FK>QFLK TFQE >AAFQFSB ?IBKAFKD QL @OB>QB > O>FK?LT BCCB@Q.
Exercise 4.13 Exercise 4.13
4OV ?IBKAFKD TFQE LQEBO JLABP, PR@E >P 021/A!1, *'%&1#01, "A/)#01, "'$$#/#,!#,
#5!*20'-,,LO +2*1'.*6.
Exercise 4.14 Exercise 4.14
4EB .>QROB LC #LAB (S005)
187
The Ecosystem Project The Ecosystem Project
Step 4 Exercise:
Take your creature from Step 3 and build a system of creatures. How can they
interact with each other? Can you use inheritance and polymorphism to create a
variety of creatures, derived from the same code base? Develop a methodology
for how they compete for resources (for example, food). Can you track a
creatures health much like we tracked a particles lifespan, removing creatures
when appropriate? What rules can you incorporate to control how creatures are
born?
(Also, you might consider using a particle system itself in the design of a creature.
What happens if your emitter is tied to the creatures location?)
#E>MQBO 4. 0>OQF@IB 3VPQBJP
188
Chapter 5. Physics Chapter 5. Physics
Libraries Libraries
IA 96/?.?F 6:=962@ .; .0A <3 3.6A5/,5605 42;2?.A6<;@ @A699 6; 1.?8;2@@ 561/
)64; 6; A526? ;645A 6; D6A;2@@ <3 A52 1.D;.J
Victor Hugo
"BCLOB TB JLSB LK QL >KVQEFKD BIPB, IBQfP OBSFPFQ PLJB LC QEB QEFKDP TBfSB ALKB FK QEB CFOPQ
CLRO @E>MQBOP. 7B E>SB:
1. ,B>OKBA >?LRQ @LK@BMQP COLJ QEB TLOIA LC MEVPF@P d 7E>Q FP > SB@QLO? 7E>Q FP >
CLO@B? 7E>Q FP > T>SB? BQ@.
2. 5KABOPQLLA QEB J>QE >KA >IDLOFQEJP ?BEFKA PR@E @LK@BMQP.
3. )JMIBJBKQBA QEB >IDLOFQEJP FK 0OL@BPPFKD TFQE >K L?GB@Q-LOFBKQBA >MMOL>@E.
4EBPB >@QFSFQFBP E>SB VFBIABA > PBQ LC JLQFLK PFJRI>QFLK BU>JMIBP, >IILTFKD RP QL @OB>QFSBIV
ABCFKB QEB MEVPF@P LC QEB TLOIAP TB ?RFIA (TEBQEBO OB>IFPQF@ LO C>KQ>PQF@>I). /C @LROPB, TB >OBKfQ
QEB CFOPQ QL QOV QEFP. 4EB TLOIA LC @LJMRQBO DO>MEF@P >KA MOLDO>JJFKD FP CRII LC PLRO@B @LAB
ABAF@>QBA QL PFJRI>QFLK. *RPQ QOV 'LLDIFKD gLMBK-PLRO@B MEVPF@P BKDFKBh >KA VLR @LRIA PMBKA
QEB OBPQ LC VLRO A>V MLROFKD LSBO OF@E >KA @LJMIBU @LAB. !KA PL TB JRPQ >PH QEB NRBPQFLK: )C
> @LAB IF?O>OV TFII Q>HB @>OB LC MEVPF@P PFJRI>QFLK, TEV PELRIA TB ?LQEBO IB>OKFKD ELT QL TOFQB
>KV LC QEB >IDLOFQEJP LROPBISBP?
(BOB FP TEBOB QEB MEFILPLMEV ?BEFKA QEFP ?LLH @LJBP FKQL MI>V. 7EFIB J>KV LC QEB IF?O>OFBP
LRQ QEBOB DFSB RP MEVPF@P (>KA PRMBO >TBPLJB >AS>K@BA MEVPF@P >Q QE>Q) CLO COBB, QEBOB >OB
4EB .>QROB LC #LAB (S005)
189
PFDKFCF@>KQ OB>PLKP CLO IB>OKFKD QEB CRKA>JBKQ>IP COLJ P@O>Q@E ?BCLOB AFSFKD FKQL IF?O>OFBP.
&FOPQ, TFQELRQ >K RKABOPQ>KAFKD LC SB@QLOP, CLO@BP, >KA QOFDLKLJBQOV, TBfA ?B @LJMIBQBIV ILPQ
GRPQ OB>AFKD QEB AL@RJBKQ>QFLK LC > IF?O>OV. 3B@LKA, BSBK QELRDE > IF?O>OV J>V Q>HB @>OB LC
QEB J>QE CLO RP, FQ TLKfQ KB@BPP>OFIV PFJMIFCV LRO @LAB. !P TBfII PBB FK > JLJBKQ, QEBOB @>K
?B > DOB>Q AB>I LC LSBOEB>A FK PFJMIV RKABOPQ>KAFKD ELT > IF?O>OV TLOHP >KA TE>Q FQ
BUMB@QP COLJ VLR @LAB-TFPB. &FK>IIV, >P TLKABOCRI >P > MEVPF@P BKDFKB JFDEQ ?B, FC VLR ILLH
ABBM ALTK FKQL VLRO EB>OQP, FQfP IFHBIV QE>Q VLR PBBH QL @OB>QB TLOIAP >KA SFPR>IFW>QFLKP QE>Q
PQOBQ@E QEB IFJFQP LC FJ>DFK>QFLK. ! IF?O>OV FP DOB>Q, ?RQ FQ MOLSFABP > IFJFQBA PBQ LC CB>QROBP.
)QfP FJMLOQ>KQ QL HKLT ?LQE TEBK QL IFSB TFQEFK IFJFQ>QFLKP FK QEB MROPRFQ LC > 0OL@BPPFKD
MOLGB@Q >KA TEBK QELPB IFJFQP MOLSB QL ?B @LKCFKFKD.
4EFP @E>MQBO FP ABAF@>QBA QL BU>JFKFKD QTL LMBK-PLRO@B MEVPF@P IF?O>OFBPd"LU2$ >KA
QLUF@IF?Pf 6BOIBQ0EVPF@P BKDFKB. 7FQE B>@E IF?O>OV, TBfII BS>IR>QB FQP MOLP >KA @LKP >KA ILLH
>Q OB>PLKP TEV VLR JFDEQ @ELLPB LKB LC QEBPB IF?O>OFBP CLO > DFSBK MOLGB@Q.
5.1 What Is Box2D and When Is It Useful? 5.1 What Is Box2D and When Is It Useful?
"LU2$ ?BD>K >P > PBQ LC MEVPF@P QRQLOF>IP TOFQQBK FK #++ ?V %OFK #>QQL CLO QEB '>JB
$BSBILMBOfP #LKCBOBK@B FK 2006. /SBO QEB I>PQ CFSB VB>OP FQ E>P BSLISBA FKQL >K OF@E >KA
BI>?LO>QB LMBK-PLRO@B MEVPF@P BKDFKB. )QfP ?BBK RPBA CLO @LRKQIBPP MOLGB@QP, JLPQ KLQ>?IV
EFDEIV PR@@BPPCRI D>JBP PR@E >P QEB >T>OA-TFKKFKD MRWWIB D>JB #O>VLK 0EVPF@P >KA QEB
ORK>T>V JL?FIB >KA Q>?IBQ EFQ !KDOV "FOAP.
/KB LC QEB HBV QEFKDP QL OB>IFWB >?LRQ "LU2$ FP QE>Q FQ FP > QORB MEVPF@P BKDFKB. "LU2$
HKLTP KLQEFKD >?LRQ @LJMRQBO DO>MEF@P >KA QEB TLOIA LC MFUBIP; FQ FP PFJMIV > IF?O>OV QE>Q
Q>HBP FK KRJ?BOP >KA PMFQP LRQ JLOB KRJ?BOP. !KA TE>Q >OB QELPB KRJ?BOP? -BQBOP,
HFILDO>JP, PB@LKAP, BQ@. !II LC "LU2$fP JB>PROBJBKQP >KA @>I@RI>QFLKP >OB CLO OB>I-TLOIA
JB>PROBJBKQPdLKIV FQP gTLOIAh FP > QTL-AFJBKPFLK>I MI>KB TFQE QLM, ?LQQLJ, IBCQ, >KA OFDEQ
BADBP. 9LR QBII FQ QEFKDP IFHB: g4EB DO>SFQV LC LRO TLOIA FP 9.81 KBTQLKP MBO HFILDO>J, >KA >
@FO@IB TFQE > O>AFRP LC CLRO JBQBOP >KA > J>PP LC CFCQV HFILDO>JP FP IL@>QBA QBK JBQBOP >?LSB
QEB TLOIAfP ?LQQLJ.h "LU2$ TFII QEBK QBII VLR QEFKDP IFHB: g/KB PB@LKA I>QBO, QEB OB@Q>KDIB FP
>Q CFSB JBQBOP COLJ QEB ?LQQLJ; QTL PB@LKAP I>QBO, FQ FP QBK JBQBOP ?BILT,h BQ@. 7EFIB QEFP
MOLSFABP CLO >K >J>WFKD >KA OB>IFPQF@ MEVPF@P BKDFKB, FQ >IPL KB@BPPFQ>QBP ILQP LC @LJMIF@>QBA
@LAB FK LOABO QL QO>KPI>QB ?>@H >KA CLOQE ?BQTBBK QEB MEVPF@P gTLOIAh (> HBV QBOJ FK "LU2$)
>KA QEB TLOIA TB T>KQ QL AO>T LK dQEB gMFUBIh TLOIA LC 0OL@BPPFKD.
3L TEBK FP FQ TLOQE FQ QL E>SB QEFP >AAFQFLK>I LSBOEB>A? )C ) GRPQ T>KQ QL PFJRI>QB > @FO@IB
C>IIFKD ALTK > 0OL@BPPFKD TFKALT TFQE DO>SFQV, AL ) OB>IIV KBBA QL TOFQB >II QEB BUQO> "LU2$
@LAB GRPQ QL DBQ QE>Q BCCB@Q? #BOQ>FKIV, QEB >KPTBO FP KL. 7B P>T ELT QL AL QEFP O>QEBO B>PFIV
FK QEB CFOPQ @E>MQBO LC QEFP ?LLH. ,BQfP @LKPFABO >KLQEBO P@BK>OFL. 7E>Q FC ) T>KQ QL E>SB >
ERKAOBA LC QELPB @FO@IBP C>IIFKD? !KA TE>Q FC QELPB @FO@IBP >OBKfQ @FO@IBP >Q >II, ?RQ FOOBDRI>OIV
PE>MBA MLIVDLKP? !KA TE>Q FC ) T>KQ QEBPB MLIVDLKP QL ?LRK@B LCC B>@E LQEBO FK > OB>IFPQF@
J>KKBO TEBK QEBV @LIIFAB?
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
190
9LR J>V E>SB KLQF@BA QE>Q QEB CFOPQ CLRO @E>MQBOP LC QEFP ?LLH, TEFIB @LSBOFKD JLQFLK >KA
CLO@BP FK ABQ>FI, E>P PHFMMBA LSBO > O>QEBO FJMLOQ>KQ >PMB@Q LC MEVPF@P PFJRI>QFLKd7C@@=G=CBG.
,BQfP MOBQBKA CLO > JLJBKQ QE>Q VLR >OBKfQ OB>AFKD > @E>MQBO >?LRQ IF?O>OFBP >KA QE>Q TB
AB@FABA OFDEQ KLT QL @LSBO ELT QL E>KAIB @LIIFPFLKP FK > M>OQF@IB PVPQBJ. 7BfA E>SB QL
BS>IR>QB >KA IB>OK QTL AFPQFK@Q >IDLOFQEJP QE>Q >AAOBPP QEBPB NRBPQFLKP:
1. (LT AL ) ABQBOJFKB FC QTL PE>MBP >OB @LIIFAFKD (F.B. FKQBOPB@QFKD)?
2. (LT AL ) ABQBOJFKB QEB PE>MBPf SBIL@FQV >CQBO QEB @LIIFPFLK?
)C TBfOB QEFKHFKD >?LRQ PE>MBP IFHB OB@Q>KDIBP LO @FO@IBP, NRBPQFLK #1 FPKfQ QLL QLRDE. 9LRfSB
IFHBIV BK@LRKQBOBA QEFP ?BCLOB. &LO BU>JMIB, TB HKLT QTL @FO@IBP >OB FKQBOPB@QFKD FC QEB
AFPQ>K@B ?BQTBBK QEBJ FP IBPP QE>K QEB PRJ LC QEBFO O>AFF.
/+. .LT QE>Q TB HKLT ELT QL ABQBOJFKB FC QTL @FO@IBP >OB @LIIFAFKD, ELT AL TB @>I@RI>QB QEBFO
SBIL@FQFBP >CQBO QEB @LIIFPFLK? 4EFP FP TEBOB TBfOB DLFKD QL PQLM LRO AFP@RPPFLK. 7EV, VLR >PH?
)QfP KLQ QE>Q RKABOPQ>KAFKD QEB J>QE ?BEFKA @LIIFPFLKP FPKfQ FJMLOQ>KQ LO S>IR>?IB. ()K C>@Q, )fJ
FK@IRAFKD >AAFQFLK>I BU>JMIBP LK QEB TB?PFQB OBI>QBA QL @LIIFPFLKP TFQELRQ > MEVPF@P IF?O>OV.)
4EB OB>PLK CLO PQLMMFKD FP QE>Q IFCB FP PELOQ (IBQ QEFP >IPL ?B > OB>PLK CLO VLR QL @LKPFABO DLFKD
LRQPFAB >KA COLIF@HFKD FKPQB>A LC MOLDO>JJFKD >IQLDBQEBO). 7B @>KfQ BUMB@Q QL J>PQBO BSBOV
ABQ>FI LC MEVPF@P PFJRI>QFLK. !KA TEFIB TB @LRIA @LKQFKRB QEFP AFP@RPPFLK CLO @FO@IBP, FQfP LKIV
DLFKD QL IB>A RP QL T>KQFKD QL TLOH TFQE OB@Q>KDIBP. !KA PQO>KDBIV PE>MBA MLIVDLKP. !KA
@ROSBA PROC>@BP. !KA PTFKDFKD MBKARIRJP @LIIFAFKD TFQE PMOFKDV PMOFKDP. !KA >KA >KA >KA
>KA.
7LOHFKD TFQE @LIIFPFLKP FK LRO 0OL@BPPFKD PHBQ@E TEFIB PQFII E>SFKD QFJB QL PMBKA TFQE LRO
COFBKAP >KA C>JFIVdQE>QfP QEB OB>PLK CLO QEFP @E>MQBO. %OFK #>QQL PMBKQ VB>OP ABSBILMFKD
PLIRQFLKP QL QEBPB HFKAP LC MOL?IBJP PL VLR ALKfQ KBBA QL BKDFKBBO QEBJ VLROPBISBP, >Q IB>PQ
CLO KLT.
F64B?2 5.1
4EB .>QROB LC #LAB (S005)
191
)K @LK@IRPFLK, FC VLR CFKA VLROPBIC ABP@OF?FKD >K FAB> CLO > 0OL@BPPFKD PHBQ@E >KA QEB TLOA
g@LIIFPFLKPh @LJBP RM, QEBK FQfP IFHBIV QFJB QL IB>OK "LU2$. (7BfII >IPL BK@LRKQBO LQEBO TLOAP
QE>Q JFDEQ IB>A VLR ALTK QEFP M>QE QL "LU2$, PR@E >P gGLFKQ,h gEFKDB,h gMRIIBV,h gJLQLO,h BQ@.)
5.2 Getting Box2D in Processing 5.2 Getting Box2D in Processing
3L, FC "LU2$ FP > MEVPF@P BKDFKB QE>Q HKLTP KLQEFKD >?LRQ MFUBI-?>PBA @LJMRQBO DO>MEF@P
>KA FP TOFQQBK FK #++, ELT >OB TB PRMMLPBA QL RPB FQ FK 0OL@BPPFKD?
4EB DLLA KBTP FP QE>Q "LU2$ FP PR@E >K >J>WFKD >KA RPBCRI IF?O>OV QE>Q BSBOVLKB T>KQP QL
RPB FQd&I>PE, *>S>P@OFMQ, 0VQELK, 2R?V MOLDO>JJBOP. /E, >KA *>S> MOLDO>JJBOP. 4EBOB FP
PLJBQEFKD @>IIBA *"LU2$, > *>S> MLOQ LC "LU2$. !KA ?B@>RPB 0OL@BPPFKD FP ?RFIQ LK QLM LC
*>S>, *"LU2$ @>K ?B RPBA AFOB@QIV FK 0OL@BPPFKD!
3L EBOBfP TEBOB TB >OB PL C>O.
i "LU2$ PFQB (EQQM://TTT.?LU2A.LOD/) CLO OBCBOBK@B.
i *"LU2$ PFQB (EQQM://TTT.G?LU2A.LOD/) CLO 0OL@BPPFKD @LJM>QF?FIFQV.
4EFP FP >II VLR KBBA QL DBQ PQ>OQBA TOFQFKD "LU2$ @LAB FK 0OL@BPPFKD. (LTBSBO, >P TB >OB
DLFKD QL PBB FK > JLJBKQ, QEBOB >OB PBSBO>I MFB@BP LC CRK@QFLK>IFQV TBfII OBMB>QBAIV KBBA FK
LRO 0OL@BPPFKD @LAB, >KA PL FQfP TLOQE E>SFKD LKB >AAFQFLK>I I>VBO ?BQTBBK LRO PHBQ@EBP
>KA *"LU2$. )fJ @>IIFKD QEFP 0"LU2$d> 0OL@BPPFKD "LU2$ gEBIMBOh IF?O>OV FK@IRABA >P M>OQ
LC QEFP ?LLHfP @LAB BU>JMIB ALTKIL>AP.
i 0"LU2$ 'FQ(R? OBMLPFQLOV (EQQM://DFQER?.@LJ/PEFCCJ>K/0"LU2$)
)QfP FJMLOQ>KQ QL OB>IFWB QE>Q 0"LU2$ FP KLQ > 0OL@BPPFKD TO>MMBO CLO >II LC "LU2$. !CQBO >II,
"LU2$ FP > QELRDEQCRIIV LOD>KFWBA >KA TBII-PQOR@QROBA !0) >KA QEBOBfP KL OB>PLK QL Q>HB FQ
>M>OQ >KA OB-FJMIBJBKQ FQ. (LTBSBO, FQfP RPBCRI QL E>SB > PJ>II PBQ LC CRK@QFLKP QE>Q EBIM VLR
DBQ VLRO "LU2$ TLOIA PBQ RM, >P TBII >P EBIM VLR CFDROB LRQ TEBOB QL AO>T VLRO "LU2$
PE>MBP. !KA QEFP FP TE>Q 0"LU2$ TFII MOLSFAB.
) PELRIA >IPL JBKQFLK ?BCLOB TB JLSB CLOT>OA QE>Q QEBOB >OB LQEBO 0OL@BPPFKD IF?O>OFBP QE>Q
TO>M "LU2$ CLO VLR. /KB ) TLRIA OB@LJJBKA Q>HFKD > ILLH >Q FP &FPF@>
(EQQM://TTT.OF@>OAJ>OUBO.@LJ/CFPF@>/) ?V 2F@>OA ->OUBO.
5.3 Box2D Basics 5.3 Box2D Basics
$L KLQ ABPM>FO! 7B OB>IIV >OB DLFKD QL DBQ QL QEB @LAB SBOV PLLK, >KA FK PLJB T>VP TBfII
?ILT LRO MOBSFLRP TLOH LRQ LC QEB T>QBO. "RQ ?BCLOB TBfOB OB>AV QL AL QE>Q, FQfP FJMLOQ>KQ QL
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
192
T>IH QEOLRDE QEB LSBO>II MOL@BPP LC RPFKD "LU2$ FK 0OL@BPPFKD. ,BQfP ?BDFK ?V TOFQFKD >
MPBRAL@LAB DBKBO>IFW>QFLK LC >II LC LRO BU>JMIBP FK #E>MQBOP 1 QEOLRDE 4.
'E()%: 'E()%:
1. #OB>QB >II QEB L?GB@QP FK LRO TLOIA.
D&A+: D&A+:
1. #>I@RI>QB >II QEB CLO@BP FK LRO TLOIA.
2. !MMIV >II QEB CLO@BP QL LRO L?GB@QP (& = - * !).
3. 5MA>QB QEB IL@>QFLKP LC >II QEB L?GB@QP ?>PBA LK QEBFO >@@BIBO>QFLK.
4. $O>T >II LC LRO L?GB@QP.
'OB>Q. ,BQfP OBTOFQB QEFP MPBRAL@LAB >P FQ TFII >MMB>O FK LRO "LU2$ BU>JMIBP.
'E()%: 'E()%:
1. #OB>QB >II QEB L?GB@QP FK LRO TLOIA.
D&A+: D&A+:
1. $O>T >II LC LRO L?GB@QP.
4EFP, LC @LROPB, FP QEB C>KQ>PV LC "LU2$. 7BfSB BIFJFK>QBA >II LC QELPB M>FKCRI PQBMP LC CFDROFKD
LRQ ELT QEB L?GB@QP >OB JLSFKD >@@LOAFKD QL SBIL@FQV >KA >@@BIBO>QFLK. "LU2$ FP DLFKD QL Q>HB
@>OB LC QEFP CLO RP! 4EB DLLA KBTP FP QE>Q QEFP ALBP >@@RO>QBIV OBCIB@Q QEB LSBO>II MOL@BPP. ,BQfP
FJ>DFKB "LU2$ >P > J>DF@ ?LU.
)K M?NOJ(), TBfOB DLFKD QL P>V QL "LU2$: g(BIIL QEBOB. (BOB >OB >II LC QEB QEFKDP ) T>KQ FK JV
TLOIA.h )K >L;Q(), TBfOB DLFKD QL MLIFQBIV >PH "LU2$: g/E, EBIIL >D>FK. )C FQfP KLQ QLL JR@E
QOLR?IB, )fA IFHB QL AO>T >II LC QELPB QEFKDP FK JV TLOIA. #LRIA VLR QBII JB TEBOB QEBV >OB?h
4EB ?>A KBTP: FQfP KLQ >P PFJMIB >P QEB >?LSB BUMI>K>QFLK TLRIA IB>A VLR QL ?BIFBSB. &LO LKB,
J>HFKD QEB PQRCC QE>Q DLBP FK QEB "LU2$ TLOIA FKSLISBP T>AFKD QEOLRDE QEB AL@RJBKQ>QFLK CLO
ELT AFCCBOBKQ HFKAP LC PE>MBP >OB ?RFIQ >KA @LKCFDROBA. 3B@LKA, TB E>SB QL OBJBJ?BO QE>Q TB
@>KfQ QBII "LU2$ >KVQEFKD >?LRQ MFUBIP, >P FQ TFII PFJMIV DBQ @LKCRPBA >KA C>II >M>OQ. "BCLOB TB
QBII "LU2$ TE>Q TB T>KQ FK LRO TLOIA, TB E>SB QL @LKSBOQ LRO MFUBI RKFQP QL "LU2$ gTLOIAh
RKFQP. !KA QEB P>JB FP QORB TEBK FQ @LJBP QFJB QL AO>T LRO PQRCC. "LU2$ FP DLFKD QL QBII RP QEB
IL@>QFLK LC QEB QEFKDP FK FQP TLOIA, TEF@E TB QEBK E>SB QL QO>KPI>QB CLO QEB MFUBI TLOIA.
SETUP SETUP
1. #OB>QB BSBOVQEFKD QE>Q IFSBP FK LRO MFUBI TLOIA.
4EB .>QROB LC #LAB (S005)
193
2. 4O>KPI>QB QEB MFUBI TLOIA FKQL QEB "LU2$ TLOIA.
DRAW DRAW
1. !PH "LU2$ TEBOB BSBOVQEFKD FP.
2. 4O>KPI>QB "LU2$fP >KPTBO FKQL QEB MFUBI TLOIA.
3. $O>T BSBOVQEFKD.
.LT QE>Q TB RKABOPQ>KA QE>Q >KVQEFKD TB @OB>QB FK LRO 0OL@BPPFKD PHBQ@E E>P QL ?B MI>@BA
FKQL QEB "LU2$ TLOIA, IBQfP ILLH >Q >K LSBOSFBT LC QEB BIBJBKQP QE>Q J>HB RM QE>Q TLOIA.
Core elements of a Box2D world: Core elements of a Box2D world:
1. +:=7/ +:=7/: ->K>DBP QEB MEVPF@P PFJRI>QFLK. )Q HKLTP BSBOVQEFKD >?LRQ QEB LSBO>II
@LLOAFK>QB PM>@B >KA >IPL PQLOBP IFPQP LC BSBOV BIBJBKQ FK QEB TLOIA (PBB 2-4
?BILT).
2. B:/D B:/D: 3BOSBP >P QEB MOFJ>OV BIBJBKQ FK QEB "LU2$ TLOIA. )Q E>P > IL@>QFLK. )Q E>P >
SBIL@FQV. 3LRKA C>JFIF>O? 4EB I>S FP BPPBKQF>IIV QEB @I>PP TBfSB ?BBK TOFQFKD LK
LRO LTK FK LRO SB@QLOP >KA CLO@BP BU>JMIBP.
3. '3,;0 '3,;0: +BBMP QO>@H LC >II QEB KB@BPP>OV @LIIFPFLK DBLJBQOV >QQ>@EBA QL > ?LAV.
4. F4C?@=0 F4C?@=0: !QQ>@EBP > PE>MB QL > ?LAV >KA PBQP MOLMBOQFBP PR@E >P ABKPFQV, COF@QFLK,
>KA OBPQFQRQFLK.
5. J:49? J:49?: !@QP >P > @LKKB@QFLK ?BQTBBK QTL ?LAFBP (LO ?BQTBBK LKB ?LAV >KA QEB
TLOIA FQPBIC).
)K QEB KBUQ CLRO PB@QFLKP, TB >OB DLFKD QL T>IH QEOLRDE B>@E LC QEB >?LSB BIBJBKQP FK ABQ>FI,
?RFIAFKD PBSBO>I BU>JMIBP >ILKD QEB T>V. "RQ CFOPQ QEBOB FP LKB LQEBO FJMLOQ>KQ BIBJBKQ TB
PELRIA ?OFBCIV AFP@RPP.
6. *0.2 *0.2: $BP@OF?BP > SB@QLO FK QEB "LU2$ TLOIA.
!KA PL EBOB TB >OB, >OOFSFKD TFQE QOBMFA>QFLK >Q >K RKCLOQRK>QB QORQE FK QEB TLOIA LC RPFKD
MEVPF@P IF?O>OFBP. !KV MEVPF@P PFJRI>QFLK FP DLFKD QL FKSLISB QEB @LK@BMQ LC > SB@QLO. 4EFP FP
QEB DLLA M>OQ. !CQBO >II, TB GRPQ PMBKQ PBSBO>I @E>MQBOP C>JFIF>OFWFKD LROPBISBP TFQE TE>Q FQ
JB>KP QL ABP@OF?B JLQFLK >KA CLO@BP TFQE SB@QLOP. 7B ALKfQ E>SB QL IB>OK >KVQEFKD KBT
@LK@BMQR>IIV.
.LT CLO QEB M>OQ QE>Q J>HBP QEB PFKDIB QB>O C>II COLJ JV BVB: TB ALKfQ DBQ QL RPB .3?=NIL.
)QfP KF@B QE>Q 0OL@BPPFKD E>P .3?=NIL CLO RP, ?RQ >KVQFJB VLR RPB > MEVPF@P IF?O>OV VLR TFII
MOL?>?IV AFP@LSBO QE>Q QEB IF?O>OV FK@IRABP FQP LTK SB@QLO FJMIBJBKQ>QFLK. 4EFP J>HBP PBKPB,
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
194
>CQBO >II; TEV PELRIA "LU2$ ?B BUMB@QBA QL HKLT >?LRQ .3?=NIL? !KA FK J>KV @>PBP, QEB
MEVPF@P BKDFKB TFII T>KQ QL FJMIBJBKQ > SB@QLO @I>PP FK > PMB@FCF@ T>V PL QE>Q FQ FP BPMB@F>IIV
@LJM>QF?IB TFQE QEB OBPQ LC QEB IF?O>OVfP @LAB. 3L TEFIB TB ALKfQ E>SB QL IB>OK >KVQEFKD KBT
@LK@BMQR>IIV, TB AL E>SB QL DBQ RPBA QL PLJB KBT K>JFKD @LKSBKQFLKP >KA PVKQ>U. ,BQfP
NRF@HIV ABJLKPQO>QB > CBT LC QEB ?>PF@P FK 3?=2 >P @LJM>OBA QL QELPB FK .3?=NIL.
,BQfP P>V TB T>KQ QL >AA QTL SB@QLOP QLDBQEBO.
PVector PVector Vec2 Vec2
.3?=NIL ; = H?Q .3?=NIL(1,-1);
.3?=NIL < = H?Q .3?=NIL(3,4);
;.;>>(<);
3?=2 ; = H?Q 3?=2(1,-1);
3?=2 < = H?Q 3?=2(3,4);
;.;>>*I=;F(<);
.3?=NIL ; = H?Q .3?=NIL(1,-1);
.3?=NIL < = H?Q .3?=NIL(3,4);
.3?=NIL = = .3?=NIL.;>>(;,<);
3?=2 ; = H?Q 3?=2(1,-1);
3?=2 < = H?Q 3?=2(3,4);
3?=2 = = ;.;>>(<);
(LT >?LRQ FC TB T>KQ QL JRIQFMIV >KA P@>IB QEBJ?
PVector PVector Vec2 Vec2
.3?=NIL ; = H?Q .3?=NIL(1,-1);
@FI;N H = 5;
;.GOFN(H);
3?=2 ; = H?Q 3?=2(1,-1);
@FI;N H = 5;
;.GOF*I=;F(H);
.3?=NIL ; = H?Q .3?=NIL(1,-1);
@FI;N H = 5;
.3?=NIL = = .3?=NIL.GOFN(;,H);
3?=2 ; = H?Q 3?=2(1,-1);
@FI;N H = 5;
3?=2 = = ;.GOF(H);
->DKFQRAB >KA KLOJ>IFWB?
PVector PVector Vec2 Vec2
.3?=NIL ; = H?Q .3?=NIL(1,-1);
@FI;N G = ;.G;A();
;.HILG;FCT?();
3?=2 ; = H?Q 3?=2(1,-1);
@FI;N G = ;.F?HANB();
;.HILG;FCT?();
!P VLR @>K PBB, QEB @LK@BMQP >OB QEB P>JB, ?RQ QEB CRK@QFLK K>JBP >KA QEB >ODRJBKQP >OB
PIFDEQIV AFCCBOBKQ. &LO BU>JMIB, FKPQB>A LC PQ>QF@ >KA KLK-PQ>QF@ ;>>() >KA GOFN(), FC > 3?=2 FP
>IQBOBA, QEB TLOA gIL@>Ih FP FK@IRABA FK QEB CRK@QFLK K>JBd;>>*I=;F(), GOFN*I=;F().
4EB .>QROB LC #LAB (S005)
195
7BfII @LSBO QEB ?>PF@P LC TE>Q VLR KBBA QL HKLT EBOB, ?RQ FC VLR >OB ILLHFKD CLO JLOB, CRII
AL@RJBKQ>QFLK LC 3?=2 @>K ?B CLRKA ?V ALTKIL>AFKD QEB *"LU2$ PLRO@B @LAB
(EQQM://@LAB.DLLDIB.@LJ/M/G?LU2A/).
5.4 Living in a Box2D World 5.4 Living in a Box2D World
4EB "LU2$ 4ILF> L?GB@Q FP FK @E>ODB LC BSBOVQEFKD. )Q J>K>DBP QEB @LLOAFK>QB PM>@B LC QEB
TLOIA, >II LC QEB PQRCC QE>Q IFSBP FK QEB TLOIA, >KA AB@FABP TEBK QFJB JLSBP CLOT>OA FK QEB
TLOIA.
)K LOABO QL E>SB "LU2$ >P M>OQ LC LRO 0OL@BPPFKD PHBQ@EBP, QEB 4ILF> FP QEB SBOV CFOPQ QEFKD
QE>Q KBBAP QL ?B PBQ RM. (BOB FP TEBOB 0"LU2$ @LJBP FK E>KAV >KA Q>HBP @>OB LC J>HFKD
QEB TLOIA CLO RP.
7EBK VLR @>II =L?;N?4ILF>(), 0"LU2$ TFII PBQ RM > ABC>RIQ DO>SFQV CLO VLR (MLFKQFKD ALTK);
ELTBSBO, VLR @>K >IT>VP >IQBO QEB DO>SFQV LC VLRO TLOIA ?V P>VFKD:
)QfP TLOQE KLQFKD QE>Q DO>SFQV ALBPKfQ E>SB QL ?B CFUBA, KLO ALBP FQ >IT>VP E>SB QL MLFKQ
ALTKT>OAP; VLR @>K >AGRPQ QEB DO>SFQV SB@QLO TEFIB VLRO MOLDO>J FP ORKKFKD. 'O>SFQV @>K ?B
QROKBA LCC ?V PBQQFKD FQ QL > (0,0) SB@QLO.
3L, TE>Q >OB QELPB KRJ?BOP 0 >KA -10? 4EFP PELRIA OBJFKA RP LC LKB LC QEB JLPQ FJMLOQ>KQ
ABQ>FIP LC RPFKD "LU2$: QEB "LU2$ @LLOAFK>QB PVPQBJ FP KLQ VLRO MFUBI @LLOAFK>QB PVPQBJ!
,BQfP ILLH >Q ELT "LU2$ >KA > 0OL@BPPFKD TFKALT QEFKH AFCCBOBKQIV LC QEBFO TLOIAP.
.IR2" <IR2>;
PIC> M?NOJ() U
<IR2> = H?Q .IR2"(NBCM);
Initializes a Box2D world with default
settings
<IR2>.=L?;N?4ILF>();
W
<IR2>.M?N%L;PCNS(0, -10);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
196
.LQF@B ELT FK "LU2$ (0,0) FP FK QEB @BKQBO >KA ID FP QEB MLPFQFSB AFOB@QFLK >ILKD QEB V->UFP!
"LU2$fP @LLOAFK>QB PVPQBJ FP GRPQ IFHB QE>Q ILSBIV LIA-C>PEFLKBA #>OQBPF>K LKB TFQE (0,0) FK QEB
@BKQBO >KA ID MLFKQFKD FK > MLPFQFSB AFOB@QFLK. 0OL@BPPFKD, LK QEB LQEBO E>KA, RPBP > QO>AFQFLK>I
@LJMRQBO DO>MEF@P @LLOAFK>QB PVPQBJ TEBOB (0,0) FP FK QEB QLM IBCQ @LOKBO >KA 8CKB FP QEB
MLPFQFSB AFOB@QFLK >ILKD QEB V->UFP. 4EFP FP TEV FC TB T>KQ L?GB@QP QL C>II ALTK TFQE DO>SFQV, TB
KBBA QL DFSB "LU2$ > DO>SFQV CLO@B TFQE > KBD>QFSB V-S>IRB.
,R@HFIV CLO RP, FC TB MOBCBO QL QEFKH FK QBOJP LC MFUBI @LLOAFK>QBP (TEF@E >P 0OL@BPPFKD
MOLDO>JJBOP, TB >OB IFHBIV QL AL), 0"LU2$ LCCBOP > PBOFBP LC EBIMBO CRK@QFLKP QE>Q @LKSBOQ
?BQTBBK MFUBI PM>@B >KA "LU2$ PM>@B. "BCLOB TB JLSB LKQL QEB KBUQ PB@QFLK >KA ?BDFK
@OB>QFKD "LU2$ ?LAFBP, IBQfP Q>HB > ILLH >Q ELT QEBPB EBIMBO CRK@QFLKP TLOH.
,BQfP P>V TB T>KQ QL QBII "LU2$ TEBOB QEB JLRPB FP FK FQP TLOIA. 7B HKLT QEB JLRPB FP
IL@>QBA >Q (GIOM?5,GIOM?6) FK 0OL@BPPFKD. 4L @LKSBOQ FQ, TB P>V TB T>KQ QL @LKSBOQ >
g@LLOAFK>QBh COLJ gMFUBIPh QL gTLOIAhd=IIL>.CR?FM1I4ILF>(). /O:
7E>Q FC TB E>A > "LU2$ TLOIA @LLOAFK>QB >KA T>KQBA QL QO>KPI>QB FQ QL LRO MFUBI PM>@B?
F64B?2 5.2
3?=2 AL;PCNS = H?Q 3?=2(0, -10);
Convert mouseX,mouseY to coordinate in
Box2D world.
3?=2 GIOM?4ILF> =
<IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6);
To demonstrate, lets just make up a world
position.
3?=2 QILF>.IM = H?Q 3?=2(-10,25);
Convert to pixel space. This is necessary
because ultimately we are going to want to
draw the elements in our window.
3?=2 JCR?F.IM = <IR2>.=IIL>4ILF>1I.CR?FM(QILF>.IM);
?FFCJM?(JCR?F.IM.R, JCR?F.IM.S,16,16);
4EB .>QROB LC #LAB (S005)
197
0"LU2$ E>P > PBQ LC CRK@QFLKP QL Q>HB @>OB LC QO>KPI>QFKD ?>@H >KA CLOQE ?BQTBBK QEB "LU2$
TLOIA >KA MFUBIP. )QfP MOL?>?IV B>PFBO QL IB>OK >?LRQ >II LC QEBPB CRK@QFLKP AROFKD QEB @LROPB
LC >@QR>IIV FJMIBJBKQFKD LRO BU>JMIBP, ?RQ IBQfP NRF@HIV ILLH LSBO QEB IFPQ LC QEB MLPPF?FIFQFBP.
Task Task Function Function
#LKSBOQ IL@>QFLK COLJ 7LOIA QL 0FUBIP 3?=2 =IIL>4ILF>1I.CR?FM(3?=2 QILF>)
#LKSBOQ IL@>QFLK COLJ 7LOIA QL 0FUBIP
3?=2 =IIL>4ILF>1I.CR?FM(@FI;N QILF>5,
@FI;N QILF>6)
#LKSBOQ IL@>QFLK COLJ 0FUBIP QL 7LOIA 3?=2 =IIL>.CR?FM1I4ILF>(3?=2 M=L??H)
#LKSBOQ IL@>QFLK COLJ 0FUBIP QL 7LOIA
3?=2 =IIL>.CR?FM1I4ILF>(@FI;N JCR?F5,
@FI;N JCR?F6)
3@>IB > AFJBKPFLK (PR@E >P EBFDEQ, TFAQE, LO O>AFRP)
COLJ 0FUBIP QL 7LOIA
@FI;N M=;F;L.CR?FM1I4ILF>(@FI;N P;F)
3@>IB > AFJBKPFLK COLJ 7LOIA QL 0FUBIP @FI;N M=;F;L4ILF>1I.CR?FM(@FI;N P;F)
4EBOB >OB >IPL >AAFQFLK>I CRK@QFLKP QE>Q >IILT VLR QL M>PP LO OB@BFSB > .3?=NIL TEBK
QO>KPI>QFKD ?>@H >KA CLOQE, ?RQ PFK@B TB >OB LKIV TLOHFKD TFQE "LU2$ FK QEB BU>JMIBP FK QEFP
@E>MQBO, FQfP B>PFBPQ QL PQF@H TFQE QEB 3?=2 @I>PP CLO >II SB@QLOP.
/K@B QEB TLOIA FP FKFQF>IFWBA, TB >OB OB>AV QL >@QR>IIV MRQ PQRCC FK QEB TLOIAd"LU2$ ?LAFBP.
5.5 Building a Box2D body 5.5 Building a Box2D body
! "LU2$ ?LAV FP QEB MOFJ>OV BIBJBKQ FK QEB "LU2$ TLOIA. )QfP QEB BNRFS>IBKQ QL QEB +IP?L
@I>PP TB ?RFIQ LK LRO LTK FK MOBSFLRP @E>MQBOPdQEB QEFKD QE>Q JLSBP >OLRKA QEB PM>@B >KA
BUMBOFBK@BP CLO@BP. )Q @>K >IPL ?B PQ>QF@ (JB>KFKD CFUBA >KA KLQ JLSFKD). )QfP FJMLOQ>KQ QL
KLQB, ELTBSBO, QE>Q > ?LAV E>P KL DBLJBQOV; FQ FPKfQ >KVQEFKD MEVPF@>I. 2>QEBO, ?LAFBP E>SB
"LU2$ PE>MBP >QQ>@EBA QL QEBJ. (4EFP T>V, > ?LAV @>K ?B > PFKDIB OB@Q>KDIB LO > OB@Q>KDIB
>QQ>@EBA QL > @FO@IB, BQ@.) 7BfII ILLH >Q PE>MBP FK > JLJBKQ; CFOPQ, IBQfP ?RFIA > ?LAV.
Step 1: Define a body. Step 1: Define a body.
4EB CFOPQ QEFKD TB E>SB QL AL FP @OB>QB > g?LAV ABCFKFQFLK.h 4EFP TFII IBQ RP ABCFKB QEB
MOLMBOQFBP LC QEB ?LAV TB FKQBKA QL J>HB. 4EFP J>V PBBJ > ?FQ >THT>OA >Q CFOPQ, ?RQ FQfP ELT
"LU2$ FP PQOR@QROBA. !KVQFJB VLR T>KQ QL J>HB > gQEFKD,h VLR E>SB QL J>HB > gQEFKD
ABCFKFQFLKh CFOPQ. 4EFP TFII ELIA QORB CLO ?LAFBP, PE>MBP, >KA GLFKQP.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
198
Make a body definition before making a
Body.
I>S"?@ <> = H?Q I>S"?@();
Step 2: Configure the body definition. Step 2: Configure the body definition.
4EB ?LAV ABCFKFQFLK FP TEBOB TB @>K PBQ PMB@FCF@ MOLMBOQFBP LO >QQOF?RQBP LC QEB ?LAV TB FKQBKA
QL J>HB. /KB >QQOF?RQB LC > ?LAV, CLO BU>JMIB, FP FQP PQ>OQFKD IL@>QFLK. ,BQfP P>V TB T>KQ QL
MLPFQFLK QEB ?LAV FK QEB @BKQBO LC QEB 0OL@BPPFKD TFKALT.
$>KDBO, A>KDBO! )fJ KLQ DLFKD QL >AAOBPP QEFP TFQE BSBOV PFKDIB BU>JMIB, ?RQ FQfP FJMLOQ>KQ QL
>Q IB>PQ MLFKQ LRQ QEB MBOFILRP M>QE TB >OB Q>HFKD TFQE QEB >?LSB IFKB LC @LAB. 2BJBJ?BO, FC TB
>OB DLFKD QL QBII "LU2$ TEBOB TB T>KQ QEB ?LAV QL PQ>OQ, TB JRPQ DFSB "LU2$ > TLOIA
@LLOAFK>QB! 9BP, TB T>KQ QL QEFKH LC FQP IL@>QFLK FK QBOJP LC MFUBIP, ?RQ "LU2$ ALBPKfQ @>OB.
!KA PL ?BCLOB TB M>PP QE>Q MLPFQFLK QL QEB ?LAV ABCFKFQFLK, TB JRPQ J>HB PROB QL RPB LKB LC
LRO EBIMBO @LKSBOPFLK CRK@QFLKP.
4EB ?LAV ABCFKFQFLK JRPQ >IPL PMB@FCV QEB gQVMBh LC ?LAV TB T>KQ QL J>HB. 4EBOB >OB QEOBB
MLPPF?FIFQFBP:
i DD9,84.. DD9,84.. 4EFP FP TE>Q TB TFII RPB JLPQ LCQBKd> gCRIIV PFJRI>QBAh ?LAV. ! AVK>JF@
?LAV JLSBP >OLRKA QEB TLOIA, @LIIFABP TFQE LQEBO ?LAFBP, >KA OBPMLKAP QL QEB CLO@BP
FK FQP BKSFOLKJBKQ.
i '?,?4.. '?,?4.. ! PQ>QF@ ?LAV FP LKB QE>Q @>KKLQ JLSB (>P FC FQ E>A >K FKCFKFQB J>PP). 7BfII RPB
PQ>QF@ ?LAFBP CLO CFUBA MI>QCLOJP >KA ?LRKA>OFBP.
i 4908,?4.. 4908,?4.. ! HFKBJ>QF@ ?LAV @>K ?B JLSBA J>KR>IIV ?V PBQQFKD FQP SBIL@FQV AFOB@QIV.
)C VLR E>SB > RPBO-@LKQOLIIBA L?GB@Q FK VLRO TLOIA, VLR @>K RPB > HFKBJ>QF@ ?LAV.
.LQB QE>Q HFKBJ>QF@ ?LAFBP @LIIFAB LKIV TFQE AVK>JF@ ?LAFBP >KA KLQ TFQE LQEBO PQ>QF@
LO HFKBJ>QF@ LKBP.
4EBOB >OB PBSBO>I LQEBO MOLMBOQFBP VLR @>K PBQ FK QEB ?LAV ABCFKFQFLK. &LO BU>JMIB, FC VLR T>KQ
VLRO ?LAV QL E>SB > CFUBA OLQ>QFLK (F.B. KBSBO OLQ>QB), VLR @>K P>V:
A Vec2 in the center of the Processing
window
3?=2 =?HN?L = H?Q 3?=2(QC>NB/2,B?CABN/2);
A Vec2 in the center of the Processing
window converted to Box2D World
coordinates!
3?=2 =?HN?L =
<IR2>.=IIL>.CR?FM1I4ILF>(QC>NB/2,B?CABN/2));
Setting the position attribute of the Box2D
body definition
<>.JIMCNCIH.M?N(=?HN?L);
<>.@CR?>/IN;NCIH = NLO?;
4EB .>QROB LC #LAB (S005)
199
9LR @>K >IPL PBQ > S>IRB CLO IFKB>O LO >KDRI>O A>JMFKD, PL QE>Q QEB L?GB@Q @LKQFKRLRPIV PILTP
>P FC QEBOB FP COF@QFLK.
)K >AAFQFLK, C>PQ-JLSFKD L?GB@QP FK "LU2$ PELRIA ?B PBQ >P ?RIIBQP. 4EFP QBIIP QEB "LU2$
BKDFKB QE>Q QEB L?GB@Q J>V JLSB SBOV NRF@HIV >KA QL @EB@H FQP @LIIFPFLKP JLOB @>OBCRIIV PL
QE>Q FQ ALBPKfQ >@@FABKQ>IIV GRJM LSBO >KLQEBO ?LAV.
<>.FCH?;L";GJCHA = 0.8;
<>.;HAOF;L";GJCHA = 0.9;
<>.<OFF?N = NLO?;
Step 3: Create the body. Step 3: Create the body.
/K@B TBfOB ALKB TFQE QEB ABCFKFQFLK (I>S"?@), TB @>K @OB>QB QEB I>S L?GB@Q FQPBIC. 0"LU2$
MOLSFABP > EBIMBO CRK@QFLK CLO QEFPd=L?;N?I>S().
The Body object is created by passing in
the Body Definition. (This allows for making
multiple bodies from one definition.)
I>S <I>S = <IR2>.=L?;N?I>S(<>);
Step 4: Set any other conditions for the bodys starting state. Step 4: Set any other conditions for the bodys starting state.
&FK>IIV, QELRDE KLQ OBNRFOBA, FC VLR T>KQ QL PBQ >KV LQEBO FKFQF>I @LKAFQFLKP CLO QEB ?LAV, PR@E
>P IFKB>O LO >KDRI>O SBIL@FQV, VLR @>K AL PL TFQE QEB KBTIV @OB>QBA I>S L?GB@Q.
Setting an arbitrary initial velocity <I>S.M?N*CH?;L3?FI=CNS(H?Q 3?=2(0,3));
Setting an arbitrary initial angular velocity <I>S.M?NAHAOF;L3?FI=CNS(1.2);
5.6 Threes Company: Bodies and Shapes and 5.6 Threes Company: Bodies and Shapes and
Fixtures Fixtures
! ?LAV LK FQP LTK ALBPKfQ MEVPF@>IIV BUFPQ FK QEB TLOIA. )QfP IFHB > PLRI TFQE KL ERJ>K CLOJ QL
FKE>?FQ. &LO > ?LAV QL E>SB J>PP, TB JRPQ CFOPQ ABCFKB > PE>MB >KA >QQ>@E QE>Q PE>MB QL QEB
?LAV TFQE PLJBQEFKD HKLTK >P > CFUQROB.
4EB GL? LC QEB "LU2$ 0B;J? @I>PP FP QL HBBM QO>@H LC >II QEB KB@BPP>OV @LIIFPFLK DBLJBQOV
>QQ>@EBA QL > ?LAV. ! PE>MB >IPL E>P PBSBO>I FJMLOQ>KQ MOLMBOQFBP QE>Q >CCB@Q QEB ?LAVfP
JLQFLK. 4EBOB FP ABKPFQV, TEF@E RIQFJ>QBIV ABQBOJFKBP QE>Q ?LAVfP J>PP. 3E>MBP >IPL E>SB
:F=7H=CB >KA F9GH=HIH=CB (g?LRK@FKBPPh) TEF@E TFII ?B ABCFKBA QEOLRDE > CFUQROB. /KB LC QEB
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
200
KF@B QEFKDP >?LRQ "LU2$fP JBQELALILDV, TEF@E PBM>O>QBP QEB @LK@BMQP LC ?LAFBP >KA PE>MBP
FKQL QTL PBM>O>QB L?GB@QP, FP QE>Q VLR @>K >QQ>@E JRIQFMIB PE>MBP QL > PFKDIB ?LAV FK LOABO QL
@OB>QB JLOB @LJMIBU CLOJP. 7BfII PBB QEFP FK > CRQROB BU>JMIB.
4L @OB>QB > PE>MB, TB KBBA QL CFOPQ AB@FAB TE>Q HFKA LC PE>MB TB T>KQ QL J>HB. &LO JLPQ
KLK-@FO@RI>O PE>MBP, > .IFSAIH0B;J? L?GB@Q TFII TLOH GRPQ CFKB. &LO BU>JMIB, IBQfP ILLH >Q ELT
TB ABCFKB > OB@Q>KDIB.
Step 1: Define a shape. Step 1: Define a shape.
.BUQ RM, TB E>SB QL ABCFKB QEB TFAQE >KA EBFDEQ LC QEB OB@Q>KDIB. ,BQfP P>V TB T>KQ LRO
OB@Q>KDIB QL ?B 150X100 MFUBIP. 2BJBJ?BO, MFUBI RKFQP >OB KL DLLA CLO "LU2$ PE>MBP! 3L TB
E>SB QL RPB LRO EBIMBO CRK@QFLKP QL @LKSBOQ QEBJ CFOPQ.
Define the shape: a polygon. .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
Scale dimensions from pixels to Box2D
world.
@FI;N <IR2"Q = <IR2>.M=;F;L.CR?FM1I4ILF>(150);
@FI;N <IR2"B = <IR2>.M=;F;L.CR?FM1I4ILF>(100);
Use setAsBox() function to define shape as
a rectangle.
JM.M?NAMIR(<IR2"Q, <IR2"B);
Step 2: Create a fixture. Step 2: Create a fixture.
4EB PE>MB >KA ?LAV >OB J>AB >P QTL PBM>O>QB BKQFQFBP. )K LOABO QL >QQ>@E > PE>MB QL > ?LAV,
TB JRPQ J>HB > CFUQROB. ! CFUQROB FP @OB>QBA, GRPQ >P TFQE QEB ?LAV, SF> > CFUQROB ABCFKFQFLK (F.B.
$CRNOL?"?@ @I>PP) >KA >PPFDKBA > PE>MB.
/K@B TB E>SB QEB CFUQROB ABCFKFQFLK, TB @>K PBQ M>O>JBQBOP QE>Q >CCB@Q QEB MEVPF@P CLO QEB
PE>MB ?BFKD >QQ>@EBA.
$CRNOL?"?@ @> = H?Q $CRNOL?"?@();
The fixture is assigned the PolygonShape
we just made.
@>.MB;J? = JM;
The coefficient of friction for the shape,
typically between 0 and 1
@>.@LC=NCIH = 0.3;
The Shapes restitution (i.e. elasticity),
typically between 0 and 1
@>.L?MNCNONCIH = 0.5;
The Shapes density, measured in kilograms
per meter squared
@>.>?HMCNS = 1.0;
4EB .>QROB LC #LAB (S005)
201
Step 3: Attach the shape to the body with the fixture. Step 3: Attach the shape to the body with the fixture.
/K@B QEB CFUQROB FP ABCFKBA, >II TB E>SB IBCQ QL AL FP >QQ>@E QEB PE>MB QL QEB ?LAV TFQE QEB
CFUQROB ?V @>IIFKD QEB =L?;N?$CRNOL?() CRK@QFLK.
) PELRIA KLQB QE>Q 3QBM 2 @>K ?B PHFMMBA FC VLR AL KLQ KBBA QL PBQ QEB MEVPF@P MOLMBOQFBP.
("LU2$ TFII RPB ABC>RIQ S>IRBP.) 9LR @>K @OB>QB > CFUQROB >KA >QQ>@E QEB PE>MB >II FK LKB PQBM
?V P>VFKD:
7EFIB JLPQ LC LRO BU>JMIBP TFII Q>HB @>OB LC >QQ>@EFKD PE>MBP LKIV LK@B TEBK QEB ?LAV FP
CFOPQ ?RFIQ, QEFP FP KLQ > IFJFQ>QFLK LC "LU2$. "LU2$ >IILTP CLO PE>MBP QL ?B @OB>QBA >KA
ABPQOLVBA LK QEB CIV.
"BCLOB TB MRQ >KV LC QEFP @LAB TBfSB ?BBK TOFQFKD FKQL > 0OL@BPPFKD PHBQ@E, IBQfP OBSFBT >II
QEB PQBMP TB QLLH QL @LKPQOR@Q > "LAV.
1. $BCFKB > ?LAV RPFKD > I>S"?@ L?GB@Q (PBQ >KV MOLMBOQFBP, PR@E >P IL@>QFLK).
2. #OB>QB QEB I>S L?GB@Q COLJ QEB ?LAV ABCFKFQFLK.
3. $BCFKB > 0B;J? L?GB@Q RPFKD .IFSAIH0B;J?, !CL=F?0B;J?, LO >KV LQEBO PE>MB
@I>PP.
4. $BCFKB > CFUQROB RPFKD $CRNOL?"?@ >KA >PPFDK QEB CFUQROB > PE>MB (PBQ >KV
MOLMBOQFBP, PR@E >P COF@QFLK, ABKPFQV, >KA OBPQFQRQFLK).
5. !QQ>@E QEB PE>MB QL QEB ?LAV.
Creates the Fixture and attaches the
Shape to the Body object
<I>S.=L?;N?$CRNOL?(@>);
Creates the Fixture and attaches the
Shape with a density of 1
<I>S.=L?;N?$CRNOL?(JM,1);
Step 1. Define the body. I>S"?@ <> = H?Q I>S"?@();
<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(QC>NB/2,B?CABN/2));
Step 2. Create the body. I>S <I>S = <IR2>.=L?;N?I>S(<>);
Step 3. Define the shape. .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
@FI;N Q = <IR2>.M=;F;L.CR?FM1I4ILF>(150);
@FI;N B = <IR2>.M=;F;L.CR?FM1I4ILF>(100);
JM.M?NAMIR(Q, B);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
202
Step 4. Define the fixture. $CRNOL?"?@ @> = H?Q $CRNOL?"?@();
@>.MB;J? = JM;
@>.>?HMCNS = 1;
@>.@LC=NCIH = 0.3;
@>.L?MNCNONCIH = 0.5;
Step 5. Attach the shape to the body with
the Fixture.
<I>S.=L?;N?$CRNOL?(@>);
+KLTFKD TE>Q VLR HKLT >?LRQ "LU2$ PL C>O, CFII FK QEB ?I>KH FK QEB @LAB ?BILT QE>Q
ABJLKPQO>QBP ELT QL J>HB > @FO@RI>O PE>MB FK "LU2$.
!CL=F?0B;J? =M = H?Q !CL=F?0B;J?();
@FI;N L;>COM = 10;
=M.G:L;>COM = ::::::::::::::::::::;
$CRNOL?"?@ @> = H?Q $CRNOL?"?@();
@>.MB;J? = =M;
@>.>?HMCNS = 1;
@>.@LC=NCIH = 0.1;
@>.L?MNCNONCIH = 0.3;
<I>S.=L?;N?$CRNOL?(@>);
Exercise 5.1 Exercise 5.1
5.7 Box2D and Processing: Reunited and It Feels So 5.7 Box2D and Processing: Reunited and It Feels So
Good Good
/K@B > ?LAV FP J>AB, FQ IFSBP FK QEB "LU2$ MEVPF@P TLOIA. "LU2$ TFII >IT>VP HKLT FQfP QEBOB,
@EB@H FQ CLO @LIIFPFLKP, JLSB FQ >MMOLMOF>QBIV >@@LOAFKD QL QEB CLO@BP, BQ@. )QfII AL >II QE>Q CLO VLR
TFQELRQ VLR E>SFKD QL IFCQ > CFKDBO! 7E>Q FQ TLKfQ AL, ELTBSBO, FP AFPMI>V QEB ?LAV CLO VLR. 4EFP
FP > DLLA QEFKD. 4EFP FP VLRO QFJB QL PEFKB. 7EBK TLOHFKD TFQE "LU2$, TE>Q TBfOB BPPBKQF>IIV
P>VFKD FP, g) T>KQ QL ?B QEB ABPFDKBO LC JV TLOIA, >KA ) T>KQ VLR, "LU2$, QL @LJMRQB >II QEB
MEVPF@P.h
.LT, "LU2$ TFII HBBM > IFPQ LC >II QEB ?LAFBP QE>Q BUFPQ FK QEB TLOIA. 4EFP @>K ?B >@@BPPBA ?V
@>IIFKD QEB 7LOIA L?GB@QfP A?NI>S*CMN() CRK@QFLK. .BSBOQEBIBPP, TE>Q )fJ DLFKD QL
ABJLKPQO>QB EBOB FP > QB@EKFNRB CLO HBBMFKD VLRO LTK ?LAV IFPQP. 9BP, QEFP J>V ?B > ?FQ
OBARKA>KQ >KA TB MBOE>MP P>@OFCF@B > ?FQ LC BCCF@FBK@V. "RQ TB JLOB QE>K J>HB RM CLO QE>Q TFQE
B>PB LC RPB. 4EFP JBQELALILDV TFII >IILT RP QL MOLDO>J IFHB TBfOB RPBA QL FK 0OL@BPPFKD, >KA
TB @>K B>PFIV HBBM QO>@H LC TEF@E ?LAFBP >OB TEF@E >KA OBKABO QEBJ >MMOLMOF>QBIV. ,BQfP
@LKPFABO QEB PQOR@QROB LC QEB CLIILTFKD 0OL@BPPFKD PHBQ@E:
4EB .>QROB LC #LAB (S005)
203
4EFP ILLHP IFHB >KV LIf 0OL@BPPFKD PHBQ@E. 7B E>SB > J>FK Q>? @>IIBA g"LUBPh >KA >
g"LRKA>OVh >KA > g"LUh Q>?. ,BQfP QEFKH >?LRQ QEB "LU Q>? CLO > JLJBKQ. 4EB "LU Q>? FP
TEBOB TB TFII TOFQB > PFJMIB @I>PP QL ABP@OF?B > IR L?GB@Q, > OB@Q>KDRI>O ?LAV FK LRO TLOIA.
,BQfP TOFQB > J>FK Q>? QE>Q @OB>QBP > KBT IR TEBKBSBO QEB JLRPB FP MOBPPBA >KA PQLOBP >II
QEB IR L?GB@QP FK >K ALL;S*CMN. (4EFP FP SBOV PFJFI>O QL LRO >MMOL>@E FK QEB M>OQF@IB PVPQBJ
BU>JMIBP COLJ #E>MQBO 4.)
F64B?2 5.3
=F;MM IR U
Our Box object has an x,y location and a
width and a height.
@FI;N R,S;
@FI;N Q,B;
IR(@FI;N R:, @FI;N S:) U
The location is initalized in the constructor
via arguments
R = R:;
S = S:;
Q = 16;
B = 16;
W
PIC> >CMJF;S() U
We draw the Box object using Processings
rect() function.
@CFF(175);
MNLIE?(0);
L?=N+I>?(!#,1#/);
L?=N(R,S,Q,B);
W
W
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
204
Example 5.1: A comfortable and cozy Processing sketch that needs a little Box2D
.LT, EBOBfP LRO >PPFDKJBKQ. 4>HB QEB >?LSB BU>JMIB SBO?>QFJ, ?RQ FKPQB>A LC AO>TFKD CFUBA
?LUBP LK QEB P@OBBK, AO>T ?LUBP QE>Q BUMBOFBK@B MEVPF@P (SF> "LU2$) >P PLLK >P QEBV >MMB>O.
7BfII KBBA QTL J>GLO PQBMP QL >@@LJMIFPE LRO DL>I.
A list to store all Box objects ALL;S*CMN<IR> <IR?M;
PIC> M?NOJ() U
MCT?(400,300);
<IR?M = H?Q ALL;S*CMN<IR>();
W
PIC> >L;Q() U
<;=EALIOH>(255);
When the mouse is pressed, add a new
Box object.
C@ (GIOM?.L?MM?>) U
IR J = H?Q IR(GIOM?5,GIOM?6);
<IR?M.;>>(J);
W
Display all the Box objects. @IL (IR <: <IR?M) U
<.>CMJF;S();
W
W
Step 1: Add Box2D to our main program (i.e. setup() and draw()). Step 1: Add Box2D to our main program (i.e. setup() and draw()).
4EFP M>OQ FP KLQ QLL QLRDE. 7B P>T QEFP >IOB>AV FK LRO AFP@RPPFLK LC ?RFIAFKD > "LU2$ TLOIA.
4EFP FP Q>HBK @>OB LC CLO RP ?V QEB 0"LU2$ EBIMBO @I>PP. 7B @>K @OB>QB > 0"LU2$ L?GB@Q >KA
FKFQF>IFWB FQ FK M?NOJ().
4EBK FK >L;Q(), TB KBBA QL J>HB PROB TB @>II LKB SBOV FJMLOQ>KQ CRK@QFLK: MN?J(). 7FQELRQ
QEFP CRK@QFLK, KLQEFKD TLRIA BSBO E>MMBK! MN?J() >AS>K@BP QEB "LU2$ TLOIA > PQBM CROQEBO FK
QFJB. )KQBOK>IIV, "LU2$ PTBBMP QEOLRDE >KA ILLHP >Q >II LC QEB "LAFBP >KA CFDROBP LRQ TE>Q QL
AL TFQE QEBJ. *RPQ @>IIFKD MN?J() LK FQP LTK JLSBP QEB "LU2$ TLOIA CLOT>OA TFQE ABC>RIQ
PBQQFKDP; ELTBSBO, FQ FP @RPQLJFW>?IB (>KA QEFP FP AL@RJBKQBA FK QEB 0"LU2$ PLRO@B).
.IR2" <IR2>;
PIC> M?NOJ() U
Initialize and create the Box2D world. <IR2> = H?Q .IR2"(NBCM);
<IR2>.=L?;N?4ILF>();
W
4EB .>QROB LC #LAB (S005)
205
PIC> >L;Q() U
We must always step through time! <IR2>.MN?J();
W
Step 2: Link every Processing Box object with a Box2D Body object. Step 2: Link every Processing Box object with a Box2D Body object.
!P LC QEFP JLJBKQ, QEB IR @I>PP FK@IRABP S>OF>?IBP CLO IL@>QFLK >KA TFAQE >KA EBFDEQ. 7E>Q
TB KLT T>KQ QL P>V FP:
g) EBOB?V OBIFKNRFPE QEB @LJJ>KA LC QEFP L?GB@QfP MLPFQFLK QL "LU2$. ) KL ILKDBO KBBA QL
HBBM QO>@H LC >KVQEFKD OBI>QBA QL IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK. )KPQB>A, ) LKIV KBBA QL
HBBM QO>@H LC > "LU2$ ?LAV >KA E>SB C>FQE QE>Q "LU2$ TFII AL QEB OBPQ.h
7B ALKfQ KBBA (L,M) >KVJLOB PFK@B, >P TBfII PBB, QEB ?LAV FQPBIC TFII HBBM QO>@H LC FQP
IL@>QFLK. 4EB ?LAV QB@EKF@>IIV @LRIA >IPL HBBM QO>@H LC QEB TFAQE >KA EBFDEQ CLO RP, ?RQ PFK@B
"LU2$ FPKfQ DLFKD QL AL >KVQEFKD QL >IQBO QELPB S>IRBP LSBO QEB IFCB LC QEB IR L?GB@Q, TB
JFDEQ >P TBII GRPQ ELIA LKQL QEBJ LROPBISBP RKQFI FQfP QFJB QL AO>T QEB IR.
4EBK, FK LRO @LKPQOR@QLO, FK >AAFQFLK QL FKFQF>IFWFKD QEB TFAQE >KA EBFDEQ, TB @>K DL >EB>A
>KA FK@IRAB >II LC QEB ?LAV >KA PE>MB @LAB TB IB>OKBA FK QEB MOBSFLRP QTL PB@QFLKP!
=F;MM IR U
Instead of any of the usual variables, we
will store a reference to a Box2D body.
I>S <I>S;
@FI;N Q;
@FI;N B;
IR() U
Q = 16;
B = 16;
Build body. I>S"?@ <> = H?Q I>S"?@();
<>.NSJ? = I>S1SJ?."6,A+'!;
<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6));
<I>S = <IR2>.=L?;N?I>S(<>);
Build shape. .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
Box2D considers the width and height of a
rectangle to be the distance from the
center to the edge (so half of what we
normally think of as width or height).
@FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2);
@FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
206
/+, TBfOB >IJLPQ QEBOB. "BCLOB TB FKQOLAR@BA "LU2$, FQ T>P B>PV QL AO>T QEB IR. 4EB
L?GB@QfP IL@>QFLK T>P PQLOBA FK S>OF>?IBP R >KA S.
"RQ KLT "LU2$ J>K>DBP QEB L?GB@QfP JLQFLK, PL TB @>K KL ILKDBO RPB LRO LTK S>OF>?IBP QL
AFPMI>V QEB PE>MB. .LQ QL CB>O! /RO IR L?GB@Q E>P > OBCBOBK@B QL QEB "LU2$ ?LAV >PPL@F>QBA
TFQE FQ. 3L >II TB KBBA QL AL FP MLIFQBIV >PH QEB ?LAV, g0>OALK JB, TEBOB >OB VLR IL@>QBA?h
3FK@B QEFP FP > Q>PH TBfII KBBA QL AL NRFQB LCQBK, 0"LU2$ FK@IRABP > EBIMBO CRK@QFLK:
A?NI>S.CR?F!IIL>().
*RPQ HKLTFKD QEB IL@>QFLK LC > ?LAV FPKfQ BKLRDE; TB >IPL KBBA QL HKLT FQP >KDIB LC OLQ>QFLK.
/K@B TB E>SB QEB IL@>QFLK >KA >KDIB, FQfP B>PV QL AFPMI>V QEB L?GB@Q RPFKD NL;HMF;N?() >KA
LIN;N?(). .LQB, ELTBSBO, QE>Q QEB "LU2$ @LLOAFK>QB PVPQBJ @LKPFABOP OLQ>QFLK FK QEB
LMMLPFQB AFOB@QFLK COLJ 0OL@BPPFKD, PL TB KBBA QL JRIQFMIV QEB >KDIB ?V -1.
JM.M?NAMIR(<IR2>4, <IR2>&);
$CRNOL?"?@ @> = H?Q $CRNOL?"?@();
@>.MB;J? = JM;
@>.>?HMCNS = 1;
Set physics parameters. @>.@LC=NCIH = 0.3;
@>.L?MNCNONCIH = 0.5;
Attach the Shape to the Body with the
Fixture.
<I>S.=L?;N?$CRNOL?(@>);
W
Drawing the object using rect() PIC> >CMJF;S() U
@CFF(175);
MNLIE?(0);
L?=N+I>?(!#,1#/);
L?=N(R,S,Q,B);
W
3?=2 JIM = <IR2>.A?NI>S.CR?F!IIL>(<I>S);
@FI;N ; = <I>S.A?NAHAF?();
4EB .>QROB LC #LAB (S005)
207
)K @>PB TB T>KQ QL E>SB L?GB@QP QE>Q @>K ?B OBJLSBA COLJ QEB "LU2$ TLOIA, FQfP >IPL RPBCRI
QL FK@IRAB > CRK@QFLK QL ABPQOLV > ?LAV, PR@E >P:
F64B?2 5.4
PIC> >CMJF;S() U
We need the Bodys location and angle. 3?=2 JIM = <IR2>.A?NI>S.CR?F!IIL>(<I>S);
@FI;N ; = <I>S.A?NAHAF?();
JOMB+;NLCR();
Using the Vec2 position and float angle to
translate and rotate the rectangle
NL;HMF;N?(JIM.R,JIM.S);
LIN;N?(-;);
@CFF(175);
MNLIE?(0);
L?=N+I>?(!#,1#/);
L?=N(0,0,Q,B);
JIJ+;NLCR();
W
This function removes a body from the
Box2D world.
PIC> ECFFI>S() U
<IR2>.>?MNLISI>S(<I>S);
W
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
208
)K QEFP @E>MQBOfP @LAB ALTKIL>AP, CFKA QEB PHBQ@E K>JBA g?LU2A=BUBO@FPB.h 5PFKD QEB
JBQELALILDV LRQIFKBA FK QEFP @E>MQBO, >AA QEB KB@BPP>OV @LAB QL QEB J>FK >KA "LU Q>?P
QL FJMIBJBKQ "LU2$ MEVPF@P. 4EB OBPRIQ PELRIA >MMB>O >P FK QEB P@OBBKPELQ >?LSB. "B
JLOB @OB>QFSB FK ELT VLR OBKABO QEB ?LUBP.
Exercise 5.2 Exercise 5.2
5.8 Fixed Box2D Objects 5.8 Fixed Box2D Objects
)K QEB BU>JMIB TB GRPQ @OB>QBA, QEB IR L?GB@QP >MMB>O >Q QEB JLRPB IL@>QFLK >KA C>II
ALTKT>OAP ARB QL "LU2$fP ABC>RIQ DO>SFQV CLO@B. 7E>Q FC TB T>KQBA QL FKPQ>II PLJB FJJLS>?IB
?LRKA>OFBP FK QEB "LU2$ TLOIA QE>Q TLRIA ?IL@H QEB M>QE LC QEB IR L?GB@QP (>P FK QEB
FIIRPQO>QFLK ?BILT)?
"LU2$ J>HBP QEFP B>PV CLO RP ?V MOLSFAFKD > JB>KP QL IL@H > ?LAV (>KA >KV >PPL@F>QBA
PE>MBP) FK MI>@B. *RPQ PBQ QEB I>S"?@ L?GB@QfP QVMB QL 01A1'!.
7B @>K >AA QEFP CB>QROB QL LRO "LUBP BU>JMIB ?V TOFQFKD > IOH>;LS @I>PP >KA E>SFKD B>@E
?LRKA>OV @OB>QB > CFUBA "LU2$ ?LAV.
I>S"?@ <> = H?Q I>S"?@();
When BodyDef type = STATIC, the Body is
locked in place.
<>.NSJ? = I>S1SJ?.01A1'!;
4EB .>QROB LC #LAB (S005)
209
Example 5.2: Falling boxes hitting boundaries
=F;MM IOH>;LS U
A boundary is a simple rectangle with x, y,
width, and height.
@FI;N R,S;
@FI;N Q,B;
I>S <;
IOH>;LS(@FI;N R:,@FI;N S:, @FI;N Q:, @FI;N B:) U
R = R:;
S = S:;
Q = Q:;
B = B:;
Build the Box2D Body and Shape. I>S"?@ <> = H?Q I>S"?@();
<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(R,S));
Make it fixed by setting type to STATIC! <>.NSJ? = I>S1SJ?.01A1'!;
< = <IR2>.=L?;N?I>S(<>);
@FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2);
@FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2);
.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
We're just a box JM.M?NAMIR(<IR2>4, <IR2>&);
Using the createFixture() shortcut <.=L?;N?$CRNOL?(JM,1);
W
Since we know it can never move, we can
just draw it the old-fashioned way, using
our original variables. No need to query
Box2D.
PIC> >CMJF;S() U
@CFF(0);
MNLIE?(0);
L?=N+I>?(!#,1#/);
L?=N(R,S,Q,B);
W
W
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
210
5.9 A Curvy Boundary 5.9 A Curvy Boundary
)C VLR T>KQ > CFUBA ?LRKA>OV QE>Q FP > @ROSBA PROC>@B (>P LMMLPBA QL > MLIVDLK), QEFP @>K ?B
>@EFBSBA TFQE QEB PE>MB !B;CH0B;J?.
4EB !B;CH0B;J? @I>PP FP >KLQEBO PE>MB IFHB .IFSAIH0B;J? LO !CL=F?0B;J?, PL QL FK@IRAB LKB
FK LRO PVPQBJ, TB CLIILT QEB P>JB PQBMP.
Step 1: Define a body. Step 1: Define a body.
The body does not need a position; the
EdgeShape will take care of that for us. It
also does not need a type, as it is STATIC
by default.
I>S"?@ <> = H?Q I>S"?@();
I>S <I>S = <IR2>.QILF>.=L?;N?I>S(<>);
Step 2: Define the Shape. Step 2: Define the Shape.
!B;CH0B;J? =B;CH = H?Q !B;CH0B;J?();
Step 3: Configure the Shape. Step 3: Configure the Shape.
4EB !B;CH0B;J? L?GB@Q FP > PBOFBP LC @LKKB@QBA SBOQF@BP. 4L @OB>QB QEB @E>FK, TB JRPQ CFOPQ
PMB@FCV >K >OO>V LC SBOQF@BP (B>@E >P > 3?=2 L?GB@Q). &LO BU>JMIB, FC TB T>KQBA > PQO>FDEQ IFKB
COLJ QEB IBCQ-E>KA PFAB LC LRO TFKALT QL QEB OFDEQ-E>KA PFAB, TB TLRIA GRPQ KBBA >K >OO>V LC
QTL SBOQF@BP: (0,150) >KA (TFAQE,150). (.LQB QE>Q FC VLR T>KQ QL @OB>QB > ILLM TEBOB QEB CFOPQ
SBOQBU @LKKB@QP QL QEB I>PQ SBOQBU FK > ILLM, VLR @>K RPB QEB !B;CH*IIJ @I>PP FKPQB>A.)
4L @OB>QB QEB @E>FK TFQE QEB SBOQF@BP, QEB >OO>V FP QEBK M>PPBA FKQL > CRK@QFLK @>IIBA
=L?;N?!B;CH().
3?=289 P?LNC=?M = H?Q 3?=2829;
Adding a vertex on the right side of window P?LNC=?M809 = <IR2>.=IIL>.CR?FM1I4ILF>(0,150);
Adding a vertex on the left side of window P?LNC=?M819 = <IR2>.=IIL>.CR?FM1I4ILF>(QC>NB,150);
If you dont want to use the entire array, you
can specify a value less than length.
=B;CH.=L?;N?!B;CH(P?LNC=?M, P?LNC=?M.F?HANB);
4EB .>QROB LC #LAB (S005)
211
Step 4: Attach the Shape to the body with a Fixture. Step 4: Attach the Shape to the body with a Fixture.
! 0B;J? FP KLQ M>OQ LC "LU2$ RKIBPP FQ FP >QQ>@EBA QL > ?LAV. %SBK FC FQ FP > CFUBA ?LRKA>OV
>KA KBSBO JLSBP, FQ JRPQ PQFII ?B >QQ>@EBA. *RPQ >P TFQE LQEBO PE>MBP, > !B;CH0B;J? L?GB@Q
@>K ?B DFSBK MOLMBOQFBP IFHB OBPQFQRQFLK >KA COF@QFLK TFQE > $CRNOL?.
.LT, FC TB T>KQ QL FK@IRAB > !B;CH0B;J? L?GB@Q FK LRO PHBQ@E, TB @>K CLIILT QEB P>JB
PQO>QBDV >P TB AFA TFQE > CFUBA ?LRKA>OV. ,BQfP TOFQB > @I>PP @>IIBA 0OL@;=?:
$CRNOL?"?@ @> = H?Q $CRNOL?"?@();
A fixture assigned to the ChainShape @>.MB;J? = =B;CH;
@>.>?HMCNS = 1;
@>.@LC=NCIH = 0.3;
@>.L?MNCNONCIH = 0.5;
<I>S.=L?;N?$CRNOL?(@>);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
212
Example 5.3: ChainShape with three hard-coded vertices
.LQF@B ELT QEB >?LSB @I>PP FK@IRABP >K ALL;S*CMN QL PQLOB > PBOFBP LC 3?=2 L?GB@QP. %SBK
QELRDE TB CRIIV FKQBKA QL PQLOB QEB @LLOAFK>QBP LC QEB @E>FK FK QEB @E>FK PE>MB FQPBIC, TB >OB
@ELLPFKD QEB B>PB LC OBARKA>K@V >KA HBBMFKD LRO LTK IFPQ LC QELPB MLFKQP >P TBII. ,>QBO,
TEBK TB DL QL AO>T QEB 0OL@;=? L?GB@Q, TB ALKfQ E>SB QL >PH "LU2$ CLO QEB IL@>QFLKP LC QEB
@E>FK PE>MBfP SBOQF@BP.
=F;MM 0OL@;=? U
ALL;S*CMN<3?=2> MOL@;=?;
0OL@;=?() U
MOL@;=? = H?Q ALL;S*CMN<3?=2>();
3 vertices in pixel coordinates MOL@;=?.;>>(H?Q 3?=2(0, B?CABN/2+50));
MOL@;=?.;>>(H?Q 3?=2(QC>NB/2, B?CABN/2+50));
MOL@;=?.;>>(H?Q 3?=2(QC>NB, B?CABN/2));
!B;CH0B;J? =B;CH = H?Q !B;CH0B;J?();
Make an array of Vec2 for the ChainShape. 3?=289 P?LNC=?M = H?Q 3?=28MOL@;=?.MCT?()9;
@IL (CHN C = 0; C < P?LNC=?M.F?HANB; C++) U
P?LNC=?M8C9 = <IR2>.=IIL>.CR?FM1I4ILF>(MOL@;=?.A?N(C));
W
Convert each vertex to Box2D World
coordinates.
Create the ChainShape with array of Vec2. =B;CH.=L?;N?!B;CH(P?LNC=?M, P?LNC=?M.F?HANB);
Attach the Shape to the Body. I>S"?@ <> = H?Q I>S"?@();
I>S <I>S = <IR2>.QILF>.=L?;N?I>S(<>);
<I>S.=L?;N?$CRNOL?(=B;CH, 1);
W
PIC> >CMJF;S() U
MNLIE?4?CABN(1);
MNLIE?(0);
HI$CFF();
Draw the ChainShape as a series of
vertices.
<?ACH0B;J?();
@IL (3?=2 P: MOL@;=?) U
P?LN?R(P.R,P.S);
W
?H>0B;J?();
W
W
4EB .>QROB LC #LAB (S005)
213
7E>Q TB KBBA FK M?NOJ() >KA >L;Q() CLO QEB 0OL@;=? L?GB@Q FP NRFQB PFJMIB, DFSBK QE>Q
"LU2$ Q>HBP @>OB LC >II LC QEB MEVPF@P CLO RP.
.IR2" <IR2>;
0OL@;=? MOL@;=?;
PIC> M?NOJ() U
MCT?(500,300);
<IR2> = H?Q .IR2"(NBCM);
<IR2>.=L?;N?4ILF>();
Make a Surface object. MOL@;=? = H?Q 0OL@;=?();
W
PIC> >L;Q() U
<IR2>.MN?J();
<;=EALIOH>(255);
Draw the Surface. MOL@;=?.>CMJF;S();
W
2BSFBT ELT TB IB>OKBA QL AO>T > T>SB M>QQBOK FK #E>MQBO 3. #OB>QB > !B;CH0B;J?
L?GB@Q LRQ LC > PFKB T>SB. 4OV RPFKD 0BOIFK KLFPB (PBB M>DB 17) >P TBII.
@6;2 D.C2 '2?96; ;<6@2
Exercise 5.3 Exercise 5.3
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
214
5.10 Complex Forms 5.10 Complex Forms
.LT QE>Q TBfSB PBBK ELT B>PV FQ FP QL J>HB
PFJMIB DBLJBQOF@ CLOJP FK "LU2$, IBQfP
FJ>DFKB QE>Q VLR T>KQ QL E>SB > JLOB
@LJMIBU CLOJ, PR@E >P > IFQQIB >IFBK PQF@H
CFDROB.
4EBOB >OB QTL PQO>QBDFBP FK "LU2$ CLO
J>HFKD CLOJP QE>Q >OB JLOB >AS>K@BA QE>K >
?>PF@ @FO@IB LO PNR>OB. /KB FP QL RPB >
.IFSAIH0B;J? FK > AFCCBOBKQ T>V. )K LRO
MOBSFLRP BU>JMIBP, TB RPBA .IFSAIH0B;J?
QL DBKBO>QB > OB@Q>KDRI>O PE>MB TFQE QEB
M?NAMIR() CRK@QFLK.
4EFP T>P > DLLA T>V QL PQ>OQ ?B@>RPB LC QEB FKEBOBKQ PFJMIF@FQV LC TLOHFKD TFQE OB@Q>KDIBP.
(LTBSBO, > .IFSAIH0B;J? L?GB@Q @>K >IPL ?B DBKBO>QBA COLJ >K >OO>V LC SB@QLOP, TEF@E
>IILTP VLR QL ?RFIA > @LJMIBQBIV @RPQLJ PE>MB >P > PBOFBP LC @LKKB@QBA SBOQF@BP. 4EFP TLOHP
SBOV PFJFI>OIV QL QEB !B;CH0B;J? @I>PP.
Example 5.4: Polygon shapes
F64B?2 5.5
.IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
JM.M?NAMIR(<IR2>4, <IR2>&);
3?=289 P?LNC=?M = H?Q 3?=2849; // AH ;LL;S I@ 4 P?=NILM
P?LNC=?M809 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q 3?=2(-15, 25));
P?LNC=?M819 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q 3?=2(15, 0));
P?LNC=?M829 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q 3?=2(20, -15));
P?LNC=?M839 = <IR2>.P?=NIL.CR?FM1I4ILF>(H?Q 3?=2(-10, -10));
Making a polygon from that array .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
JM.M?N(P?LNC=?M, P?LNC=?M.F?HANB);
4EB .>QROB LC #LAB (S005)
215
7EBK ?RFIAFKD VLRO LTK MLIVDLK FK "LU2$, VLR JRPQ OBJBJ?BO QTL FJMLOQ>KQ ABQ>FIP.
1. Order of vertices! Order of vertices! )C VLR >OB QEFKHFKD FK QBOJP LC MFUBIP (>P >?LSB) QEB SBOQF@BP
PELRIA ?B ABCFKBA FK @LRKQBO@IL@HTFPB LOABO. (7EBK QEBV >OB QO>KPI>QBA QL "LU2$
7LOIA SB@QLOP, QEBV TFII >@QR>IIV ?B FK @IL@HTFPB LOABO PFK@B QEB SBOQF@>I >UFP FP
CIFMMBA.)
2. Convex shapes only! Convex shapes only! ! @LK@>SB PE>MB FP LKB TEBOB QEB PROC>@B @ROSBP FKT>OA.
#LKSBU FP QEB LMMLPFQB (PBB FIIRPQO>QFLK ?BILT). .LQB ELT FK > @LK@>SB PE>MB
BSBOV FKQBOK>I >KDIB JRPQ ?B 180 ABDOBBP LO IBPP. "LU2$ FP KLQ @>M>?IB LC
E>KAIFKD @LIIFPFLKP CLO @LK@>SB PE>MBP. )C VLR KBBA > @LK@>SB PE>MB, VLR TFII E>SB
QL ?RFIA LKB LRQ LC JRIQFMIB @LKSBU PE>MBP (JLOB >?LRQ QE>Q FK > JLJBKQ).
F64B?2 5.6
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
216
.LT, TEBK FQ @LJBP QFJB QL AFPMI>V QEB PE>MB FK 0OL@BPPFKD, TB @>K KL ILKDBO GRPQ RPB
L?=N() LO ?FFCJM?(). 3FK@B QEB PE>MB FP ?RFIQ LRQ LC @RPQLJ SBOQF@BP, TBfII T>KQ QL RPB
0OL@BPPFKDfP <?ACH0B;J?(), ?H>0B;J?(), >KA P?LN?R() CRK@QFLKP. !P TB P>T TFQE QEB
!B;CH0B;J?, TB @LRIA @ELLPB QL PQLOB QEB MFUBI IL@>QFLKP LC QEB SBOQF@BP FK LRO LTK
ALL;S*CMN CLO AO>TFKD. (LTBSBO, FQfP >IPL RPBCRI QL PBB ELT TB @>K >PH "LU2$ QL OBMLOQ ?>@H
QL RPB QEB SBOQBU IL@>QFLKP.
F64B?2 5.7: A 0<;0.C2 @5.=2 0.; /2 1?.D; D6A5 :B9A6=92 0<;C2E @5.=2@.
PIC> >CMJF;S() U
3?=2 JIM = <IR2>.A?NI>S.CR?F!IIL>(<I>S);
@FI;N ; = <I>S.A?NAHAF?();
First we get the Fixture attached to the
Body...
$CRNOL? @ = <I>S.A?N$CRNOL?*CMN();
...then the Shape attached to the Fixture. .IFSAIH0B;J? JM = (.IFSAIH0B;J?) @.A?N0B;J?();
L?=N+I>?(!#,1#/);
JOMB+;NLCR();
NL;HMF;N?(JIM.R,JIM.S);
LIN;N?(-;);
@CFF(175);
MNLIE?(0);
<?ACH0B;J?();
@IL (CHN C = 0; C < JM.A?N3?LN?R!IOHN(); C++) U
3?=2 P = <IR2>.P?=NIL4ILF>1I.CR?FM(JM.A?N3?LN?R(C));
P?LN?R(P.R,P.S);
W
?H>0B;J?(!*-0#);
JIJ+;NLCR();
W
We can loop through that array and convert
each vertex from Box2D space to pixels.
4EB .>QROB LC #LAB (S005)
217
! MLIVDLK PE>MB TFII DBQ RP MOBQQV C>O FK "LU2$. .BSBOQEBIBPP, QEB @LKSBU PE>MB
OBNRFOBJBKQ TFII PBSBOBIV IFJFQ QEB O>KDB LC MLPPF?FIFQFBP. 4EB DLLA KBTP FP QE>Q TB @>K
@LJMIBQBIV BIFJFK>QB QEFP OBPQOF@QFLK ?V @OB>QFKD > PFKDIB "LU2$ ?LAV LRQ LC JRIQFMIB PE>MBP!
,BQfP OBQROK QL LRO IFQQIB >IFBK @OB>QROB >KA PFJMIFCV QEB PE>MB QL ?B > QEFK OB@Q>KDIB TFQE >
@FO@IB LK QLM.
(LT @>K TB ?RFIA > PFKDIB ?LAV TFQE QTL PE>MBP? ,BQfP CFOPQ OBSFBT ELT TB ?RFIQ > PFKDIB
?LAV TFQE LKB PE>MB.
.H9D 1: 9:=B9 H<9 6C8M.
.H9D 2: CF95H9 H<9 6C8M.
'?0; 3: D01490 ?30 >3,;0. '?0; 3: D01490 ?30 >3,;0.
'?0; 4: A??,.3 ?30 >3,;0 ?: ?30 -:/D. '?0; 4: A??,.3 ?30 >3,;0 ?: ?30 -:/D.
.H9D 5: "=B5@=N9 H<9 6C8MSG A5GG.
!QQ>@EFKD JLOB QE>K LKB PE>MB QL > ?LAV FP >P PFJMIB >P OBMB>QFKD PQBMP 3 >KA 4 LSBO >KA
LSBO >D>FK.
'?0; 3,: D01490 >3,;0 1. '?0; 3,: D01490 >3,;0 1.
'?0; 4,: A??,.3 >3,;0 1 ?: ?30 -:/D. '?0; 4,: A??,.3 >3,;0 1 ?: ?30 -:/D.
'?0; 3-: D01490 >3,;0 2. '?0; 3-: D01490 >3,;0 2.
'?0; 4-: A??,.3 >3,;0 2 ?: ?30 -:/D. '?0; 4-: A??,.3 >3,;0 2 ?: ?30 -:/D.
BQ@. BQ@. BQ@.
,BQfP PBB TE>Q QEFP TLRIA ILLH IFHB TFQE >@QR>I "LU2$ @LAB.
5PFKD QEB .IFSAIH0B;J? @I>PP, @OB>QB VLRO LTK MLIVDLK ABPFDK (OBJBJ?BO, FQ JRPQ
?B @LK@>SB). 3LJB MLPPF?FIFQFBP ?BILT.
Exercise 5.4 Exercise 5.4
Making the body I>S"?@ <> = H?Q I>S"?@();
<>.NSJ? = I>S1SJ?."6,A+'!;
<>.JIMCNCIH.M?N(<IR2>.=IIL>.CR?FM1I4ILF>(=?HN?L));
<I>S = <IR2>.=L?;N?I>S(<>);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
218
4EB >?LSB ILLHP MOBQQV DLLA, ?RQ P>AIV, FC TB ORK FQ, TBfII DBQ QEB CLIILTFKD OBPRIQ:
7EBK VLR >QQ>@E > PE>MB QL > ?LAV, ?V ABC>RIQ, QEB @BKQBO LC QEB PE>MB TFII ?B IL@>QBA >Q QEB
@BKQBO LC QEB ?LAV. "RQ FK LRO @>PB, FC TB Q>HB QEB @BKQBO LC QEB OB@Q>KDIB QL ?B QEB @BKQBO LC
QEB ?LAV, TB T>KQ QEB @BKQBO LC QEB @FO@IB QL ?B LCCPBQ >ILKD QEB V->UFP COLJ QEB ?LAVfP @BKQBO.
Making shape 1 (the rectangle) .IFSAIH0B;J? JM = H?Q .IFSAIH0B;J?();
@FI;N <IR2>4 = <IR2>.M=;F;L.CR?FM1I4ILF>(Q/2);
@FI;N <IR2>& = <IR2>.M=;F;L.CR?FM1I4ILF>(B/2);
M>.M?NAMIR(<IR2>4, <IR2>&);
Making shape 2 (the circle) !CL=F?0B;J? =M = H?Q !CL=F?0B;J?();
=M.G:L;>COM = <IR2>.M=;F;L.CR?FM1I4ILF>(L);
Attach both shapes with a fixture. <I>S.=L?;N?$CRNOL?(JM,1.0);
<I>S.=L?;N?$CRNOL?(=M, 1.0);
F64B?2 5.8
4EB .>QROB LC #LAB (S005)
219
4EFP FP >@EFBSBA ?V RPFKD QEB IL@>I MLPFQFLK LC > PE>MB, >@@BPPBA SF> > 3?=2 S>OF>?IB @>IIBA
G:J.
4EBK, TEBK TB DL QL AO>T QEB ?LAV, TB RPB ?LQE L?=N() >KA ?FFCJM?() TFQE QEB @FO@IB
LCCPBQ QEB P>JB T>V.
F64B?2 5.9
Our offset in pixels 3?=2 I@@M?N = H?Q 3?=2(0,-B/2);
Converting the vector to Box2D world I@@M?N = <IR2>.P?=NIL.CR?FM1I4ILF>(I@@M?N);
Setting the local position of the circle =CL=F?.G:J.M?N(I@@M?N.R,I@@M?N.S);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
220
Example 5.5: Multiple shapes on one body
&FKFPEFKD LCC QEFP PB@QFLK, ) T>KQ QL PQOBPP QEB CLIILTFKD: QEB PQRCC VLR AO>T FK VLRO 0OL@BPPFKD
TFKALT ALBPKfQ J>DF@>IIV BUMBOFBK@B MEVPF@P PFJMIV ?B@>RPB TB @OB>QBA PLJB "LU2$ ?LAFBP
>KA PE>MBP. 4EBPB BU>JMIBP TLOH ?B@>RPB TB SBOV @>OBCRIIV J>Q@EBA ELT TB AO>T LRO
BIBJBKQP TFQE ELT TB ABCFKBA QEB ?LAFBP >KA PE>MBP TB MRQ FKQL QEB "LU2$ TLOIA. )C VLR
>@@FABKQ>IIV AO>T VLRO PE>MB AFCCBOBKQIV, VLR TLKfQ DBQ >K BOOLO, KLQ COLJ 0OL@BPPFKD LO COLJ
"LU2$. (LTBSBO, VLRO PHBQ@E TFII ILLH LAA >KA QEB MEVPF@P TLKfQ TLOH @LOOB@QIV. &LO BU>JMIB,
TE>Q FC TB E>A TOFQQBK:
TEBK TB @OB>QBA QEB 3E>MB, ?RQ:
TEBK FQ @>JB QFJB QL AFPMI>V QEB PE>MB?
PIC> >CMJF;S() U
3?=2 JIM = <IR2>.A?NI>S.CR?F!IIL>(<I>S);
@FI;N ; = <I>S.A?NAHAF?();
L?=N+I>?(!#,1#/);
JOMB+;NLCR();
NL;HMF;N?(JIM.R,JIM.S);
LIN;N?(-;);
@CFF(175);
MNLIE?(0);
First the rectangle at (0,0) L?=N(0,0,Q,B);
Then the ellipse offset at (0,-h/2) ?FFCJM?(0,-B/2,L*2,L*2);
JIJ+;NLCR();
W
3?=2 I@@M?N = H?Q 3?=2(0,-B/2);
?FFCJM?(0,B/2,L*2,L*2);
4EB .>QROB LC #LAB (S005)
221
4EB OBPRIQP TLRIA ILLH IFHB QEB FJ>DB >?LSB, TEBOB @IB>OIV, QEB @LIIFPFLKP >OB KLQ
CRK@QFLKFKD >P BUMB@QBA. 4EFP FP KLQ ?B@>RPB QEB MEVPF@P FP ?OLHBK; FQfP ?B@>RPB TB AFA KLQ
@LJJRKF@>QB MOLMBOIV TFQE "LU2$, BFQEBO TEBK TB MRQ PQRCC FK QEB J>DF@ TLOIA LO NRBOFBA
QEB TLOIA CLO IL@>QFLKP.
->HB VLRO LTK IFQQIB >IFBK ?BFKD RPFKD JRIQFMIB PE>MBP >QQ>@EBA QL > PFKDIB ?LAV. 4OV
RPFKD JLOB QE>K LKB MLIVDLK QL J>HB > @LK@>SB PE>MB. 2BJBJ?BO, VLR >OBKfQ
IFJFQBA QL RPFKD QEB PE>MB AO>TFKD CRK@QFLKP FK 0OL@BPPFKD; VLR @>K RPB FJ>DBP,
@LILOP, >AA E>FO TFQE IFKBP, BQ@. 4EFKH LC QEB "LU2$ PE>MBP LKIV >P PHBIBQLKP CLO VLRO
@OB>QFSB >KA C>KQ>PQF@>I ABPFDK!
Exercise 5.5 Exercise 5.5
5.11 Feeling AttachedBox2D Joints 5.11 Feeling AttachedBox2D Joints
"LU2$ GLFKQP >IILT VLR QL @LKKB@Q LKB
?LAV QL >KLQEBO, BK>?IFKD JLOB >AS>K@BA
PFJRI>QFLKP LC PTFKDFKD MBKARIRJP, BI>PQF@
?OFADBP, PNRFPEV @E>O>@QBOP, TEBBIP
PMFKKFKD LK >K >UIB, BQ@. 4EBOB >OB J>KV
AFCCBOBKQ HFKAP LC "LU2$ GLFKQP. )K QEFP
@E>MQBO TBfOB DLFKD QL ILLH >Q QEOBB:
AFPQ>K@B GLFKQP, OBSLIRQB GLFKQP, >KA
gJLRPBh GLFKQP.
,BQfP ?BDFK TFQE > AFPQ>K@B GLFKQ, > GLFKQ QE>Q
@LKKB@QP QTL ?LAFBP TFQE > CFUBA IBKDQE.
4EB GLFKQ FP >QQ>@EBA QL B>@E ?LAV >Q >
PMB@FCFBA >K@ELO MLFKQ (> MLFKQ OBI>QFSB QL
QEB ?LAVfP @BKQBO). &LO >KV "LU2$ GLFKQ, TB
KBBA QL CLIILT QEBPB PQBMP. 4EFP, LC @LROPB,
FP PFJFI>O QL QEB JBQELALILDV TB RPBA QL
?RFIA ?LAFBP >KA PE>MBP, TFQE PLJB NRFOHP.
'?0; 1. ",60 >@=0 D:@ 3,A0 ?B: -:/40> =0,/D ?: 2:. '?0; 1. ",60 >@=0 D:@ 3,A0 ?B: -:/40> =0,/D ?: 2:.
'?0; 2. D01490 ?30 5:49?. '?0; 2. D01490 ?30 5:49?.
'?0; 3. C:9142@=0 ?30 5:49?H> ;=:;0=?40> (+3,? ,=0 ?30 -:/40>? +30=0 ,=0 ?30 ,9.3:=>? '?0; 3. C:9142@=0 ?30 5:49?H> ;=:;0=?40> (+3,? ,=0 ?30 -:/40>? +30=0 ,=0 ?30 ,9.3:=>?
+3,? 4> 4?> =0>? 7092?3? I> 4? 07,>?4. := =424/?) +3,? 4> 4?> =0>? 7092?3? I> 4? 07,>?4. := =424/?)
'?0; 4. C=0,?0 ?30 5:49?. '?0; 4. C=0,?0 ?30 5:49?.
,BQfP >PPRJB TB E>SB QTL .;LNC=F? L?GB@QP QE>Q B>@E PQLOB > OBCBOBK@B QL > "LU2$ I>S
L?GB@Q. 7BfII @>II QEBJ M>OQF@IBP J1 >KA J2.
F64B?2 5.10
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
222
/+, LKQL 3QBM 2. ,BQfP ABCFKB QEB GLFKQ.
%>PV, OFDEQ? .LT FQfP QFJB QL @LKCFDROB QEB GLFKQ. &FOPQ TB QBII QEB GLFKQ TEF@E QTL ?LAFBP FQ
@LKKB@QP:
4EBK TB PBQ RM > OBPQ IBKDQE. 2BJBJ?BO, FC LRO OBPQ IBKDQE FP FK MFUBIP, TB KBBA QL @LKSBOQ FQ!
! AFPQ>K@B GLFKQ >IPL FK@IRABP QTL LMQFLK>I PBQQFKDP QE>Q @>K J>HB QEB GLFKQ PLCQ, IFHB > PMOFKD
@LKKB@QFLK: @L?KO?H=S&T >KA >;GJCHA/;NCI.
&FK>IIV, TB @OB>QB QEB GLFKQ.
"LU2$ TLKfQ HBBM QO>@H LC TE>Q HFKA LC GLFKQ TB >OB J>HFKD, PL TB E>SB QL @>PQ FQ >P >
"CMN;H=?(ICHN RMLK @OB>QFLK.
7B @>K @OB>QB "LU2$ GLFKQP >KVTEBOB FK LRO 0OL@BPPFKD PHBQ@E. (BOBfP >K BU>JMIB LC ELT TB
JFDEQ TOFQB > @I>PP QL ABP@OF?B QTL "LU2$ ?LAFBP @LKKB@QBA TFQE > PFKDIB GLFKQ.
.;LNC=F? J1 = H?Q .;LNC=F?();
.;LNC=F? J2 = H?Q .;LNC=F?();
"CMN;H=?(ICHN"?@ >D> = H?Q "CMN;H=?(ICHN"?@();
>D>.<I>SA = J1.<I>S;
>D>.<I>S = J2.<I>S;
>D>.F?HANB = <IR2>.M=;F;L.CR?FM1I4ILF>(10);
Measured in Hz, like the frequency of
harmonic oscillation; try values between 1
and 5.
>D>.@L?KO?H=S&T = :::;
Dampens the spring; typically a number
between 0 and 1.
>D>.>;GJCHA/;NCI = :::;
"CMN;H=?(ICHN >D = ("CMN;H=?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(>D>);
4EB .>QROB LC #LAB (S005)
223
Example 5.6: DistanceJoint
=F;MM .;CL U
Two objects that each have a Box2D body .;LNC=F? J1;
.;LNC=F? J2;
Arbitrary rest length @FI;N F?H = 32;
.;CL(@FI;N R, @FI;N S) U
Problems can result if the bodies are
initialized at the same location. J1 = H?Q .;LNC=F?(R,S);
J2 = H?Q .;LNC=F?(R+L;H>IG(-1,1),S+L;H>IG(-1,1));
Making the joint! "CMN;H=?(ICHN"?@ >D> = H?Q "CMN;H=?(ICHN"?@();
>D>.<I>SA = J1.<I>S;
>D>.<I>S = J2.<I>S;
>D>.F?HANB = <IR2>.M=;F;L.CR?FM1I4ILF>(F?H);
>D>.@L?KO?H=S&T = 0; // 1LS ; P;FO? F?MM NB;H 5
>D>.>;GJCHA/;NCI = 0; // /;HA?M <?NQ??H 0 ;H> 1
"CMN;H=?(ICHN >D = ("CMN;H=?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(>D>);
W
PIC> >CMJF;S() U
3?=2 JIM1 = <IR2>.A?NI>S.CR?F!IIL>(J1.<I>S);
3?=2 JIM2 = <IR2>.A?NI>S.CR?F!IIL>(J2.<I>S);
MNLIE?(0);
FCH?(JIM1.R,JIM1.S,JIM2.R,JIM2.S);
J1.>CMJF;S();
J2.>CMJF;S();
W
W
Make the joint. Note that we aren't storing a
reference to the joint anywhere! We might
need to someday, but for now it's OK.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
224
!KLQEBO GLFKQ VLR @>K @OB>QB FK "LU2$ FP >
F9JC@IH9 >C=BH. ! OBSLIRQB GLFKQ @LKKB@QP QTL
"LU2$ ?LAFBP >Q > @LJJLK >K@ELO MLFKQ,
TEF@E @>K >IPL ?B OBCBOOBA QL >P > gEFKDB.h
4EB GLFKQ E>P >K g>KDIBh QE>Q ABP@OF?BP QEB
OBI>QFSB OLQ>QFLK LC B>@E ?LAV. 4L RPB >
OBSLIRQB GLFKQ, TB CLIILT QEB P>JB PQBMP TB
AFA TFQE QEB AFPQ>K@B GLFKQ.
#OB>QB > PFJRI>QFLK LC > ?OFADB ?V RPFKD AFPQ>K@B GLFKQP QL @LKKB@Q > PBNRBK@B LC
@FO@IBP (LO OB@Q>KDIBP) >P FIIRPQO>QBA QL QEB OFDEQ. !PPFDK > ABKPFQV LC WBOL QL IL@H QEB
BKAMLFKQP FK MI>@B. %UMBOFJBKQ TFQE AFCCBOBKQ S>IRBP QL J>HB QEB ?OFADB JLOB LO IBPP
gPMOFKDV.h )Q PELRIA >IPL ?B KLQBA QE>Q QEB GLFKQP QEBJPBISBP E>SB KL MEVPF@>I DBLJBQOV,
PL FK LOABO CLO VLRO ?OFADB KLQ QL E>SB ELIBP, PM>@FKD ?BQTBBK QEB KLABP TFII ?B
FJMLOQ>KQ.
Exercise 5.6 Exercise 5.6
F64B?2 5.11
Step 1: Make sure you have two bodies ready to go. Step 1: Make sure you have two bodies ready to go.
,BQfP >PPRJB TB E>SB QTL IR L?GB@QP, B>@E LC TEF@E PQLOBP > OBCBOBK@B QL > "LU2$ ?LAV.
IR <IR1 = H?Q IR();
IR <IR2 = H?Q IR();
4EB .>QROB LC #LAB (S005)
225
Step 2: Define the joint. Step 2: Define the joint.
.LT TB T>KQ > /?PIFON?(ICHN"?@ L?GB@Q.
/?PIFON?(ICHN"?@ LD> = H?Q /?PIFON?(ICHN"?@();
Step 3: Configure the joints properties. Step 3: Configure the joints properties.
4EB JLPQ FJMLOQ>KQ MOLMBOQFBP LC > OBSLIRQB GLFKQ >OB QEB QTL ?LAFBP FQ @LKKB@QP >P TBII >P
QEBFO JRQR>I >K@ELO MLFKQ (F.B. TEBOB QEBV >OB @LKKB@QBA). 4EBV >OB PBQ TFQE QEB CRK@QFLK
CHCNC;FCT?().
.LQF@B ELT QEB CFOPQ QTL >ODRJBKQP PMB@FCV QEB ?LAFBP >KA QEB PB@LKA MLFKQ PMB@FCFBP QEB
>K@ELO, TEF@E FK QEFP @>PB FP IL@>QBA >Q QEB @BKQBO LC QEB CFOPQ ?LAV.
!K BU@FQFKD CB>QROB LC > /?PIFON?(ICHN L?GB@Q FP QE>Q VLR @>K JLQLOFWB FQ PL FQ PMFKP
>RQLKLJLRPIV. &LO BU>JMIB:
4EB JLQLO @>K ?B BK>?IBA >KA AFP>?IBA TEFIB QEB MOLDO>J FP ORKKFKD.
&FK>IIV, QEB >?FIFQV CLO > OBSLIRQB GLFKQ QL PMFK @>K ?B @LKPQO>FKBA ?BQTBBK QTL >KDIBP. ("V
ABC>RIQ, FQ @>K OLQ>QB > CRII 360 ABDOBBP, LO 14-:.' O>AF>KP.)
LD>.CHCNC;FCT?(<IR1.<I>S, <IR2.<I>S, <IR1.<I>S.A?N4ILF>!?HN?L());
Turn on the motor. LD>.?H;<F?+INIL = NLO?;
How fast is the motor? LD>.GINIL0J??> = .'*2;
How powerful is the motor? LD>.G;R+INIL1ILKO? = 1000.0;
LD>.?H;<F?*CGCN = NLO?;
LD>.FIQ?LAHAF? = -.'/8;
LD>.OJJ?LAHAF? = .'/8;
Step 4: Create the joint Step 4: Create the joint
,BQfP Q>HB > ILLH >Q >II LC QEBPB PQBMP QLDBQEBO FK > @I>PP @>IIBA 4CH>GCFF, TEF@E @LKKB@QP
QTL ?LUBP TFQE > OBSLIRQB GLFKQ. )K QEFP @>PB, <IR1 E>P > ABKPFQV LC WBOL, PL LKIV <IR2 PMFKP
>OLRKA > CFUBA MLFKQ.
/?PIFON?(ICHN DICHN = (/?PIFON?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(LD>);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
226
Example 5.7: Spinning Windmill
=F;MM 4CH>GCFF U
Our Windmill is two boxes and one joint. /?PIFON?(ICHN DICHN;
IR <IR1;
IR <IR2;
4CH>GCFF(@FI;N R, @FI;N S) U
In this example, the Box class expects a
boolean argument that will be used to
determine if the Box is fixed or not. See
website for the Box class code.
<IR1 = H?Q IR(R,S,120,10,@;FM?);
<IR2 = H?Q IR(R,S,10,40,NLO?);
/?PIFON?(ICHN"?@ LD> = H?Q /?PIFON?(ICHN"?@();
LD>.CHCNC;FCT?(<IR1.<I>S, <IR2.<I>S, <IR1.<I>S.A?N4ILF>!?HN?L());
The joint connects two bodies and is
anchored at the center of the first body.
A motor! LD>.GINIL0J??> = .'*2;
LD>.G;R+INIL1ILKO? = 1000.0;
LD>.?H;<F?+INIL = NLO?;
DICHN = (/?PIFON?(ICHN) <IR2>.QILF>.=L?;N?(ICHN(LD>);
W Create the Joint.
Turning the motor on or off PIC> NIAAF?+INIL() U
<IIF?;H GINILMN;NOM = DICHN.CM+INIL#H;<F?>();
DICHN.?H;<F?+INIL(!GINILMN;NOM);
W
4EB .>QROB LC #LAB (S005)
227
4EB I>PQ GLFKQ TBfII ILLH >Q FP > JLRPB GLFKQ. ! JLRPB GLFKQ FP QVMF@>IIV RPBA CLO JLSFKD > ?LAV
TFQE QEB JLRPB. (LTBSBO, FQ @>K >IPL ?B RPBA QL AO>D >K L?GB@Q >OLRKA QEB P@OBBK
>@@LOAFKD QL PLJB >O?FQO>OV L >KA M. 4EB GLFKQ CRK@QFLKP ?V MRIIFKD QEB ?LAV QLT>OAP >
gQ>ODBQh MLPFQFLK.
"BCLOB TB ILLH >Q QEB +IOM?(ICHN L?GB@Q FQPBIC, IBQfP >PH LROPBISBP TEV TB BSBK KBBA FQ FK
QEB CFOPQ MI>@B. )C VLR ILLH >Q QEB "LU2$ AL@RJBKQ>QFLK, QEBOB FP > CRK@QFLK @>IIBA
M?N1L;HM@ILG() QE>Q PMB@FCF@>IIV gPBQP QEB MLPFQFLK LC QEB ?LAVfP LOFDFK >KA OLQ>QFLK
(O>AF>KP).h )C > ?LAV E>P > MLPFQFLK, @>KfQ TB GRPQ >PPFDK QEB ?LAVfP MLPFQFLK QL QEB JLRPB?
7EFIB QEFP TFII FK C>@Q JLSB QEB ?LAV, FQ TFII >IPL E>SB QEB RKCLOQRK>QB OBPRIQ LC ?OB>HFKD QEB
MEVPF@P. ,BQfP FJ>DFKB VLR ?RFIQ > QBIBMLOQ>QFLK J>@EFKB QE>Q >IILTP VLR QL QBIBMLOQ COLJ
VLRO ?BAOLLJ QL VLRO HFQ@EBK (DLLA CLO I>QB-KFDEQ PK>@HFKD). .LT, DL >EB>A >KA OBTOFQB
.BTQLKfP I>TP LC JLQFLK QL >@@LRKQ CLO QEB MLPPF?FIFQV LC QBIBMLOQ>QFLK. .LQ PL B>PV, OFDEQ?
"LU2$ E>P QEB P>JB MOL?IBJ. )C VLR J>KR>IIV >PPFDK QEB IL@>QFLK LC >K ?LAV, FQfP IFHB P>VFKD
gQBIBMLOQ QE>Q ?LAVh >KA "LU2$ KL ILKDBO HKLTP ELT QL @LJMRQB QEB MEVPF@P MOLMBOIV.
PIC> >CMJF;S() U
<IR1.>CMJF;S();
<IR2.>CMJF;S();
W
W
5PB > OBSLIRQB GLFKQ CLO QEB TEBBIP LC >
@>O. 5PB JLQLOP PL QE>Q QEB @>O AOFSBP
>RQLKLJLRPIV. 4OV RPFKD > @E>FK PE>MB
CLO QEB OL>AfP PROC>@B.
Exercise 5.7 Exercise 5.7
3?=2 GIOM? = <IR2>.M=L??H1I4ILF>(R,S);
<I>S.M?N1L;HM@ILG(GIOM?,0);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
228
(LTBSBO, "LU2$ ALBP >IILT VLR QL QFB > OLMB QL VLROPBIC >KA DBQ > COFBKA LC VLROP QL PQ>KA FK
QEB HFQ@EBK >KA AO>D VLR QEBOB. 4EFP FP TE>Q QEB +IOM?(ICHN ALBP. )QfP IFHB > PQOFKD VLR >QQ>@E
QL > ?LAV >KA MRII QLT>OAP > Q>ODBQ.
,BQfP ILLH >Q J>HFKD QEFP GLFKQ, >PPRJFKD TB E>SB > IR L?GB@Q @>IIBA <IR. 4EFP @LAB TFII ILLH
FABKQF@>I QL LRO AFPQ>K@B GLFKQ TFQE LKB PJ>II AFCCBOBK@B.
3L, TE>QfP QEFP IFKB LC @LAB >II >?LRQ?
7BII, >P TBfSB PQ>QBA, > GLFKQ FP > @LKKB@QFLK ?BQTBBK HKC ?LAFBP. 7FQE > JLRPB GLFKQ, TBfOB
P>VFKD QE>Q QEB PB@LKA ?LAV FP, TBII, QEB DOLRKA. (JJ. 7E>Q QEB EB@H FP QEB ;FCIB8 FK "LU2$?
/KB T>V QL FJ>DFKB FQ FP QL QEFKH LC QEB P@OBBK >P QEB DOLRKA. 7E>Q TBfOB ALFKD FP J>HFKD >
GLFKQ QE>Q @LKKB@QP > OB@Q>KDIB AO>TK LK QEB TFKALT TFQE QEB 0OL@BPPFKD TFKALT FQPBIC. !KA
QEB MLFKQ FK QEB TFKALT QL TEF@E QEB @LKKB@QFLK FP QFBA FP > JLSFKD Q>ODBQ.
/K@B TB E>SB > JLRPB GLFKQ, TBfII T>KQ QL RMA>QB QEB Q>ODBQ IL@>QFLK @LKQFKR>IIV TEFIB QEB
PHBQ@E FP ORKKFKD.
4L J>HB QEFP TLOH FK >K >@QR>I 0OL@BPPFKD PHBQ@E, TBfII T>KQ QL E>SB QEB CLIILTFKD:
1. B:C .7,>> B:C .7,>>d!K L?GB@Q QE>Q OBCBOBK@BP > "LU2$ ?LAV.
2. ';=492 .7,>> ';=492 .7,>>d!K L?GB@Q QE>Q J>K>DBP QEB JLRPB GLFKQ QE>Q AO>DP QEB IR L?GB@Q
>OLRKA.
Just like before, define the Joint. +IOM?(ICHN"?@ G> = H?Q +IOM?(ICHN"?@();
Whoa, this is new! C:.8E:OA = 8EN2:.=;J#HEKD:BE:O();
Attach the Box body. G>.<I>S = <IR.<I>S;
Set properties. G>.G;R$IL=? = 5000.0;
G>.@L?KO?H=S&T = 5.0;
G>.>;GJCHA/;NCI = 0.9;
Create the joint. +IOM?(ICHN GIOM?(ICHN = (+IOM?(ICHN)
<IR2>.QILF>.=L?;N?(ICHN(G>);.
C:.8E:OA = 8EN2:.=;J#HEKD:BE:O();
3?=2 GIOM?4ILF> = <IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6);
GIOM?(ICHN.M?N1;LA?N(GIOM?4ILF>);
4EB .>QROB LC #LAB (S005)
229
3. ",49 ?,- ",49 ?,-d7EBKBSBO GIOM?.L?MM?>() FP @>IIBA, QEB JLRPB GLFKQ FP @OB>QBA;
TEBKBSBO GIOM?/?F?;M?>() FP @>IIBA, QEB JLRPB GLFKQ FP ABPQOLVBA. 4EFP >IILTP RP
QL FKQBO>@Q TFQE > ?LAV LKIV TEBK QEB JLRPB FP MOBPPBA.
,BQfP Q>HB > ILLH >Q QEB J>FK Q>?. 9LR @>K CFKA QEB OBPQ LC QEB @LAB CLO QEB IR >KA 0JLCHA
@I>PPBP SF> QEB ?LLH TB?PFQB.
Example 5.8: MouseJoint demonstration
.IR2" <IR2>;
One Box IR <IR;
Object to manage MouseJoint 0JLCHA MJLCHA;
PIC> M?NOJ() U
MCT?(400,300);
<IR2> = H?Q .IR2"(NBCM);
<IR2>.=L?;N?4ILF>();
<IR = H?Q IR(QC>NB/2,B?CABN/2);
The MouseJoint is really null until we click
the mouse.
MJLCHA = H?Q 0JLCHA();
W
PIC> GIOM?.L?MM?>() U
Was the mouse clicked inside the Box? C@ (<IR.=IHN;CHM(GIOM?5, GIOM?6)) U
If so, attach the MouseJoint. MJLCHA.<CH>(GIOM?5,GIOM?6,<IR);
W
W
PIC> GIOM?/?F?;M?>() U
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
230
)QfP TLOQE KLQFKD QE>Q TEFIB QEB QB@EKFNRB CLO AO>DDFKD >K L?GB@Q >OLRKA RPFKD > +IOM?(ICHN FP
RPBCRI, "LU2$ >IPL >IILTP > ?LAV QL E>SB > )',#+A1'! QVMB.
+FKBJ>QF@ ?LAFBP @>K ?B @LKQOLIIBA ?V QEB
RPBO ?V PBQQFKD QEBFO SBIL@FQV AFOB@QIV. &LO
BU>JMIB, IBQfP P>V VLR T>KQ >K L?GB@Q QL
CLIILT > Q>ODBQ (IFHB VLRO JLRPB). 9LR @LRIA
@OB>QB > SB@QLO QE>Q MLFKQP COLJ > ?LAVfP
IL@>QFLK QL > Q>ODBQ.
When the mouse is released, were done
with the MouseJoint.
MJLCHA.>?MNLIS();
W
PIC> >L;Q() U
<;=EALIOH>(255);
<IR2>.MN?J();
We must always update the MouseJoints
target.
MJLCHA.OJ>;N?(GIOM?5,GIOM?6);
<IR.>CMJF;S();
MJLCHA.>CMJF;S();
W
5PB > JLRPB GLFKQ QL JLSB > "LU2$ ?LAV >OLRKA QEB P@OBBK >@@LOAFKD QL >K >IDLOFQEJ
LO FKMRQ LQEBO QE>K QEB JLRPB. &LO BU>JMIB, >PPFDK FQ > IL@>QFLK >@@LOAFKD QL 0BOIFK
KLFPB LO HBV MOBPPBP. /O ?RFIA VLRO LTK @LKQOLIIBO RPFKD >K !OARFKL
(EQQM://TTT.>OARFKL.@@/).
Exercise 5.8 Exercise 5.8
I>S"?@ <> = H?Q I>S"?@();
Setting the body type to Kinematic <>.NSJ? = I>S1SJ?.)',#+A1'!;
F64B?2 5.12
3?=2 JIM = <I>S.A?N4ILF>!?HN?L();
3?=2 N;LA?N = <IR2>.=IIL>.CR?FM1I4ILF>(GIOM?5,GIOM?6);
A vector pointing from the body position to
the Mouse
3?=2 P = N;LA?N.MO<(JIM);
4EB .>QROB LC #LAB (S005)
231
/K@B VLR E>SB QE>Q SB@QLO, VLR @LRIA >PPFDK FQ QL QEB ?LAVfP SBIL@FQV PL QE>Q FQ JLSBP QL QEB
Q>ODBQ.
9LR @>K >IPL AL QEB P>JB TFQE >KDRI>O SBIL@FQV (LO IB>SB FQ >ILKB >KA >IILT QEB MEVPF@P QL
Q>HB LSBO).
)Q FP FJMLOQ>KQ QL KLQB QE>Q HFKBJ>QF@ ?LAFBP AL KLQ @LIIFAB TFQE LQEBO HFKBJ>QF@ LO PQ>QF@
?LAFBP. )K QEBPB @>PBP, QEB JLRPB GLFKQ PQO>QBDV FP MOBCBO>?IB.
Assigning a bodys velocity directly,
overriding physics!
<I>S.M?N*CH?;L3?FI=CNS(P);
2BAL %UBO@FPB 5.8, ?RQ RPB > HFKBJ>QF@ ?LAV FKPQB>A.
Exercise 5.9 Exercise 5.9
5.12 Bringing It All Back Home to Forces 5.12 Bringing It All Back Home to Forces
)K #E>MQBO 2, TB PMBKQ > ILQ LC QFJB QEFKHFKD >?LRQ ?RFIAFKD BKSFOLKJBKQP TFQE JRIQFMIB
CLO@BP. !K L?GB@Q JFDEQ OBPMLKA QL DO>SFQ>QFLK>I >QQO>@QFLK, TFKA, >FO OBPFPQ>K@B, BQ@. #IB>OIV
QEBOB >OB CLO@BP >Q TLOH FK "LU2$ >P TB T>Q@E OB@Q>KDIBP >KA @FO@IBP PMFK >KA CIV >OLRKA
QEB P@OBBK. "RQ PL C>O, TBfSB LKIV E>A QEB >?FIFQV QL J>KFMRI>QB > PFKDIB DIL?>I
CLO@BdDO>SFQV.
)C TB T>KQ QL RPB >KV LC LRO #E>MQBO 2 QB@EKFNRBP TFQE "LU2$, TB KBBA ILLH KL CROQEBO QE>K
LRO QORPQV ;JJFS$IL=?() CRK@QFLK. )K LRO +IP?L @I>PP TB TOLQB > CRK@QFLK @>IIBA
;JJFS$IL=?(), TEF@E OB@BFSBA > SB@QLO, AFSFABA FQ ?V J>PP, >KA >@@RJRI>QBA FQ FKQL QEB
JLSBOfP >@@BIBO>QFLK. 7FQE "LU2$, QEB P>JB CRK@QFLK BUFPQP, ?RQ TB ALKfQ KBBA QL TOFQB FQ
LROPBISBP. )KPQB>A, TB @>K @>II QEB "LU2$ ?LAVfP ;JJFS$IL=?() CRK@QFLK!
<IR2> = H?Q .IR2"(NBCM);
<IR2>.=L?;N?4ILF>();
Setting the global gravity force <IR2>.M?N%L;PCNS(0, -20);
=F;MM IR U
I>S <I>S;
PIC> ;JJFS$IL=?(3?=2 @IL=?) U
3?=2 JIM = <I>S.A?N4ILF>!?HN?L();
Calling the Body's applyForce() function <I>S.;JJFS$IL=?(@IL=?, JIM);
W
W
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
232
(BOB TB >OB OB@BFSFKD > CLO@B SB@QLO >KA M>PPFKD FQ >ILKD QL QEB "LU2$ I>S L?GB@Q. 4EB HBV
AFCCBOBK@B FP QE>Q "LU2$ FP > JLOB PLMEFPQF@>QBA BKDFKB QE>K LRO BU>JMIBP COLJ #E>MQBO 2.
/RO B>OIFBO CLO@BP BU>JMIBP >PPRJBA QE>Q QEB CLO@B T>P >IT>VP >MMIFBA >Q QEB JLSBOfP @BKQBO.
(BOB TB DBQ QL PMB@FCV BU>@QIV TEBOB LK QEB ?LAV QEB CLO@B FP >MMIFBA. )K QEB >?LSB @LAB,
TBfOB GRPQ >MMIVFKD FQ QL QEB @BKQBO ?V >PHFKD QEB ?LAV CLO FQP @BKQBO, ?RQ QEFP @LRIA ?B
>AGRPQBA.
,BQfP P>V TB T>KQBA QL RPB > DO>SFQ>QFLK>I >QQO>@QFLK CLO@B. 2BJBJ?BO QEB @LAB TB TOLQB ?>@H
FK #E>MQBO 2 FK LRO ANNL;=NIL @I>PP?
7B @>K OBTOFQB QEB BU>@Q P>JB CRK@QFLK RPFKD 3?=2 FKPQB>A >KA RPB FQ FK > "LU2$ BU>JMIB.
.LQB ELT CLO LRO CLO@B @>I@RI>QFLK TB @>K PQ>V @LJMIBQBIV TFQEFK QEB "LU2$ @LLOAFK>QB
PVPQBJ >KA KBSBO QEFKH >?LRQ MFUBIP.
.3?=NIL ;NNL;=N(+IP?L G) U
.3?=NIL @IL=? = .3?=NIL.MO<(FI=;NCIH,G.FI=;NCIH);
@FI;N >CMN;H=? = @IL=?.G;A();
>CMN;H=? = =IHMNL;CH(>CMN;H=?,5.0,25.0);
@IL=?.HILG;FCT?();
@FI;N MNL?HANB = (A * G;MM * G.G;MM) / (>CMN;H=? * >CMN;H=?);
@IL=?.GOFN(MNL?HANB);
L?NOLH @IL=?;
W
3?=2 ;NNL;=N(+IP?L G) U
We have to ask Box2D for the locations first! 3?=2 JIM = <I>S.A?N4ILF>!?HN?L();
3?=2 GIP?L.IM = G.<I>S.A?N4ILF>!?HN?L();
3?=2 @IL=? = JIM.MO<(GIP?L.IM);
@FI;N >CMN;H=? = @IL=?.F?HANB();
>CMN;H=? = =IHMNL;CH(>CMN;H=?,1,5);
@IL=?.HILG;FCT?();
@FI;N MNL?HANB = (% * 1 * G.<I>S.G:G;MM) / (>CMN;H=? * >CMN;H=?);
Remember, its mulLocal() for Vec2. @IL=?.GOF*I=;F(MNL?HANB);
L?NOLH @IL=?;
W
4EB .>QROB LC #LAB (S005)
233
4>HB >KV BU>JMIB VLR J>AB MOBSFLRPIV RPFKD > CLO@B @>I@RI>QFLK >KA ?OFKD QE>Q CLO@B
@>I@RI>QFLK FKQL "LU2$.
Exercise 5.10 Exercise 5.10
5.13 Collision Events 5.13 Collision Events
.LT TBfSB PBBK > PROSBV LC TE>Q @>K ?B ALKB TFQE "LU2$. 3FK@B QEFP ?LLH FP KLQ @>IIBA
g4EB .>QROB LC "LU2$,h FQfP KLQ JV FKQBKQFLK QL @LSBO BSBOV PFKDIB MLPPF?IB CB>QROB LC QEB
"LU2$ BKDFKB. "RQ ELMBCRIIV ?V ILLHFKD >Q QEB ?>PF@P LC ?RFIAFKD ?LAFBP, PE>MBP, >KA GLFKQP,
TEBK FQ @LJBP QFJB QL RPB >K >PMB@Q LC "LU2$ QE>Q TB E>SBKfQ @LSBOBA, QEB PHFIIP TBfSB
D>FKBA EBOB TFII J>HB QE>Q MOL@BPP @LKPFABO>?IV IBPP M>FKCRI. 4EBOB FP LKB JLOB CB>QROB LC
"LU2$, ELTBSBO, QE>Q ) AL QEFKH FP TLOQE @LSBOFKD.
,BQfP >PH > NRBPQFLK VLRfSB IFHBIV ?BBK TLKABOFKD >?LRQ:
2<5H =: % K5BH GCA9H<=B; HC <5DD9B K<9B HKC BCL2 6C8=9G 7C@@=89? % A95B, 8CBSH ;9H A9
KFCB;R%SA H<F=@@98 H<5H BCL2 =G <5B8@=B; 5@@ C: H<9 7C@@=G=CBG :CF A9. BIH =: =H H5?9G 75F9 C:
9J9FMH<=B; :CF A9, <CK 5A % GIDDCG98 HC ?BCK K<9B H<=B;G 5F9 <5DD9B=B;?
9LRO CFOPQ QELRDEQP TEBK @LKPFABOFKD >K BSBKQ AROFKD TEF@E QTL L?GB@QP @LIIFAB JFDEQ ?B >P
CLIILTP: 7BII, FC ) HKLT >II QEB ?LAFBP FK QEB PVPQBJ, >KA ) HKLT TEBOB QEBV >OB >II IL@>QBA,
QEBK ) @>K GRPQ PQ>OQ @LJM>OFKD QEB IL@>QFLKP, PBB TEF@E LKBP >OB FKQBOPB@QFKD, >KA
ABQBOJFKB QE>Q QEBVfSB @LIIFABA. 4E>QfP > KF@B QELRDEQ, ?RQ EBIIL??!? 4EB TELIB MLFKQ LC RPFKD
"LU2$ FP QE>Q "LU2$ TFII Q>HB @>OB LC QE>Q CLO RP. )C TB >OB DLFKD QL AL QEB DBLJBQOV QL QBPQ
CLO FKQBOPB@QFLK LROPBISBP, QEBK >II TBfOB ALFKD FP OB-FJMIBJBKQFKD "LU2$.
/C @LROPB, "LU2$ E>P QELRDEQ LC QEFP MOL?IBJ ?BCLOB. )QfP > MOBQQV @LJJLK LKB. !CQBO >II, FC
VLR FKQBKA QL J>HB > ?>GFIIFLK ALII>OP PBIIFKD PLJB D>JB @>IIBA !KDOV "FOAP, VLR ?BQQBO TBII
J>HB PLJBQEFKD E>MMBK TEBK >K FII-QBJMBOBA MFDBLK PJ>PEBP FKQL > @>OA?L>OA ?LU.
"LU2$ >IBOQP VLR QL JLJBKQP LC @LIIFPFLK TFQE PLJBQEFKD @>IIBA >K gFKQBOC>@B.h )QfP TLOQE
IB>OKFKD >?LRQ FKQBOC>@BP, >K >AS>K@BA CB>QROB LC L?GB@Q-LOFBKQBA MOLDO>JJFKD. 9LR @>K
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
234
Q>HB > ILLH >Q QEB *>S> )KQBOC>@B 4RQLOF>I (EQQM://ALTKIL>A.LO>@IB.@LJ/G>S>PB/QRQLOF>I/G>S>/
@LK@BMQP/FKQBOC>@B.EQJI) >P TBII >P QEB *"LU2$ !IHN;=N*CMN?H?L @I>PP. () E>SB >IPL FK@IRABA
>K BU>JMIB LK QEB TB?PFQB QE>Q ABJLKPQO>QBP RPFKD QEB FKQBOC>@B AFOB@QIV.)
)C VLR >OB RPFKD 0"LU2$, >P TB >OB EBOB, VLR ALKfQ KBBA QL FJMIBJBKQ VLRO LTK FKQBOC>@B.
$BQB@QFKD @LIIFPFLK BSBKQP FP ALKB QEOLRDE > @>II?>@H CRK@QFLK. -R@E IFHB GIOM?.L?MM?>() FP
QOFDDBOBA TEBK QEB JLRPB FP MOBPPBA, <?ACH!IHN;=N() FP QOFDDBOBA TEBK QTL PE>MBP @LIIFAB.
"BCLOB QEB >?LSB TFII TLOH, VLR JRPQ CFOPQ IBQ 0"LU2$ HKLT VLR FKQBKA QL IFPQBK CLO @LIIFPFLKP.
(4EFP >IILTP QEB IF?O>OV QL OBAR@B LSBOEB>A ?V ABC>RIQ; FQ TLKfQ ?LQEBO IFPQBKFKD FC FQ ALBPKfQ
E>SB QL.)
4EBOB >OB CLRO @LIIFPFLK BSBKQ @>II?>@HP.
1. <?ACH!IHN;=N() d4OFDDBOBA TEBKBSBO QTL PE>MBP CFOPQ @LJB FKQL @LKQ>@Q TFQE B>@E
LQEBO.
2. ?H>!IHN;=N() d4OFDDBOBA LSBO >KA LSBO >D>FK >P ILKD >P PE>MBP @LKQFKRB QL ?B FK
@LKQ>@Q.
3. JL?0IFP?() d4OFDDBOBA ?BCLOB "LU2$ PLISBP QEB LRQ@LJB LC QEB @LIIFPFLK, F.B.
?BCLOB <?ACH!IHN;=N(). )Q @>K ?B RPBA QL AFP>?IB > @LIIFPFLK FC KB@BPP>OV.
4. JIMN0IFP?() d4OFDDBOBA >CQBO QEB LRQ@LJB LC QEB @LIIFPFLK FP PLISBA. )Q >IILTP VLR
QL D>QEBO FKCLOJ>QFLK >?LRQ QE>Q gPLIRQFLKh (HKLTK >P >K gFJMRIPBh).
4EB ABQ>FIP ?BEFKA JL?0IFP?() >KA JIMN0IFP?() >OB ?BVLKA QEB P@LMB LC QEFP ?LLH;
ELTBSBO, TB >OB DLFKD QL Q>HB > @ILPB ILLH >Q <?ACH!IHN;=N(), TEF@E TFII @LSBO QEB J>GLOFQV
LC @LKSBKQFLK>I @>PBP FK TEF@E VLR T>KQ QL QOFDDBO >K >@QFLK TEBK > @LIIFPFLK L@@ROP.
?H>!IHN;=N() TLOHP FABKQF@>IIV QL <?ACH!IHN;=N(), QEB LKIV AFCCBOBK@B ?BFKD QE>Q FQ L@@ROP
QEB JLJBKQ ?LAFBP PBM>O>QB.
The mousePressed event with which we are
comfortable.
PIC> GIOM?.L?MM?>() U
JLCHNFH("1B? GIOM? Q;M JL?MM?>!");
W
What our "beginContact" event looks like. PIC> <?ACH!IHN;=N(!IHN;=N =J) U
JLCHNFH("0IG?NBCHA =IFFC>?> CH NB? IR2" 4ILF>!");
W
PIC> M?NOJ() U
<IR2> = H?Q .IR2"(NBCM);
<IR2>.=L?;N?4ILF>();
Add this line if you want to listen for
collisions.
<IR2>.FCMN?H$IL!IFFCMCIHM();
W
4EB .>QROB LC #LAB (S005)
235
<?ACH!IHN;=N() FP TOFQQBK >P CLIILTP:
.LQF@B QE>Q QEB CRK@QFLK >?LSB FK@IRABP >K >ODRJBKQ LC QVMB !IHN;=N. ! !IHN;=N L?GB@Q
FK@IRABP >II QEB A>Q> >PPL@F>QBA TFQE > @LIIFPFLKdQEB DBLJBQOV >KA QEB CLO@BP. ,BQfP P>V TB
E>SB > 0OL@BPPFKD PHBQ@E TFQE .;LNC=F? L?GB@QP QE>Q PQLOB > OBCBOBK@B QL > "LU2$ ?LAV.
(BOB FP QEB MOL@BPP TB >OB DLFKD QL CLIILT.
PIC> <?ACH!IHN;=N(!IHN;=N =J) U
W
Step 1: Contact, could you tell me what two things collided? Step 1: Contact, could you tell me what two things collided?
.LT, TE>Q E>P @LIIFABA EBOB? )P FQ QEB ?LAFBP? 4EB PE>MBP? 4EB CFUQROBP? "LU2$ ABQB@QP
@LIIFPFLKP ?BQTBBK PE>MBP; >CQBO >II, QEBPB >OB QEB BKQFQFBP QE>Q E>SB DBLJBQOV. (LTBSBO,
?B@>RPB PE>MBP >OB >QQ>@EBA QL ?LAFBP TFQE CFUQROBP, TE>Q TB OB>IIV T>KQ QL >PH "LU2$ FP:
g#LRIA VLR QBII JB TEF@E QTL CFUQROBP @LIIFABA?h
The contact stores the fixtures as A and B. $CRNOL? @1 = =J.A?N$CRNOL?A();
$CRNOL? @2 = =J.A?N$CRNOL?();
Step 2: Fixtures, could you tell me which body you are attached to? Step 2: Fixtures, could you tell me which body you are attached to?
getBody() gives us the body to which the
Fixture is attached.
I>S <1 = @1.A?NI>S();
I>S <2 = @2.A?NI>S();
Step 3: Bodies, could you tell me which Particles you are associated Step 3: Bodies, could you tell me which Particles you are associated
with? with?
/+, QEFP FP QEB E>OABO M>OQ. !CQBO >II, "LU2$ ALBPKfQ HKLT >KVQEFKD >?LRQ LRO @LAB. 3ROB, FQ FP
ALFKD >II PLOQP LC PQRCC QL HBBM QO>@H LC QEB OBI>QFLKPEFMP ?BQTBBK PE>MBP >KA ?LAFBP >KA
GLFKQP, ?RQ FQfP RM QL RP QL J>K>DB LRO LTK L?GB@QP >KA QEBFO >PPL@F>QFLKP TFQE "LU2$
BIBJBKQP. ,R@HFIV CLO RP, "LU2$ MOLSFABP > CRK@QFLK QE>Q >IILTP RP QL >QQ>@E LRO 0OL@BPPFKD
L?GB@Q (> .;LNC=F?) QL > "LU2$ ?LAV SF> QEB M?N2M?L";N;() >KA A?N2M?L";N;() JBQELAP.
,BQfP Q>HB > ILLH >Q QEB @LKPQOR@QLO FK LRO .;LNC=F? @I>PP TEBOB QEB ?LAV FP J>AB. 7B >OB
BUM>KAFKD LRO ?LAV-J>HFKD MOL@BAROB ?V LKB IFKB LC @LAB, KLQBA ?BILT.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
236
,>QBO, FK LRO ;>>!IHN;=N() CRK@QFLK, LK@B TB HKLT QEB ?LAV, TB @>K >@@BPP QEB .;LNC=F?
L?GB@Q TFQE A?N2M?L";N;().
Example 5.9: CollisionListening
=F;MM .;LNC=F? U
I>S <I>S;
.;LNC=F?(@FI;N R, @FI;N S, @FI;N L) U
I>S"?@ <> = H?Q I>S"?@();
<>.JIMCNCIH = <IR2>.=IIL>.CR?FM1I4ILF>(R, S);
<>.NSJ? = I>S1SJ?."6,A+'!;
<I>S = <IR2>.=L?;N?I>S(<>);
!CL=F?0B;J? =M = H?Q !CL=F?0B;J?();
=M.G:L;>COM = <IR2>.M=;F;L.CR?FM1I4ILF>(L);
<I>S.=L?;N?$CRNOL?(@>,1);
"this" refers to this Particle object. We are
telling the Box2D Body to store a reference
to this Particle that we can access later.
8E:O.I;J/I;H7J7(J>?I);
W
PIC> <?ACH!IHN;=N(!IHN;=N =J) U
$CRNOL? @1 = =J.A?N$CRNOL?A();
$CRNOL? @2 = =J.A?N$CRNOL?();
I>S <1 = @1.A?NI>S();
I>S <2 = @2.A?NI>S();
When we pull the user data object out of
the Body object, we have to remind our
program that it is a Particle object. Box2D
doesnt know this.
.;LNC=F? J1 = (.;LNC=F?) <1.A?N2M?L";N;();
.;LNC=F? J2 = (.;LNC=F?) <2.A?N2M?L";N;();
Once we have the particles, we can do
anything to them. Here we just call a
function that changes their color.
J1.=B;HA?();
J2.=B;HA?();
W
4EB .>QROB LC #LAB (S005)
237
.LT, FK J>KV @>PBP, TB @>KKLQ >PPRJB QE>Q QEB L?GB@QP QE>Q @LIIFABA >OB >II .;LNC=F?
L?GB@QP. 7B JFDEQ E>SB > PHBQ@E TFQE IOH>;LS L?GB@QP, .;LNC=F? L?GB@QP, IR L?GB@QP, BQ@.
3L LCQBK TB TFII E>SB QL NRBOV QEB gRPBO A>Q>h >KA CFKA LRQ TE>Q HFKA LC L?GB@Q FQ FP ?BCLOB
MOL@BBAFKD.
)Q PELRIA >IPL ?B KLQBA QE>Q ARB QL ELT "LU2$ QOFDDBOP QEBPB @>II?>@HP, VLR @>KKLQ @OB>QB
LO ABPQOLV "LU2$ BKQFQFBP FKPFAB LC <?ACH!IHN;=N(), ?H>!IHN;=N(), JL?0IFP?(), LO
JIMN0IFP?(). )C VLR T>KQ QL AL QEFP, VLRfII KBBA QL PBQ > S>OF>?IB FKPFAB >K L?GB@Q
(PLJBQEFKD IFHB: G;LE$IL"?F?NCIH = NLO?), TEF@E VLR @EB@H AROFKD >L;Q() >KA QEBK
ABIBQB L?GB@QP.
Getting a generic object -<D?=N I1 = <1.A?N2M?L";N;();
Asking that object if its a Particle C@ (I1.A?N!F;MM() == .;LNC=F?.=F;MM) U
.;LNC=F? J = (.;LNC=F?) I1;
J.=B;HA?();
W
#LKPFABO ELT MLIVJLOMEFPJ @LRIA EBIM FK QEB >?LSB @>PB. "RFIA >K BU>JMIB FK TEF@E
PBSBO>I @I>PPBP BUQBKA LKB @I>PP >KA QEBOBCLOB BIFJFK>QB QEB KBBA CLO PR@E QBPQFKD.
Exercise 5.11 Exercise 5.11
#OB>QB > PFJRI>QFLK FK TEF@E .;LNC=F? L?GB@QP AFP>MMB>O TEBK QEBV @LIIFAB TFQE LKB
>KLQEBO. 5PB QEB JBQELALILDV ) GRPQ ABP@OF?BA.
Exercise 5.12 Exercise 5.12
5.14 A Brief InterludeIntegration Methods 5.14 A Brief InterludeIntegration Methods
(>P QEB CLIILTFKD BSBO E>MMBKBA QL VLR? 9LRfOB >Q > C>K@V @L@HQ>FI M>OQV OBD>IFKD VLRO
COFBKAP TFQE Q>II Q>IBP LC PLCQT>OB MEVPF@P PFJRI>QFLKP. 3LJBLKB MFMBP RM: g%K@E>KQFKD! "RQ
TE>Q FKQBDO>QFLK JBQELA >OB VLR RPFKD?h g7E>Q?!h VLR QEFKH QL VLROPBIC. g)KQBDO>QFLK?h
->V?B VLRfSB EB>OA QEB QBOJ ?BCLOB. !ILKD TFQE gAFCCBOBKQF>QFLK,h FQfP LKB LC QEB QTL J>FK
LMBO>QFLKP FK @>I@RIRP. 2FDEQ, @>I@RIRP. 4EB DLLA KBTP FP, TBfSB DLQQBK QEOLRDE >?LRQ 90%
LC QEB J>QBOF>I FK QEFP ?LLH OBI>QBA QL MEVPF@P PFJRI>QFLK >KA TB E>SBKfQ OB>IIV KBBABA QL
AFSB FKQL @>I@RIRP. "RQ >P TBfOB @LJFKD @ILPB QL CFKFPEFKD QEFP QLMF@, FQfP TLOQE Q>HFKD >
JLJBKQ QL BU>JFKB QEB @>I@RIRP ?BEFKA TE>Q TB E>SB ?BBK ALFKD >KA ELT FQ OBI>QBP QL QEB
JBQELALILDV FK @BOQ>FK MEVPF@P IF?O>OFBP (IFHB "LU2$ >KA QEB RM@LJFKD QLUF@IF?P).
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
238
,BQfP ?BDFK ?V >KPTBOFKD QEB NRBPQFLK: g7E>Q ALBP FKQBDO>QFLK E>SB QL AL TFQE IL@>QFLK,
SBIL@FQV, >KA >@@BIBO>QFLK?h 7BII, CFOPQ IBQfP ABCFKB /4110=09?4,?4:9 /4110=09?4,?4:9, QEB MOL@BPP LC CFKAFKD >
gABOFS>QFSB.h 4EB ABOFS>QFSB LC > CRK@QFLK FP > JB>PROB LC ELT > CRK@QFLK @E>KDBP LSBO QFJB.
#LKPFABO IL@>QFLK >KA FQP ABOFS>QFSB. ,L@>QFLK FP > MLFKQ FK PM>@B, TEFIB SBIL@FQV FP @E>KDB FK
IL@>QFLK LSBO QFJB. 4EBOBCLOB, SBIL@FQV @>K ?B ABP@OF?BA >P QEB gABOFS>QFSBh LC IL@>QFLK. 7E>Q FP
>@@BIBO>QFLK? 4EB @E>KDB FK SBIL@FQV LSBO QFJBdF.B. QEB gABOFS>QFSBh LC SBIL@FQV.
.LT QE>Q TB RKABOPQ>KA QEB ABOFS>QFSB (AFCCBOBKQF>QFLK), TB @>K ABCFKB QEB FKQBDO>I (FKQBDO>QFLK)
>P QEB FKSBOPB LC QEB ABOFS>QFSB. )K LQEBO TLOAP, QEB FKQBDO>I LC >K L?GB@QfP SBIL@FQV LSBO QFJB
QBIIP RP QEB L?GB@QfP KBT IL@>QFLK TEBK QE>Q QFJB MBOFLA BKAP. ,L@>QFLK FP QEB FKQBDO>I LC
SBIL@FQV, >KA SBIL@FQV FP QEB FKQBDO>I LC >@@BIBO>QFLK. 3FK@B LRO MEVPF@P PFJRI>QFLK FP CLRKABA
RMLK QEB MOL@BPP LC @>I@RI>QFKD >@@BIBO>QFLK ?>PBA LK CLO@BP, TB KBBA FKQBDO>QFLK QL CFDROB
LRQ TEBOB QEB L?GB@Q FP >CQBO > @BOQ>FK MBOFLA LC QFJB (IFHB LKB CO>JB LC >KFJ>QFLK!)
3L TBfSB ?BBK ALFKD FKQBDO>QFLK >II >ILKD! )Q ILLHP IFHB QEFP:
4EB >?LSB JBQELALILDV FP HKLTK >P %RIBO FKQBDO>QFLK (K>JBA CLO QEB J>QEBJ>QF@F>K ,BLKE>OA
%RIBO, MOLKLRK@BA g/FIBOh) LO QEB %RIBO JBQELA. )QfP BPPBKQF>IIV QEB PFJMIBPQ CLOJ LC FKQBDO>QFLK
>KA SBOV B>PV QL FJMIBJBKQ FK LRO @LAB (PBB QEB QTL IFKBP >?LSB!) (LTBSBO, FQ FP KLQ
KB@BPP>OFIV QEB JLPQ BCCF@FBKQ CLOJ, KLO FP FQ @ILPB QL ?BFKD QEB JLPQ >@@RO>QB. 7EV FP %RIBO
FK>@@RO>QB? ,BQfP QEFKH >?LRQ FQ QEFP T>V. 7EBK VLR AOFSB > @>O ALTK QEB OL>A MOBPPFKD QEB D>P
MBA>I TFQE VLRO CLLQ >KA >@@BIBO>QFKD, ALBP QEB @>O PFQ FK LKB IL@>QFLK >Q QFJB BNR>IP LKB
PB@LKA, QEBK AFP>MMB>O >KA PRAABKIV OB>MMB>O FK > KBT IL@>QFLK >Q QFJB BNR>IP QTL PB@LKAP,
>KA AL QEB P>JB QEFKD CLO QEOBB PB@LKAP, >KA CLRO, >KA CFSB? .L, LC @LROPB KLQ. 4EB @>O JLSBP
@LKQFKRLRPIV ALTK QEB OL>A. "RQ TE>QfP E>MMBKFKD FK LRO 0OL@BPPFKD PHBQ@E? ! @FO@IB FP >Q LKB
IL@>QFLK >Q CO>JB 0, >KLQEBO >Q CO>JB 1, >KLQEBO >Q CO>JB 2. 3ROB, >Q QEFOQV CO>JBP MBO PB@LKA,
TBfOB PBBFKD QEB FIIRPFLK LC JLQFLK. "RQ TB LKIV @>I@RI>QB > KBT IL@>QFLK BSBOV , RKFQP LC QFJB,
TEBOB>P QEB OB>I TLOIA FP MBOCB@QIV @LKQFKRLRP. 4EFP OBPRIQP FK PLJB FK>@@RO>@FBP, >P PELTK FK
QEB AF>DO>J ?BILT:
P?FI=CNS.;>>(;==?F?L;NCIH);
FI=;NCIH.;>>(P?FI=CNS);
4EB .>QROB LC #LAB (S005)
239
4EB gOB>I TLOIAh FP QEB @ROSB; %RIBO PFJRI>QFLK FP QEB PBOFBP LC IFKB PBDJBKQP.
/KB LMQFLK QL FJMOLSB LK %RIBO FP QL RPB PJ>IIBO QFJBPQBMPdFKPQB>A LC LK@B MBO CO>JB, TB
@LRIA OB@>I@RI>QB >K L?GB@QfP IL@>QFLK QTBKQV QFJBP MBO CO>JB. "RQ QEFP FPKfQ MO>@QF@>I; LRO
PHBQ@E TLRIA QEBK ORK QLL PILTIV.
) PQFII ?BIFBSB QE>Q %RIBO FP QEB ?BPQ JBQELA CLO IB>OKFKD QEB ?>PF@P, >KA FQfP >IPL MBOCB@QIV
>ABNR>QB CLO JLPQ LC QEB MOLGB@QP TB JFDEQ J>HB FK 0OL@BPPFKD. !KVQEFKD TB ILPB FK
BCCF@FBK@V LO FK>@@RO>@V TB J>HB RM FK B>PB LC RPB >KA RKABOPQ>KA>?FIFQV. &LO ?BQQBO
>@@RO>@V, "LU2$ RPBP PLJBQEFKD @>IIBA PVJMIB@QF@ %RIBO (EQQM://BK.TFHFMBAF>.LOD/TFHF/
3VJMIB@QF@=%RIBO=JBQELA), LO PBJF-BUMIF@FQ %RIBO, > PIFDEQ JLAFCF@>QFLK LC %RIBO.
4EBOB FP >IPL >K FKQBDO>QFLK JBQELA @>IIBA 2RKDB-+RQQ> (K>JBA CLO 'BOJ>K J>QEBJ>QF@F>KP
#. 2RKDB >KA -. 7. +RQQ>), TEF@E FP RPBA FK PLJB MEVPF@P BKDFKBP.
! SBOV MLMRI>O FKQBDO>QFLK JBQELA QE>Q LRO KBUQ MEVPF@P IF?O>OV RPBP FP HKLTK >P g6BOIBQ
FKQBDO>QFLK.h ! PFJMIB T>V QL ABP@OF?B 6BOIBQ FKQBDO>QFLK FP QL QEFKH LC LRO QVMF@>I JLQFLK
>IDLOFQEJ TFQELRQ SBIL@FQV. !CQBO >II, TB ALKfQ OB>IIV KBBA QL PQLOB QEB SBIL@FQV. )C TB >IT>VP
HKLT TEBOB >K L?GB@Q T>P >Q LKB MLFKQ FK QFJB >KA TEBOB FQ FP KLT, TB @>K BUQO>MLI>QB FQP
SBIL@FQV. 6BOIBQ FKQBDO>QFLK ALBP MOB@FPBIV QEFP, QELRDE FKPQB>A LC E>SFKD > S>OF>?IB CLO
SBIL@FQV, FQ @>I@RI>QBP SBIL@FQV TEFIB QEB MOLDO>J FP ORKKFKD. 6BOIBQ FKQBDO>QFLK FP M>OQF@RI>OIV
TBII PRFQBA CLO M>OQF@IB PVPQBJP, BPMB@F>IIV M>OQF@IB PVPQBJP TFQE PMOFKD @LKKB@QFLKP
?BQTBBK QEB M>OQF@IBP. 7B ALKfQ KBBA QL TLOOV >?LRQ QEB ABQ>FIP ?B@>RPB QLUF@IF?P, >P TBfII
PBB ?BILT, Q>HBP @>OB LC QEBJ CLO RP. (LTBSBO, FC VLR >OB FKQBOBPQBA, EBOB FP QEB PBJFK>I
M>MBO LK 6BOIBQ MEVPF@P, COLJ TEF@E GRPQ >?LRQ BSBOV 6BOIBQ @LJMRQBO DO>MEF@P PFJRI>QFLK FP
ABOFSBA: "!AS>K@BA #E>O>@QBO 0EVPF@P" (EQQM://TTT.D>J>PRQO>.@LJ/OBPLRO@B=DRFAB/
20030121/G>@L?PLK=MCS.EQJ). !KA LC @LROPB, VLR @>K CFKA LRQ JLOB >?LRQ 6BOIBQ FKQBDO>QFLK
COLJ 7FHFMBAF> (EQQM://BK.TFHFMBAF>.LOD/TFHF/6BOIBQ=FKQBDO>QFLK).
F64B?2 5.13
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
240
5.15 Verlet Physics with toxiclibs 5.15 Verlet Physics with toxiclibs
&OLJ QLUF@IF?P.LOD:
THCL=7@=6G =G 5B =B89D9B89BH, CD9B GCIF79 @=6F5FM 7C@@97H=CB :CF 7CADIH5H=CB5@ 89G=;B H5G?G
K=H< &5J5 & ,FC79GG=B; 89J9@CD98 6M '5FGH9B THCL=U .7<A=8H (H<IG :5F). /<9 7@5GG9G 5F9
DIFDCG9:I@@M ?9DH :5=F@M ;9B9F=7 =B CF89F HC A5L=A=N9 F9-IG9 =B 8=::9F9BH 7CBH9LHG F5B;=B; :FCA
;9B9F5H=J9 89G=;B, 5B=A5H=CB, =BH9F57H=CB/=BH9F:579 89G=;B, 85H5 J=GI5@=N5H=CB HC 5F7<=H97HIF9
5B8 8=;=H5@ :56F=75H=CB, IG9 5G H957<=B; HCC@ 5B8 ACF9.U
)K LQEBO TLOAP, TB PELRIA QE>KH LRO IR@HV PQ>OP CLO QLUF@IF?P. 7B >OB LKIV DLFKD QL CL@RP LK >
CBT BU>JMIBP OBI>QBA QL 6BOIBQ MEVPF@P, ?RQ QLUF@IF?P FK@IRABP > PRFQB LC LQEBO TLKABOCRI
M>@H>DBP QE>Q EBIM TFQE >RAFL, @LILO, DBLJBQOV, >KA JLOB. )K M>OQF@RI>O, FC VLR >OB ILLHFKD QL
TLOH TFQE CLOJ >KA C>?OF@>QFLK FK 0OL@BPPFKD, Q>HB > ILLH >Q QEB DBLJBQOV M>@H>DB. $BJLP
@>K ?B CLRKA >Q /MBK 0OL@BPPFKD (EQQM://TTT.LMBKMOL@BPPFKD.LOD/MLOQ>I/?RPBO)$=4530).
7B PELRIA KLQB QE>Q QLUF@IF?P T>P ABPFDKBA PMB@FCF@>IIV CLO RPB TFQE 0OL@BPPFKD. 4EFP FP DOB>Q
KBTP. 4EB QOLR?IB TB E>A TFQE J>HFKD "LU2$ TLOH FK 0OL@BPPFKD (JRIQFMIB @LLOAFK>QB
PVPQBJP, "LU2$ SP. *"LU2$ SP. 0"LU2$) FP KLQ >K FPPRB EBOB. QLUF@IF?P FP > IF?O>OV QE>Q VLR GRPQ
ALTKIL>A, PQF@H FK VLRO IF?O>OFBP CLIABO, >KA RPB. !KA QEB @LLOAFK>QB PVPQBJ QE>Q TBfII RPB CLO
QEB MEVPF@P BKDFKB FP QEB @LLOAFK>QB PVPQBJ LC 0OL@BPPFKD, PL KL QO>KPI>QFKD ?>@H >KA CLOQE. )K
>AAFQFLK, QLUF@IF?P FP KLQ IFJFQBA QL > 2$ TLOIA; >II LC QEB MEVPF@P PFJRI>QFLKP >KA CRK@QFLKP
TLOH FK ?LQE QTL >KA QEOBB AFJBKPFLKP. 3L ELT AL VLR AB@FAB TEF@E IF?O>OV VLR PELRIA RPB?
"LU2$ LO QLUF@IF?P? )C VLR C>II FKQL LKB LC QEB CLIILTFKD QTL @>QBDLOFBP, VLRO AB@FPFLK FP > ?FQ
B>PFBO:
1. My project involves collisions. I have circles, squares, and other strangely shaped objects 1. My project involves collisions. I have circles, squares, and other strangely shaped objects
that knock each other around and bounce off each other. that knock each other around and bounce off each other.
)K QEFP @>PB, VLR >OB DLFKD QL KBBA "LU2$. QLUF@IF?P ALBP KLQ E>KAIB @LIIFPFLKP.
2. My project involves lots of particles flying around the screen. Sometimes they attract 2. My project involves lots of particles flying around the screen. Sometimes they attract
each other. Sometimes they repel each other. And sometimes they are connected with each other. Sometimes they repel each other. And sometimes they are connected with
springs. springs.
)K QEFP @>PB, QLUF@IF?P FP IFHBIV VLRO ?BPQ @ELF@B. )Q FP PFJMIBO QL RPB QE>K "LU2$ >KA M>OQF@RI>OIV
TBII PRFQBA QL @LKKB@QBA PVPQBJP LC M>OQF@IBP. QLUF@IF?P FP >IPL SBOV EFDE MBOCLOJ>K@B, ARB QL
QEB PMBBA LC QEB 6BOIBQ FKQBDO>QFLK >IDLOFQEJ (KLQ QL JBKQFLK QEB C>@Q QE>Q QEB MOLDO>J DBQP QL
FDKLOB >II LC QEB @LIIFPFLK DBLJBQOV).
(BOB FP > IFQQIB @E>OQ QE>Q @LSBOP PLJB LC QEB CB>QROBP CLO B>@E MEVPF@P IF?O>OV.
4EB .>QROB LC #LAB (S005)
241
Feature Feature Box2D Box2D toxiclibs VerletPhysics toxiclibs VerletPhysics
#LIIFPFLK DBLJBQOV 9BP .L
3$ MEVPF@P .L 9BP
0>OQF@IB >QQO>@QFLK /
OBMRIPFLK CLO@BP
.L 9BP
3MOFKD @LKKB@QFLKP 9BP 9BP
/QEBO @LKKB@QFLKP: OBSLIRQB,
MRIIBV, DB>O, MOFPJ>QF@
9BP .L
-LQLOP 9BP .L
&OF@QFLK 9BP .L
Getting toxiclibs Getting toxiclibs
%SBOVQEFKD VLR KBBA QL ALTKIL>A >KA FKPQ>II QLUF@IF?P @>K ?B CLRKA >Q:
QLUF@IF?P (EQQM://QLUF@IF?P.LOD/)
7EBK VLR ALTKIL>A QEB IF?O>OV, VLRfII KLQF@B QE>Q FQ @LJBP TFQE BFDEQ JLARIBP (F.B. PR?-
CLIABOP), B>@E > IF?O>OV FK FQP LTK OFDEQ. &LO QEB BU>JMIBP FK QEFP @E>MQBO, VLR TFII LKIV KBBA
gSBOIBQMEVPF@Ph >KA gQLUF@IF?P@LOBh; ELTBSBO, ) OB@LJJBKA VLR Q>HB > ILLH >Q >KA @LKPFABO
RPFKD >II LC QEB JLARIBP!
/K@B VLR E>SB QEB IF?O>OV FKPQ>IIBA QL VLRO 0OL@BPPFKD IF?O>OV CLIABO
(EQQM://TFHF.MOL@BPPFKD.LOD/T/(LT=QL=)KPQ>II=>=#LKQOF?RQBA=,F?O>OV), VLR >OB OB>AV QL PQ>OQ
ILLHFKD >Q QEB CLIILTFKD BU>JMIBP.
Core Elements of VerletPhysics Core Elements of VerletPhysics
7B PMBKQ > ILQ LC QFJB TLOHFKD QEOLRDE QEB @LOB BIBJBKQP LC > "LU2$ TLOIA: TLOIA, ?LAV,
PE>MB, GLFKQ. 4EFP DFSBP RP > EB>A PQ>OQ LK RKABOPQ>KAFKD QLUF@IF?P, PFK@B FQ CLIILTP > PFJFI>O
PQOR@QROB.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
242
Box2D Box2D toxiclibs VerletPhysics toxiclibs VerletPhysics
7LOIA 6BOIBQ0EVPF@P
"LAV 6BOIBQ0>OQF@IB
3E>MB
.LQEFKD! QLUF@IF?P ALBP KLQ E>KAIB PE>MB
DBLJBQOV
&FUQROB
.LQEFKD! QLUF@IF?P ALBP KLQ E>KAIB PE>MB
DBLJBQOV
*LFKQ 6BOIBQ3MOFKD
Vectors with toxiclibs Vectors with toxiclibs
(BOB TB DL >D>FK. 2BJBJ?BO >II QE>Q QFJB TB PMBKQ IB>OKFKD QEB FKP >KA LRQP LC QEB .3?=NIL
@I>PP? 4EBK OBJBJ?BO ELT TEBK TB DLQ QL "LU2$, TB E>A QL QO>KPI>QB >II QELPB @LK@BMQP QL >
"LU2$ SB@QLO @I>PP: 3?=2? 7BII, FQfP QFJB QL AL FQ >D>FK. QLUF@IF?P >IPL FK@IRABP FQP LTK SB@QLO
@I>PPBP, LKB CLO QTL AFJBKPFLKP >KA LKB CLO QEOBB: 3?=2" >KA 3?=3".
!D>FK, QLUF@IF?P SB@QLOP >OB QEB P>JB @LK@BMQR>IIV, ?RQ TB KBBA QL IB>OK > ?FQ LC KBT PVKQ>U.
9LR @>K CFKA >II LC QEB AL@RJBKQ>QFLK CLO QEBPB SB@QLO @I>PPBP EBOB:
6B@2$ (EQQM://QLUF@IF?P.LOD/AL@P/@LOB/QLUF/DBLJ/6B@2$.EQJI)
6B@3$ (EQQM://QLUF@IF?P.LOD/AL@P/@LOB/QLUF/DBLJ/6B@3$.EQJI)
!KA IBQfP GRPQ OBSFBT PLJB LC QEB ?>PF@ SB@QLO J>QE LMBO>QFLKP TFQE .3?=NIL QO>KPI>QBA QL
3?=2" (TBfOB PQF@HFKD TFQE 2$ CLO PFJMIF@FQVfP P>HB).
PVector PVector Vec2D Vec2D
.3?=NIL ; = H?Q .3?=NIL(1,-1);
.3?=NIL < = H?Q .3?=NIL(3,4);
;.;>>(<);
3?=2" ; = H?Q 3?=2"(1,-1);
3?=2" < = H?Q 3?=2"(3,4);
;.;>>0?F@(<);
.3?=NIL ; = H?Q .3?=NIL(1,-1);
.3?=NIL < = H?Q .3?=NIL(3,4);
.3?=NIL = = .3?=NIL.;>>(;,<);
3?=2" ; = H?Q 3?=2"(1,-1);
3?=2" < = H?Q 3?=2"(3,4);
3?=2" = = ;.;>>(<);
.3?=NIL ; = H?Q .3?=NIL(1,-1);
@FI;N G = ;.G;A();
;.HILG;FCT?();
3?=2" ; = H?Q 3?=2"(1,-1);
@FI;N G = ;.G;AHCNO>?();
;.HILG;FCT?();
4EB .>QROB LC #LAB (S005)
243
Building the toxiclibs physics world Building the toxiclibs physics world
4EB CFOPQ QEFKD TB KBBA QL AL QL @OB>QB > QLUF@IF?P MEVPF@P TLOIA FK LRO BU>JMIBP FP FJMLOQ
QEB IF?O>OV FQPBIC.
4EBK TBfII KBBA > OBCBOBK@B QL LRO MEVPF@P TLOIA, > 3?LF?N.BSMC=M LO 3?LF?N.BSMC=M2"
L?GB@Q (ABMBKAFKD LK TEBQEBO TB >OB TLOHFKD FK QTL LO QEOBB AFJBKPFLKP). 4EB BU>JMIBP FK
QEFP @E>MQBO TFII LMBO>QB FK 2$ LKIV CLO PFJMIF@FQV, ?RQ QEBV @LRIA B>PFIV ?B BUQBKABA FKQL 3$
(>KA 3$ SBOPFLKP >OB >S>FI>?IB TFQE QEB @E>MQBO ALTKIL>A).
/K@B VLR E>SB VLRO 3?LF?N.BSMC=M L?GB@Q, VLR @>K PBQ PLJB DIL?>I MOLMBOQFBP CLO VLRO
TLOIA. &LO BU>JMIB, FC VLR T>KQ FQ QL E>SB E>OA ?LRKA>OFBP M>PQ TEF@E L?GB@QP @>KKLQ QO>SBI,
VLR @>K PBQ FQP IFJFQP:
)K >AAFQFLK, VLR @>K >AA DO>SFQV QL QEB MEVPF@P TLOIA TFQE > %L;PCNS?B;PCIL L?GB@Q. !
DO>SFQV ?BE>SFLO OBNRFOBP > SB@QLOdELT PQOLKD >KA FK TE>Q AFOB@QFLK FP QEB DO>SFQV?
&FK>IIV, FK LOABO QL @>I@RI>QB QEB MEVPF@P LC QEB TLOIA >KA JLSB QEB L?GB@QP FK QEB TLOIA, TB
E>SB QL @>II OJ>;N?(). 4VMF@>IIV QEFP TLRIA E>MMBK LK@B MBO CO>JB FK >L;Q().
Importing the libraries CGJILN NIRC.JBSMC=M2>.*;
CGJILN NIRC.JBSMC=M2>.<?B;PCILM.*;
CGJILN NIRC.A?IG.*;
3?LF?N.BSMC=M2" JBSMC=M;
PIC> M?NOJ() U
Creating a toxiclibs Verlet physics world JBSMC=M=H?Q 3?LF?N.BSMC=M2"();
JBSMC=M.M?N4ILF>IOH>M(H?Q /?=N(0,0,QC>NB,B?CABN));
JBSMC=M.;>>?B;PCIL(H?Q %L;PCNS?B;PCIL(H?Q 3?=2"(0,0.5)));
W
PIC> >L;Q() U
This is the same as Box2Ds step()
function
JBSMC=M.OJ>;N?();
W
5.16 Particles and Springs in toxiclibs 5.16 Particles and Springs in toxiclibs
)K QEB "LU2$ BU>JMIBP, TB P>T ELT TB @>K @OB>QB LRO LTK @I>PP (@>IIBA, P>V, .;LNC=F?)
>KA FK@IRAB > OBCBOBK@B QL > "LU2$ ?LAV.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
244
4EFP QB@EKFNRB FP PLJBTE>Q OBARKA>KQ PFK@B "LU2$ FQPBIC HBBMP QO>@H LC >II LC QEB ?LAFBP FK FQP
TLOIA. (LTBSBO, FQ >IILTP RP QL J>K>DB TEF@E ?LAV FP TEF@E (>KA QEBOBCLOB ELT B>@E ?LAV FP
AO>TK) TFQELRQ E>SFKD QL OBIV LK FQBO>QFKD QEOLRDE "LU2$fP FKQBOK>I IFPQP.
,BQfP ILLH >Q ELT TB JFDEQ Q>HB QEB P>JB >MMOL>@E TFQE QEB @I>PP 3?LF?N.;LNC=F?2" FK
QLUF@IF?P. 7B T>KQ QL J>HB LRO LTK .;LNC=F? @I>PP PL QE>Q TB @>K AO>T LRO M>OQF@IBP > @BOQ>FK
T>V >KA FK@IRAB >KV @RPQLJ MOLMBOQFBP. 7BfA MOL?>?IV TOFQB LRO @LAB >P CLIILTP:
,LLHFKD >Q QEB >?LSB, TB PELRIA CFOPQ ?B QEOFIIBA QL KLQF@B QE>Q AO>TFKD QEB M>OQF@IB FP >P PFJMIB
>P DO>??FKD QEB L >KA M >KA RPFKD QEBJ. .L >THT>OA @LKSBOPFLKP ?BQTBBK @LLOAFK>QB
PVPQBJP EBOB PFK@B QLUF@IF?P FP ABPFDKBA QL QEFKH FK MFUBIP. 3B@LKA, VLR JFDEQ KLQF@B QE>Q QEFP
.;LNC=F? @I>PPfP PLIB MROMLPB FP QL PQLOB > OBCBOBK@B QL > 3?LF?N.;LNC=F?2" L?GB@Q. 4EFP
EFKQP >Q PLJBQEFKD. 2BJBJ?BO LRO AFP@RPPFLK LC FKEBOFQ>K@B ?>@H FK #E>MQBO 4: 0>OQF@IB
3VPQBJP? 7E>Q FP > .;LNC=F? L?GB@Q LQEBO QE>K >K g>RDJBKQBAh 3?LF?N.;LNC=F?? 7EV
?LQEBO J>HFKD > SBOIBQ M>OQF@IB FKPFAB > M>OQF@IB TEBK TB @LRIA PFJMIV ?RN?H>
3?LF?N.;LNC=F??
=F;MM .;LNC=F? U
I>S <I>S;
=F;MM .;LNC=F? U
Our Particle has a reference to a
VerletParticle.
3?LF?N.;LNC=F?2" J;
.;LNC=F?(3?=2" JIM) U
A VerletParticle needs an initial location (an
x and y).
J = H?Q 3?LF?N.;LNC=F?2"(JIM);
W
PIC> >CMJF;S() U
@CFF(0,150);
MNLIE?(0);
When it comes time to draw the Particle, we
ask the VerletParticle for its x and y
coordinates.
?FFCJM?(J.R,J.S,16,16);
W
W
=F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U
.;LNC=F?(3?=2" FI=) U
Calling super() so that the object is
initialized properly
MOJ?L(FI=);
W
We want this to be just like a VerletParticle,
only with a display() method.
PIC> >CMJF;S() U
@CFF(175);
MNLIE?(0);
4EB .>QROB LC #LAB (S005)
245
2BJBJ?BO LRO JRIQF-PQBM MOL@BPP TFQE QEB "LU2$ BU>JMIBP? 7B E>A QL >PH QEB ?LAV CLO FQP
IL@>QFLK, QEBK @LKSBOQ QE>Q IL@>QFLK QL MFUBIP, QEBK RPB QE>Q IL@>QFLK FK > AO>TFKD CRK@QFLK.
.LT, ?B@>RPB TB E>SB FKEBOFQBA BSBOVQEFKD COLJ QEB 3?LF?N.;LNC=F? @I>PP, LRO LKIV PQBM
FP QL AO>T QEB PE>MB >Q R >KA S!
)K@FABKQ>IIV, FQfP FKQBOBPQFKD QL KLQB QE>Q QEB 3?LF?N.;LNC=F?2" @I>PP FP > PR?@I>PP LC 3?=2".
3L FK >AAFQFLK QL FKEBOFQFKD BSBOVQEFKD COLJ 3?LF?N.;LNC=F?2", LRO .;LNC=F? @I>PP >@QR>IIV
E>P >II LC QEB 3?=2" CRK@QFLKP >S>FI>?IB >P TBII.
7B @>K KLT @OB>QB M>OQF@IBP >KVTEBOB TFQEFK LRO PHBQ@E.
*RPQ J>HFKD > M>OQF@IB FPKfQ BKLRDE, ELTBSBO. 7B E>SB QL J>HB PROB TB QBII LRO MEVPF@P
TLOIA >?LRQ QEBJ TFQE QEB ;>>.;LNC=F?() CRK@QFLK.
)C VLR ILLH >Q QEB QLUF@IF?P AL@RJBKQ>QFLK, VLRfII PBB QE>Q QEB ;>>.;LNC=F?() BUMB@QP >
3?LF?N.;LNC=F?2" L?GB@Q.
;>>.;LNC=F?(3?LF?N.;LNC=F?2" J;LNC=F?)
!KA ELT @>K TB QEBK M>PP FKQL QEB CRK@QFLK LRO LTK .;LNC=F? L?GB@Q? 2BJBJ?BO QE>Q
LQEBO QBKBQ LC L?GB@Q-LOFBKQBA MOLDO>JJFKDdMLIVJLOMEFPJ? (BOB, ?B@>RPB LRO .;LNC=F?
@I>PP ?RN?H>M 3?LF?N.;LNC=F?2", TB @>K @ELLPB QL QOB>Q LRO M>OQF@IB FK QTL AFCCBOBKQ
T>VPd>P > .;LNC=F? LO >P > 3?LF?N.;LNC=F?2". 4EFP FP >K FK@OBAF?IV MLTBOCRI CB>QROB LC
L?GB@Q-LOFBKQBA MOLDO>JJFKD. )C TB ?RFIA LRO @RPQLJ @I>PPBP ?>PBA LK @I>PPBP COLJ
QLUF@IF?P, TB @>K RPB LRO L?GB@QP FK @LKGRK@QFLK TFQE >II LC QEB CRK@QFLKP QLUF@IF?P E>P QL
LCCBO.
)K >AAFQFLK QL QEB 3?LF?N.;LNC=F? @I>PP, QLUF@IF?P E>P > PBQ LC @I>PPBP QE>Q >IILT VLR QL
@LKKB@Q M>OQF@IBP TFQE PMOFKD CLO@BP. 4EBOB >OB QEOBB QVMBP LC PMOFKDP FK QLUF@IF?P:
i 3?LF?N0JLCHA: 4EFP @I>PP @OB>QBP > PMOFKDV @LKKB@QFLK ?BQTBBK QTL M>OQF@IBP FK
PM>@B. ! PMOFKDfP MOLMBOQFBP @>K ?B @LKCFDROBA FK PR@E > T>V >P QL @OB>QB > PQFCC
PQF@H-IFHB @LKKB@QFLK LO > EFDEIV BI>PQF@ PQOBQ@EV @LKKB@QFLK. ! M>OQF@IB @>K >IPL ?B
IL@HBA PL QE>Q LKIV LKB BKA LC QEB PMOFKD @>K JLSB.
Weve inherited x and y from VerletParticle! ?FFCJM?(R,S,16,16);
W
W
.;LNC=F? J1 = H?Q .;LNC=F?(H?Q 3?=2"(100,20));
.;LNC=F? J2 = H?Q .;LNC=F?(H?Q 3?=2"(100,180));
JBSMC=M.;>>.;LNC=F?(J1);
JBSMC=M.;>>.;LNC=F?(J2);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
246
i 3?LF?N!IHMNL;CH?>0JLCHA: ! 3?LF?N!IHMNL;CH?>0JLCHA L?GB@Q FP > PMOFKD TELPB
J>UFJRJ AFPQ>K@B @>K ?B IFJFQBA. 4EFP @>K EBIM QEB TELIB PMOFKD PVPQBJ >@EFBSB
?BQQBO PQ>?FIFQV.
i 3?LF?N+CH"CMN;H=?0JLCHA: ! 3?LF?N+CH"CMN;H=?0JLCHA L?GB@Q FP > PMOFKD QE>Q
LKIV BKCLO@BP FQP OBPQ IBKDQE FC QEB @ROOBKQ AFPQ>K@B FP IBPP QE>K FQP OBPQ IBKDQE. 4EFP FP
E>KAV FC VLR T>KQ QL BKPROB L?GB@QP >OB >Q IB>PQ > @BOQ>FK AFPQ>K@B COLJ B>@E LQEBO,
?RQ ALKfQ @>OB FC QEB AFPQ>K@B FP ?FDDBO QE>K QEB BKCLO@BA JFKFJRJ.
4EB FKEBOFQ>K@B >KA MLIVJLOMEFPJ QB@EKFNRB TB BJMILVBA FK QEB MOBSFLRP PB@QFLK >IPL
MOLSBP QL ?B RPBCRI TEBK @OB>QFKD PMOFKDP. ! PMOFKD BUMB@QP QTL M>OQF@IBP TEBK FQ FP @OB>QBA.
!KA >D>FK, ?B@>RPB LRO .;LNC=F? @I>PP ?RN?H>M 3?LF?N.;LNC=F?, > 3?LF?N0JLCHA L?GB@Q
TFII >@@BMQ LRO .;LNC=F? L?GB@QP M>PPBA FKQL QEB @LKPQOR@QLO. ,BQfP Q>HB > ILLH >Q PLJB
BU>JMIB @LAB QE>Q >PPRJBP QEB BUFPQBK@B LC LRO QTL MOBSFLRP M>OQF@IBP J1 >KA J2 >KA @OB>QBP
> @LKKB@QFLK ?BQTBBK QEBJ TFQE > DFSBK OBPQ IBKDQE >KA PQOBKDQE.
*RPQ >P TFQE M>OQF@IBP, FK LOABO CLO QEB @LKKB@QFLK QL >@QR>IIV ?B M>OQ LC QEB MEVPF@P TLOIA, TB
KBBA QL BUMIF@FQIV >AA FQ.
What is the rest length of the spring? @FI;N F?H = 80;
How strong is the spring? @FI;N MNL?HANB = 0.01;
3?LF?N0JLCHA2" MJLCHA=H?Q 3?LF?N0JLCHA2"(J1,J2,F?H,MNL?HANB);
JBSMC=M.;>>0JLCHA(MJLCHA);
5.17 Putting It All Together: A Simple Interactive 5.17 Putting It All Together: A Simple Interactive
Spring Spring
/KB QEFKD TB P>T TFQE "LU2$ FP QE>Q QEB MEVPF@P PFJRI>QFLK ?OLHB ALTK TEBK TB LSBOOLAB FQ
>KA J>KR>IIV PBQ QEB IL@>QFLK LC > ?LAV. 7FQE QLUF@IF?P, TB ALKfQ E>SB QEFP MOL?IBJ. )C TB T>KQ
QL JLSB QEB IL@>QFLK LC > M>OQF@IB, TB @>K PFJMIV PBQ FQP L >KA M IL@>QFLK J>KR>IIV. (LTBSBO,
?BCLOB TB AL PL, FQfP DBKBO>IIV > DLLA FAB> QL @>II QEB FI=E() CRK@QFLK.
FI=E() FP QVMF@>IIV RPBA QL IL@H > M>OQF@IB FK MI>@B >KA FP FABKQF@>I QL PBQQFKD > "LU2$ ?LAVfP
ABKPFQV QL 0. (LTBSBO, EBOB TB >OB DLFKD QL PELT ELT QL IL@H > M>OQF@IB QBJMLO>OFIV, JLSB FQ,
>KA QEBK RKIL@H FQ PL QE>Q FQ @LKQFKRBP QL JLSB >@@LOAFKD QL QEB MEVPF@P PFJRI>QFLK. ,BQfP P>V
VLR T>KQ QL JLSB > DFSBK M>OQF@IB TEBKBSBO VLR @IF@H QEB JLRPB.
C@ (GIOM?.L?MM?>) U
4EB .>QROB LC #LAB (S005)
247
!KA KLT TBfOB OB>AV QL MRQ >II LC QEBPB BIBJBKQP QLDBQEBO FK > PFJMIB BU>JMIB QE>Q
@LKKB@QP QTL M>OQF@IBP TFQE > PMOFKD. /KB M>OQF@IB FP IL@HBA FK MI>@B, >KA QEB LQEBO @>K ?B
JLSBA ?V AO>DDFKD QEB JLRPB. .LQB QE>Q QEFP BU>JMIB FP SFOQR>IIV FABKQF@>I QL %U>JMIB 3.11
(PBB M>DB 139).
Example 5.10: Simple Spring with toxiclibs
First lock the particle, then set the x and y,
then unlock() it.
J2.FI=E();
J2.R = GIOM?5;
J2.S = GIOM?6;
J2.OHFI=E();
W
CGJILN NIRC.JBSMC=M2>.*;
CGJILN NIRC.JBSMC=M2>.<?B;PCILM.*;
CGJILN NIRC.A?IG.*;
3?LF?N.BSMC=M2" JBSMC=M;
.;LNC=F? J1;
.;LNC=F? J2;
PIC> M?NOJ() U
MCT?(200,200);
Creating a physics world JBSMC=M=H?Q 3?LF?N.BSMC=M2"();
JBSMC=M.;>>?B;PCIL(H?Q %L;PCNS?B;PCIL2"(H?Q 3?=2"(0,0.5)));
JBSMC=M.M?N4ILF>IOH>M(H?Q /?=N(0,0,QC>NB,B?CABN));
Creating two Particles J1 = H?Q .;LNC=F?(H?Q 3?=2"(100,20));
J2 = H?Q .;LNC=F?(H?Q 3?=2"(100,180));
Locking Particle 1 in place J1.FI=E();
3?LF?N0JLCHA2" MJLCHA=H?Q 3?LF?N0JLCHA2"(J1,J2,80,0.01);
Creating one Spring
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
248
Must add everything to the world JBSMC=M.;>>.;LNC=F?(J1);
JBSMC=M.;>>.;LNC=F?(J2);
JBSMC=M.;>>0JLCHA(MJLCHA);
W
PIC> >L;Q() U
Must update the physics JBSMC=M.OJ>;N?();
<;=EALIOH>(255);
Drawing everything FCH?(J1.R,J1.S,J2.R,J2.S);
J1.>CMJF;S();
J2.>CMJF;S();
C@ (GIOM?.L?MM?>) U
Moving a Particle according to the mouse J2.FI=E();
J2.R = GIOM?5;
J2.S = GIOM?6;
J2.OHFI=E();
W
W
How cute is our simple Particle class?! =F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U
.;LNC=F?(3?=2" FI=) U
MOJ?L(FI=);
W
PIC> >CMJF;S() U
@CFF(175);
MNLIE?(0);
?FFCJM?(R,S,16,16);
W
W
5.18 Connected Systems Part I: String 5.18 Connected Systems Part I: String
4EB >?LSB BU>JMIB, QTL M>OQF@IBP @LKKB@QBA TFQE > PFKDIB PMOFKD, FP QEB @LOB ?RFIAFKD ?IL@H
CLO TE>Q QLUF@IF?Pf MEVPF@P FP M>OQF@RI>OIV TBII PRFQBA CLO: PLCQ ?LAV PFJRI>QFLKP. &LO BU>JMIB, >
PQOFKD @>K ?B PFJRI>QBA ?V @LKKB@QFKD > IFKB LC M>OQF@IBP TFQE PMOFKDP. ! ?I>KHBQ @>K ?B
PFJRI>QBA ?V @LKKB@QFKD > DOFA LC M>OQF@IBP TFQE PMOFKDP. !KA > @RQB, @RAAIV, PNRFPEV @>OQLLK
@E>O>@QBO @>K ?B PFJRI>QBA ?V > @RPQLJ I>VLRQ LC M>OQF@IBP @LKKB@QBA TFQE PMOFKDP.
4EB .>QROB LC #LAB (S005)
249
,BQfP ?BDFK ?V PFJRI>QFKD > gPLCQ MBKARIRJhd> ?L? E>KDFKD COLJ > PQOFKD, FKPQB>A LC > OFDFA
>OJ IFHB TB E>A FK #E>MQBO 3 (PBB M>DB 131). ,BQfP RPB QEB "PQOFKD" FK &FDROB 5.14 >?LSB >P
LRO JLABI.
&FOPQ, TBfII KBBA > IFPQ LC M>OQF@IBP (IBQfP RPB QEB P>JB .;LNC=F? @I>PP TB ?RFIQ FK QEB MOBSFLRP
BU>JMIB).
.LT, IBQfP P>V TB T>KQ QL E>SB 20 M>OQF@IBP, >II PM>@BA 10 MFUBIP >M>OQ.
7B @>K ILLM COLJ C BNR>IP 0 >II QEB T>V RM QL 20, TFQE B>@E M>OQF@IBfP M IL@>QFLK PBQ QL C *
10 PL QE>Q QEB CFOPQ M>OQF@IB FP >Q (0,10), QEB PB@LKA >Q (0,20), QEB QEFOA >Q (0,30), BQ@.
F64B?2 5.14
ALL;S*CMN<.;LNC=F?> J;LNC=F?M = H?Q ALL;S*CMN<.;LNC=F?>();
F64B?2 5.15
@FI;N F?H = 10;
@FI;N HOG.;LNC=F?M = 20;
@IL(CHN C=0; C < HOG.ICHNM; C++) U
Spacing them out along the x-axis .;LNC=F? J;LNC=F?=H?Q .;LNC=F?(C*F?H,10);
Add the particle to our list. JBSMC=M.;>>.;LNC=F?(J;LNC=F?);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
250
%SBK QELRDE FQfP > ?FQ OBARKA>KQ, TBfOB DLFKD QL >AA QEB M>OQF@IB QL ?LQE QEB QLUF@IF?P MEVPF@P
TLOIA >KA QL LRO LTK IFPQ. )K @>PB TB BSBKQR>IIV E>SB JRIQFMIB PQOFKDP, QEFP TFII >IILT RP QL
HKLT TEF@E M>OQF@IBP >OB @LKKB@QBA QL TEF@E PQOFKDP.
.LT CLO QEB CRK M>OQ: )QfP QFJB QL @LKKB@Q >II QEB M>OQF@IBP. 0>OQF@IB 1 TFII ?B @LKKB@QBA QL
M>OQF@IB 0, M>OQF@IB 2 QL M>OQF@IB 1, 3 QL 2, 4 QL 3, BQ@.
)K LQEBO TLOAP, M>OQF@IB C KBBAP QL ?B @LKKB@QBA QL M>OQF@IB C-1 (BU@BMQ CLO TEBK C BNR>IP
WBOL).
.LT, TE>Q FC TB T>KQ QEB PQOFKD QL E>KD COLJ > CFUBA MLFKQ? 7B @>K IL@H LKB LC QEB
M>OQF@IBPdQEB CFOPQ, QEB I>PQ, QEB JFAAIB LKB, BQ@. (BOBfP ELT TB TLRIA >@@BPP QEB CFOPQ M>OQF@IB
(FK QEB ALL;S*CMN) >KA IL@H FQ.
!KA FC TB T>KQ QL AO>T >II QEB M>OQF@IBP >P ?BFKD @LKKB@QBA TFQE > IFKB, >ILKD TFQE > @FO@IB CLO
QEB I>PQ M>OQF@IB, TB @>K RPB <?ACH0B;J?(), ?H>0B;J?(), >KA P?LN?R(), >@@BPPFKD QEB
M>OQF@IB IL@>QFLKP COLJ LRO ALL;S*CMN.
Add the particle to the physics world. J;LNC=F?M.;>>(J;LNC=F?);
W
F64B?2 5.16
C@ (C != 0) U
First we need a reference to the previous
particle.
.;LNC=F? JL?PCIOM = J;LNC=F?M.A?N(C-1);
3?LF?N0JLCHA2" MJLCHA = H?Q 3?LF?N0JLCHA2"(J;LNC=F?,JL?PCIOM,F?H,MNL?HANB);
Then we make a spring connection between
the particle and the previous particle with a
rest length and strength (both floats).
We must not forget to add the spring to the
physics world.
JBSMC=M.;>>0JLCHA(MJLCHA);
W
.;LNC=F? B?;>=J;LNC=F?M.A?N(0);
B?;>.FI=E();
4EB .>QROB LC #LAB (S005)
251
Example 5.11: Soft swinging pendulum
4EB CRII @LAB >S>FI>?IB TFQE QEB @E>MQBO ALTKIL>A >IPL ABJLKPQO>QBP ELT QL AO>D QEB Q>FI
M>OQF@IB TFQE QEB JLRPB.
MNLIE?(0);
HI$CFF();
<?ACH0B;J?();
@IL (.;LNC=F? J : J;LNC=F?M) U
Each particle is one point in the line. P?LN?R(J.R,J.S);
W
?H>0B;J?();
.;LNC=F? N;CF = J;LNC=F?M.A?N(HOG.ICHNM-1);
This draws the last particle as a circle. N;CF.>CMJF;S();
#OB>QB > E>KDFKD @ILQE PFJRI>QFLK RPFKD QEB QB@EKFNRB >?LSB, ?RQ @LKKB@Q >II QEB
M>OQF@IBP TFQE > DOFA >P ABJLKPQO>QBA FK QEB P@OBBKPELQ ?BILT.
Exercise 5.13 Exercise 5.13
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
252
5.19 Connected Systems Part II: Force-Directed 5.19 Connected Systems Part II: Force-Directed
Graph Graph
(>SB VLR BSBO BK@LRKQBOBA QEB CLIILTFKD P@BK>OFL?
g) E>SB > TELIB ?RK@E LC PQRCC ) T>KQ QL AO>T LK QEB P@OBBK >KA ) T>KQ >II QE>Q PQRCC QL ?B
PM>@BA LRQ BSBKIV FK > KF@B, KB>Q, LOD>KFWBA J>KKBO. /QEBOTFPB ) E>SB QOLR?IB PIBBMFKD >Q
KFDEQ.h
4EFP FP KLQ >K RK@LJJLK MOL?IBJ FK @LJMRQ>QFLK>I ABPFDK. /KB PLIRQFLK FP QVMF@>IIV OBCBOOBA
QL >P > gCLO@B-AFOB@QBA DO>ME.h ! CLO@B-AFOB@QBA DO>ME FP > SFPR>IFW>QFLK LC BIBJBKQPdIBQfP @>II
QEBJ gKLABPhdFK TEF@E QEB MLPFQFLKP LC QELPB KLABP >OB KLQ J>KR>IIV >PPFDKBA. 2>QEBO, QEB
KLABP >OO>KDB QEBJPBISBP >@@LOAFKD QL > PBQ LC CLO@BP. 7EFIB >KV CLO@BP @>K ?B RPBA, >
QVMF@>I BU>JMIB FKSLISBP PMOFKD CLO@BP. !KA PL QLUF@IF?P FP MBOCB@Q CLO QEFP P@BK>OFL.
(LT AL TB FJMIBJBKQ QEB >?LSB?
&FOPQ, TBfII KBBA > ,I>? @I>PP. 4EFP FP QEB B>PV M>OQ; FQ @>K ?RN?H> 3?LF?N.;LNC=F?2". 2B>IIV,
QEFP FP GRPQ TE>Q TB AFA ?BCLOB, LKIV TBfOB @>IIFKD FQ ,I>? KLT FKPQB>A LC .;LNC=F?.
.BUQ TB @>K TOFQB > @I>PP @>IIBA !FOMN?L, TEF@E TFII ABP@OF?B > IFPQ LC KLABP.
=F;MM ,I>? ?RN?H>M 3?LF?N.;LNC=F?2" U
,I>?(3?=2" JIM) U
MOJ?L(JIM);
W
PIC> >CMJF;S() U
@CFF(0,150);
MNLIE?(0);
?FFCJM?(R,S,16,16);
W
W
4EB .>QROB LC #LAB (S005)
253
,BQfP >PPRJB TB >AABA > >CMJF;S() CRK@QFLK QL AO>T >II QEB KLABP FK QEB @IRPQBO >KA
@OB>QBA > !FOMN?L L?GB@Q FK M?NOJ() >KA AFPMI>VBA FQ FK >L;Q(). )C TB O>K QEB PHBQ@E >P FP,
KLQEFKD TLRIA E>MMBK. 7EV? "B@>RPB TB CLODLQ QEB TELIB CLO@B-AFOB@QBA DO>ME M>OQ! 7B
KBBA QL @LKKB@Q BSBOV PFKDIB KLAB QL BSBOV LQEBO KLAB TFQE > CLO@B. "RQ TE>Q BU>@QIV AL TB
JB>K ?V QE>Q? ,BQfP >PPRJB TB E>SB CLRO ,I>? L?GB@QP: 0, 1, 2 >KA 3. (BOB >OB LRO
@LKKB@QFLKP:
0 9EDD;9J;: JE 1
0 9EDD;9J;: JE 2
0 9EDD;9J;: JE 3
1 9EDD;9J;: JE 2
1 9EDD;9J;: JE 3
2 9EDD;9J;: JE 3
.LQF@B QTL FJMLOQ>KQ ABQ>FIP >?LRQ LRO @LKKB@QFLK IFPQ.
i #: 9:/0 4> .:990.?0/ ?: 4?>071. #: 9:/0 4> .:990.?0/ ?: 4?>071. 7B ALKfQ E>SB 0 @LKKB@QBA QL 0 LO 1 @LKKB@QBA QL
1.
i +0 /:9H? 900/ ?: =0;0,? .:990.?4:9> 49 =0A0=>0. +0 /:9H? 900/ ?: =0;0,? .:990.?4:9> 49 =0A0=>0. )K LQEBO TLOAP, FC TBfSB >IOB>AV
P>FA 0 FP @LKKB@QBA QL 1, TB ALKfQ KBBA QL P>V 1 FP @LKKB@QBA QL 0 ?B@>RPB, TBII, FQ
>IOB>AV FP!
3L ELT AL TB TOFQB @LAB QL J>HB QEBPB @LKKB@QFLKP CLO * KRJ?BO LC KLABP?
,LLH >Q QEB IBCQ @LIRJK. )Q OB>AP: 000 11 22. 3L TB HKLT TB KBBA QL >@@BPP B>@E KLAB FK
QEB IFPQ COLJ 0 QL *-1.
=F;MM !FOMN?L U
ALL;S*CMN<,I>?> HI>?M;
Well use this variable for the rest length
between all the nodes.
@FI;N >C;G?N?L;
!FOMN?L(CHN H, @FI;N >, 3?=2" =?HN?L) U
HI>?M = H?Q ALL;S*CMN<,I>?>();
>C;G?N?L = >;
@IL (CHN C = 0; C < H; C++) U
HI>?M.;>>(H?Q ,I>?(=?HN?L.;>>(3?=2".L;H>IG3?=NIL())));
W
W
Heres a funny little detail. Were going to
have a problem if all the Node objects start
in exactly the same location. So we add a
random vector to the center location so that
each Node is slightly offset.
@IL (CHN C = 0; C < HI>?M.MCT?()-1; C++) U
3?LF?N.;LNC=F?2" HC = HI>?M.A?N(C);
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
254
.LT, TB HKLT TB KBBA QL @LKKB@Q KLAB 0 QL KLABP 1,2,3. &LO KLAB 1: 2,3. &LO KLAB 2: 3. 3L
CLO BSBOV KLAB F, TB JRPQ ILLM COLJ F+1 RKQFI QEB BKA LC QEB IFPQ.
7FQE BSBOV QTL ,I>?P TB CFKA, >II TB E>SB QL AL QEBK FP J>HB > PMOFKD.
!PPRJFKD QELPB @LKKB@QFLKP >OB J>AB FK QEB !FOMN?L @LKPQOR@QLO, TB @>K KLT @OB>QB >
@IRPQBO FK LRO J>FK Q>? >KA PBB QEB OBPRIQP!
Example 5.12: Cluster
Look how we start j at i + 1. <EH (?DJ @ = ?+1; @ < DE:;I.I?P;(); @++) Q
3?LF?N.;LNC=F?2" HD = HI>?M.A?N(D);
The Spring connects Nodes ni and nj. JBSMC=M.;>>0JLCHA(H?Q
3?LF?N0JLCHA2"(HC,HD,>C;G?N?L,0.01));
W
W
CGJILN NIRC.A?IG.*;
CGJILN NIRC.JBSMC=M2>.*;
3?LF?N.BSMC=M2" JBSMC=M;
!FOMN?L =FOMN?L;
PIC> M?NOJ() U
MCT?(300,300);
JBSMC=M=H?Q 3?LF?N.BSMC=M2"();
=FOMN?L = H?Q !FOMN?L(8,100,H?Q 3?=2"(QC>NB/2,B?CABN/2));
W
PIC> >L;Q() U
JBSMC=M.OJ>;N?();
<;=EALIOH>(255);
Make a cluster.
4EB .>QROB LC #LAB (S005)
255
Draw the cluster. =FOMN?L.>CMJF;S();
W
5PB QEB !FOMN?L PQOR@QROB >P > PHBIBQLK CLO > @RQB, @RAAIV, PNRFPEV @OB>QROB (Y I>
g.LHF> &OFBKAPh). !AA DO>SFQV >KA >IPL >IILT QEB @OB>QROB QL ?B AO>DDBA TFQE QEB
JLRPB.
Exercise 5.14 Exercise 5.14
%UM>KA QEB CLO@B-AFOB@QBA DO>ME QL E>SB JLOB QE>K LKB !FOMN?L L?GB@Q. 5PB >
3?LF?N+CH"CMN;H=?0JLCHA2" L?GB@Q QL @LKKB@Q @IRPQBO QL @IRPQBO.
Exercise 5.15 Exercise 5.15
5.20 Attraction and Repulsion Behaviors 5.20 Attraction and Repulsion Behaviors
7EBK TB ILLHBA >Q >AAFKD >K >QQO>@QFLK CLO@B QL "LU2$, TB CLRKA QE>Q QEB "LU2$ I>S
@I>PP FK@IRABA >K ;JJFS$IL=?() CRK@QFLK. !II TB KBBABA QL AL T>P @>I@RI>QB QEB >QQO>@QFLK
CLO@B (&LO@B = ' * J>PP1 * J>PP2 / AFPQ>K@B PNR>OBA) >P > SB@QLO >KA >MMIV FQ QL QEB ?LAV.
QLUF@IF?P 3?LF?N.;LNC=F? @I>PP >IPL FK@IRABP > CRK@QFLK @>IIBA ;>>$IL=?() QE>Q TB @>K RPB
QL >MMIV >KV @>I@RI>QBA CLO@B QL > M>OQF@IB.
(LTBSBO, QLUF@IF?P >IPL Q>HBP QEFP FAB> LKB PQBM CROQEBO ?V >IILTFKD RP QL >QQ>@E PLJB
@LJJLK CLO@BP (IBQfP @>II QEBJ g?BE>SFLOPh) QL M>OQF@IBP, @>I@RI>QFKD QEBJ >KA >MMIVFKD QEBJ
CLO RP! &LO BU>JMIB, FC TB >QQ>@E >K ANNL;=NCIH?B;PCIL L?GB@Q QL > M>OQF@IB, QEBK >II LQEBO
M>OQF@IBP FK QEB MEVPF@P TLOIA TFII ?B >QQO>@QBA QL QE>Q M>OQF@IB.
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
256
,BQfP P>V TB E>SB > .;LNC=F? @I>PP (QE>Q ?RN?H>M 3?LF?N.;LNC=F?).
/K@B TBfSB J>AB > .;LNC=F? L?GB@Q, TB @>K @OB>QB >K ANNL;=NCIH?B;PCIL L?GB@Q
>PPL@F>QBA TFQE QE>Q M>OQF@IB.
.LQF@B ELT QEB ?BE>SFLO FP @OB>QBA TFQE QTL M>O>JBQBOPd>CMN;H=? >KA MNL?HANB. 4EB
AFPQ>K@B PMB@FCFBP QEB O>KDB TFQEFK TEF@E QEB ?BE>SFLO TFII ?B >MMIFBA. &LO BU>JMIB, FK QEB
>?LSB P@BK>OFL, LKIV LQEBO M>OQF@IBP TFQEFK QTBKQV MFUBIP TFII CBBI QEB >QQO>@QFLK CLO@B. 4EB
PQOBKDQE, LC @LROPB, PMB@FCFBP ELT PQOLKD QEB CLO@B FP.
&FK>IIV, FK LOABO CLO QEB CLO@B QL ?B >@QFS>QBA, QEB ?BE>SFLO KBBAP QL ?B >AABA QL QEB MEVPF@P
TLOIA.
4EFP JB>KP BSBOVQEFKD QE>Q IFSBP FK QEB MEVPF@P PFJRI>QFLK TFII >IT>VP ?B >QQO>@QBA QL QE>Q
M>OQF@IB, >P ILKD >P FQ FP TFQEFK QEB AFPQ>K@B QEOBPELIA.
%SBK QELRDE QLUF@IF?P ALBP KLQ E>KAIB @LIIFPFLKP, VLR @>K @OB>QB > @LIIFPFLK-IFHB BCCB@Q ?V
>AAFKD > OBMRIPFSB ?BE>SFLO QL B>@E >KA BSBOV M>OQF@IB (PL QE>Q BSBOV M>OQF@IB OBMBIP BSBOV
LQEBO M>OQF@IB). ,BQfP ILLH >Q ELT TB JFDEQ JLAFCV LRO .;LNC=F? @I>PP QL AL QEFP.
7B @LRIA KLT OB@OB>QB LRO >QQO>@QFLK BU>JMIB ?V E>SFKD > PFKDIB ANNL;=NIL L?GB@Q QE>Q
BUBOQP >K >QQO>@QFLK ?BE>SFLO LSBO QEB BKQFOB TFKALT.
.;LNC=F? J = H?Q .;LNC=F?(H?Q 3?=2"(200,200));
@FI;N >CMN;H=? = 20;
@FI;N MNL?HANB = 0.1;
ANNL;=NCIH?B;PCIL <?B;PCIL = H?Q ANNL;=NCIH?B;PCIL(J, >CMN;H=?, MNL?HANB);
JBSMC=M.;>>?B;PCIL(<?B;PCIL);
=F;MM .;LNC=F? ?RN?H>M 3?LF?N.;LNC=F?2" U
Weve added a radius to every Particle. @FI;N L;
.;LNC=F? (3?=2" FI=) U
MOJ?L(FI=);
L = 4;
JBSMC=M.;>>?B;PCIL(H?Q ANNL;=NCIH?B;PCIL(NBCM, L*4, -1));
W
PIC> >CMJF;S () U
@CFF (255);
MNLIE? (255);
?FFCJM? (R, S, L*2, L*2);
W
W
Every time a Particle is made, an
AttractionBehavior is generated and added
to the physics world. Note that when the
strength is negative, its a repulsive force!
4EB .>QROB LC #LAB (S005)
257
Example 5.13: Attraction/Repulsion
=F;MM ANNL;=NIL ?RN?H>M 3?LF?N.;LNC=F?2" U
@FI;N L;
ANNL;=NIL (3?=2" FI=) U
MOJ?L (FI=);
L = 24;
JBSMC=M.;>>?B;PCIL(H?Q ANNL;=NCIH?B;PCIL(NBCM, QC>NB, 0.1));
W
PIC> >CMJF;S () U
@CFF(0);
?FFCJM? (R, S, L*2, L*2);
W
W
The AttractionBehavior distance equals
the width so that it covers the entire
window.
#OB>QB >K L?GB@Q QE>Q ?LQE >QQO>@QP >KA OBMBIP. 7E>Q FC FQ >QQO>@QP >KV M>OQF@IB QE>Q FP C>O
>T>V ?RQ OBMBIP QELPB M>OQF@IBP >Q > PELOQ AFPQ>K@B?
Exercise 5.16 Exercise 5.16
5PB ANNL;=NCIH?B;PCIL FK @LKGRK@QFLK TFQE PMOFKD CLO@BP.
Exercise 5.17 Exercise 5.17
#E>MQBO 5. 0EVPF@P ,F?O>OFBP
258
The Ecosystem Project The Ecosystem Project
Step 5 Exercise:
Take your system of creatures from Step 4 and use a physics engine to drive their
motion and behaviors. Some possibilities:
Use Box2D to allow collisions between creatures. Consider triggering
events when creatures collide.
Use Box2D to augment the design of your creatures. Build a skeleton with
distance joints or make appendages with revolute joints.
Use toxiclibs to augment the design of your creature. Use a chain of
toxiclibs particles for tentacles or a mesh of springs as a skeleton.
Use toxiclibs to add attraction and repulsion behaviors to your creatures.
Use spring (or joint) connections between objects to control their
interactions. Create and delete these springs on the fly. Consider making
these connections visible or invisible to the viewer.
4EB .>QROB LC #LAB (S005)
259
Chapter 6. Chapter 6.
Autonomous Agents Autonomous Agents
I*56@ 6@ .; 2E2?06@2 6; 360A6<;.9 @062;02, <? @062;02 360A6<;, 63 F<B 9682 A5.A
/2AA2?.J
Valentino Braitenberg
"BIFBSB FQ LO KLQ, QEBOB FP > MROMLPB. 7BII, >Q IB>PQ QEBOBfP > MROMLPB QL QEB CFOPQ CFSB @E>MQBOP
LC QEFP ?LLH. 7B @LRIA PQLM OFDEQ EBOB; >CQBO >II, TBfSB ILLHBA >Q PBSBO>I AFCCBOBKQ T>VP LC
JLABIFKD JLQFLK >KA PFJRI>QFKD MEVPF@P. !KDOV "FOAP, EBOB TB @LJB!
3QFII, IBQfP QEFKH CLO > JLJBKQ. 7EV >OB TB EBOB? 4EB B5HIF9 LC @LAB, OFDEQ? 7E>Q E>SB TB
?BBK ABPFDKFKD PL C>O? )K>KFJ>QB L?GB@QP. ,FCBIBPP PE>MBP PFQQFKD LK LRO P@OBBKP QE>Q CILM
>OLRKA TEBK >CCB@QBA ?V CLO@BP FK QEBFO BKSFOLKJBKQ. 7E>Q FC TB @LRIA ?OB>QEB IFCB FKQL
QELPB PE>MBP? 7E>Q FC QELPB PE>MBP @LRIA IFSB ?V QEBFO LTK ORIBP? #>K PE>MBP E>SB ELMBP
>KA AOB>JP >KA CB>OP? 4EFP FP TE>Q TB >OB EBOB FK QEFP @E>MQBO QL ALdABSBILM 5IHCBCACIG
5;9BHG.
6.1 Forces from Within 6.1 Forces from Within
4EB QBOJ ,@?:9:8:@> ,209? ,@?:9:8:@> ,209? DBKBO>IIV OBCBOP QL >K BKQFQV QE>Q J>HBP FQP LTK @ELF@BP >?LRQ
ELT QL >@Q FK FQP BKSFOLKJBKQ TFQELRQ >KV FKCIRBK@B COLJ > IB>ABO LO DIL?>I MI>K. &LO RP,
g>@QFKDh TFII JB>K JLSFKD. 4EFP >AAFQFLK FP > PFDKFCF@>KQ @LK@BMQR>I IB>M. )KPQB>A LC > ?LU
PFQQFKD LK > ?LRKA>OV T>FQFKD QL ?B MRPEBA ?V >KLQEBO C>IIFKD ?LU, TB >OB KLT DLFKD QL
#E>MQBO 6. !RQLKLJLRP !DBKQP
260
ABPFDK > ?LU QE>Q E>P QEB >?FIFQV >KA gABPFOBh QL IB>M LRQ LC QEB T>V LC QE>Q LQEBO C>IIFKD ?LU, FC
FQ PL @ELLPBP. 7EFIB QEB @LK@BMQ LC CLO@BP QE>Q @LJB COLJ TFQEFK FP > J>GLO PEFCQ FK LRO ABPFDK
QEFKHFKD, LRO @LAB ?>PB TFII ?>OBIV @E>KDB, >P QEBPB ABPFOBP >KA >@QFLKP >OB PFJMIV
QE>Qd:CF79G.
(BOB >OB QEOBB HBV @LJMLKBKQP LC >RQLKLJLRP >DBKQP QE>Q TBfII T>KQ QL HBBM FK JFKA >P TB
?RFIA LRO BU>JMIBP.
i An autonomous agent has a An autonomous agent has a 7484?0/ 7484?0/ ability to perceive environment. ability to perceive environment. )Q J>HBP
PBKPB QE>Q > IFSFKD, ?OB>QEFKD ?BFKD PELRIA E>SB >K >T>OBKBPP LC FQP BKSFOLKJBKQ.
7E>Q ALBP QEFP JB>K CLO RP, ELTBSBO? !P TB ILLH >Q BU>JMIBP FK QEFP @E>MQBO, TB
TFII MLFKQ LRQ MOLDO>JJFKD QB@EKFNRBP CLO >IILTFKD L?GB@QP QL PQLOB OBCBOBK@BP QL
LQEBO L?GB@QP >KA QEBOBCLOB gMBO@BFSBh QEBFO BKSFOLKJBKQ. )QfP >IPL @OR@F>I QE>Q TB
@LKPFABO QEB TLOA @=A=H98 EBOB. !OB TB ABPFDKFKD >K >II-HKLTFKD OB@Q>KDIB QE>Q CIFBP
>OLRKA > 0OL@BPPFKD TFKALT, >T>OB LC BSBOVQEFKD BIPB FK QE>Q TFKALT? /O >OB TB
@OB>QFKD > PE>MB QE>Q @>K LKIV BU>JFKB >KV LQEBO L?GB@Q TFQEFK CFCQBBK MFUBIP LC
FQPBIC? /C @LROPB, QEBOB FP KL OFDEQ >KPTBO QL QEFP NRBPQFLK; FQ >II ABMBKAP. 7BfII
BUMILOB PLJB MLPPF?FIFQFBP >P TB JLSB CLOT>OA. &LO > PFJRI>QFLK QL CBBI JLOB
gK>QRO>I,h ELTBSBO, IFJFQ>QFLKP >OB > DLLA QEFKD. !K FKPB@Q, CLO BU>JMIB, J>V LKIV ?B
>T>OB LC QEB PFDEQP >KA PJBIIP QE>Q FJJBAF>QBIV PROOLRKA FQ. &LO > OB>I-TLOIA
@OB>QROB, TB @LRIA PQRAV QEB BU>@Q P@FBK@B LC QEBPB IFJFQ>QFLKP. ,R@HFIV CLO RP, TB
@>K GRPQ J>HB PQRCC RM >KA QOV FQ LRQ.
i An autonomous agent processes the information from its environment and An autonomous agent processes the information from its environment and
calculates an action. calculates an action. 4EFP TFII ?B QEB B>PV M>OQ CLO RP, >P QEB >@QFLK FP > CLO@B. 4EB
BKSFOLKJBKQ JFDEQ QBII QEB >DBKQ QE>Q QEBOBfP > ?FD P@>OV-ILLHFKD PE>OH PTFJJFKD
OFDEQ >Q FQ, >KA QEB >@QFLK TFII ?B > MLTBOCRI CLO@B FK QEB LMMLPFQB AFOB@QFLK.
i An autonomous agent has no leader. An autonomous agent has no leader. 4EFP QEFOA MOFK@FMIB FP PLJBQEFKD TB @>OB >
IFQQIB IBPP >?LRQ. !CQBO >II, FC VLR >OB ABPFDKFKD > PVPQBJ TEBOB FQ J>HBP PBKPB QL
E>SB > IB>ABO ?>OHFKD @LJJ>KAP >Q S>OFLRP BKQFQFBP, QEBK QE>QfP TE>Q VLRfII T>KQ QL
FJMIBJBKQ. .BSBOQEBIBPP, J>KV LC QEBPB BU>JMIBP TFII E>SB KL IB>ABO CLO >K
FJMLOQ>KQ OB>PLK. !P TB DBQ QL QEB BKA LC QEFP @E>MQBO >KA BU>JFKB DOLRM
?BE>SFLOP, TB TFII ILLH >Q ABPFDKFKD @LIIB@QFLKP LC >RQLKLJLRP >DBKQP QE>Q BUEF?FQ
QEB MOLMBOQFBP LC @LJMIBU PVPQBJPd FKQBIIFDBKQ >KA PQOR@QROBA DOLRM AVK>JF@P QE>Q
BJBODB KLQ COLJ > IB>ABO, ?RQ COLJ QEB IL@>I FKQBO>@QFLKP LC QEB BIBJBKQP
QEBJPBISBP.
)K QEB I>QB 1980P, @LJMRQBO P@FBKQFPQ #O>FD 2BVKLIAP (EQQM://TTT.OBA3A.@LJ/@TO/) ABSBILMBA
>IDLOFQEJF@ PQBBOFKD ?BE>SFLOP CLO >KFJ>QBA @E>O>@QBOP. 4EBPB ?BE>SFLOP >IILTBA FKAFSFAR>I
BIBJBKQP QL K>SFD>QB QEBFO AFDFQ>I BKSFOLKJBKQP FK > gIFCBIFHBh J>KKBO TFQE PQO>QBDFBP CLO
CIBBFKD, T>KABOFKD, >OOFSFKD, MROPRFKD, BS>AFKD, BQ@. 5PBA FK QEB @>PB LC > PFKDIB >RQLKLJLRP
>DBKQ, QEBPB ?BE>SFLOP >OB C>FOIV PFJMIB QL RKABOPQ>KA >KA FJMIBJBKQ. )K >AAFQFLK, ?V ?RFIAFKD
> PVPQBJ LC JRIQFMIB @E>O>@QBOP QE>Q PQBBO QEBJPBISBP >@@LOAFKD QL PFJMIB, IL@>IIV ?>PBA ORIBP,
PROMOFPFKD IBSBIP LC @LJMIBUFQV BJBODB. 4EB JLPQ C>JLRP BU>JMIB FP 2BVKLIAPfP g?LFAPh
JLABI CLO gCIL@HFKD/PT>OJFKDh ?BE>SFLO.
4EB .>QROB LC #LAB (S005)
261
6.2 Vehicles and Steering 6.2 Vehicles and Steering
.LT QE>Q TB RKABOPQ>KA QEB @LOB @LK@BMQP ?BEFKA >RQLKLJLRP >DBKQP, TB @>K ?BDFK
TOFQFKD QEB @LAB. 4EBOB >OB J>KV MI>@BP TEBOB TB @LRIA PQ>OQ. !OQFCF@F>I PFJRI>QFLKP LC >KQ
>KA QBOJFQB @LILKFBP >OB C>KQ>PQF@ ABJLKPQO>QFLKP LC PVPQBJP LC >RQLKLJLRP >DBKQP. (&LO
JLOB LK QEFP QLMF@, ) BK@LRO>DB VLR QL OB>A /IFH@9G, /9FA=H9G, 5B8 /F5::=7 &5AG ?V -FQ@EBI
2BPKF@H.) (LTBSBO, TB T>KQ QL ?BDFK ?V BU>JFKFKD >DBKQ ?BE>SFLOP QE>Q ?RFIA LK QEB TLOH
TBfSB ALKB FK QEB CFOPQ CFSB @E>MQBOP LC QEFP ?LLH: JLABIFKD JLQFLK TFQE SB@QLOP >KA AOFSFKD
JLQFLK TFQE CLO@BP. !KA PL FQfP QFJB QL OBK>JB LRO +IP?L @I>PP QE>Q ?B@>JB LRO .;LNC=F?
@I>PP LK@B >D>FK. 4EFP QFJB TB >OB DLFKD QL @>II FQ 3?BC=F?.
)K EFP 1999 M>MBO g3QBBOFKD "BE>SFLOP CLO !RQLKLJLRP #E>O>@QBOP,h 2BVKLIAP RPBP QEB TLOA
gSBEF@IBh QL ABP@OF?B EFP >RQLKLJLRP >DBKQP, PL TB TFII CLIILT PRFQ.
Why Vehicle? Why Vehicle?
)K 1986, )Q>IF>K KBROLP@FBKQFPQ >KA @V?BOKBQF@FPQ 6>IBKQFKL "O>FQBK?BOD ABP@OF?BA >
PBOFBP LC EVMLQEBQF@>I SBEF@IBP TFQE PFJMIB FKQBOK>I PQOR@QROBP FK EFP ?LLH 19<=7@9G:
!LD9F=A9BHG =B .MBH<9H=7 ,GM7<C@C;M. "O>FQBK?BOD >ODRBP QE>Q EFP BUQO>LOAFK>OFIV
PFJMIB JB@E>KF@>I SBEF@IBP J>KFCBPQ ?BE>SFLOP PR@E >P CB>O, >DDOBPPFLK, ILSB,
CLOBPFDEQ, >KA LMQFJFPJ. 2BVKLIAP QLLH EFP FKPMFO>QFLK COLJ "O>FQBK?BOD, >KA TBfII
Q>HB LROP COLJ 2BVKLIAP.
2BVKLIAP ABP@OF?BP QEB JLQFLK LC =895@=N98 SBEF@IBP (FAB>IFWBA ?B@>RPB TB >OB KLQ
@LK@BOKBA TFQE QEB >@QR>I BKDFKBBOFKD LC PR@E SBEF@IBP, ?RQ PFJMIV >PPRJB QE>Q QEBV BUFPQ
>KA TFII OBPMLKA QL LRO ORIBP) >P > PBOFBP LC QEOBB I>VBOPdAction Selection Action Selection, Steering Steering, >KA
Locomotion Locomotion.
1. A.?4:9 '070.?4:9. A.?4:9 '070.?4:9. ! SBEF@IB E>P > DL>I (LO DL>IP) >KA @>K PBIB@Q >K >@QFLK (LO >
@LJ?FK>QFLK LC >@QFLKP) ?>PBA LK QE>Q DL>I. 4EFP FP BPPBKQF>IIV TEBOB TB IBCQ LCC
TFQE >RQLKLJLRP >DBKQP. 4EB SBEF@IB Q>HBP > ILLH >Q FQP BKSFOLKJBKQ >KA
@>I@RI>QBP >K >@QFLK ?>PBA LK > ABPFOB: g) PBB > WLJ?FB J>O@EFKD QLT>OAP JB.
3FK@B ) ALKfQ T>KQ JV ?O>FKP QL ?B B>QBK, )fJ DLFKD QL CIBB COLJ QEB WLJ?FB.h 4EB
DL>I FP QL HBBM LKBfP ?O>FKP >KA QEB >@QFLK FP QL CIBB. 2BVKLIAPfP M>MBO ABP@OF?BP
J>KV DL>IP >KA >PPL@F>QBA >@QFLKP PR@E >P: PBBH > Q>ODBQ, >SLFA >K L?PQ>@IB, >KA
=F;MM 3?BC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
// 4B;N ?FM? >I Q? H??> NI ;>>?
#E>MQBO 6. !RQLKLJLRP !DBKQP
262
CLIILT > M>QE. )K > JLJBKQ, TBfII PQ>OQ ?RFIAFKD QEBPB BU>JMIBP LRQ TFQE 0OL@BPPFKD
@LAB.
2. '?00=492. '?00=492. /K@B >K >@QFLK E>P ?BBK PBIB@QBA, QEB SBEF@IB E>P QL @>I@RI>QB FQP KBUQ
JLSB. &LO RP, QEB KBUQ JLSB TFII ?B > CLO@B; JLOB PMB@FCF@>IIV, > PQBBOFKD CLO@B.
,R@HFIV, 2BVKLIAP E>P ABSBILMBA > PFJMIB PQBBOFKD CLO@B CLOJRI> QE>Q TBfII RPB
QEOLRDELRQ QEB BU>JMIBP FK QEFP @E>MQBO: >?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09? >?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09?
A07:.4?D A07:.4?D. 7BfII DBQ FKQL QEB ABQ>FIP LC QEFP CLOJRI> >KA TEV FQ TLOHP PL BCCB@QFSBIV FK
QEB KBUQ PB@QFLK.
3. !:.:8:?4:9. !:.:8:?4:9. &LO QEB JLPQ M>OQ, TBfOB DLFKD QL FDKLOB QEFP QEFOA I>VBO. )K QEB @>PB LC
CIBBFKD WLJ?FBP, QEB IL@LJLQFLK @LRIA ?B ABP@OF?BA >P gIBCQ CLLQ, OFDEQ CLLQ, IBCQ CLLQ,
OFDEQ CLLQ, >P C>PQ >P VLR @>K.h )K LRO 0OL@BPPFKD TLOIA, ELTBSBO, > OB@Q>KDIB LO @FO@IB
LO QOF>KDIBfP >@QR>I JLSBJBKQ >@OLPP > TFKALT FP FOOBIBS>KQ DFSBK QE>Q FQfP >II >K
FIIRPFLK FK QEB CFOPQ MI>@B. .BSBOQEBIBPP, QEFP FPKfQ QL P>V QE>Q VLR PELRIA FDKLOB
IL@LJLQFLK BKQFOBIV. 9LR TFII CFKA DOB>Q S>IRB FK QEFKHFKD >?LRQ QEB IL@LJLQFSB ABPFDK
LC VLRO SBEF@IB >KA ELT VLR @ELLPB QL >KFJ>QB FQ. 4EB BU>JMIBP FK QEFP @E>MQBO TFII
OBJ>FK SFPR>IIV ?>OB, >KA > DLLA BUBO@FPB TLRIA ?B QL BI>?LO>QB LK QEB >KFJ>QFLK
PQVIB d@LRIA VLR >AA PMFKKFKD TEBBIP LO LP@FII>QFKD M>AAIBP LO PERCCIFKD IBDP?
5IQFJ>QBIV, QEB JLPQ FJMLOQ>KQ I>VBO CLO VLR QL @LKPFABO FP #1dA7H=CB .9@97H=CB. 7E>Q >OB QEB
BIBJBKQP LC VLRO PVPQBJ >KA TE>Q >OB QEBFO DL>IP? )K QEFP @E>MQBO, TB >OB DLFKD QL ILLH >Q >
PBOFBP LC PQBBOFKD ?BE>SFLOP (F.B. >@QFLKP): PBBH, CIBB, CLIILT > M>QE, CLIILT > CILT CFBIA, CIL@H
TFQE VLRO KBFDE?LOP, BQ@. )QfP FJMLOQ>KQ QL OB>IFWB, ELTBSBO, QE>Q QEB MLFKQ LC RKABOPQ>KAFKD
ELT QL TOFQB QEB @LAB CLO QEBPB ?BE>SFLOP FP KLQ ?B@>RPB VLR PELRIA RPB QEBJ FK >II LC VLRO
MOLGB@QP. 2>QEBO, QEBPB >OB > PBQ LC ?RFIAFKD ?IL@HP, > CLRKA>QFLK COLJ TEF@E VLR @>K ABPFDK
>KA ABSBILM SBEF@IBP TFQE @OB>QFSB DL>IP >KA KBT >KA BU@FQFKD ?BE>SFLOP. !KA BSBK QELRDE
TB TFII QEFKH IFQBO>IIV FK QEFP @E>MQBO (CLIILT QE>Q MFUBI!), VLR PELRIA >IILT VLROPBIC QL QEFKH JLOB
>?PQO>@QIV (IFHB "O>FQBK?BOD). 7E>Q TLRIA FQ JB>K CLO VLRO SBEF@IB QL E>SB gILSBh LO gCB>Oh >P FQP
DL>I, FQP AOFSFKD CLO@B? &FK>IIV (>KA TBfII >AAOBPP QEFP I>QBO FK QEB @E>MQBO), VLR TLKfQ DBQ SBOV
C>O ?V ABSBILMFKD PFJRI>QFLKP TFQE LKIV LKB >@QFLK. 9BP, LRO CFOPQ BU>JMIB TFII ?B gPBBH >
Q>ODBQ.h "RQ CLO VLR QL ?B @OB>QFSBdQL J>HB QEBPB PQBBOFKD ?BE>SFLOP MCIF CKBdFQ TFII >II
@LJB ALTK QL JFUFKD >KA J>Q@EFKD JRIQFMIB >@QFLKP TFQEFK QEB P>JB SBEF@IB. 3L SFBT QEBPB
BU>JMIBP KLQ >P PFKDRI>O ?BE>SFLOP QL ?B BJRI>QBA, ?RQ >P MFB@BP LC > I>ODBO MRWWIB QE>Q VLR
TFII BSBKQR>IIV >PPBJ?IB.
6.3 The Steering Force 6.3 The Steering Force
7B @>K BKQBOQ>FK LROPBISBP ?V AFP@RPPFKD QEB QEBLOBQF@>I MOFK@FMIBP ?BEFKA >RQLKLJLRP
>DBKQP >KA PQBBOFKD >P JR@E >P TB IFHB, ?RQ TB @>KfQ DBQ >KVTEBOB TFQELRQ CFOPQ
RKABOPQ>KAFKD QEB @LK@BMQ LC > PQBBOFKD CLO@B. #LKPFABO QEB CLIILTFKD P@BK>OFL. ! SBEF@IB
JLSFKD TFQE SBIL@FQV ABPFOBP QL PBBH > Q>ODBQ.
4EB .>QROB LC #LAB (S005)
263
)QP DL>I >KA PR?PBNRBKQ >@QFLK FP QL PBBH
QEB Q>ODBQ FK &FDROB 6.1. )C VLR QEFKH ?>@H QL
#E>MQBO 2, VLR JFDEQ ?BDFK ?V J>HFKD QEB
Q>ODBQ >K >QQO>@QLO >KA >MMIV > DO>SFQ>QFLK>I
CLO@B QE>Q MRIIP QEB SBEF@IB QL QEB Q>ODBQ.
4EFP TLRIA ?B > MBOCB@QIV OB>PLK>?IB
PLIRQFLK, ?RQ @LK@BMQR>IIV FQfP KLQ TE>Q
TBfOB ILLHFKD CLO EBOB. 7B ALKfQ T>KQ QL
PFJMIV @>I@RI>QB > CLO@B QE>Q MRPEBP QEB
SBEF@IB QLT>OAP FQP Q>ODBQ; O>QEBO, TB >OB
>PHFKD QEB SBEF@IB QL J>HB >K FKQBIIFDBKQ
AB@FPFLK QL PQBBO QLT>OAP QEB Q>ODBQ ?>PBA
LK FQP MBO@BMQFLK LC FQP PQ>QB >KA
BKSFOLKJBKQ (F.B. ELT C>PQ >KA FK TE>Q
AFOB@QFLK FP FQ @ROOBKQIV JLSFKD). 4EB SBEF@IB PELRIA ILLH >Q ELT FQ ABPFOBP QL JLSB (> SB@QLO
MLFKQFKD QL QEB Q>ODBQ), @LJM>OB QE>Q DL>I TFQE ELT NRF@HIV FQ FP @ROOBKQIV JLSFKD (FQP
SBIL@FQV), >KA >MMIV > CLO@B >@@LOAFKDIV.
>?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09? A07:.4?D >?00=492 1:=.0 = /0>4=0/ A07:.4?D - .@==09? A07:.4?D
/O >P TB JFDEQ TOFQB FK 0OL@BPPFKD:
)K QEB >?LSB CLOJRI>, SBIL@FQV FP KL MOL?IBJ. !CQBO >II, TBfSB DLQ > S>OF>?IB CLO QE>Q.
(LTBSBO, TB ALKfQ E>SB QEB 89G=F98 J9@C7=HM; QEFP FP PLJBQEFKD TB E>SB QL @>I@RI>QB. ,BQfP
Q>HB > ILLH >Q &FDROB 6.2. )C TBfSB ABCFKBA QEB SBEF@IBfP DL>I >P gPBBHFKD QEB Q>ODBQ,h QEBK FQP
ABPFOBA SBIL@FQV FP > SB@QLO QE>Q MLFKQP COLJ FQP @ROOBKQ IL@>QFLK QL QEB Q>ODBQ IL@>QFLK.
!PPRJFKD > .3?=NIL Q>ODBQ, TB QEBK E>SB:
F64B?2 6.1
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
F64B?2 6.2
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
#E>MQBO 6. !RQLKLJLRP !DBKQP
264
"RQ QEFP FPKfQ M>OQF@RI>OIV OB>IFPQF@. 7E>Q FC TB E>SB > SBOV EFDE-OBPLIRQFLK TFKALT >KA QEB
Q>ODBQ FP QELRP>KAP LC MFUBIP >T>V? 3ROB, QEB SBEF@IB JFDEQ ABPFOB QL QBIBMLOQ FQPBIC FKPQ>KQIV QL
QEB Q>ODBQ IL@>QFLK TFQE > J>PPFSB SBIL@FQV, ?RQ QEFP TLKfQ J>HB CLO >K BCCB@QFSB >KFJ>QFLK.
7E>Q TB OB>IIV T>KQ QL P>V FP:
/<9 J9<=7@9 89G=F9G HC ACJ9 HCK5F8G H<9 H5F;9H 5H A5L=AIA GD998.
)K LQEBO TLOAP, QEB SB@QLO PELRIA MLFKQ COLJ IL@>QFLK QL Q>ODBQ >KA TFQE > J>DKFQRAB BNR>I QL
J>UFJRJ PMBBA (F.B. QEB C>PQBPQ QEB SBEF@IB @>K DL). 3L CFOPQ, TB KBBA QL J>HB PROB TB >AA >
S>OF>?IB QL LRO 3?BC=F? @I>PP QE>Q PQLOBP J>UFJRJ PMBBA.
4EBK, FK LRO ABPFOBA SBIL@FQV @>I@RI>QFLK, TB P@>IB >@@LOAFKD QL J>UFJRJ PMBBA.
0RQQFKD QEFP >II QLDBQEBO, TB @>K TOFQB > CRK@QFLK @>IIBA M??E() QE>Q OB@BFSBP > .3?=NIL Q>ODBQ
>KA @>I@RI>QBP > PQBBOFKD CLO@B QLT>OAP QE>Q Q>ODBQ.
=F;MM 3?BC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
Maximum speed @FI;N G;RMJ??>;
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
F64B?2 6.3
PIC> M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
Calculating the desired velocity to target at
max speed
>?MCL?>.GOFN(G;RMJ??>);
Reynoldss formula for steering force .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
4EB .>QROB LC #LAB (S005)
265
.LQB ELT FK QEB >?LSB CRK@QFLK TB CFKFPE ?V M>PPFKD QEB PQBBOFKD CLO@B FKQL ;JJFS$IL=?().
4EFP >PPRJBP QE>Q TB >OB ?>PFKD QEFP BU>JMIB LK QEB CLRKA>QFLK TB ?RFIQ FK #E>MQBO 2 (PBB
M>DB 66). (LTBSBO, VLR @LRIA GRPQ >P B>PFIV RPB QEB PQBBOFKD CLO@B TFQE "LU2$fP
;JJFS$IL=?() CRK@QFLK LO QLUF@IF?Pf ;>>$IL=?() CRK@QFLK.
3L TEV ALBP QEFP >II TLOH PL TBII? ,BQfP PBB TE>Q QEB PQBBOFKD CLO@B ILLHP IFHB OBI>QFSB QL QEB
SBEF@IB >KA Q>ODBQ IL@>QFLKP.
!D>FK, KLQF@B ELT QEFP FP KLQ >Q >II QEB P>JB CLO@B >P DO>SFQ>QFLK>I >QQO>@QFLK. 2BJBJ?BO LKB
LC LRO MOFK@FMIBP LC >RQLKLJLRP >DBKQP: !K >RQLKLJLRP >DBKQ E>P > @=A=H98 >?FIFQV QL
MBO@BFSB FQP BKSFOLKJBKQ. (BOB FP QE>Q >?FIFQV, PR?QIV BJ?BAABA FKQL 2BVKLIAPfP PQBBOFKD
CLOJRI>. )C QEB SBEF@IB TBOBKfQ JLSFKD >Q >II (WBOL SBIL@FQV), ABPFOBA JFKRP SBIL@FQV TLRIA ?B
BNR>I QL ABPFOBA. "RQ QEFP FP KLQ QEB @>PB. 4EB SBEF@IB FP >T>OB LC FQP LTK SBIL@FQV >KA FQP
PQBBOFKD CLO@B @LJMBKP>QBP >@@LOAFKDIV. 4EFP @OB>QBP > JLOB >@QFSB PFJRI>QFLK, >P QEB T>V
FK TEF@E QEB SBEF@IB JLSBP QLT>OAP QEB Q>ODBQP ABMBKAP LK QEB T>V FQ FP JLSFKD FK QEB CFOPQ
MI>@B.
)K >II LC QEFP BU@FQBJBKQ, ELTBSBO, TBfSB JFPPBA LKB I>PQ PQBM. 7E>Q PLOQ LC SBEF@IB FP QEFP? )P
FQ > PRMBO PIBBH O>@B @>O TFQE >J>WFKD E>KAIFKD? /O > DF>KQ ->@H QOR@H QE>Q KBBAP > ILQ LC
>AS>K@B KLQF@B QL QROK? ! DO>@BCRI M>KA>, LO > IRJ?BOFKD BIBME>KQ? /RO BU>JMIB @LAB, >P FQ
PQ>KAP, E>P KL CB>QROB QL >@@LRKQ CLO QEFP S>OF>?FIFQV FK PQBBOFKD >?FIFQV. 3QBBOFKD >?FIFQV @>K
?B @LKQOLIIBA ?V IFJFQFKD QEB J>DKFQRAB LC QEB PQBBOFKD CLO@B. ,BQfP @>II QE>Q IFJFQ QEB
gJ>UFJRJ CLO@Bh (LO G;R@IL=? CLO PELOQ). !KA PL CFK>IIV, TB E>SB:
Using our physics model and applying the
force to the objects acceleration
;JJFS$IL=?(MN??L);
W
F64B?2 6.4
=F;MM 3?BC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
#E>MQBO 6. !RQLKLJLRP !DBKQP
266
CLIILTBA ?V:
,FJFQFKD QEB PQBBOFKD CLO@B ?OFKDP RM >K FJMLOQ>KQ MLFKQ. 7B JRPQ >IT>VP OBJBJ?BO QE>Q FQfP
KLQ >@QR>IIV LRO DL>I QL DBQ QEB SBEF@IB QL QEB Q>ODBQ >P C>PQ >P MLPPF?IB. )C QE>Q TBOB QEB @>PB,
TB TLRIA GRPQ P>V gIL@>QFLK BNR>IP Q>ODBQh >KA QEBOB QEB SBEF@IB TLRIA ?B. /RO DL>I, >P
2BVKLIAP MRQP FQ, FP QL JLSB QEB SBEF@IB FK > gIFCBIFHB >KA FJMOLSFP>QFLK>I J>KKBO.h 7BfOB QOVFKD
QL J>HB FQ >MMB>O >P FC QEB SBEF@IB FP PQBBOFKD FQP T>V QL QEB Q>ODBQ, >KA PL FQfP RM QL RP QL MI>V
TFQE QEB CLO@BP >KA S>OF>?IBP LC QEB PVPQBJ QL PFJRI>QB > DFSBK ?BE>SFLO. &LO BU>JMIB, > I>ODB
J>UFJRJ PQBBOFKD CLO@B TLRIA OBPRIQ FK > SBOV AFCCBOBKQ M>QE QE>K > PJ>II LKB. /KB FP KLQ
FKEBOBKQIV ?BQQBO LO TLOPB QE>K QEB LQEBO; FQ ABMBKAP LK VLRO ABPFOBA BCCB@Q. (!KA LC @LROPB,
QEBPB S>IRBP KBBA KLQ ?B CFUBA >KA @LRIA @E>KDB ?>PBA LK LQEBO @LKAFQFLKP. 0BOE>MP >
SBEF@IB E>P EB>IQE: QEB EFDEBO QEB EB>IQE, QEB ?BQQBO FQ @>K PQBBO.)
(BOB FP QEB CRII 3?BC=F? @I>PP, FK@LOMLO>QFKD QEB OBPQ LC QEB BIBJBKQP COLJ QEB #E>MQBO 2
+IP?L L?GB@Q.
Maximum speed @FI;N G;RMJ??>;
Now we also have maximum force. @FI;N G;R@IL=?;
PIC> M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
Limit the magnitude of the steering force. MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
F64B?2 6.5
4EB .>QROB LC #LAB (S005)
267
Example 6.1: Seeking a target
=F;MM 3?BC=F? U
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
Additional variable for size @FI;N L;
@FI;N G;R@IL=?;
@FI;N G;RMJ??>;
3?BC=F?(@FI;N R, @FI;N S) U
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
P?FI=CNS = H?Q .3?=NIL(0,0);
FI=;NCIH = H?Q .3?=NIL(R,S);
L = 3.0;
Arbitrary values for maxspeed and force;
try varying these!
G;RMJ??> = 4;
G;R@IL=? = 0.1;
W
Our standard Euler integration motion
model
PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(G;RMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
Newtons second law; we could divide by
mass if we wanted.
PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
;==?F?L;NCIH.;>>(@IL=?);
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
268
Our seek steering force algorithm PIC> M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
PIC> >CMJF;S() U
Vehicle is a triangle pointing in the direction
of velocity; since it is drawn pointing up, we
rotate it an additional 90 degrees.
@FI;N NB?N; = P?FI=CNS.B?;>CHA2"() + .'/2;
@CFF(175);
MNLIE?(0);
JOMB+;NLCR();
NL;HMF;N?(FI=;NCIH.R,FI=;NCIH.S);
LIN;N?(NB?N;);
<?ACH0B;J?();
P?LN?R(0, -L*2);
P?LN?R(-L, L*2);
P?LN?R(L, L*2);
?H>0B;J?(!*-0#);
JIJ+;NLCR();
W
)JMIBJBKQ > gCIBBFKDh PQBBOFKD ?BE>SFLO (ABPFOBA SB@QLO FP FKSBOPB LC gPBBHh).
Exercise 6.1 Exercise 6.1
)JMIBJBKQ PBBHFKD > JLSFKD Q>ODBQ, LCQBK OBCBOOBA QL >P gMROPRFQ.h )K QEFP @>PB, VLRO
ABPFOBA SB@QLO TLKfQ MLFKQ QLT>OAP QEB L?GB@QfP @ROOBKQ IL@>QFLK, ?RQ O>QEBO FQP gCRQROBh
IL@>QFLK >P BUQO>MLI>QBA COLJ FQP @ROOBKQ SBIL@FQV. 7BfII PBB QEFP >?FIFQV CLO > SBEF@IB QL
gMOBAF@Q QEB CRQROBh FK I>QBO BU>JMIBP.
Exercise 6.2 Exercise 6.2
#OB>QB > PHBQ@E TEBOB > SBEF@IBfP J>UFJRJ CLO@B >KA J>UFJRJ PMBBA AL KLQ OBJ>FK
@LKPQ>KQ, ?RQ O>QEBO S>OV >@@LOAFKD QL BKSFOLKJBKQ>I C>@QLOP.
Exercise 6.3 Exercise 6.3
4EB .>QROB LC #LAB (S005)
269
6.4 Arriving Behavior 6.4 Arriving Behavior
!CQBO TLOHFKD CLO > ?FQ TFQE QEB PBBHFKD ?BE>SFLO, VLR MOL?>?IV >OB >PHFKD VLROPBIC, g7E>Q FC
) T>KQ JV SBEF@IB QL PILT ALTK >P FQ >MMOL>@EBP QEB Q>ODBQ?h "BCLOB TB @>K BSBK ?BDFK QL
>KPTBO QEFP NRBPQFLK, TB PELRIA ILLH >Q QEB OB>PLKP ?BEFKA TEV QEB PBBH ?BE>SFLO @>RPBP
QEB SBEF@IB QL CIV M>PQ QEB Q>ODBQ PL QE>Q FQ E>P QL QROK >OLRKA >KA DL ?>@H. ,BQfP @LKPFABO QEB
?O>FK LC > PBBHFKD SBEF@IB. 7E>Q FP FQ QEFKHFKD?
&O>JB 1: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 2: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 3: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 4: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 5: ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
BQ@.
4EB SBEF@IB FP PL DLPE A>OK BU@FQBA >?LRQ DBQQFKD QL QEB Q>ODBQ QE>Q FQ ALBPKfQ ?LQEBO QL
J>HB >KV FKQBIIFDBKQ AB@FPFLKP >?LRQ FQP PMBBA OBI>QFSB QL QEB Q>ODBQfP MOLUFJFQV. 7EBQEBO FQfP
C>O >T>V LO SBOV @ILPB, FQ >IT>VP T>KQP QL DL >P C>PQ >P MLPPF?IB.
)K PLJB @>PBP, QEFP FP QEB ABPFOBA ?BE>SFLO (FC > JFPPFIB FP CIVFKD >Q > Q>ODBQ, FQ PELRIA >IT>VP
QO>SBI >Q J>UFJRJ PMBBA.) (LTBSBO, FK J>KV LQEBO @>PBP (> @>O MRIIFKD FKQL > M>OHFKD PMLQ, >
?BB I>KAFKD LK > CILTBO), QEB SBEF@IBfP QELRDEQ MOL@BPP KBBAP QL @LKPFABO FQP PMBBA OBI>QFSB
QL QEB AFPQ>K@B COLJ FQP Q>ODBQ. &LO BU>JMIB:
&O>JB 1: )fJ SBOV C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 2: )fJ SBOV C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 3: )fJ PLJBTE>Q C>O >T>V. ) T>KQ QL DL >P C>PQ >P MLPPF?IB QLT>OAP QEB Q>ODBQ!
&O>JB 4: )fJ DBQQFKD @ILPB. ) T>KQ QL DL JLOB PILTIV QLT>OAP QEB Q>ODBQ!
&O>JB 5: )fJ >IJLPQ QEBOB. ) T>KQ QL DL SBOV PILTIV QLT>OAP QEB Q>ODBQ!
&O>JB 6: )fJ QEBOB. ) T>KQ QL PQLM!
(LT @>K TB FJMIBJBKQ QEFP g>OOFSFKDh ?BE>SFLO FK @LAB? ,BQfP OBQROK QL LRO M??E() CRK@QFLK
>KA CFKA QEB IFKB LC @LAB TEBOB TB PBQ QEB J>DKFQRAB LC QEB ABPFOBA SBIL@FQV.
F64B?2 6.6
F64B?2 6.7
#E>MQBO 6. !RQLKLJLRP !DBKQP
270
)K %U>JMIB 6.1, QEB J>DKFQRAB LC QEB ABPFOBA SB@QLO FP >IT>VP gJ>UFJRJh PMBBA.
7E>Q FC TB FKPQB>A P>FA QEB ABPFOBA SBIL@FQV FP BNR>I QL E>IC QEB AFPQ>K@B?
7EFIB QEFP KF@BIV ABJLKPQO>QBP LRO DL>I LC > ABPFOBA PMBBA QFBA QL LRO AFPQ>K@B COLJ QEB
Q>ODBQ, FQfP KLQ M>OQF@RI>OIV OB>PLK>?IB. !CQBO >II, 10 MFUBIP >T>V FP O>QEBO @ILPB >KA > ABPFOBA
PMBBA LC 5 FP O>QEBO I>ODB. 3LJBQEFKD IFHB > ABPFOBA SBIL@FQV TFQE > J>DKFQRAB LC 5% LC QEB
AFPQ>K@B TLRIA TLOH JR@E ?BQQBO.
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
F64B?2 6.8
F64B?2 6.9
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.>CP(2);
4EB .>QROB LC #LAB (S005)
271
2BVKLIAP ABP@OF?BP > JLOB PLMEFPQF@>QBA >MMOL>@E. ,BQfP FJ>DFKB > @FO@IB >OLRKA QEB Q>ODBQ
TFQE > DFSBK O>AFRP. )C QEB SBEF@IB FP TFQEFK QE>Q @FO@IB, FQ PILTP ALTKd>Q QEB BADB LC QEB
@FO@IB, FQP ABPFOBA PMBBA FP J>UFJRJ PMBBA, >KA >Q QEB Q>ODBQ FQPBIC, FQP ABPFOBA PMBBA FP 0.
)K LQEBO TLOAP, FC QEB AFPQ>K@B COLJ QEB Q>ODBQ FP IBPP QE>K O, QEB ABPFOBA PMBBA FP ?BQTBBK 0
>KA J>UFJRJ PMBBA J>MMBA >@@LOAFKD QL QE>Q AFPQ>K@B.
Example 6.2: Arrive steering behavior
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.GOFN(0.05);
F64B?2 6.10
PIC> ;LLCP?(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
The distance is the magnitude of the vector
pointing from location to target.
@FI;N > = >?MCL?>.G;A();
>?MCL?>.HILG;FCT?();
#E>MQBO 6. !RQLKLJLRP !DBKQP
272
4EB >OOFSB ?BE>SFLO FP > DOB>Q ABJLKPQO>QFLK LC QEB J>DF@ LC gABPFOBA JFKRP SBIL@FQV.h ,BQfP
BU>JFKB QEFP JLABI >D>FK OBI>QFSB QL ELT TB @>I@RI>QBA CLO@BP FK B>OIFBO @E>MQBOP. )K QEB
gDO>SFQ>QFLK>I >QQO>@QFLKh BU>JMIBP, QEB CLO@B >IT>VP MLFKQBA AFOB@QIV COLJ QEB L?GB@Q QL QEB
Q>ODBQ (QEB BU>@Q AFOB@QFLK LC QEB ABPFOBA SBIL@FQV), TEBQEBO QEB CLO@B T>P PQOLKD LO TB>H.
4EB PQBBOFKD CRK@QFLK, ELTBSBO, P>VP: g) E>SB QEB >?FIFQV QL MBO@BFSB QEB BKSFOLKJBKQ.h 4EB
CLO@B FPKfQ ?>PBA LK GRPQ QEB ABPFOBA SBIL@FQV, ?RQ LK QEB ABPFOBA SBIL@FQV OBI>QFSB QL QEB @ROOBKQ
SBIL@FQV. /KIV QEFKDP QE>Q >OB >IFSB @>K HKLT QEBFO @ROOBKQ SBIL@FQV. ! ?LU C>IIFKD LCC > Q>?IB
ALBPKfQ HKLT FQfP C>IIFKD. ! @EBBQ>E @E>PFKD FQP MOBV, ELTBSBO, HKLTP FQ FP @E>PFKD.
4EB PQBBOFKD CLO@B, QEBOBCLOB, FP BPPBKQF>IIV > J>KFCBPQ>QFLK LC QEB @ROOBKQ SBIL@FQVfP 0==:= 0==:=: ")fJ
PRMMLPBA QL ?B DLFKD QEFP C>PQ FK QEFP AFOB@QFLK, ?RQ )fJ >@QR>IIV DLFKD QEFP C>PQ FK >KLQEBO
AFOB@QFLK. -V BOOLO FP QEB AFCCBOBK@B ?BQTBBK TEBOB ) T>KQ QL DL >KA TEBOB ) >J @ROOBKQIV
DLFKD." 4>HFKD QE>Q BOOLO >KA >MMIVFKD FQ >P > PQBBOFKD CLO@B OBPRIQP FK JLOB AVK>JF@, IFCBIFHB
PFJRI>QFLKP. 7FQE DO>SFQ>QFLK>I >QQO>@QFLK, VLR TLRIA KBSBO E>SB > CLO@B MLFKQFKD >T>V COLJ QEB
Q>ODBQ, KL J>QQBO ELT @ILPB. "RQ TFQE >OOFSFKD SF> PQBBOFKD, FC VLR >OB JLSFKD QLL C>PQ QLT>OAP
QEB Q>ODBQ, QEB BOOLO TLRIA >@QR>IIV QBII VLR QL PILT ALTK!
If we are closer than 100 pixels... C@ (> < 100) U
...set the magnitude according to how close
we are.
@FI;N G = G;J(>,0,100,0,G;RMJ??>);
>?MCL?>.GOFN(G);
W ?FM? U
Otherwise, proceed at maximum speed. >?MCL?>.GOFN(G;RMJ??>);
W
The usual steering = desired - velocity .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
F64B?2 6.11
4EB .>QROB LC #LAB (S005)
273
6.5 Your Own Desires: Desired Velocity 6.5 Your Own Desires: Desired Velocity
4EB CFOPQ QTL BU>JMIBP TBfSB @LSBOBAdPBBH >KA >OOFSBd?LFI ALTK QL @>I@RI>QFKD > PFKDIB
SB@QLO CLO B>@E ?BE>SFLO: QEB 89G=F98 SBIL@FQV. !KA FK C>@Q, BSBOV PFKDIB LKB LC 2BVKLIAPfP
PQBBOFKD ?BE>SFLOP CLIILTP QEFP P>JB M>QQBOK. )K QEFP @E>MQBO, TBfOB DLFKD QL T>IH QEOLRDE
PBSBO>I JLOB LC 2BVKLIAPfP ?BE>SFLOPdCILT CFBIA, M>QE-CLIILTFKD, CIL@HFKD. &FOPQ, ELTBSBO, )
T>KQ QL BJME>PFWB >D>FK QE>Q QEBPB >OB 9L5AD@9GdABJLKPQO>QFLKP LC @LJJLK PQBBOFKD
?BE>SFLOP QE>Q >OB RPBCRI FK MOL@BARO>I >KFJ>QFLK. 4EBV >OB KLQ QEB ?B->II >KA BKA->II LC
TE>Q MCI @>K AL. !P ILKD >P VLR @>K @LJB RM TFQE > SB@QLO QE>Q ABP@OF?BP > SBEF@IBfP
89G=F98 SBIL@FQV, QEBK VLR E>SB @OB>QBA VLRO LTK PQBBOFKD ?BE>SFLO.
,BQfP PBB ELT 2BVKLIAP ABCFKBP QEB ABPFOBA SBIL@FQV CLO EFP T>KABOFKD ?BE>SFLO.
I,.;12?6;4 6@ . AF=2 <3 ?.;1<: @A22?6;4 D5605 5.@ @<:2 9<;4 A2?: <?12?: A52 @A22?6;4
16?20A6<; <; <;2 3?.:2 6@ ?29.A21 A< A52 @A22?6;4 16?20A6<; <; A52 ;2EA 3?.:2. *56@ =?<1B02@
:<?2 6;A2?2@A6;4 :<A6<; A5.;, 3<? 2E.:=92, @6:=9F 42;2?.A6;4 . ?.;1<: @A22?6;4 16?20A6<;
2.05 3?.:2.J
HC?.64 (2F;<91@ (5AA=://DDD.?2131.0<:/0D?/@A22?/,.;12?.5A:9)
&LO 2BVKLIAP, QEB DL>I LC T>KABOFKD FP KLQ
PFJMIV O>KALJ JLQFLK, ?RQ O>QEBO > PBKPB
LC JLSFKD FK LKB AFOB@QFLK CLO > IFQQIB TEFIB,
T>KABOFKD LCC QL QEB KBUQ CLO > IFQQIB ?FQ, >KA
PL LK >KA PL CLOQE. 3L ELT ALBP 2BVKLIAP
@>I@RI>QB > ABPFOBA SB@QLO QL >@EFBSB PR@E
>K BCCB@Q?
&FDROB 6.12 FIIRPQO>QBP ELT QEB SBEF@IB
MOBAF@QP FQP CRQROB IL@>QFLK >P > CFUBA
AFPQ>K@B FK COLKQ LC FQ (FK QEB AFOB@QFLK LC FQP
SBIL@FQV), AO>TP > @FO@IB TFQE O>AFRP L >Q QE>Q
IL@>QFLK, >KA MF@HP > O>KALJ MLFKQ >ILKD
QEB @FO@RJCBOBK@B LC QEB @FO@IB. 4E>Q
O>KALJ MLFKQ JLSBP O>KALJIV >OLRKA QEB
@FO@IB FK B>@E CO>JB LC >KFJ>QFLK. !KA QE>Q
O>KALJ MLFKQ FP QEB SBEF@IBfP Q>ODBQ, FQP ABPFOBA SB@QLO MLFKQFKD FK QE>Q AFOB@QFLK.
3LRKAP > ?FQ >?PROA, OFDEQ? /O, >Q QEB SBOV IB>PQ, O>QEBO >O?FQO>OV. )K C>@Q, QEFP FP > SBOV @IBSBO
>KA QELRDEQCRI PLIRQFLKdFQ RPBP O>KALJKBPP QL AOFSB > SBEF@IBfP PQBBOFKD, ?RQ @LKPQO>FKP QE>Q
O>KALJKBPP >ILKD QEB M>QE LC > @FO@IB QL HBBM QEB SBEF@IBfP JLSBJBKQ COLJ >MMB>OFKD
GFQQBOV, >KA, TBII, O>KALJ.
"RQ QEB PBBJFKDIV O>KALJ >KA >O?FQO>OV K>QROB LC QEFP PLIRQFLK PELRIA AOFSB ELJB QEB MLFKQ
)fJ QOVFKD QL J>HBdQEBPB >OB J>AB-RM ?BE>SFLOP FKPMFOBA ?V OB>I-IFCB JLQFLK. 9LR @>K GRPQ
>P B>PFIV @LK@L@Q PLJB BI>?LO>QB P@BK>OFL QL @LJMRQB > ABPFOBA SBIL@FQV VLROPBIC. !KA VLR
PELRIA.
F64B?2 6.12
#E>MQBO 6. !RQLKLJLRP !DBKQP
274
,BQfP P>V TB T>KQ QL @OB>QB > PQBBOFKD ?BE>SFLO @>IIBA gPQ>V TFQEFK T>IIP.h 7BfII ABCFKB QEB
ABPFOBA SBIL@FQV >P:
I1 , A034.70 .:80> B4?349 , /4>?,9.0 I1 , A034.70 .:80> B4?349 , /4>?,9.0 A :1 , B,77, 4? /0>4=0> ?: 8:A0 ,? 8,C48@8 >;00/ 49 :1 , B,77, 4? /0>4=0> ?: 8:A0 ,? 8,C48@8 >;00/ 49
?30 :;;:>4?0 /4=0.?4:9 :1 ?30 B,77. ?30 :;;:>4?0 /4=0.?4:9 :1 ?30 B,77.
)C TB ABCFKB QEB T>IIP LC QEB PM>@B >P QEB BADBP LC > 0OL@BPPFKD TFKALT >KA QEB AFPQ>K@B > >P
25, QEB @LAB FP O>QEBO PFJMIB.
7OFQB QEB @LAB CLO 2BVKLIAPfP T>KABOFKD ?BE>SFLO. 5PB MLI>O @LLOAFK>QBP QL @>I@RI>QB
QEB SBEF@IBfP Q>ODBQ >ILKD > @FO@RI>O M>QE.
Exercise 6.4 Exercise 6.4
F64B?2 6.13
4EB .>QROB LC #LAB (S005)
275
Example 6.3: Stay within walls steering behavior
C@ (FI=;NCIH. R > 25) U
.3?=NIL >?MCL?> = H?Q .3?=NIL(G;RMJ??>,P?FI=CNS.S);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>, P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
Make a desired vector that retains the y
direction of the vehicle but points the x
direction directly away from the windows
left edge.
#LJB RM TFQE VLRO LTK >O?FQO>OV P@EBJB CLO @>I@RI>QFKD > ABPFOBA SBIL@FQV.
Exercise 6.5 Exercise 6.5
6.6 Flow Fields 6.6 Flow Fields
.LT ?>@H QL QEB Q>PH >Q E>KA. ,BQfP BU>JFKB > @LRMIB JLOB LC 2BVKLIAPfP PQBBOFKD
?BE>SFLOP. &FOPQ, 17:B 1407/ 1:77:B492 17:B 1407/ 1:77:B492. 7E>Q FP > CILT CFBIA? 4EFKH LC VLRO 0OL@BPPFKD TFKALT
>P > DOFA. )K B>@E @BII LC QEB DOFA IFSBP >K >OOLT MLFKQFKD FK PLJB AFOB@QFLKdVLR HKLT, >
SB@QLO. !P > SBEF@IB JLSBP >OLRKA QEB P@OBBK, FQ >PHP, g(BV, TE>Q >OOLT FP ?BKB>QE JB?
4E>QfP JV ABPFOBA SBIL@FQV!h
#E>MQBO 6. !RQLKLJLRP !DBKQP
276
2BVKLIAPfP CILT CFBIA CLIILTFKD BU>JMIB E>P QEB SBEF@IB MOBAF@QFKD FQP CRQROB IL@>QFLK >KA
CLIILTFKD QEB SB@QLO >Q QE>Q PMLQ, ?RQ CLO PFJMIF@FQVfP P>HB, TBfII E>SB QEB SBEF@IB PFJMIV ILLH QL
QEB SB@QLO >Q FQP @ROOBKQ IL@>QFLK.
"BCLOB TB @>K TOFQB QEB >AAFQFLK>I @LAB CLO LRO 3?BC=F? @I>PP, TBfII KBBA QL ?RFIA > @I>PP QE>Q
ABP@OF?BP QEB CILT CFBIA FQPBIC, QEB DOFA LC SB@QLOP. ! QTL-AFJBKPFLK>I >OO>V FP > @LKSBKFBKQ A>Q>
PQOR@QROB FK TEF@E QL PQLOB > DOFA LC FKCLOJ>QFLK. )C VLR >OB KLQ C>JFIF>O TFQE 2$ >OO>VP, )
PRDDBPQ OBSFBTFKD QEFP LKIFKB 0OL@BPPFKD QRQLOF>I: 2$ >OO>V (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/
2A>OO>V/). 4EB 2$ >OO>V FP @LKSBKFBKQ ?B@>RPB TB OBCBOBK@B B>@E BIBJBKQ TFQE QTL FKAF@BP,
TEF@E TB @>K QEFKH LC >P @LIRJKP >KA OLTP.
.LQF@B ELT TB >OB ABCFKFKD > QEFOA S>OF>?IB @>IIBA L?MIFONCIH >?LSB. 7E>Q FP QEFP S>OF>?IB?
,BQfP P>V TB E>SB > 0OL@BPPFKD TFKALT QE>Q FP 200 MFUBIP TFAB ?V 200 MFUBIP EFDE. 7B @LRIA
J>HB > CILT CFBIA QE>Q E>P > .3?=NIL L?GB@Q CLO BSBOV PFKDIB MFUBI, LO 40,000 .3?=NILP (200 *
200). 4EFP FPKfQ QBOOF?IV RKOB>PLK>?IB, ?RQ FK LRO @>PB, FQfP LSBOHFII. 7B ALKfQ KBBA > .3?=NIL
F64B?2 6.14
=F;MM $FIQ$C?F> U
Declaring a 2D array of PVectors .3?=NIL8989 @C?F>;
How many columns and how many rows in
the grid?
CHN =IFM, LIQM;
Resolution of grid relative to window width
and height in pixels
CHN L?MIFONCIH;
4EB .>QROB LC #LAB (S005)
277
CLO BSBOV PFKDIB MFUBI; TB @>K >@EFBSB QEB P>JB BCCB@Q ?V E>SFKD, P>V, LKB BSBOV QBK MFUBIP
(20 * 20 = 400). 7B RPB QEFP OBPLIRQFLK QL ABCFKB QEB KRJ?BO LC @LIRJKP >KA OLTP ?>PBA LK
QEB PFWB LC QEB TFKALT AFSFABA ?V OBPLIRQFLK:
.LT QE>Q TBfSB PBQ RM QEB CILT CFBIAfP A>Q> PQOR@QROBP, FQfP QFJB QL @LJMRQB QEB SB@QLOP FK QEB
CILT CFBIA FQPBIC. (LT AL TB AL QE>Q? (LTBSBO TB CBBI IFHB FQ! 0BOE>MP TB T>KQ QL E>SB BSBOV
SB@QLO FK QEB CILT CFBIA MLFKQFKD QL QEB OFDEQ.
/O MBOE>MP TB T>KQ QEB SB@QLOP QL MLFKQ FK O>KALJ AFOB@QFLKP.
$FIQ$C?F>() U
L?MIFONCIH = 10;
Total columns equals width divided by
resolution.
=IFM = QC>NB/L?MIFONCIH;
Total rows equals height divided by
resolution.
LIQM = B?CABN/L?MIFONCIH;
@C?F> = H?Q .3?=NIL8=IFM98LIQM9;
W
F64B?2 6.15
Using a nested loop to hit every column
and every row of the flow field
@IL (CHN C = 0; C < =IFM; C++) U
@IL (CHN D = 0; D < LIQM; D++) U
Arbitrary decision to make each vector
point to the right
@C?F>8C98D9 = H?Q .3?=NIL(1,0);
W
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
278
7E>Q FC TB RPB 2$ 0BOIFK KLFPB (J>MMBA QL >K >KDIB)?
F64B?2 6.16
@IL (CHN C = 0; C < =IFM; C++) U
@IL (CHN D = 0; D < LIQM; D++) U
A random PVector @C?F>8C98D9 = .3?=NIL.2"();
W
W
F64B?2 6.17
@FI;N RI@@ = 0;
@IL (CHN C = 0; C < =IFM; C++) U
@FI;N SI@@ = 0;
@IL (CHN D = 0; D < LIQM; D++) U
@FI;N NB?N; = G;J(HICM?(RI@@,SI@@),0,1,0,14-:.');
@C?F>8C98D9 = H?Q .3?=NIL(=IM(NB?N;),MCH(NB?N;));
SI@@ += 0.1;
W
RI@@ += 0.1;
W
Noise
4EB .>QROB LC #LAB (S005)
279
.LT TBfOB DBQQFKD PLJBTEBOB. &ILT CFBIAP @>K ?B RPBA CLO PFJRI>QFKD S>OFLRP BCCB@QP, PR@E
>P >K FOOBDRI>O DRPQ LC TFKA LO QEB JB>KABOFKD M>QE LC > OFSBO. #>I@RI>QFKD QEB AFOB@QFLK LC
VLRO SB@QLOP RPFKD 0BOIFK KLFPB FP LKB T>V QL >@EFBSB PR@E >K BCCB@Q. /C @LROPB, QEBOBfP KL
g@LOOB@Qh T>V QL @>I@RI>QB QEB SB@QLOP LC > CILT CFBIA; FQfP OB>IIV RM QL VLR QL AB@FAB TE>Q
VLRfOB ILLHFKD QL PFJRI>QB.
.LT QE>Q TB E>SB > QTL-AFJBKPFLK>I >OO>V PQLOFKD >II LC QEB CILT CFBIA SB@QLOP, TB KBBA >
T>V CLO > SBEF@IB QL ILLH RM FQP ABPFOBA SB@QLO FK QEB CILT CFBIA. ,BQfP P>V TB E>SB > SBEF@IB
QE>Q IFSBP >Q > .3?=NIL: FQP IL@>QFLK. 7B CFOPQ KBBA QL AFSFAB ?V QEB OBPLIRQFLK LC QEB DOFA. &LO
BU>JMIB, FC QEB OBPLIRQFLK FP 10 >KA QEB SBEF@IB FP >Q (100,50), TB KBBA QL ILLH RM @LIRJK 10
>KA OLT 5.
"B@>RPB > SBEF@IB @LRIA QEBLOBQF@>IIV T>KABO LCC QEB 0OL@BPPFKD TFKALT, FQfP >IPL RPBCRI CLO
RP QL BJMILV QEB =IHMNL;CH() CRK@QFLK QL J>HB PROB TB ALKfQ ILLH LRQPFAB LC QEB CILT CFBIA
>OO>V. (BOB FP > CRK@QFLK TBfII @>II FIIEOJ() QE>Q DLBP FK QEB $FIQ$C?F> @I>PPdFQ OB@BFSBP >
.3?=NIL (MOBPRJ>?IV QEB IL@>QFLK LC LRO SBEF@IB) >KA OBQROKP QEB @LOOBPMLKAFKD CILT CFBIA
.3?=NIL CLO QE>Q IL@>QFLK.
7OFQB QEB @LAB QL @>I@RI>QB > .3?=NIL >Q BSBOV IL@>QFLK FK QEB CILT CFBIA QE>Q MLFKQP
QLT>OAP QEB @BKQBO LC > TFKALT.
.3?=NIL P = H?Q .3?=NIL(::::::::::::,::::::::::::);
P.::::::::::::::();
@C?F>8C98D9 = P;
Exercise 6.6 Exercise 6.6
CHN =IFOGH = CHN(FI=;NCIH.R/L?MIFONCIH);
CHN LIQ = CHN(FI=;NCIH.S/L?MIFONCIH);
.3?=NIL FIIEOJ(.3?=NIL FIIEOJ) U
Using constrain()
CHN =IFOGH = CHN(=IHMNL;CH(FIIEOJ.R/L?MIFONCIH,0,=IFM-1));
CHN LIQ = CHN(=IHMNL;CH(FIIEOJ.S/L?MIFONCIH,0,LIQM-1));
#E>MQBO 6. !RQLKLJLRP !DBKQP
280
"BCLOB TB JLSB LK QL QEB 3?BC=F? @I>PP, IBQfP Q>HB > ILLH >Q QEB $FIQ$C?F> @I>PP >II QLDBQEBO.
3L IBQfP >PPRJB TB E>SB > $FIQ$C?F> L?GB@Q @>IIBA gCILTh. 5PFKD QEB FIIEOJ() CRK@QFLK
>?LSB, LRO SBEF@IB @>K QEBK OBQOFBSB > ABPFOBA SB@QLO COLJ QEB CILT CFBIA >KA RPB 2BVKLIAPfP
ORIBP (PQBBOFKD = ABPFOBA - SBIL@FQV) QL @>I@RI>QB > PQBBOFKD CLO@B.
Note the use of get() to ensure we return a
copy of the PVector.
L?NOLH @C?F>8=IFOGH98LIQ9.A?N();
W
=F;MM $FIQ$C?F> U
A flow field is a two-dimensional array of
PVectors.
.3?=NIL8989 @C?F>;
CHN =IFM, LIQM;
CHN L?MIFONCIH;
$FIQ$C?F>(CHN L) U
L?MIFONCIH = L;
Determine the number of columns and
rows.
=IFM = QC>NB/L?MIFONCIH;
LIQM = B?CABN/L?MIFONCIH;
@C?F> = H?Q .3?=NIL8=IFM98LIQM9;
CHCN();
W
PIC> CHCN() U
@FI;N RI@@ = 0;
@IL (CHN C = 0; C < =IFM; C++) U
@FI;N SI@@ = 0;
@IL (CHN D = 0; D < LIQM; D++) U
@FI;N NB?N; = G;J(HICM?(RI@@,SI@@),0,1,0,14-:.');
In this example, we use Perlin noise to seed
the vectors.
@C?F>8C98D9 = H?Q .3?=NIL(=IM(NB?N;),MCH(NB?N;));
SI@@ += 0.1;
W
RI@@ += 0.1;
W
W
Polar to Cartesian coordinate transformation
to get x and y components of the vector
A function to return a PVector based on a
location
.3?=NIL FIIEOJ(.3?=NIL FIIEOJ) U
CHN =IFOGH = CHN(=IHMNL;CH(FIIEOJ.R/L?MIFONCIH,0,=IFM-1));
CHN LIQ = CHN(=IHMNL;CH(FIIEOJ.S/L?MIFONCIH,0,LIQM-1));
L?NOLH @C?F>8=IFOGH98LIQ9.A?N();
W
W
4EB .>QROB LC #LAB (S005)
281
Example 6.4: Flow field following
=F;MM 3?BC=F? U
PIC> @IFFIQ($FIQ$C?F> @FIQ) U
What is the vector at that spot in the flow
field?
.3?=NIL >?MCL?> = @FIQ.FIIEOJ(FI=;NCIH);
>?MCL?>.GOFN(G;RMJ??>);
Steering is desired minus velocity .3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,
P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
!A>MQ QEB CILT CFBIA BU>JMIB PL QE>Q QEB .3?=NILP @E>KDB LSBO QFJB. ((FKQ: QOV RPFKD
QEB QEFOA AFJBKPFLK LC 0BOIFK KLFPB!)
Exercise 6.7 Exercise 6.7
#>K VLR PBBA > CILT CFBIA COLJ > .'G;A?? &LO BU>JMIB, QOV E>SFKD QEB .3?=NILP MLFKQ
COLJ A>OH QL IFDEQ @LILOP (LO SF@B SBOP>).
Exercise 6.8 Exercise 6.8
6.7 The Dot Product 6.7 The Dot Product
)K > JLJBKQ, TBfOB DLFKD QL TLOH QEOLRDE QEB >IDLOFQEJ (>ILKD TFQE >@@LJM>KVFKD
J>QEBJ>QF@P) >KA @LAB CLO >KLQEBO LC #O>FD 2BVKLIAPfP PQBBOFKD ?BE>SFLOP: 0>QE &LIILTFKD
(EQQM://TTT.OBA3A.@LJ/@TO/PQBBO/0>QE&LIILT.EQJI). "BCLOB TB @>K AL QEFP, ELTBSBO, TB E>SB
#E>MQBO 6. !RQLKLJLRP !DBKQP
282
QL PMBKA PLJB QFJB IB>OKFKD >?LRQ >KLQEBO MFB@B LC SB@QLO J>QE QE>Q TB PHFMMBA FK #E>MQBO
1dQEB ALQ MOLAR@Q. 7B E>SBKfQ KBBABA FQ VBQ, ?RQ FQfP IFHBIV DLFKD QL MOLSB NRFQB RPBCRI CLO VLR
(?BVLKA GRPQ QEFP M>QE-CLIILTFKD BU>JMIB), PL TBfII DL LSBO FQ FK ABQ>FI KLT.
2BJBJ?BO >II QEB ?>PF@ SB@QLO J>QE TB @LSBOBA FK #E>MQBO 1? !AA, PR?QO>@Q, JRIQFMIV, >KA
AFSFAB?
.LQF@B ELT FK QEB >?LSB AF>DO>J, SB@QLO JRIQFMIF@>QFLK FKSLISBP JRIQFMIVFKD > SB@QLO ?V >
P@>I>O S>IRB. 4EFP J>HBP PBKPB; TEBK TB T>KQ > SB@QLO QL ?B QTF@B >P I>ODB (?RQ C>@FKD QEB
P>JB AFOB@QFLK), TB JRIQFMIV FQ ?V 2. 7EBK TB T>KQ FQ QL ?B E>IC QEB PFWB, TB JRIQFMIV FQ ?V 0.5.
(LTBSBO, QEBOB >OB QTL LQEBO AI@H=D@=75H=CB-@=?9 LMBO>QFLKP TFQE SB@QLOP QE>Q >OB RPBCRI FK
@BOQ>FK P@BK>OFLPdQEB ALQ MOLAR@Q >KA QEB @OLPP MOLAR@Q. &LO KLT TBfOB DLFKD QL CL@RP LK QEB
ALQ MOLAR@Q, TEF@E FP ABCFKBA >P CLIILTP. !PPRJB SB@QLOP A

>KA B

:
A

= (
a
x
, a
y
)
B

= (
b
x
, b
y
)
4(% $/4 02/$5#4: A

=
a
x
b
x
+ a
y
b
y
&LO BU>JMIB, FC TB E>SB QEB CLIILTFKD QTL SB@QLOP:
A

= (3, 5)
B

= (10, 1)
A

= 3 * 10 + 5 * 1 = 30 + 5 = 35
.LQF@B QE>Q QEB OBPRIQ LC QEB ALQ MOLAR@Q FP > P@>I>O S>IRB (> PFKDIB KRJ?BO) >KA KLQ > SB@QLO.
)K 0OL@BPPFKD, QEFP TLRIA QO>KPI>QB QL:
F64B?2 6.18
4EB .>QROB LC #LAB (S005)
283
!KA FC TB TBOB QL ILLH FK QEB DRQP LC QEB .3?=NIL PLRO@B, TBfA CFKA > MOBQQV PFJMIB
FJMIBJBKQ>QFLK LC QEFP CRK@QFLK:
4EFP FP PFJMIB BKLRDE, ?RQ TEV AL TB KBBA QEB ALQ MOLAR@Q, >KA TEBK FP FQ DLFKD QL ?B
RPBCRI CLO RP FK @LAB?
/KB LC QEB JLOB @LJJLK RPBP LC QEB ALQ MOLAR@Q FP QL CFKA QEB >KDIB ?BQTBBK QTL SB@QLOP.
!KLQEBO T>V FK TEF@E QEB ALQ MOLAR@Q @>K ?B BUMOBPPBA FP:
A

= A

cos()
)K LQEBO TLOAP, ! ALQ " FP BNR>I QL QEB J>DKFQRAB LC ! QFJBP J>DKFQRAB LC " QFJBP @LPFKB LC
QEBQ> (TFQE QEBQ> ABCFKBA >P H<9 5B;@9 69HK99B H<9 HKC J97HCFG A 5B8 B).
4EB QTL CLOJRI>P CLO ALQ MOLAR@Q @>K ?B ABOFSBA COLJ LKB >KLQEBO TFQE QOFDLKLJBQOV
(EQQM://J>QETLOIA.TLICO>J.@LJ/$LQ0OLAR@Q.EQJI), ?RQ CLO LRO MROMLPBP TB @>K ?B E>MMV TFQE
LMBO>QFKD LK QEB >PPRJMQFLK QE>Q:
A

= A

cos()
A

=
a
x
b
x
+ a
y
b
y
?LQE ELIA QORB >KA QEBOBCLOB:
a
x
b
x
+ a
y
b
y
= A

cos()
.LT, IBQfP PQ>OQ TFQE QEB CLIILTFKD MOL?IBJ.
7B E>SB QEB SB@QLOP ! >KA ":
A

= (10, 2)
B

= (4, 3)
7B KLT E>SB > PFQR>QFLK FK TEF@E TB HKLT
BSBOVQEFKD BU@BMQ CLO QEBQ>. 7B HKLT QEB
@LJMLKBKQP LC QEB SB@QLO >KA @>K
@>I@RI>QB QEB J>DKFQRAB LC B>@E SB@QLO. 7B
@>K QEBOBCLOB PLISB CLO @LPFKB LC QEBQ>:
cos() = ( A

) / ( A

)
.3?=NIL ; = H?Q .3?=NIL(-3,5);
.3?=NIL < = H?Q .3?=NIL(10,1);
The PVector class includes a function to
calculate the dot product.
@FI;N H = ;.>IN(<);
JO<FC= @FI;N >IN(.3?=NIL P) U
L?NOLH R*P.R + S*P.S + T*P.T;
W
F64B?2 6.19
#E>MQBO 6. !RQLKLJLRP !DBKQP
284
4L PLISB CLO QEBQ>, TB @>K Q>HB QEB FKSBOPB @LPFKB (LCQBK BUMOBPPBA >P 7CG=B9
-1
LO 5F77CG=B9).
= cos
1
( ( A

) / ( A

) )
,BQfP KLT AL QEB J>QE TFQE >@QR>I KRJ?BOP:
A

= 10.2
B

= 5
4EBOBCLOB:
= cos
1
( ( 10 4 + 2 -3 ) / ( 10.2 5 ) )
= cos
1
( 34 / 51 )
= 48

4EB 0OL@BPPFKD SBOPFLK LC QEFP TLRIA ?B:


!KA, >D>FK, FC TB TBOB QL AFD FKQL QEB DRQP LC QEB 0OL@BPPFKD PLRO@B @LAB, TB TLRIA PBB >
CRK@QFLK QE>Q FJMIBJBKQP QEFP BU>@Q >IDLOFQEJ.
! @LRMIB QEFKDP QL KLQB EBOB:
.3?=NIL ; = H?Q .3?=NIL(10,2);
.3?=NIL < = H?Q .3?=NIL(4,-3);
@FI;N NB?N; = ;=IM(;.>IN(<) / (;.G;A() * <.G;A()));
MN;NC= JO<FC= @FI;N ;HAF??NQ??H(.3?=NIL P1, .3?=NIL P2) U
@FI;N >IN = P1.>IN(P2);
@FI;N NB?N; = (@FI;N) +;NB.;=IM(>IN / (P1.G;A() * P2.G;A()));
L?NOLH NB?N;;
W
#OB>QB > PHBQ@E QE>Q AFPMI>VP QEB >KDIB
?BQTBBK QTL .3?=NIL L?GB@QP.
Exercise 6.9 Exercise 6.9
4EB .>QROB LC #LAB (S005)
285
1. )C QTL SB@QLOP ( A

>KA B

) >OB LOQELDLK>I (F.B. MBOMBKAF@RI>O), QEB ALQ MOLAR@Q ( A

)
FP BNR>I QL 0.
2. )C QTL SB@QLOP >OB RKFQ SB@QLOP, QEBK QEB ALQ MOLAR@Q FP PFJMIV BNR>I QL @LPFKB LC
QEB >KDIB ?BQTBBK QEBJ, F.B. A

= cos() FC A

>KA B

>OB LC IBKDQE 1.
6.8 Path Following 6.8 Path Following
.LT QE>Q TBfSB DLQ > ?>PF@ RKABOPQ>KAFKD LC QEB ALQ MOLAR@Q RKABO LRO ?BIQ, TB @>K OBQROK
QL > AFP@RPPFLK LC #O>FD 2BVKLIAPfP M>QE-CLIILTFKD >IDLOFQEJ. ,BQfP NRF@HIV @I>OFCV PLJBQEFKD.
7B >OB Q>IHFKD >?LRQ M>QE :C@@CK=B;, KLQ M>QE :=B8=B;. 0>QECFKAFKD OBCBOP QL > OBPB>O@E QLMF@
(@LJJLKIV PQRAFBA FK >OQFCF@F>I FKQBIIFDBK@B) QE>Q FKSLISBP PLISFKD CLO QEB PELOQBPQ AFPQ>K@B
?BQTBBK QTL MLFKQP, LCQBK FK > J>WB. 7FQE ;,?3 1:77:B492 ;,?3 1:77:B492, QEB M>QE >IOB>AV BUFPQP >KA TBfOB
>PHFKD > SBEF@IB QL CLIILT QE>Q M>QE.
"BCLOB TB TLOH LRQ QEB FKAFSFAR>I MFB@BP, IBQfP Q>HB > ILLH >Q QEB LSBO>II >IDLOFQEJ CLO M>QE
CLIILTFKD, >P ABCFKBA ?V 2BVKLIAP.
7BfII CFOPQ ABCFKB TE>Q TB JB>K ?V > M>QE. 4EBOB >OB J>KV T>VP TB @LRIA FJMIBJBKQ > M>QE,
?RQ CLO RP, > PFJMIB T>V TFII ?B QL ABCFKB > M>QE >P > PBOFBP LC @LKKB@QBA MLFKQP:
F64B?2 6.20
#E>MQBO 6. !RQLKLJLRP !DBKQP
286
!K BSBK PFJMIBO M>QE TLRIA ?B > IFKB ?BQTBBK QTL MLFKQP.
7BfOB >IPL DLFKD QL @LKPFABO > M>QE QL E>SB > O>AFRP. )C TB QEFKH LC QEB M>QE >P > OL>A, QEB
O>AFRP ABQBOJFKBP QEB OL>AfP TFAQE. 7FQE > PJ>IIBO O>AFRP, LRO SBEF@IBP TFII E>SB QL CLIILT QEB
M>QE JLOB @ILPBIV; > TFABO O>AFRP TFII >IILT QEBJ QL PQO>V > ?FQ JLOB.
0RQQFKD QEFP FKQL > @I>PP, TB E>SB:
F64B?2 6.21: '.A5
F64B?2 6.22: )6:=92 =.A5
=F;MM .;NB U
A Path is only two points, start and end. .3?=NIL MN;LN;
.3?=NIL ?H>;
A path has a radius, i.e. how wide it is. @FI;N L;>COM;
.;NB() U
4EB .>QROB LC #LAB (S005)
287
.LT, IBQfP >PPRJB TB E>SB > SBEF@IB (>P ABMF@QBA ?BILT) LRQPFAB LC QEB M>QEfP O>AFRP,
JLSFKD TFQE > SBIL@FQV.
4EB CFOPQ QEFKD TB T>KQ QL AL FP MOBAF@Q, >PPRJFKD > @LKPQ>KQ SBIL@FQV, TEBOB QE>Q SBEF@IB TFII
?B FK QEB CRQROB.
/K@B TB E>SB QE>Q IL@>QFLK, FQfP KLT LRO GL? QL CFKA LRQ QEB SBEF@IBfP @ROOBKQ AFPQ>K@B COLJ
QEB M>QE LC QE>Q MOBAF@QBA IL@>QFLK. )C FQfP SBOV C>O >T>V, TBII, QEBK, TBfSB PQO>VBA COLJ QEB
M>QE >KA KBBA QL PQBBO ?>@H QLT>OAP FQ. )C FQfP @ILPB, QEBK TBfOB ALFKD /+ >KA >OB CLIILTFKD
QEB M>QE KF@BIV.
Picking some arbitrary values to initialize
the path
L;>COM = 20;
MN;LN = H?Q .3?=NIL(0,B?CABN/3);
?H> = H?Q .3?=NIL(QC>NB,2*B?CABN/3);
W
PIC> >CMJF;S() U // "CMJF;S NB? J;NB.
MNLIE?4?CABN(L;>COM*2);
MNLIE?(0,100);
FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S);
MNLIE?4?CABN(1);
MNLIE?(0);
FCH?(MN;LN.R,MN;LN.S,?H>.R,?H>.S);
W
W
F64B?2 6.23
Start by making a copy of the velocity. .3?=NIL JL?>C=N = P?F.A?N();
Normalize it and look 25 pixels ahead by
scaling the vector up.
JL?>C=N.HILG;FCT?();
JL?>C=N.GOFN(25);
Add vector to location to find the predicted
location.
.3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N);
#E>MQBO 6. !RQLKLJLRP !DBKQP
288
3L, ELT AL TB CFKA QEB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB? 4EFP @LK@BMQ FP HBV. 4EB AFPQ>K@B
?BQTBBK > MLFKQ >KA > IFKB FP ABCFKBA >P QEB IBKDQE LC QEB KLOJ>I ?BQTBBK QE>Q MLFKQ >KA IFKB.
4EB KLOJ>I FP > SB@QLO QE>Q BUQBKAP COLJ QE>Q MLFKQ >KA FP MBOMBKAF@RI>O QL QEB IFKB.
,BQfP CFDROB LRQ TE>Q TB AL HKLT. 7B HKLT TB E>SB > SB@QLO (@>II FQ A

) QE>Q BUQBKAP COLJ QEB


M>QEfP PQ>OQFKD MLFKQ QL QEB SBEF@IBfP MOBAF@QBA IL@>QFLK.
7B >IPL HKLT QE>Q TB @>K ABCFKB > SB@QLO (@>II FQ B

) QE>Q MLFKQP COLJ QEB PQ>OQ LC QEB M>QE QL QEB


BKA.
.LT, TFQE ?>PF@ QOFDLKLJBQOV, TB HKLT QE>Q QEB AFPQ>K@B COLJ QEB M>QEfP PQ>OQ QL QEB KLOJ>I
MLFKQ FP: RAR * 9EI(J>;J7).
)C TB HKBT QEBQ>, TB @LRIA B>PFIV ABCFKB QE>Q KLOJ>I MLFKQ >P CLIILTP:
F64B?2 6.24
.3?=NIL ; = .3?=NIL.MO<(JL?>C=N*I=,J;NB.MN;LN);
.3?=NIL < = .3?=NIL.MO<(J;NB.?H>,J;NB.MN;LN);
F64B?2 6.25
4EB .>QROB LC #LAB (S005)
289
!KA FC QEB ALQ MOLAR@Q E>P Q>RDEQ RP >KVQEFKD, FQfP QE>Q DFSBK QTL SB@QLOP, TB @>K DBQ QEBQ>,
QEB >KDIB ?BQTBBK.
7EFIB QEB >?LSB @LAB TFII TLOH, QEBOBfP LKB JLOB PFJMIFCF@>QFLK TB @>K J>HB. )C VLRfII
KLQF@B, QEB ABPFOBA J>DKFQRAB CLO SB@QLO B

FP:
7.C7=()*9EI(J>;J7)
TEF@E FP QEB @LAB QO>KPI>QFLK LC:
A

cos()
!KA FC VLR OB@>II:
A

= A

cos()
.LT, TE>Q FC SB@QLO B

FP > RKFQ SB@QLO, F.B. IBKDQE 1? 4EBK:


A

= A

1 cos()
LO
A

= A

cos()
!KA TE>Q >OB TB ALFKD FK LRO @LAB? .LOJ>IFWFKD ?!
"B@>RPB LC QEFP C>@Q, TB @>K PFJMIFCV LRO @LAB >P:
The distance from START to NORMAL @FI;N > = ;.G;A()*=IM(NB?N;);
<.HILG;FCT?();
Scale PVector b to that distance. <.GOFN(>);
The normal point can be found by adding
the scaled version of b to the paths starting
point.
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
What is theta? The angle between A and B @FI;N NB?N; = .3?=NIL.;HAF??NQ??H(;,<);
<.HILG;FCT?();
<.GOFN(;.G;A()*=IM(NB?N;));
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
<.HILG;FCT?();
@FI;N NB?N; = .3?=NIL.;HAF??NQ??H(;,<);
<.HILG;FCT?();
#E>MQBO 6. !RQLKLJLRP !DBKQP
290
4EFP MOL@BPP FP @LJJLKIV HKLTK >P gP@>I>O MOLGB@QFLK.h FAF .:>(G) 4> ?30 >.,7,= ;=:50.?4:9 :1 FAF .:>(G) 4> ?30 >.,7,= ;=:50.?4:9 :1
A :9?: B. A :9?: B.
/K@B TB E>SB QEB KLOJ>I MLFKQ >ILKD QEB M>QE, TB E>SB QL AB@FAB TEBQEBO QEB SBEF@IB PELRIA
PQBBO QLT>OAP QEB M>QE >KA ELT. 2BVKLIAPfP >IDLOFQEJ PQ>QBP QE>Q QEB SBEF@IB PELRIA LKIV PQBBO
QLT>OAP QEB M>QE FC FQ PQO>VP ?BVLKA QEB M>QE (F.B., FC QEB AFPQ>K@B ?BQTBBK QEB KLOJ>I MLFKQ >KA
QEB MOBAF@QBA CRQROB IL@>QFLK FP DOB>QBO QE>K QEB M>QE O>AFRP).
We can use the dot product to scale bs
length.
<.GOFN(;.>IN(<));
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(J;NB.MN;LN,<);
F64B?2 6.26
F64B?2 6.27
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
If the vehicle is outside the path, seek the
target.
C@ (>CMN;H=? > J;NB.L;>COM) U
We dont have to work out the desired
velocity and steering force; all that is taken
care of by seek(), which we already wrote in
Example 6.1.
M??E(N;LA?N);
W
4EB .>QROB LC #LAB (S005)
291
"RQ TE>Q FP QEB Q>ODBQ?
2BVKLIAPfP >IDLOFQEJ FKSLISBP MF@HFKD > MLFKQ >EB>A LC QEB KLOJ>I LK QEB M>QE (PBB PQBM #3
>?LSB). "RQ CLO PFJMIF@FQV, TB @LRIA GRPQ P>V QE>Q QEB Q>ODBQ FP QEB KLOJ>I FQPBIC. 4EFP TFII TLOH
C>FOIV TBII:
3FK@B TB HKLT QEB SB@QLO QE>Q ABCFKBP QEB M>QE (TBfOB @>IIFKD FQ g"h), TB @>K FJMIBJBKQ
2BVKLIAPfP gMLFKQ >EB>A LK QEB M>QEh TFQELRQ QLL JR@E QOLR?IB.
0RQQFKD FQ >II QLDBQEBO, TB E>SB QEB CLIILTFKD PQBBOFKD CRK@QFLK FK LRO 3?BC=F? @I>PP.
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
C@ (>CMN;H=? > J;NB.L;>COM) U
Seek the normal point on the path. M??E(HILG;F.ICHN);
W
F64B?2 6.28
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
C@ (>CMN;H=? > J;NB.L;>COM) U
Normalize and scale b (pick 25 pixels
arbitrarily).
<.HILG;FCT?();
<.GOFN(25);
By adding b to normalPoint, we now move
25 pixels ahead on the path.
.3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN,<);
M??E(N;LA?N);
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
292
Example 6.5: Simple path following
.LT, VLR J>V KLQF@B >?LSB QE>Q FKPQB>A LC RPFKD >II QE>Q ALQ MOLAR@Q/P@>I>O MOLGB@QFLK @LAB QL
CFKA QEB KLOJ>I MLFKQ, TB FKPQB>A @>II > CRK@QFLK: A?N,ILG;F.ICHN(). )K @>PBP IFHB QEFP, FQfP
RPBCRI QL ?OB>H LRQ QEB @LAB QE>Q MBOCLOJP > PMB@FCF@ Q>PH (CFKAFKD > KLOJ>I MLFKQ) FKQL >
CRK@QFLK QE>Q FQ @>K ?B RPBA DBKBOF@>IIV FK >KV @>PB TEBOB FQ FP OBNRFOBA. 4EB CRK@QFLK Q>HBP
QEOBB .3?=NILP: QEB CFOPQ ABCFKBP > MLFKQ FK #>OQBPF>K PM>@B >KA QEB PB@LKA >KA QEFOA
>ODRJBKQP ABCFKB > IFKB PBDJBKQ.
PIC> @IFFIQ(.;NB J) U
Step 1: Predict the vehicles future location. .3?=NIL JL?>C=N = P?F.A?N();
JL?>C=N.HILG;FCT?();
JL?>C=N.GOFN(25);
.3?=NIL JL?>C=N*I= = .3?=NIL.;>>(FI=, JL?>C=N);
Step 2: Find the normal point along the
path.
.3?=NIL ; = J.MN;LN;
.3?=NIL < = J.?H>;
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
Step 3: Move a little further along the path
and set a target.
.3?=NIL >CL = .3?=NIL.MO<(<, ;);
>CL.HILG;FCT?();
>CL.GOFN(10);
.3?=NIL N;LA?N = .3?=NIL.;>>(HILG;F.ICHN, >CL);
Step 4: If we are off the path, seek that
target in order to stay on the path.
@FI;N >CMN;H=? =
.3?=NIL.>CMN(HILG;F.ICHN, JL?>C=N*I=);
C@ (>CMN;H=? > J.L;>COM) U
M??E(N;LA?N);
W
W
4EB .>QROB LC #LAB (S005)
293
7E>Q AL TB E>SB PL C>O? 7B E>SB > .;NB @I>PP QE>Q ABCFKBP > M>QE >P > IFKB ?BQTBBK QTL
MLFKQP. 7B E>SB > 3?BC=F? @I>PP QE>Q ABCFKBP > SBEF@IB QE>Q @>K CLIILT QEB M>QE (RPFKD >
PQBBOFKD ?BE>SFLO QL PBBH > Q>ODBQ >ILKD QEB M>QE). 7E>Q FP JFPPFKD?
4>HB > ABBM ?OB>QE. 7BfOB >IJLPQ QEBOB.
F64B?2 6.29
.3?=NIL A?N,ILG;F.ICHN(.3?=NIL J, .3?=NIL ;, .3?=NIL <) U
PVector that points from a to p .3?=NIL ;J = .3?=NIL.MO<(J, ;);
PVector that points from a to b .3?=NIL ;< = .3?=NIL.MO<(<, ;);
Using the dot product for scalar projection ;<.HILG;FCT?();
;<.GOFN(;J.>IN(;<));
Finding the normal point along the line
segment
.3?=NIL HILG;F.ICHN = .3?=NIL.;>>(;, ;<);
L?NOLH HILG;F.ICHN;
W
6.9 Path Following with Multiple Segments 6.9 Path Following with Multiple Segments
7BfSB ?RFIQ > DOB>Q BU>JMIB PL C>O, VBP, ?RQ FQfP MOBQQV A>OK IFJFQFKD. !CQBO >II, TE>Q FC TB T>KQ
LRO M>QE QL ?B PLJBQEFKD QE>Q ILLHP JLOB IFHB:
F64B?2 6.30
#E>MQBO 6. !RQLKLJLRP !DBKQP
294
7EFIB FQfP QORB QE>Q TB @LRIA J>HB QEFP BU>JMIB TLOH CLO > @ROSBA M>QE, TBfOB JR@E IBPP IFHBIV
QL BKA RM KBBAFKD > @LLI @LJMOBPP LK LRO CLOBEB>A FC TB PQF@H TFQE IFKB PBDJBKQP. )K QEB BKA,
TB @>K >IT>VP BJMILV QEB P>JB QB@EKFNRB TB AFP@LSBOBA TFQE "LU2$dTB @>K AO>T TE>QBSBO
C>K@V @ROSBA M>QE TB T>KQ >KA >MMOLUFJ>QB FQ ?BEFKA QEB P@BKBP TFQE PFJMIB DBLJBQOF@
CLOJP.
3L, TE>QfP QEB MOL?IBJ? )C TB J>AB M>QE CLIILTFKD TLOH TFQE LKB IFKB PBDJBKQ, ELT AL TB
J>HB FQ TLOH TFQE > PBOFBP LC @LKKB@QBA IFKB PBDJBKQP? ,BQfP Q>HB > ILLH >D>FK >Q LRO SBEF@IB
AOFSFKD >ILKD QEB P@OBBK. 3>V TB >OOFSB >Q 3QBM 3.
'?0; 3: F49/ , ?,=20? ;:49? :9 ?30 ;,?3. '?0; 3: F49/ , ?,=20? ;:49? :9 ?30 ;,?3.
4L CFKA QEB Q>ODBQ, TB KBBA QL CFKA QEB KLOJ>I QL QEB IFKB PBDJBKQ. "RQ KLT QE>Q TB E>SB >
PBOFBP LC IFKB PBDJBKQP, TB E>SB > PBOFBP LC KLOJ>I MLFKQP (PBB >?LSB)! 7EF@E LKB AL TB
@ELLPB? 4EB PLIRQFLK TBfII BJMILV FP QL MF@H QEB KLOJ>I MLFKQ QE>Q FP (>) @ILPBPQ >KA (?) LK QEB
M>QE FQPBIC.
)C TB E>SB > MLFKQ >KA >K FKCFKFQBIV ILKD IFKB, TBfII >IT>VP E>SB > KLOJ>I. "RQ, >P FK QEB M>QE-
CLIILTFKD BU>JMIB, FC TB E>SB > MLFKQ >KA > IFKB PBDJBKQ, TB TLKfQ KB@BPP>OFIV CFKA > KLOJ>I
QE>Q FP LK QEB IFKB PBDJBKQ FQPBIC. 3L FC QEFP E>MMBKP CLO >KV LC QEB PBDJBKQP, TB @>K AFPNR>IFCV
QELPB KLOJ>IP. /K@B TB >OB IBCQ TFQE KLOJ>IP QE>Q >OB LK QEB M>QE FQPBIC (LKIV QTL FK QEB >?LSB
AF>DO>J), TB PFJMIV MF@H QEB LKB QE>Q FP @ILPBPQ QL LRO SBEF@IBfP IL@>QFLK.
F64B?2 6.31
F64B?2 6.32
4EB .>QROB LC #LAB (S005)
295
)K LOABO QL TOFQB QEB @LAB CLO QEFP, TBfII E>SB QL BUM>KA LRO .;NB @I>PP QL E>SB >K
ALL;S*CMN LC MLFKQP (O>QEBO QE>K GRPQ QTL, > PQ>OQ >KA >K BKA).
.LT QE>Q TB E>SB QEB .;NB @I>PP ABCFKBA, FQfP QEB SBEF@IBfP QROK QL AB>I TFQE JRIQFMIB IFKB
PBDJBKQP. !II TB AFA ?BCLOB T>P CFKA QEB KLOJ>I CLO LKB IFKB PBDJBKQ. 7B @>K KLT CFKA QEB
KLOJ>IP CLO >II QEB IFKB PBDJBKQP FK > ILLM.
=F;MM .;NB U
A Path is now an ArrayList of points
(PVector objects).
ALL;S*CMN<.3?=NIL> JICHNM;
@FI;N L;>COM;
.;NB() U
L;>COM = 20;
JICHNM = H?Q ALL;S*CMN<.3?=NIL>();
W
This function allows us to add points to the
path.
PIC> ;>>.ICHN(@FI;N R, @FI;N S) U .
.3?=NIL JICHN = H?Q .3?=NIL(R,S);
JICHNM.;>>(JICHN);
W
Display the path as a series of points. PIC> >CMJF;S() U
MNLIE?(0);
HI$CFF();
<?ACH0B;J?();
@IL (.3?=NIL P : JICHNM) U
P?LN?R(P.R,P.S);
W
?H>0B;J?();
W
W
@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U
.3?=NIL ; = J.JICHNM.A?N(C);
.3?=NIL < = J.JICHNM.A?N(C+1);
#E>MQBO 6. !RQLKLJLRP !DBKQP
296
4EBK TB PELRIA J>HB PROB QEB KLOJ>I MLFKQ FP >@QR>IIV ?BQTBBK MLFKQP ; >KA <. 3FK@B TB
HKLT LRO M>QE DLBP COLJ IBCQ QL OFDEQ FK QEFP BU>JMIB, TB @>K QBPQ FC QEB L @LJMLKBKQ LC
HILG;F.ICHN FP LRQPFAB QEB L @LJMLKBKQP LC ; >KA <.
!P > IFQQIB QOF@H, TBfII P>V QE>Q FC FQfP KLQ TFQEFK QEB IFKB PBDJBKQ, IBQfP GRPQ MOBQBKA QEB BKA MLFKQ
LC QE>Q IFKB PBDJBKQ FP QEB KLOJ>I. 4EFP TFII BKPROB QE>Q LRO SBEF@IB >IT>VP PQ>VP LK QEB M>QE,
BSBK FC FQ PQO>VP LRQ LC QEB ?LRKAP LC LRO IFKB PBDJBKQP.
&FK>IIV, TBfII KBBA QL J>HB PROB TB CFKA QEB KLOJ>I MLFKQ QE>Q FP @ILPBPQ QL LRO SBEF@IB. 4L
>@@LJMIFPE QEFP, TB PQ>OQ TFQE > SBOV EFDE gTLOIA OB@LOAh AFPQ>K@B >KA FQBO>QB QEOLRDE B>@E
KLOJ>I MLFKQ QL PBB FC FQ ?B>QP QEB OB@LOA (F.B. FP IBPP QE>K). %>@E QFJB > KLOJ>I MLFKQ ?B>QP QEB
OB@LOA, QEB TLOIA OB@LOA FP RMA>QBA >KA QEB TFKKFKD MLFKQ FP PQLOBA FK > S>OF>?IB K>JBA
N;LA?N. !Q QEB BKA LC QEB ILLM, TBfII E>SB QEB @ILPBPQ KLOJ>I MLFKQ FK QE>Q S>OF>?IB.
Example 6.6: Path following
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
Finding the normals for each line segment
C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U
Use the end point of the segment as our
normal point if we cant find one.
HILG;F.ICHN = <.A?N();
W
.3?=NIL N;LA?N = HOFF;
4EB .>QROB LC #LAB (S005)
297
Start with a very high record that can easily
be beaten.
@FI;N QILF>/?=IL> = 1000000;
@IL (CHN C = 0; C < J.JICHNM.MCT?()-1; C++) U
.3?=NIL ; = J.JICHNM.A?N(C);
.3?=NIL < = J.JICHNM.A?N(C+1);
.3?=NIL HILG;F.ICHN = A?N,ILG;F.ICHN(JL?>C=N*I=, ;, <);
C@ (HILG;F.ICHN.R < ;.R VV HILG;F.ICHN.R > <.R) U
HILG;F.ICHN = <.A?N();
W
@FI;N >CMN;H=? = .3?=NIL.>CMN(JL?>C=N*I=, HILG;F.ICHN);
If we beat the record, then this should be
our target!
C@ (>CMN;H=? < QILF>/?=IL>) U
QILF>/?=IL> = >CMN;H=?;
N;LA?N = HILG;F.ICHN.A?N();
W
W
5MA>QB QEB M>QE-CLIILTFKD BU>JMIB PL QE>Q QEB M>QE @>K DL FK >KV AFOB@QFLK. ((FKQ:
VLRfII KBBA QL RPB QEB GCH() >KA G;R() CRK@QFLK TEBK ABQBOJFKFKD FC QEB KLOJ>I MLFKQ
FP FKPFAB QEB IFKB PBDJBKQ.)
C@ (HILG;F.ICHN.R < ::::(::::,::::) VV HILG;F.ICHN.R > ::::(::::,::::)) U
HILG;F.ICHN = <.A?N();
W
Exercise 6.10 Exercise 6.10
#OB>QB > M>QE QE>Q @E>KDBP LSBO QFJB. #>K QEB MLFKQP QE>Q ABCFKB QEB M>QE FQPBIC E>SB
QEBFO LTK PQBBOFKD ?BE>SFLOP?
Exercise 6.11 Exercise 6.11
6.10 Complex Systems 6.10 Complex Systems
2BJBJ?BO LRO MROMLPB? 4L ?OB>QEB IFCB FKQL QEB QEFKDP QE>Q JLSB >OLRKA LRO 0OL@BPPFKD
TFKALTP? "V IB>OKFKD QL TOFQB QEB @LAB CLO >K >RQLKLJLRP >DBKQ >KA ?RFIAFKD > PBOFBP LC
BU>JMIBP LC FKAFSFAR>I ?BE>SFLOP, ELMBCRIIV LRO PLRIP CBBI > IFQQIB JLOB CRII. "RQ QEFP FP KL
MI>@B QL PQLM >KA OBPQ LK LRO I>ROBIP. 7BfOB GRPQ DBQQFKD PQ>OQBA. !CQBO >II, QEBOB FP > ABBMBO
MROMLPB >Q TLOH EBOB. 9BP, > SBEF@IB FP > PFJRI>QBA ?BFKD QE>Q J>HBP AB@FPFLKP >?LRQ ELT
QL PBBH >KA CILT >KA CLIILT. "RQ TE>Q FP > IFCB IBA >ILKB, TFQELRQ QEB ILSB >KA PRMMLOQ LC
#E>MQBO 6. !RQLKLJLRP !DBKQP
298
LQEBOP? /RO MROMLPB EBOB FP KLQ LKIV QL ?RFIA FKAFSFAR>I ?BE>SFLOP CLO LRO SBEF@IBP, ?RQ QL MRQ
LRO SBEF@IBP FKQL PVPQBJP LC J>KV SBEF@IBP >KA >IILT QELPB SBEF@IBP QL FKQBO>@Q TFQE B>@E
LQEBO.
,BQfP QEFKH >?LRQ > QFKV, @O>TIFKD >KQdLKB PFKDIB >KQ. !K >KQ FP >K >RQLKLJLRP >DBKQ; FQ @>K
MBO@BFSB FQP BKSFOLKJBKQ (RPFKD >KQBKK>B QL D>QEBO FKCLOJ>QFLK >?LRQ QEB AFOB@QFLK >KA
PQOBKDQE LC @EBJF@>I PFDK>IP) >KA J>HB AB@FPFLKP >?LRQ ELT QL JLSB ?>PBA LK QELPB PFDK>IP.
"RQ @>K > PFKDIB >KQ >@QFKD >ILKB ?RFIA > KBPQ, D>QEBO CLLA, ABCBKA FQP NRBBK? !K >KQ FP >
PFJMIB RKFQ >KA @>K LKIV MBO@BFSB FQP FJJBAF>QB BKSFOLKJBKQ. ! @LILKV LC >KQP, ELTBSBO, FP >
PLMEFPQF@>QBA @LJMIBU PVPQBJ, > gPRMBOLOD>KFPJh FK TEF@E QEB @LJMLKBKQP TLOH QLDBQEBO QL
>@@LJMIFPE AFCCF@RIQ >KA @LJMIF@>QBA DL>IP.
7B T>KQ QL Q>HB TE>Q TBfSB IB>OKBA AROFKD QEB MOL@BPP LC ?RFIAFKD >RQLKLJLRP >DBKQP FK
0OL@BPPFKD FKQL PFJRI>QFLKP QE>Q FKSLISB J>KV >DBKQP LMBO>QFKD FK M>O>IIBId>DBKQP QE>Q E>SB
>K >?FIFQV QL MBO@BFSB KLQ LKIV QEBFO MEVPF@>I BKSFOLKJBKQ ?RQ >IPL QEB >@QFLKP LC QEBFO CBIILT
>DBKQP, >KA QEBK >@Q >@@LOAFKDIV. 7B T>KQ QL @OB>QB @LJMIBU PVPQBJP FK 0OL@BPPFKD.
7E>Q FP > @LJMIBU PVPQBJ? ! @LJMIBU PVPQBJ FP QVMF@>IIV ABCFKBA >P > PVPQBJ QE>Q FP gJLOB
QE>K QEB PRJ LC FQP M>OQP.h 7EFIB QEB FKAFSFAR>I BIBJBKQP LC QEB PVPQBJ J>V ?B FK@OBAF?IV
PFJMIB >KA B>PFIV RKABOPQLLA, QEB ?BE>SFLO LC QEB PVPQBJ >P > TELIB @>K ?B EFDEIV @LJMIBU,
FKQBIIFDBKQ, >KA AFCCF@RIQ QL MOBAF@Q. (BOB >OB QEOBB HBV MOFK@FMIBP LC @LJMIBU PVPQBJP.
i '48;70 @94?> B4?3 >3:=?-=,920 =07,?4:9>34;>. '48;70 @94?> B4?3 >3:=?-=,920 =07,?4:9>34;>. 4EFP FP TE>Q TBfSB ?BBK ?RFIAFKD >II
>ILKD: SBEF@IBP QE>Q E>SB > IFJFQBA MBO@BMQFLK LC QEBFO BKSFOLKJBKQ.
i '48;70 @94?> :;0=,?0 49 ;,=,7707. '48;70 @94?> :;0=,?0 49 ;,=,7707. 4EFP FP TE>Q TB KBBA QL PFJRI>QB FK @LAB. &LO
BSBOV @V@IB QEOLRDE 0OL@BPPFKDfP >L;Q() ILLM, B>@E RKFQ TFII AB@FAB ELT QL JLSB (QL
@OB>QB QEB >MMB>O>K@B LC QEBJ >II TLOHFKD FK M>O>IIBI).
i 'D>?08 ,> , B3:70 0C34-4?> 080=209? ;309:809,. 'D>?08 ,> , B3:70 0C34-4?> 080=209? ;309:809,. /RQ LC QEB FKQBO>@QFLKP ?BQTBBK
QEBPB PFJMIB RKFQP BJBODBP @LJMIBU ?BE>SFLO, M>QQBOKP, >KA FKQBIIFDBK@B. (BOB TBfOB
Q>IHFKD >?LRQ QEB OBPRIQ TB >OB ELMFKD CLO FK LRO PHBQ@EBP. 9BP, TB HKLT QEFP
E>MMBKP FK K>QROB (>KQ @LILKFBP, QBOJFQBP, JFDO>QFLK M>QQBOKP, B>OQENR>HBP,
PKLTCI>HBP, BQ@.), ?RQ @>K TB >@EFBSB QEB P>JB OBPRIQ FK LRO 0OL@BPPFKD PHBQ@EBP?
&LIILTFKD >OB QEOBB >AAFQFLK>I CB>QROBP LC @LJMIBU PVPQBJP QE>Q TFII EBIM CO>JB QEB AFP@RPPFLK,
>P TBII >P MOLSFAB DRFABIFKBP CLO CB>QROBP TB TFII T>KQ QL FK@IRAB FK LRO PLCQT>OB PFJRI>QFLKP.
)QfP FJMLOQ>KQ QL >@HKLTIBADB QE>Q QEFP FP > CRWWV PBQ LC @E>O>@QBOFPQF@P >KA KLQ >II @LJMIBU
PVPQBJP E>SB >II LC QEBJ.
i #:9-7490,=4?D. #:9-7490,=4?D. 4EFP >PMB@Q LC @LJMIBU PVPQBJP FP LCQBK @>PR>IIV OBCBOOBA QL >P gQEB
?RQQBOCIV BCCB@Q,h @LFKBA ?V J>QEBJ>QF@F>K >KA JBQBLOLILDFPQ %AT>OA .LOQLK ,LOBKW,
> MFLKBBO FK QEB PQRAV LC @E>LP QEBLOV. )K 1961, ,LOBKW T>P ORKKFKD > @LJMRQBO
TB>QEBO PFJRI>QFLK CLO QEB PB@LKA QFJB >KA, MBOE>MP QL P>SB > IFQQIB QFJB, QVMBA FK >
PQ>OQFKD S>IRB LC 0.506 FKPQB>A LC 0.506127. 4EB BKA OBPRIQ T>P @LJMIBQBIV AFCCBOBKQ
COLJ QEB CFOPQ OBPRIQ LC QEB PFJRI>QFLK. )K LQEBO TLOAP, QEB QEBLOV FP QE>Q > PFKDIB
?RQQBOCIV CI>MMFKD FQP TFKDP LK QEB LQEBO PFAB LC QEB TLOIA @LRIA @>RPB > J>PPFSB
4EB .>QROB LC #LAB (S005)
299
TB>QEBO PEFCQ >KA ORFK LRO TBBHBKA >Q QEB ?B>@E. 7B @>II FQ gKLK-IFKB>Oh ?B@>RPB
QEBOB FPKfQ > IFKB>O OBI>QFLKPEFM ?BQTBBK > @E>KDB FK FKFQF>I @LKAFQFLKP >KA >
@E>KDB FK LRQ@LJB. ! PJ>II @E>KDB FK FKFQF>I @LKAFQFLKP @>K E>SB > J>PPFSB BCCB@Q
LK QEB LRQ@LJB. .LK-IFKB>O PVPQBJP >OB > PRMBOPBQ LC @E>LQF@ PVPQBJP. )K QEB KBUQ
@E>MQBO, TBfII PBB ELT BSBK FK > PVPQBJ LC J>KV WBOLP >KA LKBP, FC TB @E>KDB GRPQ
LKB ?FQ, QEB OBPRIQ TFII ?B @LJMIBQBIV AFCCBOBKQ.
i C:8;0?4?4:9 ,9/ .::;0=,?4:9. C:8;0?4?4:9 ,9/ .::;0=,?4:9. /KB LC QEB QEFKDP QE>Q LCQBK J>HBP > @LJMIBU
PVPQBJ QF@H FP QEB MOBPBK@B LC ?LQE @LJMBQFQFLK >KA @LLMBO>QFLK ?BQTBBK QEB
BIBJBKQP. )K LRO RM@LJFKD CIL@HFKD PVPQBJ, TB TFII E>SB QEOBB ORIBPd>IFDKJBKQ,
@LEBPFLK, >KA PBM>O>QFLK. !IFDKJBKQ >KA @LEBPFLK TFII >PH QEB BIBJBKQP QL
g@LLMBO>QBhdF.B. TLOH QLDBQEBO QL PQ>V QLDBQEBO >KA JLSB QLDBQEBO. 3BM>O>QFLK,
ELTBSBO, TFII >PH QEB BIBJBKQP QL g@LJMBQBh CLO PM>@B. !P TB DBQ QL QEB CIL@HFKD
PVPQBJ, QOV Q>HFKD LRQ QEB @LLMBO>QFLK LO QEB @LJMBQFQFLK >KA VLRfII PBB ELT VLR
>OB IBCQ TFQELRQ @LJMIBUFQV. #LJMBQFQFLK >KA @LLMBO>QFLK >OB CLRKA FK IFSFKD
@LJMIBU PVPQBJP, ?RQ KLQ FK KLK-IFSFKD @LJMIBU PVPQBJP IFHB QEB TB>QEBO.
i F00/-,.6. F00/-,.6. #LJMIBU PVPQBJP LCQBK FK@IRAB > CBBA?>@H ILLM TEBOB QEB QEB LRQMRQ
LC QEB PVPQBJ FP CBA ?>@H FKQL QEB PVPQBJ QL FKCIRBK@B FQP ?BE>SFLO FK > MLPFQFSB LO
KBD>QFSB AFOB@QFLK. ,BQfP P>V VLR AOFSB QL TLOH B>@E A>V ?B@>RPB QEB MOF@B LC D>P
FP ILT. )K C>@Q, BSBOVLKB AOFSBP QL TLOH. 4EB MOF@B LC D>P DLBP RM >P ABJ>KA
?BDFKP QL BU@BBA PRMMIV. 9LR, >KA BSBOVLKB BIPB, AB@FAB QL Q>HB QEB QO>FK QL TLOH
?B@>RPB AOFSFKD FP QLL BUMBKPFSB. !KA QEB MOF@B LC D>P AB@IFKBP >P QEB ABJ>KA
AB@IFKBP. 4EB MOF@B LC D>P FP ?LQE QEB FKMRQ LC QEB PVPQBJ (ABQBOJFKFKD TEBQEBO
VLR @ELLPB QL AOFSB LO OFAB QEB QO>FK) >KA QEB LRQMRQ (QEB ABJ>KA QE>Q OBPRIQP COLJ
VLRO @ELF@B). ) PELRIA KLQB QE>Q B@LKLJF@ JLABIP (IFHB PRMMIV/ABJ>KA, QEB PQL@H
J>OHBQ) >OB LKB BU>JMIB LC > ERJ>K @LJMIBU PVPQBJ. /QEBOP FK@IRAB C>AP >KA
QOBKAP, BIB@QFLKP, @OLTAP, >KA QO>CCF@ CILT.
#LJMIBUFQV TFII PBOSB >P > QEBJB CLO QEB OBJ>FKFKD @LKQBKQ FK QEFP ?LLH. )K QEFP @E>MQBO, TBfII
?BDFK ?V >AAFKD LKB JLOB CB>QROB QL LRO 3?BC=F? @I>PP: >K >?FIFQV QL ILLH >Q KBFDE?LOFKD
SBEF@IBP.
6.11 Group Behaviors (or: Lets not run into each 6.11 Group Behaviors (or: Lets not run into each
other) other)
! DOLRM FP @BOQ>FKIV KLQ > KBT @LK@BMQ. 7BfSB ALKB QEFP ?BCLOBdFK #E>MQBO 4, TEBOB TB
ABSBILMBA > CO>JBTLOH CLO J>K>DFKD @LIIB@QFLKP LC M>OQF@IBP FK > .;LNC=F?0SMN?G @I>PP.
4EBOB, TB PQLOBA > IFPQ LC M>OQF@IBP FK >K ALL;S*CMN. 7BfII AL QEB P>JB QEFKD EBOB: PQLOB >
?RK@E LC 3?BC=F? L?GB@QP FK >K ALL;S*CMN.
#E>MQBO 6. !RQLKLJLRP !DBKQP
300
.LT TEBK FQ @LJBP QFJB QL AB>I TFQE >II QEB SBEF@IBP FK >L;Q(), TB PFJMIV ILLM QEOLRDE >II LC
QEBJ >KA @>II QEB KB@BPP>OV CRK@QFLKP.
/+, PL J>V?B TB T>KQ QL >AA > ?BE>SFLO, > CLO@B QL ?B >MMIFBA QL >II QEB SBEF@IBP. 4EFP @LRIA
?B PBBHFKD QEB JLRPB.
"RQ QE>QfP >K FKAFSFAR>I ?BE>SFLO. 7BfSB >IOB>AV PMBKQ QEFOQV-LAA M>DBP TLOOVFKD >?LRQ
FKAFSFAR>I ?BE>SFLOP. 7BfOB EBOB ?B@>RPB TB T>KQ QL >MMIV > DOLRM ?BE>SFLO. ,BQfP ?BDFK TFQE
PBM>O>QFLK, > ?BE>SFLO QE>Q @LJJ>KAP, g!SLFA @LIIFAFKD TFQE VLRO KBFDE?LOP!h
)P QE>Q OFDEQ? )Q PLRKAP DLLA, ?RQ FQfP KLQ. 7E>QfP JFPPFKD? )K QEB @>PB LC PBBH, TB P>FA, g3BBH
GIOM?5 >KA GIOM?6.h )K QEB @>PB LC PBM>O>QB, TBfOB P>VFKD gPBM>O>QB COLJ 9J9FMCB9 9@G9.h
7EL FP BSBOVLKB BIPB? )QfP QEB IFPQ LC >II QEB LQEBO SBEF@IBP.
4EFP FP QEB ?FD IB>M ?BVLKA TE>Q TB AFA ?BCLOB TFQE M>OQF@IB PVPQBJP. )KPQB>A LC E>SFKD B>@E
BIBJBKQ (M>OQF@IB LO SBEF@IB) LMBO>QB LK FQP LTK, TBfOB KLT P>VFKD, g(BV VLR, QEB SBEF@IB!
7EBK FQ @LJBP QFJB CLO VLR QL LMBO>QB, VLR KBBA QL LMBO>QB TFQE >K >T>OBKBPP LC BSBOVLKB
BIPB. 3L )fJ DLFKD QL DL >EB>A >KA M>PP VLR QEB ALL;S*CMN LC BSBOVLKB BIPB.h
4EFP FP ELT TBfSB J>MMBA LRQ M?NOJ() >KA >L;Q() QL AB>I TFQE > DOLRM ?BE>SFLO.
Declare an ArrayList of Vehicle objects. ALL;S*CMN<3?BC=F?> P?BC=F?M;
PIC> M?NOJ() U
Initialize and fill the ArrayList with a bunch of
Vehicles.
P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>;
@IL (CHN C = 0; C < 100; C++) U
P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN)));
W
W
PIC> >L;Q()U
@IL (3?BC=F? P : P?BC=F?M) U
P.OJ>;N?();
P.>CMJF;S();
W
W
P.M??E(GIOM?5,GIOM?6);
P.M?J;L;N?();
P.M?J;L;N?(P?BC=F?M);
4EB .>QROB LC #LAB (S005)
301
/C @LROPB, QEFP FP GRPQ QEB ?BDFKKFKD. 4EB
OB>I TLOH E>MMBKP FKPFAB QEB M?J;L;N?()
CRK@QFLK FQPBIC. ,BQfP CFDROB LRQ ELT TB T>KQ
QL ABCFKB PBM>O>QFLK. 2BVKLIAP PQ>QBP:
g3QBBO QL >SLFA @OLTAFKD.h )K LQEBO TLOAP, FC
> DFSBK SBEF@IB FP QLL @ILPB QL VLR, PQBBO
>T>V COLJ QE>Q SBEF@IB. 3LRKA C>JFIF>O? 2BJBJ?BO QEB PBBH ?BE>SFLO TEBOB > SBEF@IB PQBBOP
QLT>OAP > Q>ODBQ? 2BSBOPB QE>Q CLO@B >KA TB E>SB QEB CIBB ?BE>SFLO.
"RQ TE>Q FC JLOB QE>K LKB SBEF@IB FP QLL
@ILPB? )K QEFP @>PB, TBfII ABCFKB PBM>O>QFLK
>P QEB >SBO>DB LC >II QEB SB@QLOP MLFKQFKD
>T>V COLJ >KV @ILPB SBEF@IBP.
,BQfP ?BDFK QL TOFQB QEB @LAB. !P TB GRPQ
TLOHBA LRQ, TBfOB TOFQFKD > CRK@QFLK @>IIBA
M?J;L;N?() QE>Q OB@BFSBP >K ALL;S*CMN LC
6BEF@IB L?GB@QP >P >K >ODRJBKQ.
)KPFAB QEFP CRK@QFLK, TBfOB DLFKD QL ILLM QEOLRDE >II LC QEB SBEF@IBP >KA PBB FC >KV >OB QLL
@ILPB.
ALL;S*CMN<3?BC=F?> P?BC=F?M;
PIC> M?NOJ() U
MCT?(320,240);
P?BC=F?M = H?Q ALL;S*CMN<3?BC=F?>();
@IL (CHN C = 0; C < 100; C++) U
P?BC=F?M.;>>(H?Q 3?BC=F?(L;H>IG(QC>NB),L;H>IG(B?CABN)));
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL (3?BC=F? P : P?BC=F?M) U
This is really the only new thing were
doing in this section. Were asking a
Vehicle object to examine all the other
vehicles in the process of calculating a
separation force.
L.I;F7H7J;(L;>?9B;I);
P.OJ>;N?();
P.>CMJF;S();
W
W
F64B?2 6.33
F64B?2 6.34
PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
302
.LQF@B ELT FK QEB >?LSB @LAB, TB >OB KLQ LKIV @EB@HFKD FC QEB AFPQ>K@B FP IBPP QE>K > ABPFOBA
PBM>O>QFLK (F.B. QLL @ILPB!), ?RQ >IPL FC QEB AFPQ>K@B FP DOB>QBO QE>K WBOL. 4EFP FP > IFQQIB QOF@H QE>Q
J>HBP PROB TB ALKfQ >PH > SBEF@IB QL PBM>O>QB COLJ FQPBIC. 2BJBJ?BO, >II QEB SBEF@IBP >OB FK
QEB ALL;S*CMN, PL FC VLR >OBKfQ @>OBCRI VLRfII ?B @LJM>OFKD B>@E SBEF@IB QL FQPBIC!
/K@B TB HKLT QE>Q QTL SBEF@IBP >OB QLL @ILPB, TB KBBA QL J>HB > SB@QLO QE>Q MLFKQP >T>V
COLJ QEB LCCBKAFKD SBEF@IB.
4EFP FP KLQ BKLRDE. 7B E>SB QE>Q SB@QLO KLT, ?RQ TB KBBA QL J>HB PROB TB @>I@RI>QB QEB
>SBO>DB LC >II SB@QLOP MLFKQFKD >T>V COLJ @ILPB SBEF@IBP. (LT AL TB @LJMRQB >SBO>DB? 7B
>AA RM >II QEB SB@QLOP >KA AFSFAB ?V QEB QLQ>I.
This variable specifies how close is too
close.
@FI;N >?MCL?>M?J;L;NCIH = 20;
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
What is the distance between me and
another Vehicle?
Any code here will be executed if the
Vehicle is within 20 pixels.
W
W
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
.3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH);
>C@@.HILG;FCT?();
W
A PVector pointing away from the others
location
Start with an empty PVector. +0;9JEH IKC = D;M +0;9JEH();
?DJ 9EKDJ = 0;
We have to keep track of how many
Vehicles are too close.
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
+0;9JEH :?<< = +0;9JEH.IK8(BE97J?ED, EJ>;H.BE97J?ED);
>C@@.HILG;FCT?();
Add all the vectors together and increment
the count.
IKC.7::(:?<<);
=IOHN++;
W
W
4EB .>QROB LC #LAB (S005)
303
/K@B TB E>SB QEB >SBO>DB SB@QLO (PQLOBA FK QEB .3?=NIL L?GB@Q gPRJh), QE>Q .3?=NIL @>K ?B
P@>IBA QL J>UFJRJ PMBBA >KA ?B@LJB LRO ABPFOBA SBIL@FQVdTB 89G=F9 QL JLSB FK QE>Q
AFOB@QFLK >Q J>UFJRJ PMBBA! !KA LK@B TB E>SB QEB ABPFOBA SBIL@FQV, FQfP QEB P>JB LIA
2BVKLIAP PQLOV: PQBBOFKD BNR>IP ABPFOBA JFKRP SBIL@FQV.
,BQfP PBB QEB CRK@QFLK FK FQP BKQFOBQV. 4EBOB >OB QTL >AAFQFLK>I FJMOLSBJBKQP, KLQBA FK QEB
@LAB @LJJBKQP.
Example 6.7: Group behavior: Separation
We have to make sure we found at least
one close vehicle. We dont want to bother
doing anything if nothing is too close (not to
mention we cant divide by zero!)
?< (9EKDJ > 0) Q
IKC.:?L(9EKDJ);
S
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
Scale average to maxspeed (this becomes
desired).
MOG.M?N+;A(G;RMJ??>);
Reynoldss steering formula .3?=NIL MN??L = .3?=NIL.MO<(MOG,P?F);
MN??L.FCGCN(G;R@IL=?);
Apply the force to the Vehicles
acceleration.
;JJFS$IL=?(MN??L);
W
PIC> M?J;L;N? (ALL;S*CMN<3?BC=F?> P?BC=F?M) U
#E>MQBO 6. !RQLKLJLRP !DBKQP
304
Note how the desired separation is based
on the Vehicles size.
<BE7J :;I?H;:I;F7H7J?ED = H*2;
.3?=NIL MOG = H?Q .3?=NIL();
CHN =IOHN = 0;
@IL (3?BC=F? INB?L : P?BC=F?M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, INB?L.FI=;NCIH);
C@ ((> > 0) && (> < >?MCL?>M?J;L;NCIH)) U
.3?=NIL >C@@ = .3?=NIL.MO<(FI=;NCIH, INB?L.FI=;NCIH);
>C@@.HILG;FCT?();
What is the magnitude of the PVector
pointing away from the other vehicle? The
closer it is, the more we should flee. The
farther, the less. So we divide by the
distance to weight it appropriately.
:?<<.:?L(:);
MOG.;>>(>C@@);
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
MOG.HILG;FCT?();
MOG.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(MOG, P?F);
MN??L.FCGCN(G;R@IL=?);
;JJFS$IL=?(MN??L);
W
W
2BTOFQB M?J;L;N?() QL TLOH FK QEB LMMLPFQB C>PEFLK (g@LEBPFLKh). )C > SBEF@IB FP ?BVLKA
> @BOQ>FK AFPQ>K@B, PQBBO QLT>OAP QE>Q SBEF@IB. 4EFP TFII HBBM QEB DOLRM QLDBQEBO. (.LQB
QE>Q FK > JLJBKQ, TBfOB DLFKD QL ILLH >Q TE>Q E>MMBKP TEBK TB E>SB ?LQE @LEBPFLK
>KA PBM>O>QFLK FK QEB P>JB PFJRI>QFLK.)
Exercise 6.12 Exercise 6.12
4EB .>QROB LC #LAB (S005)
305
!AA QEB PBM>O>QFLK CLO@B QL M>QE CLIILTFKD QL @OB>QB > PFJRI>QFLK LC 2BVKLIAPfP
g#OLTA 0>QE &LIILTFKD.h
Exercise 6.13 Exercise 6.13
6.12 Combinations 6.12 Combinations
4EB MOBSFLRP QTL BUBO@FPBP EFKQ >Q TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ >PMB@Q LC QEFP
@E>MQBO. !CQBO >II, TE>Q FP > 0OL@BPPFKD PHBQ@E TFQE LKB PQBBOFKD CLO@B @LJM>OBA QL LKB TFQE
J>KV? (LT @LRIA TB BSBK ?BDFK QL PFJRI>QB BJBODBK@B FK LRO PHBQ@EBP TFQE LKIV LKB ORIB?
4EB JLPQ BU@FQFKD >KA FKQOFDRFKD ?BE>SFLOP TFII @LJB COLJ JFUFKD >KA J>Q@EFKD JRIQFMIB
PQBBOFKD CLO@BP, >KA TBfII KBBA > JB@E>KFPJ CLO ALFKD PL.
9LR J>V ?B QEFKHFKD, g$RE, QEFP FP KLQEFKD KBT. 7B AL QEFP >II QEB QFJB.h 9LR TLRIA ?B OFDEQ.
)K C>@Q, TB AFA QEFP >P B>OIV >P #E>MQBO 2.
(BOB TB E>SB > JLSBO QE>Q OBPMLKAP QL QTL CLO@BP. 4EFP >II TLOHP KF@BIV ?B@>RPB LC QEB T>V
TB ABPFDKBA QEB +IP?L @I>PP QL >@@RJRI>QB QEB CLO@B SB@QLOP FKQL FQP >@@BIBO>QFLK SB@QLO. )K
QEFP @E>MQBO, ELTBSBO, LRO CLO@BP PQBJ COLJ FKQBOK>I ABPFOBP LC QEB JLSBOP (KLT @>IIBA
SBEF@IBP). !KA QELPB ABPFOBP @>K ?B TBFDEQBA. ,BQfP @LKPFABO > PHBQ@E TEBOB >II SBEF@IBP
E>SB QTL ABPFOBP:
i '006 ?30 8:@>0 7:.,?4:9. '006 ?30 8:@>0 7:.,?4:9.
i '0;,=,?0 1=:8 ,9D A034.70> ?3,? ,=0 ?:: .7:>0. '0;,=,?0 1=:8 ,9D A034.70> ?3,? ,=0 ?:: .7:>0.
.3?=NIL QCH> = H?Q .3?=NIL(0.001,0);
.3?=NIL AL;PCNS = H?Q .3?=NIL(0,0.1);
GIP?L.;JJFS$IL=?(QCH>);
GIP?L.;JJFS$IL=?(AL;PCNS);
#E>MQBO 6. !RQLKLJLRP !DBKQP
306
7B JFDEQ ?BDFK ?V >AAFKD > CRK@QFLK QL QEB 3?BC=F? @I>PP QE>Q J>K>DBP >II LC QEB ?BE>SFLOP.
,BQfP @>II FQ ;JJFS?B;PCILM().
(BOB TB PBB ELT > PFKDIB CRK@QFLK Q>HBP @>OB LC @>IIFKD QEB LQEBO CRK@QFLKP QE>Q >MMIV QEB
CLO@BPdM?J;L;N?() >KA M??E(). 7B @LRIA PQ>OQ JR@HFKD >OLRKA TFQE QELPB CRK@QFLKP >KA PBB
FC TB @>K >AGRPQ QEB PQOBKDQE LC QEB CLO@BP QEBV >OB @>I@RI>QFKD. "RQ FQ TLRIA ?B B>PFBO CLO RP QL
>PH QELPB CRK@QFLKP QL OBQROK QEB CLO@BP PL QE>Q TB @>K >AGRPQ QEBFO PQOBKDQE ?BCLOB >MMIVFKD
QEBJ QL QEB SBEF@IBfP >@@BIBO>QFLK.
,BQfP ILLH >Q ELT QEB PBBH CRK@QFLK @E>KDBA.
4EFP FP > PR?QIB @E>KDB, ?RQ FK@OBAF?IV FJMLOQ>KQ CLO RP: FQ >IILTP RP QL >IQBO QEB PQOBKDQE LC
QEBPB CLO@BP FK LKB MI>@B.
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
M?J;L;N?(P?BC=F?M);
M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
W
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
.3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M);
.3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
We have to apply the force here since
seek() and separate() no longer do so.
;JJFS$IL=?(M?J;L;N?);
;JJFS$IL=?(M??E);
W
.3?=NIL M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?F);
MN??L.FCGCN(G;R@IL=?);
Instead of applying the force we return the
PVector.
;JJFS$IL=?(MN??L);
L?NOLH MN??L;
W
4EB .>QROB LC #LAB (S005)
307
Example 6.8: Combining steering behaviors: Seek and separate
PIC> ;JJFS?B;PCILM(ALL;S*CMN<3?BC=F?> P?BC=F?M) U
.3?=NIL M?J;L;N? = M?J;L;N?(P?BC=F?M);
.3?=NIL M??E = M??E(H?Q .3?=NIL(GIOM?5,GIOM?6));
These values can be whatever you want
them to be! They can be variables that are
customized for each vehicle, or they can
change over time.
I;F7H7J;.CKBJ(1.5);
I;;A.CKBJ(0.5);
;JJFS$IL=?(M?J;L;N?);
;JJFS$IL=?(M??E);
W
2BAL %U>JMIB 6.8 PL QE>Q QEB ?BE>SFLO TBFDEQP >OB KLQ @LKPQ>KQP. 7E>Q E>MMBKP FC
QEBV @E>KDB LSBO QFJB (>@@LOAFKD QL > PFKB T>SB LO 0BOIFK KLFPB)? /O FC PLJB SBEF@IBP
>OB JLOB @LK@BOKBA TFQE PBBHFKD >KA LQEBOP JLOB @LK@BOKBA TFQE PBM>O>QFKD? #>K
VLR FKQOLAR@B LQEBO PQBBOFKD ?BE>SFLOP >P TBII?
Exercise 6.14 Exercise 6.14
6.13 Flocking 6.13 Flocking
&IL@HFKD FP >K DOLRM >KFJ>I ?BE>SFLO QE>Q FP @E>O>@QBOFPQF@ LC J>KV IFSFKD @OB>QROBP, PR@E >P
?FOAP, CFPE, >KA FKPB@QP. )K 1986, #O>FD 2BVKLIAP @OB>QBA > @LJMRQBO PFJRI>QFLK LC CIL@HFKD
?BE>SFLO >KA AL@RJBKQBA QEB >IDLOFQEJ FK EFP M>MBO, g&IL@HP, (BOAP, >KA 3@ELLIP: !
$FPQOF?RQBA "BE>SFLO>I -LABI.h 2B@OB>QFKD QEFP PFJRI>QFLK FK 0OL@BPPFKD TFII ?OFKD QLDBQEBO
>II QEB @LK@BMQP FK QEFP @E>MQBO.
1. 29 K=@@ IG9 H<9 GH99F=B; :CF79 :CFAI@5 (GH99F = 89G=F98 - J9@C7=HM) HC =AD@9A9BH H<9
FI@9G C: :@C7?=B;.
2. /<9G9 GH99F=B; :CF79G K=@@ 69 ;FCID 69<5J=CFG 5B8 F9EI=F9 957< J9<=7@9 HC @CC? 5H
5@@ H<9 CH<9F J9<=7@9G.
3. 29 K=@@ 7CA6=B9 5B8 K9=;<H AI@H=D@9 :CF79G.
4. /<9 F9GI@H K=@@ 69 5 7CAD@9L GMGH9AR=BH9@@=;9BH ;FCID 69<5J=CF K=@@ 9A9F;9 :FCA
H<9 G=AD@9 FI@9G C: :@C7?=B; K=H<CIH H<9 DF9G9B79 C: 5 79BHF5@=N98 GMGH9A CF
@9589F.
4EB DLLA KBTP FP, TBfSB >IOB>AV ALKB FQBJP 1 QEOLRDE 3 FK QEFP @E>MQBO, PL QEFP PB@QFLK TFII
?B >?LRQ GRPQ MRQQFKD FQ >II QLDBQEBO >KA PBBFKD QEB OBPRIQ.
#E>MQBO 6. !RQLKLJLRP !DBKQP
308
"BCLOB TB ?BDFK, ) PELRIA JBKQFLK QE>Q TBfOB DLFKD QL @E>KDB QEB K>JB LC LRO 3?BC=F? @I>PP
(VBQ >D>FK). 2BVKLIAP RPBP QEB QBOJ g?LFAh (> J>AB-RM TLOA QE>Q OBCBOP QL > ?FOA-IFHB L?GB@Q) QL
ABP@OF?B QEB BIBJBKQP LC > CIL@HFKD PVPQBJ >KA TB TFII AL QEB P>JB.
,BQfP Q>HB >K LSBOSFBT LC QEB QEOBB ORIBP LC CIL@HFKD.
1. '0;,=,?4:9 '0;,=,?4:9 (>IPL HKLTK >P g>SLFA>K@Bh): 3QBBO QL >SLFA @LIIFAFKD TFQE VLRO
KBFDE?LOP.
2. A7429809? A7429809? (>IPL HKLTK >P g@LMVh): 3QBBO FK QEB P>JB AFOB@QFLK >P VLRO KBFDE?LOP.
3. C:30>4:9 C:30>4:9 (>IPL HKLTK >P g@BKQBOh): 3QBBO QLT>OAP QEB @BKQBO LC VLRO KBFDE?LOP (PQ>V
TFQE QEB DOLRM).
*RPQ >P TB AFA TFQE LRO PBM>O>QB >KA PBBH BU>JMIB, TBfII T>KQ LRO IC> L?GB@QP QL E>SB >
PFKDIB CRK@QFLK QE>Q J>K>DBP >II QEB >?LSB ?BE>SFLOP. 7BfII @>II QEFP CRK@QFLK @FI=E().
.LT, FQfP GRPQ > J>QQBO LC FJMIBJBKQFKD QEB QEOBB ORIBP. 7B AFA PBM>O>QFLK ?BCLOB; FQfP FABKQF@>I
QL LRO MOBSFLRP BU>JMIB. ,BQfP Q>HB > ILLH >Q >IFDKJBKQ, LO PQBBOFKD FK QEB P>JB AFOB@QFLK >P
F64B?2 6.35
PIC> @FI=E(ALL;S*CMN<IC>> <IC>M) U
The three flocking rules .3?=NIL M?J = M?J;L;N?(<IC>M);
.3?=NIL ;FC = ;FCAH(<IC>M);
.3?=NIL =IB = =IB?MCIH(<IC>M);
Arbitrary weights for these forces (Try
different ones!)
M?J.GOFN(1.5);
;FC.GOFN(1.0);
=IB.GOFN(1.0);
Applying all the forces ;JJFS$IL=?(M?J);
;JJFS$IL=?(;FC);
;JJFS$IL=?(=IB);
W
4EB .>QROB LC #LAB (S005)
309
VLRO KBFDE?LOP. !P TFQE >II LC LRO PQBBOFKD ?BE>SFLOP, TBfSB DLQ QL ?LFI ALTK QEFP @LK@BMQ
FKQL > ABPFOB: QEB ?LFAfP ABPFOBA SBIL@FQV FP QEB >SBO>DB SBIL@FQV LC FQP KBFDE?LOP.
3L LRO >IDLOFQEJ FP QL @>I@RI>QB QEB >SBO>DB SBIL@FQV LC >II QEB LQEBO ?LFAP >KA PBQ QE>Q QL
ABPFOBA.
4EB >?LSB FP MOBQQV DLLA, ?RQ FQfP JFPPFKD LKB O>QEBO @OR@F>I ABQ>FI. /KB LC QEB HBV MOFK@FMIBP
?BEFKA @LJMIBU PVPQBJP IFHB CIL@HFKD FP QE>Q QEB BIBJBKQP (FK QEFP @>PB, ?LFAP) E>SB PELOQ-
O>KDB OBI>QFLKPEFMP. 4EFKHFKD >?LRQ >KQP >D>FK, FQfP MOBQQV B>PV QL FJ>DFKB >K >KQ ?BFKD >?IB
QL PBKPB FQP FJJBAF>QB BKSFOLKJBKQ, ?RQ IBPP PL >K >KQ E>SFKD >K >T>OBKBPP LC TE>Q
>KLQEBO >KQ FP ALFKD ERKAOBAP LC CBBQ >T>V. 4EB C>@Q QE>Q QEB >KQP @>K MBOCLOJ PR@E
@LJMIBU @LIIB@QFSB ?BE>SFLO COLJ LKIV QEBPB KBFDE?LOFKD OBI>QFLKPEFMP FP TE>Q J>HBP QEBJ
PL BU@FQFKD FK QEB CFOPQ MI>@B.
)K LRO >IFDKJBKQ CRK@QFLK, TBfOB Q>HFKD QEB >SBO>DB SBIL@FQV LC >II QEB ?LFAP, TEBOB>P TB
PELRIA OB>IIV LKIV ?B ILLHFKD >Q QEB ?LFAP TFQEFK > @BOQ>FK AFPQ>K@B. 4E>Q AFPQ>K@B QEOBPELIA
FP RM QL VLR, LC @LROPB. 9LR @LRIA ABPFDK ?LFAP QE>Q @>K PBB LKIV QTBKQV MFUBIP >T>V LO
?LFAP QE>Q @>K PBB > ERKAOBA MFUBIP >T>V.
.3?=NIL ;FCAH (ALL;S*CMN<IC>> <IC>M) U
Add up all the velocities and divide by the
total to calculate the average velocity.
.3?=NIL MOG = H?Q .3?=NIL(0,0);
@IL (IC> INB?L : <IC>M) U
MOG.;>>(INB?L.P?FI=CNS);
W
MOG.>CP(<IC>M.MCT?());
We desire to go in that direction at
maximum speed.
MOG.M?N+;A(G;RMJ??>);
Reynoldss steering force formula .3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
310
-R@E IFHB TB AFA TFQE PBM>O>QFLK (LKIV @>I@RI>QFKD > CLO@B CLO LQEBOP TFQEFK > @BOQ>FK AFPQ>K@B),
TBfII T>KQ QL AL QEB P>JB TFQE >IFDKJBKQ (>KA @LEBPFLK).
F64B?2 6.36
.3?=NIL ;FCAH (ALL;S*CMN<IC>> <IC>M) U
This is an arbitrary value and could vary
from boid to boid.
@FI;N H?CAB<IL>CMN = 50;
.3?=NIL MOG = H?Q .3?=NIL(0,0);
CHN =IOHN = 0;
@IL (IC> INB?L : <IC>M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH);
C@ ((> > 0) && (> < H?CAB<IL>CMN)) U
MOG.;>>(INB?L.P?FI=CNS);
For an average, we need to keep track of
how many boids are within the distance.
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
MOG.HILG;FCT?();
MOG.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(MOG,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
If we dont find any close boids, the steering
force is zero.
W ?FM? U
L?NOLH H?Q .3?=NIL(0,0);
W
W
4EB .>QROB LC #LAB (S005)
311
&FK>IIV, TB >OB OB>AV CLO @LEBPFLK. (BOB LRO @LAB FP SFOQR>IIV FABKQF@>I QL QE>Q CLO
>IFDKJBKQdLKIV FKPQB>A LC @>I@RI>QFKD QEB >SBO>DB SBIL@FQV LC QEB ?LFAfP KBFDE?LOP, TB T>KQ
QL @>I@RI>QB QEB >SBO>DB IL@>QFLK LC QEB ?LFAfP KBFDE?LOP (>KA RPB QE>Q >P > Q>ODBQ QL PBBH).
)QfP >IPL TLOQE Q>HFKD QEB QFJB QL TOFQB > @I>PP @>IIBA $FI=E, TEF@E TFII ?B SFOQR>IIV FABKQF@>I
QL QEB .;LNC=F?0SMN?G @I>PP TB TOLQB FK #E>MQBO 4 TFQE LKIV LKB QFKV @E>KDB: 7EBK TB
@>II LOH() LK B>@E IC> L?GB@Q (>P TB AFA QL B>@E .;LNC=F? L?GB@Q), TBfII M>PP FK >
OBCBOBK@B QL QEB BKQFOB ALL;S*CMN LC ?LFAP.
#>K VLR TOFQB QEB >?LSB @LAB PL QE>Q
?LFAP @>K LKIV PBB LQEBO ?LFAP QE>Q >OB
>@QR>IIV TFQEFK QEBFO gMBOFMEBO>Ih SFPFLK
(>P FC QEBV E>A BVBP)?
Exercise 6.15 Exercise 6.15
.3?=NIL =IB?MCIH (ALL;S*CMN<IC>> <IC>M) U
@FI;N H?CAB<IL>CMN = 50;
.3?=NIL MOG = H?Q .3?=NIL(0,0);
CHN =IOHN = 0;
@IL (IC> INB?L : <IC>M) U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,INB?L.FI=;NCIH);
C@ ((> > 0) && (> < H?CAB<IL>CMN)) U
Adding up all the others locations MOG.;>>(INB?L.FI=;NCIH);
=IOHN++;
W
W
C@ (=IOHN > 0) U
MOG.>CP(=IOHN);
Here we make use of the seek() function
we wrote in Example 6.8. The target we
seek is the average location of our
neighbors.
H;JKHD I;;A(IKC);
W ?FM? U
L?NOLH H?Q .3?=NIL(0,0);
W
W
#E>MQBO 6. !RQLKLJLRP !DBKQP
312
!KA LRO J>FK MOLDO>J TFII ILLH IFHB:
Example 6.9: Flocking
=F;MM $FI=E U
ALL;S*CMN<IC>> <IC>M;
$FI=E() U
<IC>M = H?Q ALL;S*CMN<IC>>();
W
PIC> LOH() U
@IL (IC> < : <IC>M) U
Each Boid object must know about all the
other Boids.
8.HKD(8E?:I);
W
W
PIC> ;>>IC>(IC> <) U
<IC>M.;>>(<);
W
W
A Flock object manages the entire group. $FI=E @FI=E;
PIC> M?NOJ() U
MCT?(300,200);
@FI=E = H?Q $FI=E();
@IL (CHN C = 0; C < 100; C++) U
IC> < = H?Q IC>(QC>NB/2,B?CABN/2);
The Flock starts out with 100 Boids. @FI=E.;>>IC>(<);
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
@FI=E.LOH();
W
4EB .>QROB LC #LAB (S005)
313
#LJ?FKB CIL@HFKD TFQE PLJB LQEBO PQBBOFKD ?BE>SFLOP.
Exercise 6.16 Exercise 6.16
)K EFP ?LLH 4EB CCADIH5H=CB5@ B95IHM
C: *5HIF9 (-)4 0OBPP, 2000), '>OV &I>HB
ABP@OF?BP > CLROQE ORIB CLO CIL@HFKD:
g6FBT: JLSB I>QBO>IIV >T>V COLJ >KV
?LFA QE>Q ?IL@HP QEB SFBT.h (>SB VLRO
?LFAP CLIILT QEFP ORIB.
Exercise 6.17 Exercise 6.17
#OB>QB > CIL@HFKD PFJRI>QFLK TEBOB >II LC QEB M>O>JBQBOP (G9D5F5H=CB K9=;<H, 7C<9G=CB
K9=;<H, 5@=;BA9BH K9=;<H, A5L=AIA :CF79, A5L=AIA GD998) @E>KDB LSBO QFJB. 4EBV
@LRIA ?B @LKQOLIIBA ?V 0BOIFK KLFPB LO ?V RPBO FKQBO>@QFLK. (&LO BU>JMIB, VLR @LRIA
RPB > IF?O>OV PR@E >P @LKQOLIM5 (EQQM://TTT.PLG>JL.AB/IF?O>OFBP/@LKQOLI05/) QL QFB QEB
S>IRBP QL PIFABO MLPFQFLKP.)
Exercise 6.18 Exercise 6.18
6FPR>IFWB QEB CIL@H FK >K BKQFOBIV AFCCBOBKQ T>V.
Exercise 6.19 Exercise 6.19
#E>MQBO 6. !RQLKLJLRP !DBKQP
314
6.14 Algorithmic Efficiency (or: Why does my $@(*%! 6.14 Algorithmic Efficiency (or: Why does my $@(*%!
run so slowly?) run so slowly?)
) TLRIA IFHB QL EFAB QEB A>OH QORQE ?BEFKA TBfSB GRPQ ALKB, ?B@>RPB ) TLRIA IFHB VLR QL ?B
E>MMV >KA IFSB > CRICFIIFKD >KA JB>KFKDCRI IFCB. "RQ ) >IPL TLRIA IFHB QL ?B >?IB QL PIBBM >Q KFDEQ
TFQELRQ TLOOVFKD >?LRQ VLR PL JR@E. 3L FQ FP TFQE > EB>SV EB>OQ QE>Q ) JRPQ ?OFKD RM QEFP QLMF@.
'OLRM ?BE>SFLOP >OB TLKABOCRI. "RQ QEBV @>K ?B PILT, >KA QEB JLOB BIBJBKQP FK QEB DOLRM,
QEB PILTBO QEBV @>K ?B. 5PR>IIV, TEBK TB Q>IH >?LRQ 0OL@BPPFKD PHBQ@EBP ORKKFKD PILTIV, FQfP
?B@>RPB AO>TFKD QL QEB P@OBBK @>K ?B PILTdQEB JLOB VLR AO>T, QEB PILTBO VLRO PHBQ@E ORKP.
4EFP FP >@QR>IIV > @>PB, ELTBSBO, TEBOB QEB PILTKBPP ABOFSBP COLJ QEB >IDLOFQEJ FQPBIC. ,BQfP
AFP@RPP.
#LJMRQBO P@FBKQFPQP @I>PPFCV >IDLOFQEJP TFQE PLJBQEFKD @>IIBA g"FD / KLQ>QFLK,h TEF@E
ABP@OF?BP QEB BCCF@FBK@V LC >K >IDLOFQEJ: ELT J>KV @LJMRQ>QFLK>I @V@IBP ALBP FQ OBNRFOB QL
@LJMIBQB? ,BQfP @LKPFABO > PFJMIB >K>ILD PB>O@E MOL?IBJ. 9LR E>SB > ?>PHBQ @LKQ>FKFKD LKB
ERKAOBA @EL@LI>QB QOB>QP, LKIV LKB LC TEF@E FP MROB A>OH @EL@LI>QB. 4E>QfP QEB LKB VLR T>KQ
QL B>Q. 4L CFKA FQ, VLR MF@H QEB @EL@LI>QBP LRQ LC QEB ?>PHBQ LKB ?V LKB. 3ROB, VLR JFDEQ ?B
IR@HV >KA CFKA FQ LK QEB CFOPQ QOV, ?RQ FK QEB TLOPQ-@>PB P@BK>OFL VLR E>SB QL @EB@H >II LKB
ERKAOBA ?BCLOB VLR CFKA QEB A>OH @EL@LI>QB. 4L CFKA LKB QEFKD FK LKB ERKAOBA, VLR E>SB QL
@EB@H LKB ERKAOBA QEFKDP (LO QL CFKA LKB QEFKD FK . QEFKDP, VLR E>SB QL @EB@H . QFJBP.) 9LRO
"FD / .LQ>QFLK FP .. 4EFP, FK@FABKQ>IIV, FP QEB "FD / .LQ>QFLK QE>Q ABP@OF?BP LRO PFJMIB M>OQF@IB
PVPQBJ. )C TB E>SB . M>OQF@IBP, TB E>SB QL ORK >KA AFPMI>V QELPB M>OQF@IBP . QFJBP.
.LT, IBQfP QEFKH >?LRQ > DOLRM ?BE>SFLO (PR@E >P CIL@HFKD). &LO BSBOV IC> L?GB@Q, TB E>SB QL
@EB@H BSBOV LQEBO IC> L?GB@Q (CLO FQP SBIL@FQV >KA IL@>QFLK). ,BQfP P>V TB E>SB LKB ERKAOBA
?LFAP. &LO ?LFA #1, TB KBBA QL @EB@H LKB ERKAOBA ?LFAP; CLO ?LFA #2, TB KBBA QL @EB@H LKB
ERKAOBA ?LFAP, >KA PL LK >KA PL CLOQE. &LO LKB ERKAOBA ?LFAP, TB KBBA QL MBOCLOJ LKB
ERKAOBA QFJBP LKB ERKAOBA @EB@HP, LO QBK QELRP>KA. .L MOL?IBJ: @LJMRQBOP >OB C>PQ >KA @>K
AL QEFKDP QBK QELRP>KA QFJBP MOBQQV B>PFIV. ,BQfP QOV LKB QELRP>KA.
1,000 U 1,000 = 1,000,000 @V@IBP.
/+, QEFP FP O>QEBO PILT, ?RQ PQFII PLJBTE>Q J>K>DB>?IB. ,BQfP QOV 10,000 BIBJBKQP:
10,000 U 10,000 BIBJBKQP = 100,000,000 @V@IBP.
.LT, TBfOB OB>IIV DBQQFKD PILT. 2B>IIV, OB>IIV, OB>IIV PILT.
.LQF@B PLJBQEFKD LAA? !P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V > C>@QLO LC 10, QEB KRJ?BO LC
OBNRFOBA @V@IBP FK@OB>PBP ?V > C>@QLO LC 100. /O >P QEB KRJ?BO LC BIBJBKQP FK@OB>PBP ?V >
C>@QLO LC ., QEB @V@IBP FK@OB>PB ?V > C>@QLO LC . QFJBP .. 4EFP FP HKLTK >P "FD / .LQ>QFLK .-
3NR>OBA.
) HKLT TE>Q VLR >OB QEFKHFKD. 9LR >OB QEFKHFKD: g.L MOL?IBJ; TFQE CIL@HFKD, TB LKIV KBBA QL
@LKPFABO QEB ?LFAP QE>Q >OB @ILPB QL LQEBO ?LFAP. 3L BSBK FC TB E>SB 1,000 ?LFAP, TB @>K GRPQ
ILLH >Q, P>V, QEB 5 @ILPBPQ ?LFAP >KA QEBK TB LKIV E>SB 5,000 @V@IBP.h 9LR M>RPB CLO >
4EB .>QROB LC #LAB (S005)
315
JLJBKQ, >KA QEBK PQ>OQ QEFKHFKD: g3L CLO B>@E ?LFA ) GRPQ KBBA QL @EB@H >II QEB ?LFAP >KA CFKA
QEB CFSB @ILPBPQ LKBP >KA )fJ DLLA!h 3BB QEB @>Q@E-22? %SBK FC TB LKIV T>KQ QL ILLH >Q QEB
@ILPB LKBP, QEB LKIV T>V QL HKLT TE>Q QEB @ILPB LKBP >OB TLRIA ?B QL @EB@H >II LC QEBJ.
/O FP QEBOB >KLQEBO T>V?
,BQfP Q>HB > KRJ?BO QE>Q TB JFDEQ >@QR>IIV T>KQ QL RPB, ?RQ TLRIA PQFII ORK QLL PILTIV: 2,000
(4,000,000 @V@IBP OBNRFOBA).
7E>Q FC TB @LRIA AFSFAB QEB P@OBBK FKQL > DOFA? 7B TLRIA Q>HB >II 2,000 ?LFAP >KA >PPFDK
B>@E ?LFA QL > @BII TFQEFK QE>Q DOFA. 7B TLRIA QEBK ?B >?IB QL ILLH >Q B>@E ?LFA >KA
@LJM>OB FQ QL FQP KBFDE?LOP TFQEFK QE>Q @BII >Q >KV DFSBK JLJBKQ. )J>DFKB > 10 U 10 DOFA. )K >
PVPQBJ LC 2,000 BIBJBKQP, LK >SBO>DB, >MMOLUFJ>QBIV 20 BIBJBKQP TLRIA ?B CLRKA FK B>@E
@BII (20 U 10 U 10 = 2,000). %>@E @BII TLRIA QEBK OBNRFOB 20 U 20 = 400 @V@IBP. 7FQE 100 @BIIP,
TBfA E>SB 100 U 400 = 40,000 @V@IBP, > J>PPFSB P>SFKDP LSBO 4,000,000.
4EFP QB@EKFNRB FP HKLTK >P g?FK-I>QQF@B PM>QF>I PR?AFSFPFLKh >KA FP LRQIFKBA FK JLOB ABQ>FI FK
(PROMOFPB, PROMOFPB) 2BVKLIAPfP 2000 M>MBO, g)KQBO>@QFLK TFQE 'OLRMP LC !RQLKLJLRP
#E>O>@QBOPh (EQQM://TTT.OBA3A.@LJ/@TO/M>MBOP/2000/MFM.MAC). (LT AL TB FJMIBJBKQ PR@E
>K >IDLOFQEJ FK 0OL@BPPFKD? /KB T>V FP QL HBBM JRIQFMIB ALL;S*CMNP. /KB ALL;S*CMN TLRIA
HBBM QO>@H LC >II QEB ?LFAP, GRPQ IFHB FK LRO CIL@HFKD BU>JMIB.
)K >AAFQFLK QL QE>Q ALL;S*CMN, TB PQLOB >K >AAFQFLK>I OBCBOBK@B QL B>@E IC> L?GB@Q FK >
QTL-AFJBKPFLK>I ALL;S*CMN. &LO B>@E @BII FK QEB DOFA, QEBOB FP >K ALL;S*CMN QE>Q QO>@HP QEB
L?GB@QP FK QE>Q @BII.
F64B?2 6.37
ALL;S*CMN<IC>> <IC>M;
#E>MQBO 6. !RQLKLJLRP !DBKQP
316
)K QEB J>FK >L;Q() ILLM, B>@E IC> L?GB@Q QEBK OBDFPQBOP FQPBIC FK QEB >MMOLMOF>QB @BII
>@@LOAFKD QL FQP IL@>QFLK.
4EBK TEBK FQ @LJBP QFJB QL E>SB QEB ?LFAP @EB@H CLO KBFDE?LOP, QEBV @>K ILLH >Q LKIV QELPB FK
QEBFO M>OQF@RI>O @BII (FK QORQE, TB >IPL KBBA QL @EB@H KBFDE?LOFKD @BIIP QL AB>I TFQE ?LOABO
@>PBP).
Example 6.10: Bin-lattice spatial subdivision
7BfOB LKIV @LSBOFKD QEB ?>PF@P EBOB; CLO QEB CRII @LAB, @EB@H QEB ?LLHfP TB?PFQB.
.LT, QEBOB >OB @BOQ>FKIV CI>TP TFQE QEFP PVPQBJ. 7E>Q FC >II QEB ?LFAP @LKDOBD>QB FK QEB @LOKBO
>KA IFSB FK QEB P>JB @BII? 4EBK ALKfQ TB E>SB QL @EB@H >II 2,000 >D>FKPQ >II 2,000?
4EB DLLA KBTP FP QE>Q QEFP KBBA CLO LMQFJFW>QFLK FP > @LJJLK LKB >KA QEBOB >OB > TFAB
S>OFBQV LC PFJFI>O QB@EKFNRBP LRQ QEBOB. &LO RP, FQfP IFHBIV QE>Q > ?>PF@ >MMOL>@E TFII ?B DLLA
BKLRDE (FK JLPQ @>PBP, VLR TLKfQ KBBA LKB >Q >II.) &LO >KLQEBO, JLOB PLMEFPQF@>QBA >MMOL>@E,
@EB@H LRQ QLUF@IF?P' /@QOBB BU>JMIBP (EQQM://QLUF@IF?P.LOD/2010/02/KBT-M>@H>DB-PFJRQFIP/).
ALL;S*CMN<IC>>8989 ALC>;
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH;
CHN LIQ = CHN(<IC>.S) /L?MIFONCIH;
ALC>8=IFOGH98LIQ9.;>>(<IC>);
CHN =IFOGH = CHN(<IC>.R) / L?MIFONCIH;
CHN LIQ = CHN(<IC>.S) /L?MIFONCIH;
<IC>.@FI=E(<IC>M);
Instead of looking at all the boids, just this
cell
<IC>.@FI=E(ALC>8=IFOGH98LIQ9);
6.15 A Few Last Notes: Optimization Tricks 6.15 A Few Last Notes: Optimization Tricks
4EFP FP PLJBQEFKD LC > JLJBKQLRP L@@>PFLK. 4EB BKA LC #E>MQBO 6 J>OHP QEB BKA LC LRO PQLOV
LC JLQFLK (FK QEB @LKQBUQ LC QEFP ?LLH, QE>Q FP). 7B PQ>OQBA TFQE QEB @LK@BMQ LC > SB@QLO, JLSBA
LK QL CLO@BP, ABPFDKBA PVPQBJP LC J>KV BIBJBKQP, BU>JFKBA MEVPF@P IF?O>OFBP, ?RFIQ BKQFQFBP
TFQE ELMBP >KA AOB>JP >KA CB>OP, >KA PFJRI>QBA BJBODBK@B. 4EB PQLOV ALBPKfQ BKA EBOB, ?RQ
FQ ALBP Q>HB > ?FQ LC > QROK. 4EB KBUQ QTL @E>MQBOP TLKfQ CL@RP LK JLSFKD ?LAFBP, ?RQ O>QEBO LK
PVPQBJP LC ORIBP. "BCLOB TB DBQ QEBOB, ) E>SB > CBT NRF@H FQBJP )fA IFHB QL JBKQFLK QE>Q >OB
FJMLOQ>KQ TEBK TLOHFKD TFQE QEB BU>JMIBP FK #E>MQBOP 1 QEOLRDE 6. 4EBV >IPL OBI>QB QL
LMQFJFWFKD VLRO @LAB, TEF@E CFQP FK TFQE QEB MOBSFLRP PB@QFLK.
4EB .>QROB LC #LAB (S005)
317
1) Magnitude squared (or sometimes distance squared) 1) Magnitude squared (or sometimes distance squared)
7E>Q FP J>DKFQRAB PNR>OBA >KA TEBK PELRIA VLR RPB FQ? ,BQfP OBSFPFQ ELT QEB J>DKFQRAB LC
> SB@QLO FP @>I@RI>QBA.
->DKFQRAB OBNRFOBP QEB PNR>OB OLLQ LMBO>QFLK. !KA FQ PELRIA. !CQBO >II, FC VLR T>KQ QEB
J>DKFQRAB LC > SB@QLO, QEBK VLRfSB DLQ QL ILLH RM QEB 0VQE>DLOB>K QEBLOBJ >KA @LJMRQB FQ
(TB AFA QEFP FK #E>MQBO 1). (LTBSBO, FC VLR @LRIA PLJBELT PHFM RPFKD QEB PNR>OB OLLQ, VLRO
@LAB TLRIA ORK C>PQBO. ,BQfP @LKPFABO > PFQR>QFLK TEBOB VLR GRPQ T>KQ QL HKLT QEB OBI>QFSB
J>DKFQRAB LC > SB@QLO. &LO BU>JMIB, FP QEB J>DKFQRAB DOB>QBO QE>K QBK? (!PPRJB > .3?=NIL
P.)
7BII, QEFP FP BNRFS>IBKQ QL P>VFKD:
!KA ELT FP J>DKFQRAB PNR>OBA @>I@RI>QBA?
3>JB >P J>DKFQRAB, ?RQ TFQELRQ QEB PNR>OB OLLQ. )K QEB @>PB LC > PFKDIB .3?=NIL L?GB@Q, QEFP
TFII KBSBO J>HB > PFDKFCF@>KQ AFCCBOBK@B LK > 0OL@BPPFKD PHBQ@E. (LTBSBO, FC VLR >OB
@LJMRQFKD QEB J>DKFQRAB LC QELRP>KAP LC .3?=NIL L?GB@QP B>@E QFJB QEOLRDE >L;Q(), RPFKD
G;A0K() FKPQB>A LC G;A() @LRIA EBIM VLRO @LAB ORK > TBB ?FQ C>PQBO. (.LQB: G;A0K() FP LKIV
>S>FI>?IB FK 0OL@BPPFKD 2.0>1 LO I>QBO.)
@FI;N G;A() U
L?NOLH MKLN(R*R + S*S);
W
C@ (P.G;A() > 10) U
// "I 0IG?NBCHA!
W
C@ (P.G;A0K() > 100) U
// "I 0IG?NBCHA!
W
@FI;N G;A0K() U
L?NOLH R*R + S*S;
W
2) Sine and cosine lookup tables 2) Sine and cosine lookup tables
4EBOBfP > M>QQBOK EBOB. 7E>Q HFKAP LC CRK@QFLKP >OB PILT QL @LJMRQB? 3NR>OB OLLQ. 3FKB.
#LPFKB. 4>KDBKQ. !D>FK, FC VLR GRPQ KBBA > PFKB LO @LPFKB S>IRB EBOB LO QEBOB FK VLRO @LAB,
VLR >OB KBSBO DLFKD QL ORK FKQL > MOL?IBJ. "RQ TE>Q FC VLR E>A PLJBQEFKD IFHB QEFP?
#E>MQBO 6. !RQLKLJLRP !DBKQP
318
3ROB, QEFP FP > QLQ>IIV OFAF@RILRP @LAB PKFMMBQ QE>Q VLR TLRIA KBSBO TOFQB. "RQ FQ FIIRPQO>QBP >
@BOQ>FK MLFKQ. )C VLR >OB @>I@RI>QFKD QEB PFKB LC MF QBK QELRP>KA QFJBP, TEV KLQ GRPQ @>I@RI>QB FQ
LK@B, P>SB QE>Q S>IRB, >KA OBCBO QL FQ TEBKBSBO KB@BPP>OV? 4EFP FP QEB MOFK@FMIB ?BEFKA PFKB
>KA @LPFKB ILLHRM Q>?IBP. )KPQB>A LC @>IIFKD QEB PFKB >KA @LPFKB CRK@QFLKP FK VLRO @LAB
TEBKBSBO VLR KBBA QEBJ, VLR @>K ?RFIA >K >OO>V QE>Q PQLOBP QEB OBPRIQP LC PFKB >KA @LPFKB >Q
>KDIBP ?BQTBBK 0 >KA 14-:.' >KA GRPQ ILLH RM QEB S>IRBP TEBK VLR KBBA QEBJ. &LO BU>JMIB,
EBOB >OB QTL >OO>VP QE>Q PQLOB QEB PFKB >KA @LPFKB S>IRBP CLO BSBOV >KDIB, 0 QL 359 ABDOBBP.
.LT, TE>Q FC VLR KBBA QEB S>IRB LC PFKB LC MF?
! JLOB PLMEFPQF@>QBA BU>JMIB LC QEFP QB@EKFNRB FP >S>FI>?IB LK QEB 0OL@BPPFKD TFHF
(EQQM://TFHF.MOL@BPPFKD.LOD/T/3FK/#LP=ILLH-RM=Q>?IB).
PIC> >L;Q() U
@IL (CHN C = 0; C < 10000; C++) U
JLCHNFH(MCH(.'));
W
W
@FI;N MCHP;FO?M89 = H?Q @FI;N83609;
@FI;N =IMP;FO?M89 = H?Q @FI;N83609;
@IL (CHN C = 0; C < 360; C++) U
MCHP;FO?M8C9 = MCH(L;>C;HM(C));
=IMP;FO?M8C9 = =IM(L;>C;HM(C));
W
CHN ;HAF? = CHN(>?AL??M(.'));
@FI;N ;HMQ?L = MCHP;FO?M8;HAF?9;
3) Making gajillions of unnecessary PVector objects 3) Making gajillions of unnecessary PVector objects
) E>SB QL >AJFQ, ) >J MBOE>MP QEB ?FDDBPQ @RIMOFQ LC QEFP I>PQ KLQB. )K C>@Q, FK QEB FKQBOBPQ LC
TOFQFKD @IB>O >KA RKABOPQ>KA>?IB BU>JMIBP, ) LCQBK @ELLPB QL J>HB BUQO> .3?=NIL L?GB@QP
TEBK ) >?PLIRQBIV AL KLQ KBBA QL. &LO QEB JLPQ M>OQ, QEFP FP KLQ > MOL?IBJ >Q >II. "RQ
PLJBQFJBP, FQ @>K ?B. ,BQfP Q>HB > ILLH >Q >K BU>JMIB.
,BQfP P>V LRO ALL;S*CMN LC SBEF@IBP E>P LKB QELRP>KA SBEF@IBP FK FQ. 7B GRPQ J>AB LKB
QELRP>KA KBT .3?=NIL L?GB@QP BSBOV PFKDIB QFJB QEOLRDE >L;Q(). .LT, LK >KV LIf I>MQLM LO
ABPHQLM @LJMRQBO VLRfSB MRO@E>PBA FK OB@BKQ QFJBP, VLRO PHBQ@E TFII IFHBIV KLQ OBDFPQBO >
@LJMI>FKQ, ORK PILTIV, LO E>SB >KV MOL?IBJP. !CQBO >II, VLRfSB DLQ QLKP LC 2!-, >KA *>S> TFII
PIC> >L;Q() U
@IL (3?BC=F? P : P?BC=F?M) U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
P.M??E(GIOM?);
W
W
4EB .>QROB LC #LAB (S005)
319
?B >?IB QL E>KAIB J>HFKD > QELRP>KA LO PL QBJMLO>OV L?GB@QP >KA AFPMLPB LC QEBJ TFQELRQ
JR@E LC > MOL?IBJ.
)C VLRO KRJ?BOP DOLT I>ODBO (>KA QEBV B>PFIV @LRIA) LO MBOE>MP JLOB IFHBIV, FC VLR >OB
TLOHFKD TFQE 0OL@BPPFKD LK !KAOLFA, VLR TFII >IJLPQ @BOQ>FKIV ORK FKQL > MOL?IBJ. )K @>PBP
IFHB QEFP VLR T>KQ QL ILLH CLO T>VP QL OBAR@B QEB KRJ?BO LC .3?=NIL L?GB@QP VLR J>HB. !K
L?SFLRP CFU CLO QEB >?LSB @LAB FP:
.LT VLRfSB J>AB GRPQ LKB .3?=NIL FKPQB>A LC LKB QELRP>KA. %SBK ?BQQBO, VLR @LRIA QROK
QEB .3?=NIL FKQL > DIL?>I S>OF>?IB >KA GRPQ >PPFDK QEB R >KA S S>IRB:
.LT VLR KBSBO J>HB > KBT .3?=NIL; VLR RPB GRPQ LKB LSBO QEB IBKDQE LC VLRO PHBQ@E!
4EOLRDELRQ QEB ?LLHfP BU>JMIBP, VLR @>K CFKA ILQP LC LMMLOQRKFQFBP QL OBAR@B QEB KRJ?BO LC
QBJMLO>OV L?GB@QP. ,BQfP ILLH >Q LKB JLOB. (BOB FP > PKFMMBQ COLJ LRO M??E() CRK@QFLK.
3BB ELT TBfSB J>AB QTL .3?=NIL L?GB@QP? &FOPQ, TB CFDROB LRQ QEB ABPFOBA SB@QLO, QEBK TB
@>I@RI>QB QEB PQBBOFKD CLO@B. .LQF@B ELT TB @LRIA OBTOFQB QEFP QL @OB>QB LKIV LKB .3?=NIL.
PIC> >L;Q() U
.3?=NIL GIOM? = H?Q .3?=NIL(GIOM?5,GIOM?6);
@IL (3?BC=F? P : P?BC=F?M) U
P.M??E(GIOM?);
W
W
.3?=NIL GIOM? = H?Q .3?=NIL();
PIC> >L;Q() U
GIOM?.R = GIOM?5;
GIOM?.S = GIOM?6;
@IL (3?BC=F? P : P?BC=F?M) U
P.M??E(GIOM?);
W
W
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
Create a new PVector to store the steering
force.
+0;9JEH IJ;;H = +0;9JEH.IK8(:;I?H;:,L;BE9?JO);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N, FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
#E>MQBO 6. !RQLKLJLRP !DBKQP
320
7B ALKfQ >@QR>IIV KBBA > PB@LKA .3?=NIL @>IIBA MN??L. 7B @LRIA GRPQ RPB QEB ABPFOBA
.3?=NIL L?GB@Q >KA QROK FQ FKQL QEB PQBBOFKD CLO@B ?V PR?QO>@QFKD SBIL@FQV. ) AFAKfQ AL QEFP FK JV
BU>JMIB ?B@>RPB FQ FP JLOB @LKCRPFKD QL OB>A. "RQ FK PLJB @>PBP, FQ J>V ?B DOB>QIV JLOB
BCCF@FBKQ.
Calculate the steering force in the desired
PVector.
:;I?H;:.IK8(L;BE9?JO);
>?MCL?>.FCGCN(G;R@IL=?);
L?NOLH >?MCL?>;
%IFJFK>QB >P J>KV QBJMLO>OV .3?=NIL L?GB@QP COLJ QEB CIL@HFKD BU>JMIB >P MLPPF?IB.
!IPL RPB G;A0K() TEBOB MLPPF?IB.
Exercise 6.20 Exercise 6.20
5PB PQBBOFKD ?BE>SFLOP TFQE "LU2$ LO QLUF@IF?P.
Exercise 6.21 Exercise 6.21
4EB .>QROB LC #LAB (S005)
321
The Ecosystem Project The Ecosystem Project
Step 6 Exercise:
Use the concept of steering forces to drive the behavior of the creatures in your
ecosystem. Some possibilities:
Create schools or flocks of creatures.
Use a seeking behavior for creatures to search for food (for chasing
moving prey, consider pursuit).
Use a flow field for the ecosystem environment. For example, how does
your system behave if the creatures live in a flowing river?
Build a creature with countless steering behaviors (as many as you can
reasonably add). Think about ways to vary the weights of these
behaviors so that you can dial those behaviors up and down, mixing and
matching on the fly. How are creatures initial weights set? What rules
drive how the weights change over time?
Complex systems can be nested. Can you design a single creature out of
a flock of boids? And can you then make a flock of those creatures?
Complex systems can have memory (and be adaptive). Can the history of
your ecosystem affect the behavior in its current state? (This could be
the driving force behind how the creatures adjust their steering force
weights.)
#E>MQBO 6. !RQLKLJLRP !DBKQP
322
Chapter 7. Cellular Chapter 7. Cellular
Automata Automata
I*< =9.F 9632 F<B :B@A 5.C2 . 3.6?9F 9.?42 052082?/<.?1 .;1 . =92;A63B9
@B==9F <3 39.A 0<B;A2?@ <3 AD< 0<9<?@. !A 6@ =<@@6/92 A< D<?8 D6A5 =2;069 .;1
4?.=5 =.=2? /BA 6A 6@ :B05 2.@62?, =.?A60B9.?9F 3<? /246;;2?@, A< B@2
0<B;A2?@ .;1 . /<.?1.J
Martin Gardner, .7=9BH=:=7 AA9F=75B (October 1970)
)K QEFP @E>MQBO, TBfOB DLFKD QL Q>HB > ?OB>H COLJ Q>IHFKD >?LRQ SB@QLOP >KA JLQFLK. )K C>@Q, QEB
OBPQ LC QEB ?LLH TFII JLPQIV CL@RP LK PVPQBJP >KA >IDLOFQEJP (>I?BFQ LKBP QE>Q TB @>K, PELRIA,
>KA TFII >MMIV QL JLSFKD ?LAFBP). )K QEB MOBSFLRP @E>MQBO, TB BK@LRKQBOBA LRO CFOPQ 0OL@BPPFKD
BU>JMIB LC > @LJMIBU PVPQBJ: CIL@HFKD. 7B ?OFBCIV PQ>QBA QEB @LOB MOFK@FMIBP ?BEFKA @LJMIBU
PVPQBJP: JLOB QE>K QEB PRJ LC FQP M>OQP, > @LJMIBU PVPQBJ FP > PVPQBJ LC BIBJBKQP, LMBO>QFKD
FK M>O>IIBI, TFQE PELOQ-O>KDB OBI>QFLKPEFMP QE>Q >P > TELIB BUEF?FQ BJBODBKQ ?BE>SFLO. 4EFP
BKQFOB @E>MQBO FP DLFKD QL ?B ABAF@>QBA QL ?RFIAFKD >KLQEBO @LJMIBU PVPQBJ PFJRI>QFLK FK
0OL@BPPFKD. /AAIV, TB >OB DLFKD QL Q>HB PLJB PQBMP ?>@HT>OA >KA PFJMIFCV QEB BIBJBKQP LC
LRO PVPQBJ. .L ILKDBO >OB QEB FKAFSFAR>I BIBJBKQP DLFKD QL ?B JBJ?BOP LC > MEVPF@P TLOIA;
FKPQB>A TB TFII ?RFIA > PVPQBJ LRQ LC QEB PFJMIBPQ AFDFQ>I BIBJBKQ MLPPF?IB, > PFKDIB ?FQ. 4EFP ?FQ
FP DLFKD QL ?B @>IIBA > @BII >KA FQP S>IRB (0 LO 1) TFII ?B @>IIBA FQP PQ>QB. 7LOHFKD TFQE PR@E
PFJMIB BIBJBKQP TFII EBIM RP RKABOPQ>KA JLOB LC QEB ABQ>FIP ?BEFKA ELT @LJMIBU PVPQBJP
TLOH, >KA TBfII >IPL ?B >?IB QL BI>?LO>QB LK PLJB MOLDO>JJFKD QB@EKFNRBP QE>Q TB @>K >MMIV
QL @LAB-?>PBA MOLGB@QP.
4EB .>QROB LC #LAB (S005)
323
7.1 What Is a Cellular Automaton? 7.1 What Is a Cellular Automaton?
&FOPQ, IBQfP DBQ LKB QEFKD PQO>FDEQ. 4EB QBOJ .077@7,= ,@?:8,?, .077@7,= ,@?:8,?, FP MIRO>I. /RO @LAB BU>JMIBP
TFII PFJRI>QB GRPQ LKBd> .077@7,= ,@?:8,?:9 .077@7,= ,@?:8,?:9, PFKDRI>O. 4L PFJMIFCV LRO IFSBP, TBfII >IPL OBCBO
QL @BIIRI>O >RQLJ>Q> >P g#!.h
)K #E>MQBOP 1 QEOLRDE 6, LRO L?GB@QP (JLSBO, M>OQF@IB, SBEF@IB, ?LFA) DBKBO>IIV BUFPQBA FK LKIV
LKB gPQ>QB.h 4EBV JFDEQ E>SB JLSBA >OLRKA TFQE >AS>K@BA ?BE>SFLOP >KA MEVPF@P, ?RQ
RIQFJ>QBIV QEBV OBJ>FKBA QEB P>JB QVMB LC L?GB@Q LSBO QEB @LROPB LC QEBFO AFDFQ>I IFCBQFJB.
7BfSB >IIRABA QL QEB MLPPF?FIFQV QE>Q QEBPB BKQFQFBP @>K @E>KDB LSBO QFJB (CLO BU>JMIB, QEB
TBFDEQP LC PQBBOFKD gABPFOBPh @>K S>OV), ?RQ TB E>SBKfQ CRIIV MRQ QEFP FKQL MO>@QF@B. )K QEFP
@LKQBUQ, @BIIRI>O >RQLJ>Q> J>HB > DOB>Q CFOPQ PQBM FK ?RFIAFKD > PVPQBJ LC J>KV L?GB@QP QE>Q
E>SB S>OVFKD PQ>QBP LSBO QFJB.
! @BIIRI>O >RQLJ>QLK FP > JLABI LC > PVPQBJ LC g@BIIh L?GB@QP TFQE QEB CLIILTFKD
@E>O>@QBOFPQF@P.
i 4EB @BIIP IFSB LK > 2=4/ 2=4/. (7BfII PBB BU>JMIBP FK ?LQE LKB >KA QTL AFJBKPFLKP FK
QEFP @E>MQBO, QELRDE > @BIIRI>O >RQLJ>QLK @>K BUFPQ FK >KV CFKFQB KRJ?BO LC
AFJBKPFLKP.)
i %>@E @BII E>P > >?,?0 >?,?0. 4EB KRJ?BO LC PQ>QB MLPPF?FIFQFBP FP QVMF@>IIV CFKFQB. 4EB
PFJMIBPQ BU>JMIB E>P QEB QTL MLPPF?FIFQFBP LC 1 >KA 0 (LQEBOTFPB OBCBOOBA QL >P gLKh
>KA gLCCh LO g>IFSBh >KA gAB>Ah).
i %>@E @BII E>P > 90423-:=3::/ 90423-:=3::/. 4EFP @>K ?B ABCFKBA FK >KV KRJ?BO LC T>VP, ?RQ FQ FP
QVMF@>IIV > IFPQ LC >AG>@BKQ @BIIP.
4EB ABSBILMJBKQ LC @BIIRI>O >RQLJ>Q> PVPQBJP FP QVMF@>IIV >QQOF?RQBA QL 3Q>KFP[>T 5I>J >KA
*LEK SLK .BRJ>KK, TEL TBOB ?LQE OBPB>O@EBOP >Q QEB ,LP !I>JLP .>QFLK>I ,>?LO>QLOV FK
F64B?2 7.1
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
324
.BT -BUF@L FK QEB 1940P. 5I>J T>P PQRAVFKD QEB DOLTQE LC @OVPQ>IP >KA SLK .BRJ>KK T>P
FJ>DFKFKD > TLOIA LC PBIC-OBMIF@>QFKD OL?LQP. 4E>QfP OFDEQ, OL?LQP QE>Q ?RFIA @LMFBP LC
QEBJPBISBP. /K@B TB PBB PLJB BU>JMIBP LC #! SFPR>IFWBA, FQfII ?B @IB>O ELT LKB JFDEQ
FJ>DFKB JLABIFKD @OVPQ>I DOLTQE; QEB OL?LQP FAB> FP MBOE>MP IBPP L?SFLRP. #LKPFABO QEB ABPFDK
LC > OL?LQ >P > M>QQBOK LK > DOFA LC @BIIP (QEFKH LC CFIIFKD FK PLJB PNR>OBP LK > MFB@B LC DO>ME
M>MBO). .LT @LKPFABO > PBQ LC PFJMIB ORIBP QE>Q TLRIA >IILT QE>Q M>QQBOK QL @OB>QB @LMFBP LC
FQPBIC LK QE>Q DOFA. 4EFP FP BPPBKQF>IIV QEB MOL@BPP LC > #! QE>Q BUEF?FQP ?BE>SFLO PFJFI>O QL
?FLILDF@>I OBMOLAR@QFLK >KA BSLIRQFLK. ()K@FABKQ>IIV, SLK .BRJ>KKfP @BIIP E>A QTBKQV-KFKB
MLPPF?IB PQ>QBP.) 6LK .BRJ>KKfP TLOH FK PBIC-OBMIF@>QFLK >KA #! FP @LK@BMQR>IIV PFJFI>O QL
TE>Q FP MOL?>?IV QEB JLPQ C>JLRP @BIIRI>O >RQLJ>QLK: QEB g'>JB LC ,FCB,h TEF@E TB TFII
AFP@RPP FK ABQ>FI FK PB@QFLK 7.3.
0BOE>MP QEB JLPQ PFDKFCF@>KQ P@FBKQFCF@ (>KA IBKDQEV) TLOH PQRAVFKD @BIIRI>O >RQLJ>Q> >OOFSBA FK
2002: 3QBMEBK 7LICO>JfP 1,280-M>DB A *9K '=B8 C: .7=9B79 (EQQM://TTT.TLICO>JP@FBK@B.@LJ/
KHPLKIFKB/QL@.EQJI). !S>FI>?IB FK FQP BKQFOBQV CLO COBB LKIFKB, 7LICO>JfP ?LLH AFP@RPPBP ELT #!
>OB KLQ PFJMIV KB>Q QOF@HP, ?RQ >OB OBIBS>KQ QL QEB PQRAV LC ?FLILDV, @EBJFPQOV, MEVPF@P, >KA >II
?O>K@EBP LC P@FBK@B. 4EFP @E>MQBO TFII ?>OBIV P@O>Q@E QEB PROC>@B LC QEB QEBLOFBP 7LICO>J
LRQIFKBP (TB TFII CL@RP LK QEB @LAB FJMIBJBKQ>QFLK) PL FC QEB BU>JMIBP MOLSFABA PM>OH VLRO
@ROFLPFQV, VLRfII CFKA MIBKQV JLOB QL OB>A >?LRQ FK EFP ?LLH.
7.2 Elementary Cellular Automata 7.2 Elementary Cellular Automata
4EB BU>JMIBP FK QEFP @E>MQBO TFII ?BDFK TFQE > PFJRI>QFLK LC 7LICO>JfP TLOH. 4L RKABOPQ>KA
7LICO>JfP BIBJBKQ>OV #!, TB PELRIA >PH LROPBISBP QEB NRBPQFLK: g7E>Q FP QEB PFJMIBPQ @BIIRI>O
>RQLJ>QLK TB @>K FJ>DFKB?h 7E>QfP BU@FQFKD >?LRQ QEFP NRBPQFLK >KA FQP >KPTBO FP QE>Q BSBK
TFQE QEB PFJMIBPQ #! FJ>DFK>?IB, TB TFII PBB QEB MOLMBOQFBP LC @LJMIBU PVPQBJP >Q TLOH.
,BQfP ?RFIA 7LICO>JfP BIBJBKQ>OV #! COLJ P@O>Q@E. #LK@BMQP CFOPQ, QEBK @LAB. 7E>Q >OB QEB
QEOBB HBV BIBJBKQP LC > #!?
1) G=4/ G=4/. 4EB PFJMIBPQ DOFA TLRIA ?B LKB-AFJBKPFLK>I: > IFKB LC @BIIP.
2) '?,?0> '?,?0>. 4EB PFJMIBPQ PBQ LC PQ>QBP (?BVLKA E>SFKD LKIV LKB PQ>QB) TLRIA ?B QTL PQ>QBP: 0 LO
1.
F64B?2 7.2
F64B?2 7.3
4EB .>QROB LC #LAB (S005)
325
3) #0423-:=3::/ #0423-:=3::/. 4EB PFJMIBPQ KBFDE?LOELLA FK LKB AFJBKPFLK CLO >KV DFSBK @BII TLRIA ?B
QEB @BII FQPBIC >KA FQP QTL >AG>@BKQ KBFDE?LOP: LKB QL QEB IBCQ >KA LKB QL QEB OFDEQ.
3L TB ?BDFK TFQE > IFKB LC @BIIP, B>@E TFQE >K FKFQF>I PQ>QB (IBQfP P>V FQ FP O>KALJ), >KA B>@E
TFQE QTL KBFDE?LOP. 7BfII E>SB QL CFDROB LRQ TE>Q TB T>KQ QL AL TFQE QEB @BIIP LK QEB BADBP
(PFK@B QELPB E>SB LKIV LKB KBFDE?LO B>@E), ?RQ QEFP FP PLJBQEFKD TB @>K PLOQ LRQ I>QBO.
7B E>SBKfQ VBQ AFP@RPPBA, ELTBSBO, TE>Q FP MBOE>MP QEB JLPQ FJMLOQ>KQ ABQ>FI LC ELT
@BIIRI>O >RQLJ>Q> TLOHdH=A9. 7BfOB KLQ OB>IIV Q>IHFKD >?LRQ OB>I-TLOIA QFJB EBOB, ?RQ >?LRQ
QEB #! IFSFKD LSBO > MBOFLA LC H=A9, TEF@E @LRIA >IPL ?B @>IIBA > 2090=,?4:9 2090=,?4:9 >KA, FK LRO
@>PB, TFII IFHBIV OBCBO QL QEB 1=,80 .:@9? 1=,80 .:@9? LC >K >KFJ>QFLK. 4EB CFDROBP >?LSB PELT RP QEB #!
>Q QFJB BNR>IP 0 LO DBKBO>QFLK 0. 4EB NRBPQFLKP TB E>SB QL >PH LROPBISBP >OB: $CK 8C K9
7CADIH9 H<9 GH5H9G :CF 5@@ 79@@G 5H ;9B9F5H=CB 1? AB8 ;9B9F5H=CB 2? !KA PL LK >KA PL CLOQE.
,BQfP P>V TB E>SB >K FKAFSFAR>I @BII FK QEB #!, >KA IBQfP @>II FQ #%,,. 4EB CLOJRI> CLO
@>I@RI>QFKD #%,,fP PQ>QB >Q >KV DFSBK QFJB N FP >P CLIILTP:
C!'' IJ7J; 7J J?C; J = <(C!'' D;?=>8EH>EE: 7J J?C; J - 1)
)K LQEBO TLOAP, > @BIIfP KBT PQ>QB FP > CRK@QFLK LC >II QEB PQ>QBP FK QEB @BIIfP KBFDE?LOELLA >Q
QEB MOBSFLRP JLJBKQ FK QFJB (LO AROFKD QEB MOBSFLRP DBKBO>QFLK). 7B @>I@RI>QB > KBT PQ>QB
S>IRB ?V ILLHFKD >Q >II QEB MOBSFLRP KBFDE?LO PQ>QBP.
F64B?2 7.4: A ;2645/<?5<<1 6@ A5?22 0299@.
F64B?2 7.5: *52 2142 0299 <;9F 5.@ . ;2645/<?5<<1 <3 AD<.
F64B?2 7.6
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
326
.LT, FK QEB TLOIA LC @BIIRI>O >RQLJ>Q>, QEBOB >OB J>KV T>VP TB @LRIA @LJMRQB > @BIIfP PQ>QB
COLJ > DOLRM LC @BIIP. #LKPFABO ?IROOFKD >K FJ>DB. ('RBPP TE>Q? )J>DB MOL@BPPFKD TLOHP TFQE
#!-IFHB ORIBP.) ! MFUBIfP KBT PQ>QB (F.B. FQP @LILO) FP QEB >SBO>DB LC >II LC FQP KBFDE?LOPf @LILOP.
7B @LRIA >IPL P>V QE>Q > @BIIfP KBT PQ>QB FP QEB PRJ LC >II LC FQP KBFDE?LOPf PQ>QBP. 7FQE
7LICO>JfP BIBJBKQ>OV #!, ELTBSBO, TB @>K >@QR>IIV AL PLJBQEFKD > ?FQ PFJMIBO >KA PBBJFKDIV
>?PROA: 7B @>K ILLH >Q >II QEB MLPPF?IB @LKCFDRO>QFLKP LC > @BII >KA FQP KBFDE?LO >KA ABCFKB QEB
PQ>QB LRQ@LJB CLO BSBOV MLPPF?IB @LKCFDRO>QFLK. )Q PBBJP OFAF@RILRPdTLRIAKfQ QEBOB ?B T>V QLL
J>KV MLPPF?FIFQFBP CLO QEFP QL ?B MO>@QF@>I? ,BQfP DFSB FQ > QOV.
7B E>SB QEOBB @BIIP, B>@E TFQE > PQ>QB LC 0 LO 1. (LT J>KV MLPPF?IB T>VP @>K TB @LKCFDROB
QEB PQ>QBP? )C VLR ILSB ?FK>OV, VLRfII KLQF@B QE>Q QEOBB @BIIP ABCFKB > 3-?FQ KRJ?BO, >KA ELT EFDE
@>K VLR @LRKQ TFQE 3 ?FQP? 5M QL 8. ,BQfP E>SB > ILLH.
/K@B TB E>SB ABCFKBA >II QEB MLPPF?IB KBFDE?LOELLAP, TB KBBA QL ABCFKB >K LRQ@LJB (KBT
PQ>QB S>IRB: 0 LO 1) CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK.
4EB PQ>KA>OA 7LICO>J JLABI FP QL PQ>OQ DBKBO>QFLK 0 TFQE >II @BIIP E>SFKD > PQ>QB LC 0 BU@BMQ
CLO QEB JFAAIB @BII, TEF@E PELRIA E>SB > PQ>QB LC 1.
F64B?2 7.7
F64B?2 7.8
F64B?2 7.9
F64B?2 7.10
4EB .>QROB LC #LAB (S005)
327
2BCBOOFKD QL QEB ORIBPBQ >?LSB, IBQfP PBB ELT > DFSBK @BII (TBfII MF@H QEB @BKQBO LKB) TLRIA
@E>KDB COLJ DBKBO>QFLK 0 QL DBKBO>QFLK 1.
4OV >MMIVFKD QEB P>JB ILDF@ QL >II LC QEB @BIIP >?LSB >KA CFII FK QEB BJMQV @BIIP.
.LT, IBQfP DL M>PQ GRPQ LKB DBKBO>QFLK >KA @LILO QEB @BIIP d0 JB>KP TEFQB, 1 JB>KP
?I>@Hd>KA PQ>@H QEB DBKBO>QFLKP, TFQE B>@E KBT DBKBO>QFLK >MMB>OFKD ?BILT QEB MOBSFLRP
LKB.
4EB ILT-OBPLIRQFLK PE>MB TBfOB PBBFKD >?LSB FP QEB g3FBOMF\PHF QOF>KDIB.h .>JBA >CQBO QEB
0LIFPE J>QEBJ>QF@F>K 7>@[>T 3FBOMF\PHF, FQfP > CO>@Q>I M>QQBOK QE>Q TBfII BU>JFKB FK QEB KBUQ
@E>MQBO. 4E>QfP OFDEQ: QEFP FK@OBAF?IV PFJMIB PVPQBJ LC 0P >KA 1P, TFQE IFQQIB KBFDE?LOELLAP LC
QEOBB @BIIP, @>K DBKBO>QB > PE>MB >P PLMEFPQF@>QBA >KA ABQ>FIBA >P QEB 3FBOMF\PHF QOF>KDIB.
,BQfP ILLH >Q FQ >D>FK, LKIV TFQE B>@E @BII > PFKDIB MFUBI TFAB PL QE>Q QEB OBPLIRQFLK FP JR@E
EFDEBO.
F64B?2 7.11
F64B?2 7.12: (B92 90
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
328
4EFP M>OQF@RI>O OBPRIQ AFAKfQ E>MMBK ?V >@@FABKQ. ) MF@HBA QEFP PBQ LC ORIBP ?B@>RPB LC QEB
M>QQBOK FQ DBKBO>QBP. 4>HB > ILLH >Q &FDROB 7.8 LKB JLOB QFJB. .LQF@B ELT QEBOB >OB BFDEQ
MLPPF?IB KBFDE?LOELLA @LKCFDRO>QFLKP; TB QEBOBCLOB ABCFKB > gORIBPBQh >P > IFPQ LC 8 ?FQP.
3L QEFP M>OQF@RI>O ORIB @>K ?B FIIRPQO>QBA >P CLIILTP:
%FDEQ 0P >KA 1P JB>KP >K 8-?FQ KRJ?BO. (LT J>KV @LJ?FK>QFLKP LC BFDEQ 0P >KA 1P >OB QEBOB?
256. 4EFP FP GRPQ IFHB ELT TB ABCFKB QEB @LJMLKBKQP LC >K 2'" @LILO. 7B DBQ 8 ?FQP CLO OBA,
DOBBK, >KA ?IRB, JB>KFKD TB J>HB @LILOP TFQE S>IRBP COLJ 0 QL 255 (256 MLPPF?FIFQFBP).
)K QBOJP LC > 7LICO>J BIBJBKQ>OV #!, TB E>SB KLT AFP@LSBOBA QE>Q QEBOB >OB 256 MLPPF?IB
ORIBPBQP. 4EB >?LSB ORIBPBQ FP @LJJLKIV OBCBOOBA QL >P g2RIB 90h ?B@>RPB FC VLR @LKSBOQ QEB
?FK>OV PBNRBK@Bd01011010dQL > AB@FJ>I KRJ?BO, VLRfII DBQ QEB FKQBDBO 90. ,BQfP QOV ILLHFKD >Q
QEB OBPRIQP LC >KLQEBO ORIBPBQ.
F64B?2 7.13: (B92 90
F64B?2 7.14: (B92 90
4EB .>QROB LC #LAB (S005)
329
!P TB @>K KLT PBB, QEB PFJMIB >@Q LC
@OB>QFKD > #! >KA ABCFKFKD > ORIBPBQ ALBP
KLQ DR>O>KQBB SFPR>IIV FKQBOBPQFKD OBPRIQP.
/RQ LC >II 256 ORIBPBQP, LKIV > E>KACRI
MOLAR@B @LJMBIIFKD LRQ@LJBP. (LTBSBO,
FQfP NRFQB FK@OBAF?IB QE>Q BSBK LKB LC QEBPB
ORIBPBQP CLO > LKB-AFJBKPFLK>I #! TFQE LKIV
QTL MLPPF?IB PQ>QBP @>K MOLAR@B QEB
M>QQBOKP TB PBB BSBOV A>V FK K>QROB (PBB
&FDROB 7.16), >KA FQ ABJLKPQO>QBP ELT
S>IR>?IB QEBPB PVPQBJP @>K ?B FK
PFJRI>QFLK >KA M>QQBOK DBKBO>QFLK.
"BCLOB TB DL QLL C>O ALTK QEB OL>A LC ELT
7LICO>J @I>PPFCFBP QEB OBPRIQP LC S>OVFKD
ORIBPBQP, IBQfP ILLH >Q ELT TB >@QR>IIV ?RFIA
> 0OL@BPPFKD PHBQ@E QE>Q DBKBO>QBP QEB
7LICO>J #! >KA SFPR>IFWBP FQ LKP@OBBK.
F64B?2 7.15: (B92 222
F64B?2 7.16: A *2EA692 C<;2 );.69 (C<;B@ A2EA692),
C<1 <92, G?2.A B.??62? (223, AB@A?.96., 7
AB4B@A 2005. '5<A<4?.=52?: (605.?1 #6;4
?605.?1@?2@2.?05.0.;<;.0<:..B
7.3 How to Program an Elementary CA 7.3 How to Program an Elementary CA
9LR J>V ?B QEFKHFKD: g/+, )fSB DLQ QEFP @BII QEFKD. !KA QEB @BII QEFKD E>P PLJB MOLMBOQFBP,
IFHB > PQ>QB, TE>Q DBKBO>QFLK FQfP LK, TEL FQP KBFDE?LOP >OB, TEBOB FQ IFSBP MFUBI-TFPB LK QEB
P@OBBK. !KA J>V?B FQ E>P PLJB CRK@QFLKP: FQ @>K AFPMI>V FQPBIC, FQ @>K DBKBO>QB FQP KBT PQ>QB,
BQ@.h 4EFP IFKB LC QEFKHFKD FP >K BU@BIIBKQ LKB >KA TLRIA IFHBIV IB>A VLR QL TOFQB PLJB @LAB
IFHB QEFP:
=F;MM !?FF U
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
330
4EFP IFKB LC QEFKHFKD, ELTBSBO, FP KLQ QEB OL>A TB TFII CFOPQ QO>SBI. ,>QBO FK QEFP @E>MQBO, TB TFII
AFP@RPP TEV >K L?GB@Q-LOFBKQBA >MMOL>@E @LRIA MOLSB S>IR>?IB FK ABSBILMFKD > #! PFJRI>QFLK,
?RQ QL ?BDFK, TB @>K TLOH TFQE > JLOB BIBJBKQ>OV A>Q> PQOR@QROB. !CQBO >II, TE>Q FP >K
BIBJBKQ>OV #! ?RQ > IFPQ LC 0P >KA 1P? #BOQ>FKIV, TB @LRIA ABP@OF?B QEB CLIILTFKD #!
DBKBO>QFLK RPFKD >K >OO>V:
4L AO>T QE>Q >OO>V, TB PFJMIV @EB@H FC TBfSB DLQ > 0 LO > 1 >KA @OB>QB > CFII >@@LOAFKDIV.
.LT QE>Q TB E>SB QEB >OO>V QL ABP@OF?B QEB @BII PQ>QBP LC > DFSBK DBKBO>QFLK (TEF@E TBfII
RIQFJ>QBIV @LKPFABO QEB g@ROOBKQh DBKBO>QFLK), TB KBBA > JB@E>KFPJ ?V TEF@E QL @LJMRQB QEB
KBUQ DBKBO>QFLK. ,BQfP QEFKH >?LRQ QEB MPBRAL@LAB LC TE>Q TB >OB ALFKD >Q QEB JLJBKQ.
For every cell in the array: For every cell in the array:
i (,60 , 7::6 ,? ?30 90423-:=3::/ >?,?0>: 701?, 84//70, =423?. (,60 , 7::6 ,? ?30 90423-:=3::/ >?,?0>: 701?, 84//70, =423?.
i !::6 @; ?30 90B A,7@0 1:= ?30 .077 >?,?0 ,..:=/492 ?: >:80 =@70>0?. !::6 @; ?30 90B A,7@0 1:= ?30 .077 >?,?0 ,..:=/492 ?: >:80 =@70>0?.
i '0? ?30 .077H> >?,?0 ?: ?3,? 90B A,7@0. '0? ?30 .077H> >?,?0 ?: ?3,? 90B A,7@0.
4EFP J>V IB>A VLR QL TOFQB PLJB @LAB IFHB QEFP:
F64B?2 7.17
CHN89 =?FFM = U1,0,1,0,0,0,0,1,0,1,1,1,0,0,0,1,1,1,0,0W;
Loop through every cell. @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
C@ (=?FFM8C9 == 0) @CFF(255);
Create a fill based on its state (0 or 1). ?FM? @CFF(0);
MNLIE?(0);
L?=N(C*50,0,50,50);
W
For every cell in the array... @IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
...take a look at the neighborhood. CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
Look up the new value according to the
rules.
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
4EB .>QROB LC #LAB (S005)
331
7BfOB C>FOIV @ILPB QL DBQQFKD QEFP OFDEQ, ?RQ TBfSB J>AB LKB JFKLO ?IRKABO >KA LKB J>GLO
?IRKABO FK QEB >?LSB @LAB. ,BQfP Q>IH >?LRQ TE>Q TBfSB ALKB TBII PL C>O.
.LQF@B ELT B>PV FQ FP QL ILLH >Q > @BIIfP KBFDE?LOP. "B@>RPB >K >OO>V FP >K LOABOBA IFPQ LC
A>Q>, TB @>K RPB QEB C>@Q QE>Q QEB FKAF@BP >OB KRJ?BOBA QL HKLT TEF@E @BIIP >OB KBUQ QL
TEF@E @BIIP. 7B HKLT QE>Q @BII KRJ?BO 15, CLO BU>JMIB, E>P @BII 14 QL FQP IBCQ >KA 16 QL FQP
OFDEQ. -LOB DBKBO>IIV, TB @>K P>V QE>Q CLO >KV @BII C, FQP KBFDE?LOP >OB C-1 >KA C+1.
7BfOB >IPL C>OJFKD LRQ QEB @>I@RI>QFLK LC > KBT PQ>QB S>IRB QL PLJB CRK@QFLK @>IIBA LOF?M().
/?SFLRPIV, TBfOB DLFKD QL E>SB QL TOFQB QEFP CRK@QFLK LROPBISBP, ?RQ QEB MLFKQ TBfOB J>HFKD
EBOB FP JLARI>OFQV. 7B E>SB > ?>PF@ CO>JBTLOH CLO QEB #! FK QEFP CRK@QFLK, >KA FC TB I>QBO
T>KQ QL @E>KDB ELT QEB ORIBP LMBO>QB, TB ALKfQ E>SB QL QLR@E QE>Q CO>JBTLOH; TB @>K
PFJMIV OBTOFQB QEB LOF?M() CRK@QFLK QL @LJMRQB QEB KBT PQ>QBP AFCCBOBKQIV.
3L TE>Q E>SB TB ALKB TOLKD? ,BQfP Q>IH QEOLRDE ELT QEB @LAB TFII BUB@RQB. &FOPQ, TB ILLH
>Q @BII FKABU C BNR>IP 0. .LT IBQfP ILLH >Q 0fP KBFDE?LOP. ,BCQ FP FKABU -1. -FAAIB FP FKABU 0.
!KA OFDEQ FP FKABU 1. (LTBSBO, LRO >OO>V ?V ABCFKFQFLK ALBP KLQ E>SB >K BIBJBKQ TFQE QEB
FKABU -1. )Q PQ>OQP TFQE 0. 4EFP FP > MOL?IBJ TBfSB >IIRABA QL ?BCLOB: QEB BADB @>PBP.
(LT AL TB AB>I TFQE QEB @BIIP LK QEB BADB TEL ALKfQ E>SB > KBFDE?LO QL ?LQE QEBFO IBCQ >KA
QEBFO OFDEQ? (BOB >OB QEOBB MLPPF?IB PLIRQFLKP QL QEFP MOL?IBJ:
1. E/20> =08,49 .:9>?,9?. E/20> =08,49 .:9>?,9?. 4EFP FP MBOE>MP QEB PFJMIBPQ PLIRQFLK. 7B KBSBO ?LQEBO QL
BS>IR>QB QEB BADBP >KA >IT>VP IB>SB QEBFO PQ>QB S>IRB @LKPQ>KQ (0 LO 1).
2. E/20> B=,; ,=:@9/. E/20> B=,; ,=:@9/. 4EFKH LC QEB #! >P > PQOFM LC M>MBO >KA QROK QE>Q PQOFM LC
M>MBO FKQL > OFKD. 4EB @BII LK QEB IBCQ BADB FP > KBFDE?LO LC QEB @BII LK QEB OFDEQ
>KA SF@B SBOP>. 4EFP @>K @OB>QB QEB >MMB>O>K@B LC >K FKCFKFQB DOFA >KA FP MOL?>?IV
QEB JLPQ RPBA PLIRQFLK.
3. E/20> 3,A0 /4110=09? 90423-:=3::/> ,9/ =@70>. E/20> 3,A0 /4110=09? 90423-:=3::/> ,9/ =@70>. )C TB T>KQBA QL, TB @LRIA QOB>Q
QEB BADB @BIIP AFCCBOBKQIV >KA @OB>QB ORIBP CLO @BIIP QE>Q E>SB > KBFDE?LOELLA LC
QTL FKPQB>A LC QEOBB. 9LR J>V T>KQ QL AL QEFP FK PLJB @FO@RJPQ>K@BP, ?RQ FK LRO
@>PB, FQfP DLFKD QL ?B > ILQ LC BUQO> IFKBP LC @LAB CLO IFQQIB ?BKBCFQ.
4L J>HB QEB @LAB B>PFBPQ QL OB>A >KA RKABOPQ>KA OFDEQ KLT, TBfII DL TFQE LMQFLK #1 >KA GRPQ
PHFM QEB BADB @>PBP, IB>SFKD QEBFO S>IRBP @LKPQ>KQ. 4EFP @>K ?B >@@LJMIFPEBA ?V PQ>OQFKD QEB
ILLM LKB @BII I>QBO >KA BKAFKD LKB @BII B>OIFBO:
Set the cells state to the new value. =?FF8C9 = H?QMN;N?;
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
332
4EBOBfP LKB JLOB MOL?IBJ TB E>SB QL CFU ?BCLOB TBfOB ALKB. )QfP PR?QIB >KA VLR TLKfQ DBQ >
@LJMFI>QFLK BOOLO; QEB #! GRPQ TLKfQ MBOCLOJ @LOOB@QIV. (LTBSBO, FABKQFCVFKD QEFP MOL?IBJ FP
>?PLIRQBIV CRKA>JBKQ>I QL QEB QB@EKFNRBP ?BEFKA MOLDO>JJFKD #! PFJRI>QFLKP. )Q >II IFBP FK QEFP
IFKB LC @LAB:
4EFP PBBJP IFHB > MBOCB@QIV FKKL@BKQ IFKB. !CQBO >II, TBfSB @LJMRQBA QEB KBT PQ>QB S>IRB >KA
TBfOB PFJMIV DFSFKD QEB @BII FQP KBT PQ>QB. "RQ FK QEB KBUQ FQBO>QFLK, VLRfII AFP@LSBO > J>PPFSB
?RD. ,BQfP P>V TBfSB GRPQ @LJMRQBA QEB KBT PQ>QB CLO @BII #5. 7E>Q AL TB AL KBUQ? 7B
@>I@RI>QB QEB KBT PQ>QB S>IRB CLO @BII #6.
C9@@ #6, ;9B9F5H=CB 0 = GCA9 GH5H9, 0 CF 1
C9@@ #6, ;9B9F5H=CB 1 = 5 :IB7H=CB C: GH5H9G :CF .077 #5 .077 #5, 79@@ #6, 5B8 79@@ #7 5H *;9B9F5H=CB 0*
.LQF@B ELT TB KBBA QEB S>IRB LC @BII #5 >Q DBKBO>QFLK 0 FK LOABO QL @>I@RI>QB @BII #6fP KBT
PQ>QB >Q DBKBO>QFLK 1? ! @BIIfP KBT PQ>QB FP > CRK@QFLK LC QEB MOBSFLRP KBFDE?LO PQ>QBP. $L TB
HKLT @BII #5fP S>IRB >Q DBKBO>QFLK 0? 2BJBJ?BO, 0OL@BPPFKD GRPQ BUB@RQBP QEFP IFKB LC @LAB
CLO = = 5.
/K@B QEFP E>MMBKP, TB KL ILKDBO E>SB >@@BPP QL @BII #5fP PQ>QB >Q DBKBO>QFLK 0, >KA @BII FKABU
5 FP PQLOFKD QEB S>IRB CLO DBKBO>QFLK 1. 7B @>KKLQ LSBOTOFQB QEB S>IRBP FK QEB >OO>V TEFIB TB
>OB MOL@BPPFKD QEB >OO>V, ?B@>RPB TB KBBA QELPB S>IRBP QL @>I@RI>QB QEB KBT S>IRBP. !
PLIRQFLK QL QEFP MOL?IBJ FP QL E>SB QTL >OO>VP, LKB QL PQLOB QEB @ROOBKQ DBKBO>QFLK PQ>QBP >KA
LKB CLO QEB KBUQ DBKBO>QFLK PQ>QBP.
A loop that ignores the first and last cell <EH (?DJ ? = 1; ? < 9;BBI.B;D=J>-1; ?++) Q
CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
=?FF8C9 = H?QMN;N?;
W
=?FF8C9 = H?QMN;N?;
=?FF8C9 = H?QMN;N?;
Another array to store the states for the next
generation.
?DJ45 D;M9;BBI = D;M ?DJ49;BBI.B;D=J>5;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
Look at the states from the current array. CHN F?@N = =?FF8C-19;
CHN GC>>F? = =?FF8C9;
CHN LCABN = =?FF8C+19;
CHN H?QMN;N? = LOF?M(F?@N,GC>>F?,LCABN);
Saving the new state in the new array D;M9;BBI4?5 = D;MIJ7J;;
W
4EB .>QROB LC #LAB (S005)
333
/K@B QEB BKQFOB >OO>V LC S>IRBP FP MOL@BPPBA, TB @>K QEBK AFP@>OA QEB LIA >OO>V >KA PBQ FQ
BNR>I QL QEB KBT >OO>V LC PQ>QBP.
7BfOB >IJLPQ ALKB. 4EB >?LSB @LAB FP @LJMIBQB BU@BMQ CLO QEB C>@Q QE>Q TB E>SBKfQ VBQ
TOFQQBK QEB LOF?M() CRK@QFLK QE>Q @LJMRQBP QEB KBT PQ>QB S>IRB ?>PBA LK QEB KBFDE?LOELLA
(IBCQ, JFAAIB, >KA OFDEQ @BIIP). 7B HKLT QE>Q CRK@QFLK KBBAP QL OBQROK >K FKQBDBO (0 LO 1) >P
TBII >P OB@BFSB QEOBB >ODRJBKQP (CLO QEB QEOBB KBFDE?LOP).
.LT, QEBOB >OB J>KV T>VP TB @LRIA TOFQB QEFP CRK@QFLK, ?RQ )fA IFHB QL PQ>OQ TFQE > ILKD-
TFKABA LKB QE>Q TFII ELMBCRIIV MOLSFAB > @IB>O FIIRPQO>QFLK LC TE>Q TB >OB ALFKD.
,BQfP CFOPQ BPQ>?IFPE ELT TB >OB PQLOFKD QEB ORIBPBQ. 4EB ORIBPBQ, FC VLR OBJBJ?BO COLJ QEB
MOBSFLRP PB@QFLK, FP > PBOFBP LC 8 ?FQP (0 LO 1) QE>Q ABCFKBP QE>Q LRQ@LJB CLO BSBOV MLPPF?IB
KBFDE?LOELLA @LKCFDRO>QFLK.
7B @>K PQLOB QEFP ORIBPBQ FK 0OL@BPPFKD >P >K >OO>V.
!KA QEBK P>V:
)C IBCQ, JFAAIB, >KA OFDEQ >II E>SB QEB PQ>QB 1, QEBK QE>Q J>Q@EBP QEB @LKCFDRO>QFLK 111 >KA QEB
KBT PQ>QB PELRIA ?B BNR>I QL QEB CFOPQ S>IRB FK QEB ORIBPBQ >OO>V. 7B @>K KLT ARMIF@>QB QEFP
PQO>QBDV CLO >II BFDEQ MLPPF?FIFQFBP.
The new generation becomes the current
generation.
9;BBI = D;M9;BBI;
Function receives 3 ints and returns 1. CHN LOF?M (CHN ;, CHN <, CHN =) U
F64B?2 7.14 (?2=2.A21)
CHN89 LOF?M?N = U0,1,0,1,1,0,1,0W;
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
334
) IFHB E>SFKD QEB BU>JMIB TOFQQBK >P >?LSB ?B@>RPB FQ ABP@OF?BP IFKB ?V IFKB BU>@QIV TE>Q FP
E>MMBKFKD CLO B>@E KBFDE?LOELLA @LKCFDRO>QFLK. (LTBSBO, FQfP KLQ > DOB>Q PLIRQFLK. !CQBO >II,
TE>Q FC TB ABPFDK > #! QE>Q E>P 4 MLPPF?IB PQ>QBP (0-3) >KA PRAABKIV TB E>SB 64 MLPPF?IB
KBFDE?LOELLA @LKCFDRO>QFLKP? 7FQE 10 MLPPF?IB PQ>QBP, TB E>SB 1,000 @LKCFDRO>QFLKP. #BOQ>FKIV
TB ALKfQ T>KQ QL QVMB FK 1,000 IFKBP LC @LAB!
!KLQEBO PLIRQFLK, QELRDE MBOE>MP > ?FQ JLOB AFCCF@RIQ QL CLIILT, FP QL @LKSBOQ QEB KBFDE?LOELLA
@LKCFDRO>QFLK (> 3-?FQ KRJ?BO) FKQL > OBDRI>O FKQBDBO >KA RPB QE>Q S>IRB >P QEB FKABU FKQL QEB
ORIBPBQ >OO>V. 4EFP @>K ?B ALKB FK *>S> IFHB PL.
4EBOBfP LKB QFKV MOL?IBJ TFQE QEFP PLIRQFLK, ELTBSBO. ,BQfP P>V TB >OB FJMIBJBKQFKD ORIB 222:
!KA TB E>SB QEB KBFDE?LOELLA g111h. 4EB OBPRIQFKD PQ>QB FP BNR>I QL ORIBPBQ FKABU 0, >P TB PBB
FK QEB CFOPQ T>V TB TOLQB QEB CRK@QFLK.
)C TB @LKSBOQ g111h QL > AB@FJ>I KRJ?BO, TB DBQ 7. "RQ TB ALKfQ T>KQ ORIBPBQ;7<; TB T>KQ
ORIBPBQ;0<. &LO QEFP QL TLOH, TB KBBA QL TOFQB QEB ORIBPBQ TFQE QEB ?FQP FK OBSBOPB LOABO, F.B.
CHN LOF?M (CHN ;, CHN <, CHN =) U
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
?FM? C@ (; == 1 && < == 1 && = == 0) L?NOLH LOF?M?N819;
?FM? C@ (; == 1 && < == 0 && = == 1) L?NOLH LOF?M?N829;
?FM? C@ (; == 1 && < == 0 && = == 0) L?NOLH LOF?M?N839;
?FM? C@ (; == 0 && < == 1 && = == 1) L?NOLH LOF?M?N849;
?FM? C@ (; == 0 && < == 1 && = == 0) L?NOLH LOF?M?N859;
?FM? C@ (; == 0 && < == 0 && = == 1) L?NOLH LOF?M?N869;
?FM? C@ (; == 0 && < == 0 && = == 0) L?NOLH LOF?M?N879;
For this function to be valid, we have to
make sure something is returned in cases
where the states do not match one of the
eight possibilities. We know this is
impossible given the rest of our code, but
Processing does not.
L?NOLH 0;
W
CHN LOF?M (CHN ;, CHN <, CHN =) U
A quick way to join three bits into a String 0NLCHA M = "" + ; + < + =;
The second argument 2 indicates that we
intend to parse a binary number (base 2).
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
Rule 222 CHN89 LOF?M?N = U1,1,0,1,1,1,1,0W;
C@ (; == 1 && < == 1 && = == 1) L?NOLH LOF?M?N809;
4EB .>QROB LC #LAB (S005)
335
3L C>O FK QEFP PB@QFLK, TBfSB TOFQQBK BSBOVQEFKD TB KBBA QL @LJMRQB QEB DBKBO>QFLKP CLO >
7LICO>J BIBJBKQ>OV #!. ,BQfP Q>HB > JLJBKQ QL LOD>KFWB QEB >?LSB @LAB FKQL > @I>PP, TEF@E
TFII RIQFJ>QBIV EBIM FK QEB ABPFDK LC LRO LSBO>II PHBQ@E.
Rule 222 in reverse order CHN89 LOF?M?N = U0,1,1,1,1,0,1,1W;
=F;MM !A U
We need an array for the cells and one for
the rules.
CHN89 =?FFM;
CHN89 LOF?M?N;
!A() U
=?FFM = H?Q CHN8QC>NB9;
Arbitrarily starting with rule 90 LOF?M?N = U0,1,0,1,1,0,1,0W;
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
=?FFM8C9 = 0;
W
All cells start with state 0, except the center
cell has state 1.
=?FFM8=?FFM.F?HANB/29 = 1;
W
PIC> A?H?L;N?() U
Compute the next generation. CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
CHN F?@N = =?FFM8C-19;
CHN G? = =?FFM8C9;
CHN LCABN = =?FFM8C+19;
H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN);
W
=?FFM = H?RNA?H;
W
Look up a new state from the ruleset. CHN LOF?M (CHN ;, CHN <, CHN =) U
0NLCHA M = "" + ; + < + =;
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
W
7.4 Drawing an Elementary CA 7.4 Drawing an Elementary CA
7E>QfP JFPPFKD? 0OBPRJ>?IV, FQfP LRO FKQBKQFLK QL AFPMI>V @BIIP >KA QEBFO PQ>QBP FK SFPR>I CLOJ.
!P TB P>T B>OIFBO, QEB PQ>KA>OA QB@EKFNRB CLO ALFKD QEFP FP QL PQ>@H QEB DBKBO>QFLKP LKB LK
QLM LC B>@E LQEBO >KA AO>T > OB@Q>KDIB QE>Q FP ?I>@H (CLO PQ>QB 1) LO TEFQB (CLO PQ>QB 0).
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
336
"BCLOB TB FJMIBJBKQ QEFP M>OQF@RI>O SFPR>IFW>QFLK, )fA IFHB QL MLFKQ LRQ QTL QEFKDP.
/KB, QEFP SFPR>I FKQBOMOBQ>QFLK LC QEB A>Q> FP @LJMIBQBIV IFQBO>I. )QfP RPBCRI CLO ABJLKPQO>QFKD QEB
>IDLOFQEJP >KA OBPRIQP LC 7LICO>JfP BIBJBKQ>OV #!, ?RQ FQ PELRIAKfQ KB@BPP>OFIV AOFSB VLRO LTK
MBOPLK>I TLOH. )QfP O>QEBO RKIFHBIV QE>Q VLR >OB ?RFIAFKD > MOLGB@Q QE>Q KBBAP MOB@FPBIV QEFP
>IDLOFQEJ TFQE QEFP SFPR>I PQVIB. 3L TEFIB IB>OKFKD QL AO>T QEB #! FK QEFP T>V TFII EBIM VLR
RKABOPQ>KA >KA FJMIBJBKQ #! PVPQBJP, QEFP PHFII PELRIA BUFPQ LKIV >P > CLRKA>QFLK.
3B@LKA, QEB C>@Q QE>Q TB >OB SFPR>IFWFKD > LKB-AFJBKPFLK>I #! TFQE > QTL-AFJBKPFLK>I FJ>DB
@>K ?B @LKCRPFKD. )QfP SBOV FJMLOQ>KQ QL OBJBJ?BO QE>Q QEFP FP KLQ > 2$ #!. 7B >OB PFJMIV
@ELLPFKD QL PELT > EFPQLOV LC >II QEB DBKBO>QFLKP PQ>@HBA SBOQF@>IIV. 4EFP QB@EKFNRB @OB>QBP >
QTL-AFJBKPFLK>I FJ>DB LRQ LC J>KV FKPQ>K@BP LC LKB-AFJBKPFLK>I A>Q>. "RQ QEB PVPQBJ FQPBIC FP
LKB-AFJBKPFLK>I. ,>QBO, TB >OB DLFKD QL ILLH >Q >K >@QR>I 2$ #! (QEB '>JB LC ,FCB) >KA
AFP@RPP ELT TB JFDEQ @ELLPB QL AFPMI>V PR@E > PVPQBJ.
4EB DLLA KBTP FP QE>Q AO>TFKD QEB #! FP KLQ M>OQF@RI>OIV AFCCF@RIQ. ,BQfP ?BDFK ?V ILLHFKD >Q
ELT TB TLRIA OBKABO > PFKDIB DBKBO>QFLK. !PPRJB TB E>SB > 0OL@BPPFKD TFKALT 600 MFUBIP
TFAB >KA TB T>KQ B>@E @BII QL ?B > 10U10 PNR>OB. 7B QEBOBCLOB E>SB > #! TFQE 60 @BIIP. /C
@LROPB, TB @>K @>I@RI>QB QEFP S>IRB AVK>JF@>IIV.
!PPRJFKD TBfSB DLKB QEOLRDE QEB MOL@BPP LC DBKBO>QFKD QEB @BII PQ>QBP (TEF@E TB AFA FK QEB
MOBSFLRP PB@QFLK), TB @>K KLT ILLM QEOLRDE QEB BKQFOB >OO>V LC @BIIP, AO>TFKD > ?I>@H @BII TEBK
QEB PQ>QB FP 1 >KA > TEFQB LKB TEBK QEB PQ>QB FP 0.
F64B?2 7.12 (?2=2.A21)
CHN Q = 10;
How many cells fit across given a certain
width
CHN89 =?FFM = H?Q CHN8QC>NB/Q9;
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
Black or white fill? C@ (=?FFM8C9 == 1) @CFF(0);
?FM? @CFF(255);
4EB .>QROB LC #LAB (S005)
337
)K QORQE, TB @LRIA LMQFJFWB QEB >?LSB ?V E>SFKD > TEFQB ?>@HDOLRKA >KA LKIV AO>TFKD TEBK
QEBOB FP > ?I>@H @BII (P>SFKD RP QEB TLOH LC AO>TFKD J>KV TEFQB PNR>OBP), ?RQ FK JLPQ @>PBP
QEFP PLIRQFLK FP DLLA BKLRDE (>KA KB@BPP>OV CLO LQEBO JLOB PLMEFPQF@>QBA ABPFDKP TFQE
S>OVFKD @LILOP, BQ@.) !IPL, FC TB T>KQBA B>@E @BII QL ?B OBMOBPBKQBA >P > PFKDIB MFUBI, TB
TLRIA KLQ T>KQ QL RPB 0OL@BPPFKDfP L?=N() CRK@QFLK, ?RQ O>QEBO >@@BPP QEB MFUBI >OO>V
AFOB@QIV.
)K QEB >?LSB @LAB, VLRfII KLQF@B QEB V-IL@>QFLK CLO B>@E OB@Q>KDIB FP 0. )C TB T>KQ QEB
DBKBO>QFLKP QL ?B AO>TK KBUQ QL B>@E LQEBO, TFQE B>@E OLT LC @BIIP J>OHFKD > KBT
DBKBO>QFLK, TBfII >IPL KBBA QL @LJMRQB > V-IL@>QFLK ?>PBA LK ELT J>KV FQBO>QFLKP LC QEB #!
TBfSB BUB@RQBA. 7B @LRIA >@@LJMIFPE QEFP ?V >AAFKD > gDBKBO>QFLKh S>OF>?IB (>K FKQBDBO) QL
LRO #! @I>PP >KA FK@OBJBKQFKD FQ B>@E QFJB QEOLRDE A?H?L;N?(). 7FQE QEBPB >AAFQFLKP, TB
@>K KLT ILLH >Q QEB #! @I>PP TFQE >II QEB CB>QROBP CLO ?LQE @LJMRQFKD >KA AO>TFKD QEB #!.
Example 7.1: Wolfram elementary cellular automata
Notice how the x-location is the cell index
times the cell width. In the above scenario,
this would give us cells located at x equals
0, 10, 20, 30, all the way up to 600.
L?=N(C*Q, 0, Q, Q);
W
=F;MM !A U
CHN89 =?FFM;
CHN89 LOF?M?N;
CHN Q = 10;
The CA should keep track of how many
generations.
CHN A?H?L;NCIH = 0;
!A() U
=?FFM = H?Q CHN8QC>NB/Q9;
LOF?M?N = U0,1,0,1,1,0,1,0W;
=?FFM8=?FFM.F?HANB/29 = 1;
W
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
338
Function to compute the next generation PIC> A?H?L;N?() U
CHN89 H?RNA?H = H?Q CHN8=?FFM.F?HANB9;
@IL (CHN C = 1; C < =?FFM.F?HANB-1; C++) U
CHN F?@N = =?FFM8C-19;
CHN G? = =?FFM8C9;
CHN LCABN = =?FFM8C+19;
H?RNA?H8C9 = LOF?M(F?@N, G?, LCABN);
W
=?FFM = H?RNA?H;
Increment the generation counter. A?H?L;NCIH++;
W
CHN LOF?M(CHN ;, CHN <, CHN =) U
0NLCHA M = "" + ; + < + =;
CHN CH>?R = 'HN?A?L.J;LM?'HN(M,2);
L?NOLH LOF?M?N8CH>?R9;
W
@IL (CHN C = 0; C < =?FFM.F?HANB; C++) U
C@ (=?FFM8C9 == 1) @CFF(0);
?FM? @CFF(255);
Set the y-location according to the
generation.
L?=N(C*Q, A?H?L;NCIH*Q, Q, Q);
W
W
%UM>KA %U>JMIB 7.1 QL E>SB QEB CLIILTFKD CB>QROB: TEBK QEB #! OB>@EBP QEB ?LQQLJ LC
QEB 0OL@BPPFKD TFKALT, QEB #! PQ>OQP LSBO TFQE > KBT, O>KALJ ORIBPBQ.
Exercise 7.1 Exercise 7.1
%U>JFKB TE>Q M>QQBOKP L@@RO FC VLR FKFQF>IFWB QEB CFOPQ DBKBO>QFLK TFQE B>@E @BII E>SFKD >
O>KALJ PQ>QB.
Exercise 7.2 Exercise 7.2
6FPR>IFWB QEB #! FK > KLK-QO>AFQFLK>I T>V. "OB>H >II QEB ORIBP VLR @>K; ALKfQ CBBI QFBA QL
RPFKD PNR>OBP LK > MBOCB@Q DOFA TFQE ?I>@H >KA TEFQB.
Exercise 7.3 Exercise 7.3
4EB .>QROB LC #LAB (S005)
339
#OB>QB > SFPR>IFW>QFLK LC QEB #! QE>Q P@OLIIP RMT>OAP >P QEB DBKBO>QFLKP FK@OB>PB PL
QE>Q VLR @>K SFBT QEB DBKBO>QFLKP QL gFKCFKFQV.h (FKQ: FKPQB>A LC HBBMFKD QO>@H LC LKIV
LKB DBKBO>QFLK >Q > QFJB, VLRfII KBBA QL PQLOB > EFPQLOV LC DBKBO>QFLKP, >IT>VP >AAFKD
> KBT LKB >KA ABIBQFKD QEB LIABPQ LKB FK B>@E CO>JB.
Exercise 7.4 Exercise 7.4
7.5 Wolfram Classification 7.5 Wolfram Classification
"BCLOB TB JLSB LK QL ILLHFKD >Q #! FK QTL AFJBKPFLKP, FQfP TLOQE Q>HFKD > ?OFBC ILLH >Q
7LICO>JfP @I>PPFCF@>QFLK CLO @BIIRI>O >RQLJ>Q>. !P TB KLQBA B>OIFBO, QEB S>PQ J>GLOFQV LC
BIBJBKQ>OV #! ORIBPBQP MOLAR@B RKFKPMFOFKD OBPRIQP, TEFIB PLJB OBPRIQ FK TLKAOLRPIV
@LJMIBU M>QQBOKP IFHB QELPB CLRKA FK K>QROB. 7LICO>J E>P AFSFABA RM QEB O>KDB LC LRQ@LJBP
FKQL CLRO @I>PPBP:
C7,>> 1: )941:=84?D. C7,>> 1: )941:=84?D. #I>PP 1 #!P BKA RM, >CQBO PLJB KRJ?BO LC DBKBO>QFLKP, TFQE BSBOV @BII
@LKPQ>KQ. 4EFP FP KLQ QBOOF?IV BU@FQFKD QL T>Q@E. 2RIB 222 (>?LSB) FP > @I>PP 1 #!; FC VLR ORK FQ
CLO BKLRDE DBKBO>QFLKP, BSBOV @BII TFII BSBKQR>IIV ?B@LJB >KA OBJ>FK ?I>@H.
F64B?2 7.18: (B92 222
F64B?2 7.19: (B92 190
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
340
C7,>> 2: &0;0?4?4:9. C7,>> 2: &0;0?4?4:9. ,FHB @I>PP 1 #!P, @I>PP 2 #!P OBJ>FK PQ>?IB, ?RQ QEB @BII PQ>QBP >OB KLQ
@LKPQ>KQ. 2>QEBO, QEBV LP@FII>QB FK PLJB OBDRI>O M>QQBOK ?>@H >KA CLOQE COLJ 0 QL 1 QL 0 QL 1 >KA
PL LK. )K ORIB 190 (>?LSB), B>@E @BII CLIILTP QEB PBNRBK@B 11101110111011101110.
C7,>> 3: &,9/:8. C7,>> 3: &,9/:8. #I>PP 3 #!P >MMB>O O>KALJ >KA E>SB KL B>PFIV AFP@BOKF?IB M>QQBOK. )K C>@Q,
ORIB 30 (>?LSB) FP RPBA >P > O>KALJ KRJ?BO DBKBO>QLO FK 7LICO>JfP ->QEBJ>QF@> PLCQT>OB.
!D>FK, QEFP FP > JLJBKQ TEBOB TB @>K CBBI >J>WBA QE>Q PR@E > PFJMIB PVPQBJ TFQE PFJMIB ORIBP
@>K ABP@BKA FKQL > @E>LQF@ >KA O>KALJ M>QQBOK.
C7,>> 4: C:8;70C4?D. C7,>> 4: C:8;70C4?D. #I>PP 4 #!P @>K ?B QELRDEQ LC >P > JFU ?BQTBBK @I>PP 2 >KA @I>PP 3.
/KB @>K CFKA OBMBQFQFSB, LP@FII>QFKD M>QQBOKP FKPFAB QEB #!, ?RQ TEBOB >KA TEBK QEBPB M>QQBOKP
>MMB>O FP RKMOBAF@Q>?IB >KA PBBJFKDIV O>KALJ. #I>PP 4 #! BUEF?FQ QEB MOLMBOQFBP LC @LJMIBU
PVPQBJP QE>Q TB ABP@OF?BA B>OIFBO FK QEFP @E>MQBO >KA FK #E>MQBO 6. )C > @I>PP 3 #! TLTBA VLR,
QEBK > @I>PP 4 IFHB 2RIB 110 >?LSB PELRIA OB>IIV ?ILT VLRO JFKA.
F64B?2 7.20: (B92 30
F64B?2 7.21: (B92 110
%UBO@FPB: #OB>QB > 0OL@BPPFKD PHBQ@E QE>Q P>SBP >K FJ>DB CLO BSBOV MLPPF?IB ORIBPBQ.
#>K VLR @I>PPFCV QEBJ?
Exercise 7.5 Exercise 7.5
4EB .>QROB LC #LAB (S005)
341
7.6 The Game of Life 7.6 The Game of Life
4EB KBUQ PQBM TB >OB DLFKD QL Q>HB FP QL JLSB COLJ > LKB-AFJBKPFLK>I #! QL > QTL-
AFJBKPFLK>I LKB. 4EFP TFII FKQOLAR@B PLJB >AAFQFLK>I @LJMIBUFQV; B>@E @BII TFII E>SB > ?FDDBO
KBFDE?LOELLA, ?RQ QE>Q TFII LMBK RM QEB ALLO QL > O>KDB LC MLPPF?IB >MMIF@>QFLKP. !CQBO >II,
JLPQ LC TE>Q TB AL FK @LJMRQBO DO>MEF@P IFSBP FK QTL AFJBKPFLKP, >KA QEFP @E>MQBO TFII
ABJLKPQO>QB ELT QL >MMIV #! QEFKHFKD QL TE>Q TB AO>T FK LRO 0OL@BPPFKD PHBQ@EBP.
)K 1970, ->OQFK '>OAKBO TOLQB >K >OQF@IB FK .7=9BH=:=7 AA9F=75B QE>Q AL@RJBKQBA
J>QEBJ>QF@F>K *LEK #LKT>VfP KBT g'>JB LC ,FCB,h ABP@OF?FKD FQ >P gOB@OB>QFLK>Ih
J>QEBJ>QF@P >KA PRDDBPQFKD QE>Q QEB OB>ABO DBQ LRQ > @EBPP?L>OA >KA PLJB @EB@HBOP >KA
gMI>V.h 7EFIB QEB '>JB LC ,FCB E>P ?B@LJB PLJBQEFKD LC > @LJMRQ>QFLK>I @IF@EZ (J>HB KLQB
LC QEB JVOF>A MOLGB@QP QE>Q AFPMI>V QEB '>JB LC ,FCB LK ,%$P, P@OBBKP, MOLGB@QFLK PROC>@BP,
BQ@.), FQ FP PQFII FJMLOQ>KQ CLO RP QL ?RFIA FQ COLJ P@O>Q@E. &LO LKB, FQ MOLSFABP > DLLA
LMMLOQRKFQV QL MO>@QF@B LRO PHFIIP TFQE QTL-AFJBKPFLK>I >OO>VP, L?GB@Q LOFBKQ>QFLK, BQ@. "RQ
MBOE>MP JLOB FJMLOQ>KQIV, FQP @LOB MOFK@FMIBP >OB QFBA AFOB@QIV QL LRO @LOB DL>IPdPFJRI>QFKD
QEB K>QRO>I TLOIA TFQE @LAB. 4ELRDE TB J>V T>KQ QL >SLFA PFJMIV ARMIF@>QFKD FQ TFQELRQ >
DOB>Q AB>I LC QELRDEQ LO @>OB, QEB >IDLOFQEJ >KA FQP QB@EKF@>I FJMIBJBKQ>QFLK TFII MOLSFAB RP
TFQE QEB FKPMFO>QFLK >KA CLRKA>QFLK QL ?RFIA PFJRI>QFLKP QE>Q BUEF?FQ QEB @E>O>@QBOFPQF@P >KA
?BE>SFLOP LC ?FLILDF@>I PVPQBJP LC OBMOLAR@QFLK.
5KIFHB SLK .BRJ>KK, TEL @OB>QBA >K BUQO>LOAFK>OFIV @LJMIBU PVPQBJ LC PQ>QBP >KA ORIBP,
#LKT>V T>KQBA QL >@EFBSB > PFJFI>O gIFCBIFHBh OBPRIQ TFQE QEB PFJMIBPQ PBQ LC ORIBP MLPPF?IB.
->OQFK '>OAKBO LRQIFKBA #LKT>VfP DL>IP >P CLIILTP:
I1. *52?2 @5<B91 /2 ;< 6;6A6.9 =.AA2?; 3<? D5605 A52?2 6@ . @6:=92 =?<<3 A5.A A52 =<=B9.A6<;
0.; 4?<D D6A5<BA 96:6A. 2. *52?2 @5<B91 /2 6;6A6.9 =.AA2?;@ A5.A .==.?2;A9F 1< 4?<D
D6A5<BA 96:6A. 3. *52?2 @5<B91 /2 @6:=92 6;6A6.9 =.AA2?;@ A5.A 4?<D .;1 05.;42 3<? .
0<;@612?./92 =2?6<1 <3 A6:2 /23<?2 0<:6;4 A< .; 2;1 6; A5?22 =<@@6/92 D.F@: 3.16;4 .D.F
0<:=92A29F (3?<: <C2?0?<D16;4 <? /20<:6;4 A<< @=.?@2), @2AA96;4 6;A< . @A./92
0<;364B?.A6<; A5.A ?2:.6;@ B;05.;421 A52?2.3A2?, <? 2;A2?6;4 .; <@0699.A6;4 =5.@2 6;
D5605 A52F ?2=2.A .; 2;192@@ 0F092 <3 AD< <? :<?2 =2?6<1@.J
H$.?A6; G.?1;2?, Scientific American (http://www.ibiblio.org/lifepatterns/
october1970.html) 223 (&0A</2? 1970): 120-123.
4EB >?LSB JFDEQ PLRKA > ?FQ @OVMQF@, ?RQ FQ BPPBKQF>IIV ABP@OF?BP > 7LICO>J @I>PP 4 #!. 4EB
#! PELRIA ?B M>QQBOKBA ?RQ RKMOBAF@Q>?IB LSBO QFJB, BSBKQR>IIV PBQQIFKD FKQL > RKFCLOJ LO
LP@FII>QFKD PQ>QB. )K LQEBO TLOAP, QELRDE #LKT>V AFAKfQ RPB QEFP QBOJFKLILDV, FQ PELRIA E>SB
>II QELPB MOLMBOQFBP LC > 7CAD@9L GMGH9A QE>Q TB HBBM JBKQFLKFKD.
,BQfP ILLH >Q ELT QEB '>JB LC ,FCB TLOHP. )Q TLKfQ Q>HB RM QLL JR@E QFJB LO PM>@B, PFK@B
TBfSB @LSBOBA QEB ?>PF@P LC #! >IOB>AV.
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
342
&FOPQ, FKPQB>A LC > IFKB LC @BIIP, TB KLT E>SB
> QTL-AFJBKPFLK>I J>QOFU LC @BIIP. !P TFQE
QEB BIBJBKQ>OV #!, QEB MLPPF?IB PQ>QBP >OB 0
LO 1. /KIV FK QEFP @>PB, PFK@B TBfOB Q>IHFKD
>?LRQ gIFCB," 0 JB>KP AB>A >KA 1 JB>KP
>IFSB.
4EB @BIIfP KBFDE?LOELLA E>P >IPL BUM>KABA.
)C > KBFDE?LO FP >K >AG>@BKQ @BII, >
KBFDE?LOELLA FP KLT KFKB @BIIP FKPQB>A LC
QEOBB.
7FQE QEOBB @BIIP, TB E>A > 3-?FQ KRJ?BO LO
BFDEQ MLPPF?IB @LKCFDRO>QFLKP. 7FQE KFKB
@BIIP, TB E>SB 9 ?FQP, LO 512 MLPPF?IB
KBFDE?LOELLAP. )K JLPQ @>PBP, FQ TLRIA ?B
FJMO>@QF@>I QL ABCFKB >K LRQ@LJB CLO BSBOV
PFKDIB MLPPF?FIFQV. 4EB '>JB LC ,FCB DBQP
>OLRKA QEFP MOL?IBJ ?V ABCFKFKD > PBQ LC ORIBP >@@LOAFKD QL DBKBO>I @E>O>@QBOFPQF@P LC QEB
KBFDE?LOELLA. )K LQEBO TLOAP, FP QEB KBFDE?LOELLA LSBOMLMRI>QBA TFQE IFCB? 3ROOLRKABA ?V
AB>QE? /O GRPQ OFDEQ? (BOB >OB QEB ORIBP LC IFCB.
1. D0,?3. D0,?3. )C > @BII FP >IFSB (PQ>QB = 1) FQ TFII AFB (PQ>QB ?B@LJBP 0) RKABO QEB CLIILTFKD
@FO@RJPQ>K@BP.
$A0=;:;@7,?4:9: $A0=;:;@7,?4:9: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ AFBP.
!:907490>>: !:907490>>: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ AFBP.
2. B4=?3. B4=?3. )C > @BII FP AB>A (PQ>QB = 0) FQ TFII @LJB QL IFCB (PQ>QB ?B@LJBP 1) FC FQ E>P BU>@QIV
QEOBB >IFSB KBFDE?LOP (KL JLOB, KL IBPP).
3. '?,>4>. '?,>4>. )K >II LQEBO @>PBP, QEB @BII PQ>QB ALBP KLQ @E>KDB. 4L ?B QELOLRDE, IBQfP
ABP@OF?B QELPB P@BK>OFLP.
'?,D492 A74A0: '?,D492 A74A0: )C > @BII FP >IFSB >KA E>P BU>@QIV QTL LO QEOBB IFSB KBFDE?LOP,
FQ PQ>VP >IFSB.
'?,D492 D0,/: '?,D492 D0,/: )C > @BII FP AB>A >KA E>P >KVQEFKD LQEBO QE>K QEOBB IFSB
KBFDE?LOP, FQ PQ>VP AB>A.
,BQfP ILLH >Q > CBT BU>JMIBP.
F64B?2 7.22
4EB .>QROB LC #LAB (S005)
343
7FQE QEB BIBJBKQ>OV #!, TB TBOB >?IB QL ILLH >Q >II QEB DBKBO>QFLKP KBUQ QL B>@E LQEBO,
PQ>@HBA >P OLTP FK > 2$ DOFA. 7FQE QEB '>JB LC ,FCB, ELTBSBO, QEB #! FQPBIC FP FK QTL
AFJBKPFLKP. 7B @LRIA QOV @OB>QFKD >K BI>?LO>QB 3$ SFPR>IFW>QFLK LC QEB OBPRIQP >KA PQ>@H >II
QEB DBKBO>QFLKP FK > @R?B PQOR@QROB (>KA FK C>@Q, VLR JFDEQ T>KQ QL QOV QEFP >P >K BUBO@FPB).
.BSBOQEBIBPP, QEB QVMF@>I T>V QEB '>JB LC ,FCB FP AFPMI>VBA FP QL QOB>Q B>@E DBKBO>QFLK >P >
PFKDIB CO>JB FK >K >KFJ>QFLK. 3L FKPQB>A LC SFBTFKD >II QEB DBKBO>QFLKP >Q LK@B, TB PBB QEBJ
LKB >Q > QFJB, >KA QEB OBPRIQ OBPBJ?IBP O>MFAIV DOLTFKD ?>@QBOF> FK > MBQOF AFPE.
/KB LC QEB BU@FQFKD >PMB@QP LC QEB '>JB LC ,FCB FP QE>Q QEBOB >OB FKFQF>I M>QQBOKP QE>Q VFBIA
FKQOFDRFKD OBPRIQP. &LO BU>JMIB, PLJB OBJ>FK PQ>QF@ >KA KBSBO @E>KDB.
4EBOB >OB M>QQBOKP QE>Q LP@FII>QB ?>@H >KA CLOQE ?BQTBBK QTL PQ>QBP.
F64B?2 7.23
F64B?2 7.24
F64B?2 7.25
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
344
!KA QEBOB >OB >IPL M>QQBOKP QE>Q COLJ DBKBO>QFLK QL DBKBO>QFLK JLSB >?LRQ QEB DOFA. ()QfP
FJMLOQ>KQ QL KLQB QE>Q QEB @BIIP QEBJPBISBP >OBKfQ >@QR>IIV JLSFKD, >IQELRDE TB PBB QEB
>MMB>O>K@B LC JLQFLK FK QEB OBPRIQ >P QEB @BIIP QROK LK >KA LCC.)
)C VLR >OB FKQBOBPQBA FK QEBPB M>QQBOKP, QEBOB >OB PBSBO>I DLLA gLRQ LC QEB ?LUh '>JB LC ,FCB
ABJLKPQO>QFLKP LKIFKB QE>Q >IILT VLR QL @LKCFDROB QEB #!fP FKFQF>I PQ>QB >KA T>Q@E FQ ORK >Q
S>OVFKD PMBBAP. 4TL BU>JMIBP VLR JFDEQ T>KQ QL BU>JFKB >OB:
i %UMILOFKD %JBODBK@B (EQQM://IIH.JBAF>.JFQ.BAR/MOLGB@QP/BJBODBK@B/) ?V -FQ@EBI
2BPKF@H >KA "OF>K 3FISBOJ>K, ,FCBILKD +FKABOD>OQBK 'OLRM, -)4 -BAF> ,>?LO>QLOV
i #LKT>VfP '>JB LC ,FCB (EQQM://PQBSBKHIFPB.DFQER?.@LJ/#LKT>VP'>JB/C,FCB) ?V
3QBSBK +IFPB (RPBP 0OL@BPPFKD.GP!)
&LO QEB BU>JMIB TBfII ?RFIA COLJ P@O>Q@E FK QEB KBUQ PB@QFLK, FQ TFII ?B B>PFBO QL PFJMIV
O>KALJIV PBQ QEB PQ>QBP CLO B>@E @BII.
F64B?2 7.26
7.7 Programming the Game of Life 7.7 Programming the Game of Life
.LT TB GRPQ KBBA QL BUQBKA LRO @LAB COLJ QEB 7LICO>J #! QL QTL AFJBKPFLKP. 7B RPBA >
LKB-AFJBKPFLK>I >OO>V QL PQLOB QEB IFPQ LC @BII PQ>QBP ?BCLOB, >KA CLO QEB '>JB LC ,FCB, TB @>K
RPB > QTL-AFJBKPFLK>I >OO>V (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/2A>OO>V/).
7BfII ?BDFK ?V FKFQF>IFWFKD B>@E @BII LC QEB ?L>OA TFQE > O>KALJ PQ>QB: 0 LO 1.
CHN8989 <I;L> = H?Q CHN8=IFOGHM98LIQM9;
@IL (CHN R = 0; R < =IFOGHM; R++) U
@IL (CHN S = 0; S < LIQM; S++) U
Initialize each cell with a 0 or 1. =OLL?HN8R98S9 = CHN(L;H>IG(2));
W
W
4EB .>QROB LC #LAB (S005)
345
!KA QL @LJMRQB QEB KBUQ DBKBO>QFLK, GRPQ >P ?BCLOB, TB KBBA > COBPE 2$ >OO>V QL TOFQB QL >P
TB >K>IVWB B>@E @BIIfP KBFDE?LOELLA >KA @>I@RI>QB > KBT PQ>QB.
/+. "BCLOB TB @>K PLOQ LRQ ELT QL >@QR>IIV
@>I@RI>QB QEB KBT PQ>QB, TB KBBA QL HKLT
ELT TB @>K OBCBOBK@B B>@E @BIIfP KBFDE?LO.
)K QEB @>PB LC QEB 1$ #!, QEFP T>P PFJMIB: FC
> @BII FKABU T>P C, FQP KBFDE?LOP TBOB F-1
>KA F+1. (BOB B>@E @BII ALBPKfQ E>SB >
PFKDIB FKABU, ?RQ O>QEBO > @LIRJK >KA OLT
FKABU: U,V. !P PELTK FK &FDROB 7.27, TB @>K
PBB QE>Q FQP KBFDE?LOP >OB: (L-1,M-1) (L,M-1),
(L+1,M-2), (L-1,M), (L+1,M), (L-1,M+1), (L,M+1), >KA
(L+1,M+1).
!II LC QEB '>JB LC ,FCB ORIBP LMBO>QB ?V
HKLTFKD ELT J>KV KBFDE?LOP >OB >IFSB. 3L
FC TB @OB>QB > KBFDE?LO @LRKQBO S>OF>?IB
>KA FK@OBJBKQ FQ B>@E QFJB TB CFKA >
KBFDE?LO TFQE > PQ>QB LC 1, TBfII E>SB QEB
QLQ>I LC IFSB KBFDE?LOP.
CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9;
@IL (CHN R = 0; R < =IFOGHM; R++) U
@IL (CHN S = 0; S < LIQM; S++) U
We need a new state for each cell. H?RN8R98S9 = :::::::::::::::?;
W
W
F64B?2 7.27
CHN H?CAB<ILM = 0;
Top row of neighbors C@ (<I;L>8R-198S-19 == 1) H?CAB<ILM++;
C@ (<I;L>8R 98S-19 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S-19 == 1) H?CAB<ILM++;
Middle row of neighbors (note we dont
count self)
C@ (<I;L>8R-198S9 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S9 == 1) H?CAB<ILM++;
Bottom row of neighbors C@ (<I;L>8R-198S+19 == 1) H?CAB<ILM++;
C@ (<I;L>8R 98S+19 == 1) H?CAB<ILM++;
C@ (<I;L>8R+198S+19 == 1) H?CAB<ILM++;
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
346
!KA >D>FK, GRPQ >P TFQE QEB 7LICO>J #!, TB CFKA LROPBISBP FK > PFQR>QFLK TEBOB QEB >?LSB FP >
RPBCRI >KA @IB>O T>V QL TOFQB QEB @LAB CLO QB>@EFKD MROMLPBP, >IILTFKD RP QL PBB BSBOV PQBM
(B>@E QFJB TB CFKA > KBFDE?LO TFQE > PQ>QB LC LKB, TB FK@OB>PB > @LRKQBO). .BSBOQEBIBPP, FQfP >
?FQ PFIIV QL P>V, g)C QEB @BII PQ>QB BNR>IP LKB, >AA LKB QL > @LRKQBOh TEBK TB @LRIA GRPQ P>V,
g!AA QEB @BII PQ>QB QL > @LRKQBO.h !CQBO >II, FC QEB PQ>QB FP LKIV > 0 LO 1, QEB PRJ LC >II QEB
KBFDE?LOPf PQ>QBP TFII VFBIA QEB QLQ>I KRJ?BO LC IFSB @BIIP. 3FK@B QEB KBFDE?LOP >OB >OO>KDBA FK >
JFKF 3U3 DOFA, TB @>K >AA QEBJ >II RM TFQE >KLQEBO ILLM.
/C @LROPB, TBfSB J>AB > JFPQ>HB FK QEB @LAB >?LSB. )K QEB '>JB LC ,FCB, QEB @BII FQPBIC ALBP
KLQ @LRKQ >P LKB LC QEB KBFDE?LOP. 7B @LRIA RPB > @LKAFQFLK>I QL PHFM >AAFKD QEB PQ>QB TEBK
?LQE C >KA D BNR>I 0, ?RQ >KLQEBO LMQFLK TLRIA ?B QL GRPQ PR?QO>@Q QEB @BII PQ>QB LK@B TBfSB
CFKFPEBA QEB ILLM.
&FK>IIV, LK@B TB HKLT QEB QLQ>I KRJ?BO LC IFSB KBFDE?LOP, TB @>K AB@FAB TE>Q QEB @BIIfP KBT
PQ>QB PELRIA ?B >@@LOAFKD QL QEB ORIBP: ?FOQE, AB>QE, LO PQ>PFP.
0RQQFKD QEFP >II QLDBQEBO, TB E>SB:
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
Add up all the neighbors states. H?CAB<ILM += <I;L>8R+C98S+D9;
W
W
Whoops! Subtract the cells state, which we
dont want in the total.
H?CAB<ILM -= <I;L>8R98S9;
If it is alive and has less than 2 live
neighbors, it dies from loneliness.
C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM < 2)) U
H?RN8R98S9 = 0;
W
If it is alive and has more than 3 live
neighbors, it dies from overpopulation.
?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > 3)) U
H?RN8R98S9 = 0;
W
If it is dead and has exactly 3 live
neighbors, it is born!
?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) U
H?RN8R98S9 = 1;
W
In all other cases, its state remains the
same.
?FM? U
H?RN8R98S9 = <I;L>8R98S9;
W
The next board CHN8989 H?RN = H?Q CHN8=IFOGHM98LIQM9;
4EB .>QROB LC #LAB (S005)
347
&FK>IIV, LK@B QEB KBUQ DBKBO>QFLK FP @>I@RI>QBA, TB @>K BJMILV QEB P>JB JBQELA TB RPBA QL
AO>T QEB 7LICO>J #!d> PNR>OB CLO B>@E PMLQ, TEFQB CLO LCC, ?I>@H CLO LK.
Example 7.2: Game of Life
Looping but skipping the edge cells @IL (CHN R = 1; R < =IFOGHM-1; R++) U
@IL (CHN S = 1; S < LIQM-1; S++) U
Add up all the neighbor states to calculate
the number of live neighbors.
CHN H?CAB<ILM = 0;
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
H?CAB<ILM += <I;L>8R+C98S+D9;
W
W
Correct by subtracting the cell state itself. H?CAB<ILM -= <I;L>8R98S9;
The rules of life!
C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM < 2)) H?RN8R98S9 = 0;
?FM? C@ ((<I;L>8R98S9 == 1) && (H?CAB<ILM > 3)) H?RN8R98S9 = 0;
?FM? C@ ((<I;L>8R98S9 == 0) && (H?CAB<ILM == 3)) H?RN8R98S9 = 1;
?FM? H?RN8R98S9 = <I;L>8R98S9;
W
W
The 2D array next is now the current
board.
<I;L> = H?RN;
@IL ( CHN C = 0; C < =IFOGHM;C++) U
@IL ( CHN D = 0; D < LIQM;D++) U
Black when state = 1 C@ ((<I;L>8C98D9 == 1)) @CFF(0);
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
348
White when state = 0 ?FM? @CFF(255);
MNLIE?(0);
L?=N(C*Q, D*Q, Q, Q);
W
W
#OB>QB > '>JB LC ,FCB PFJRI>QFLK QE>Q >IILTP VLR QL J>KR>IIV @LKCFDROB QEB DOFA ?V
AO>TFKD LO TFQE PMB@FCF@ HKLTK M>QQBOKP.
Exercise 7.6 Exercise 7.6
)JMIBJBKQ gTO>M->OLRKAh CLO QEB '>JB LC ,FCB PL QE>Q @BIIP LK QEB BADBP E>SB
KBFDE?LOP LK QEB LMMLPFQB PFAB LC QEB DOFA.
Exercise 7.7 Exercise 7.7
7EFIB QEB >?LSB PLIRQFLK (%U>JMIB 7.2) FP @LKSBKFBKQ, FQ FP KLQ M>OQF@RI>OIV JBJLOV-
BCCF@FBKQ. )Q @OB>QBP > KBT 2$ >OO>V CLO BSBOV CO>JB LC >KFJ>QFLK! 4EFP J>QQBOP SBOV IFQQIB
CLO > 0OL@BPPFKD ABPHQLM >MMIF@>QFLK, ?RQ FC VLR TBOB FJMIBJBKQFKD QEB '>JB LC ,FCB LK
> JF@OL@LKQOLIIBO LO JL?FIB ABSF@B, VLRfA T>KQ QL ?B JLOB @>OBCRI. /KB PLIRQFLK FP QL
E>SB LKIV QTL >OO>VP >KA @LKPQ>KQIV PT>M QEBJ, TOFQFKD QEB KBUQ PBQ LC PQ>QBP FKQL
TEF@EBSBO LKB FPKfQ QEB @ROOBKQ >OO>V. )JMIBJBKQ QEFP M>OQF@RI>O PLIRQFLK.
Exercise 7.8 Exercise 7.8
7.8 Object-Oriented Cells 7.8 Object-Oriented Cells
/SBO QEB @LROPB LC QEB MOBSFLRP PFU @E>MQBOP, TBfSB PILTIV ?RFIQ BU>JMIBP LC PVPQBJP LC
C6>97HG TFQE MOLMBOQFBP QE>Q JLSB >?LRQ QEB P@OBBK. !KA FK QEFP @E>MQBO, >IQELRDE TBfSB ?BBK
Q>IHFKD >?LRQ > g@BIIh >P FC FQ TBOB >K L?GB@Q, TB >@QR>IIV E>SBKfQ ?BBK RPFKD >KV L?GB@Q
LOFBKQ>QFLK FK LRO @LAB (LQEBO QE>K > @I>PP QL ABP@OF?B QEB #! PVPQBJ >P > TELIB). 4EFP E>P
TLOHBA ?B@>RPB > @BII FP PR@E >K BKLOJLRPIV PFJMIB L?GB@Q (> PFKDIB ?FQ). (LTBSBO, FK >
JLJBKQ, TB >OB DLFKD QL AFP@RPP PLJB FAB>P CLO CROQEBO ABSBILMFKD #! PVPQBJP, J>KV LC
TEF@E FKSLISB HBBMFKD QO>@H LC JRIQFMIB MOLMBOQFBP CLO B>@E @BII. &LO BU>JMIB, TE>Q FC > @BII
KBBABA QL OBJBJ?BO FQP I>PQ QBK PQ>QBP? /O TE>Q FC TB T>KQBA QL >MMIV PLJB LC LRO JLQFLK
>KA MEVPF@P QEFKHFKD QL > #! >KA E>SB QEB @BIIP JLSB >?LRQ QEB TFKALT, AVK>JF@>IIV
@E>KDFKD QEBFO KBFDE?LOP COLJ CO>JB QL CO>JB?
4EB .>QROB LC #LAB (S005)
349
4L >@@LJMIFPE >KV LC QEBPB FAB>P (>KA JLOB), FQ TLRIA ?B EBIMCRI QL PBB ELT TB JFDEQ QOB>Q
> @BII >P >K L?GB@Q TFQE JRIQFMIB MOLMBOQFBP, O>QEBO QE>K >P > PFKDIB 0 LO 1. 4L PELT QEFP, IBQfP
GRPQ OB@OB>QB QEB '>JB LC ,FCB PFJRI>QFLK. /KIV FKPQB>A LC:
,BQfP E>SB:
TEBOB !?FF FP > @I>PP TB TFII TOFQB. 7E>Q >OB QEB MOLMBOQFBP LC > !?FF L?GB@Q? )K LRO '>JB
LC ,FCB BU>JMIB, B>@E @BII E>P > IL@>QFLK >KA PFWB, >P TBII >P > PQ>QB.
)K QEB KLK-//0 SBOPFLK, TB RPBA > PBM>O>QB 2$ >OO>V QL HBBM QO>@H LC QEB PQ>QBP CLO QEB
@ROOBKQ >KA KBUQ DBKBO>QFLK. "V J>HFKD > @BII >K L?GB@Q, ELTBSBO, B>@E @BII @LRIA HBBM
QO>@H LC ?LQE PQ>QBP. )K QEFP @>PB, TBfII QEFKH LC QEB @BII >P OBJBJ?BOFKD FQP MOBSFLRP PQ>QB (CLO
TEBK KBT PQ>QBP KBBA QL ?B @LJMRQBA).
4EFP >IILTP RP QL SFPR>IFWB JLOB FKCLOJ>QFLK >?LRQ TE>Q QEB PQ>QB FP ALFKD. &LO BU>JMIB, TB
@LRIA @ELLPB QL @LILO > @BII AFCCBOBKQIV FC FQP PQ>QB E>P @E>KDBA. &LO BU>JMIB:
Example 7.3: Game of Life OOP
CHN8989 <I;L>;
!?FF8989 <I;L>;
=F;MM !?FF U
Location and size @FI;N R, S;
@FI;N Q;
What is the cells state? CHN MN;N?;
What was its previous state? CHN JL?PCIOM;
PIC> >CMJF;S() U
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
350
.LQ JR@E BIPB >?LRQ QEB @LAB (>Q IB>PQ CLO LRO MROMLPBP EBOB) E>P QL @E>KDB. 4EB KBFDE?LOP
@>K PQFII ?B @LRKQBA QEB P>JB T>V; QEB AFCCBOBK@B FP QE>Q TB KLT KBBA QL OBCBO QL QEB L?GB@QfP
PQ>QB S>OF>?IBP >P TB ILLM QEOLRDE QEB 2$ >OO>V.
If the cell is born, color it blue! C@ (JL?PCIOM == 0 && MN;N? == 1) @CFF(0,0,255);
?FM? C@ (MN;N? == 1) @CFF(0);
?FM? C@ (JL?PCIOM == 1 && MN;N? == 0) @CFF(255,0,0);
?FM? @CFF(255);
L?=N(R, S, Q, Q);
W
If the cell dies, color it red!
@IL (CHN R = 1; R < =IFOGHM-1; R++) U
@IL (CHN S = 1; S < LIQM-1; S++) U
CHN H?CAB<ILM = 0;
@IL (CHN C = -1; C <= 1; C++) U
@IL (CHN D = -1; D <= 1; D++) U
Use the previous state when tracking
neighbors.
D;?=>8EHI += 8E7H:4N+?54O+@5.FH;L?EKI;
W
W
H?CAB<ILM -= <I;L>8R98S9.JL?PCIOM;
We are calling a function newState() to
assign a new state to each cell.
C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM < 2)) <I;L>8R98S9.H?Q0N;N?(0);
?FM? C@ ((<I;L>8R98S9.MN;N? == 1) && (H?CAB<ILM > 3)) <I;L>8R98S9.H?Q0N;N?(0);
?FM? C@ ((<I;L>8R98S9.MN;N? == 0) && (H?CAB<ILM == 3)) <I;L>8R98S9.H?Q0N;N?(1);
else do nothing! W
W
7.9 Variations of Traditional CA 7.9 Variations of Traditional CA
.LT QE>Q TB E>SB @LSBOBA QEB ?>PF@ @LK@BMQP, >IDLOFQEJP, >KA MOLDO>JJFKD PQO>QBDFBP
?BEFKA QEB JLPQ C>JLRP 1$ >KA 2$ @BIIRI>O >RQLJ>Q>, FQfP QFJB QL QEFKH >?LRQ ELT VLR JFDEQ
Q>HB QEFP CLRKA>QFLK LC @LAB >KA ?RFIA LK FQ, ABSBILMFKD @OB>QFSB >MMIF@>QFLKP LC #!P FK VLRO
LTK TLOH. )K QEFP PB@QFLK, TBfII Q>IH QEOLRDE PLJB FAB>P CLO BUM>KAFKD QEB CB>QROBP LC QEB #!
BU>JMIBP. %U>JMIB >KPTBOP QL B>@E LC QEBPB BUBO@FPBP @>K ?B CLRKA LK QEB ?LLH TB?PFQB.
1) #:9-=0.?,92@7,= G=4/> 1) #:9-=0.?,92@7,= G=4/>. 4EBOBfP KL M>OQF@RI>O OB>PLK TEV VLR PELRIA IFJFQ VLROPBIC QL E>SFKD
VLRO @BIIP LK > OB@Q>KDRI>O DOFA. 7E>Q E>MMBKP FC VLR ABPFDK > #! TFQE >KLQEBO QVMB LC PE>MB?
4EB .>QROB LC #LAB (S005)
351
2) %=:-,-474>?4. 2) %=:-,-474>?4.. 4EB ORIBP LC > #! ALKfQ KB@BPP>OFIV E>SB QL ABCFKB >K BU>@Q LRQ@LJB.
3) C:9?49@:@> 3) C:9?49@:@>. 7BfSB ILLHBA >Q BU>JMIBP TEBOB QEB @BIIfP PQ>QB @>K LKIV ?B > 1 LO > 0. "RQ
TE>Q FC QEB @BIIfP PQ>QB T>P > CIL>QFKD MLFKQ KRJ?BO ?BQTBBK 0 >KA 1?
4) I8,20 %=:.0>>492 4) I8,20 %=:.0>>492. 7B ?OFBCIV QLR@EBA LK QEFP B>OIFBO, ?RQ J>KV FJ>DB-MOL@BPPFKD
>IDLOFQEJP LMBO>QB LK #!-IFHB ORIBP. "IROOFKD >K FJ>DB FP @OB>QFKD > KBT MFUBI LRQ LC QEB
>SBO>DB LC > KBFDE?LOELLA LC MFUBIP. 3FJRI>QFLKP LC FKH AFPMBOPFKD LK M>MBO LO T>QBO
OFMMIFKD LSBO >K FJ>DB @>K ?B >@EFBSBA TFQE #! ORIBP.
#OB>QB > #! RPFKD > DOFA LC EBU>DLKP (>P ?BILT), B>@E TFQE PFU KBFDE?LOP.
Exercise 7.9 Exercise 7.9
2BTOFQB QEB '>JB LC ,FCB ORIBP >P CLIILTP:
/SBOMLMRI>QFLK: )C QEB @BII E>P CLRO LO JLOB >IFSB KBFDE?LOP, FQ E>P > 80% @E>K@B LC
AVFKD.
,LKBIFKBPP: )C QEB @BII E>P LKB LO CBTBO >IFSB KBFDE?LOP, FQ E>P > 60% @E>K@B LC AVFKD.
%Q@.
Exercise 7.10 Exercise 7.10
!A>MQ 7LICO>J BIBJBKQ>OV #! QL E>SB QEB PQ>QB ?B > CIL>Q. 9LR @LRIA ABCFKB ORIBP
PR@E >P, g)C QEB PQ>QB FP DOB>QBO QE>K 0.5h LO gjIBPP QE>K 0.2.h
Exercise 7.11 Exercise 7.11
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
352
5) H4>?:=4.,7 5) H4>?:=4.,7. )K QEB '>JB LC ,FCB L?GB@Q-LOFBKQBA BU>JMIB, TB RPBA QTL S>OF>?IBP QL HBBM
QO>@H LC FQP PQ>QB: @ROOBKQ >KA MOBSFLRP. 7E>Q FC VLR RPB >K >OO>V QL HBBM QO>@H LC > @BIIfP PQ>QB
EFPQLOV? 4EFP OBI>QBP QL QEB FAB> LC > g@LJMIBU >A>MQFSB PVPQBJ,h LKB QE>Q E>P QEB >?FIFQV QL
>A>MQ >KA @E>KDB FQP ORIBP LSBO QFJB ?V IB>OKFKD COLJ FQP EFPQLOV. 7BfII PBB >K BU>JMIB LC QEFP
FK #E>MQBO 10: .BRO>I .BQTLOHP.
6) ":A492 .077> 6) ":A492 .077>. )K QEBPB ?>PF@ BU>JMIBP, @BIIP E>SB > CFUBA MLPFQFLK LK > DOFA, ?RQ VLR @LRIA
?RFIA > #! TFQE @BIIP QE>Q E>SB KL CFUBA MLPFQFLK >KA FKPQB>A JLSB >?LRQ QEB P@OBBK.
7) #0>?492 7) #0>?492. !KLQEBO CB>QROB LC @LJMIBU PVPQBJP FP QE>Q QEBV @>K ?B KBPQBA. /RO TLOIA QBKAP QL
TLOH QEFP T>V: > @FQV FP > @LJMIBU PVPQBJ LC MBLMIB, > MBOPLK FP > @LJMIBU PVPQBJ LC LOD>KP,
>K LOD>K FP > @LJMIBU PVPQBJ LC @BIIP, >KA PL LK >KA PL CLOQE.
#OB>QB > #! FK TEF@E > MFUBI FP > @BII >KA > @LILO FP FQP PQ>QB.
Exercise 7.12 Exercise 7.12
6FPR>IFWB QEB '>JB LC ,FCB ?V @LILOFKD B>@E @BII >@@LOAFKD QL ELT ILKD FQfP ?BBK >IFSB LO
AB>A. #>K VLR >IPL RPB QEB @BIIfP EFPQLOV QL FKCLOJ QEB ORIBP?
Exercise 7.13 Exercise 7.13
5PB #! ORIBP FK > CIL@HFKD PVPQBJ. 7E>Q FC B>@E ?LFA E>A > PQ>QB (QE>Q MBOE>MP FKCLOJP
FQP PQBBOFKD ?BE>SFLOP) >KA FQP KBFDE?LOELLA @E>KDBA COLJ CO>JB QL CO>JB >P FQ JLSBA
@ILPBO QL LO CROQEBO COLJ LQEBO ?LFAP?
Exercise 7.14 Exercise 7.14
$BPFDK > #! FK TEF@E B>@E @BII FQPBIC FP > PJ>IIBO #! LO > PVPQBJ LC ?LFAP.
Exercise 7.15 Exercise 7.15
4EB .>QROB LC #LAB (S005)
353
The Ecosystem Project The Ecosystem Project
Step 7 Exercise:
Incorporate cellular automata into your ecosystem. Some possibilities:
Give each creature a state. How can that state drive their behavior?
Taking inspiration from CA, how can that state change over time
according to its neighbors states?
Consider the ecosystems world to be a CA. The creatures move from
tile to tile. Each tile has a stateis it land? water? food?
Use a CA to generate a pattern for the design of a creature in your
ecosystem.
#E>MQBO 7. #BIIRI>O !RQLJ>Q>
354
Chapter 8. Fractals Chapter 8. Fractals
I'.A5<9<460.9 :<;@A2?@! 0?621 A52 A2??63621 :.A52:.A606.;
EC2?F <;2 <3 A52: . @=96;A2? 6; :F 2F2
! 5.A2 A52 '2.;< )=.02 .;1 A52 "<05 CB?C2
! 32.? A52 C.;A<? *2?;.?F )2A
*52 )62?=6;@86 G.@82A :.82@ :2 D.;;. 0?F
A;1 . :6996<; :692@ .D.F . /BAA2?39F 39.==21 6A@ D6;4@
&; . 0<91 %<C2:/2? 1.F . :.; ;.:21 B2;<6A $.;129/?<A D.@ /<?;J
Jonathan Coulton, lyrics from Mandelbrot Set
/K@B RMLK > QFJB, ) QLLH > @LROPB FK EFDE P@ELLI @>IIBA g'BLJBQOV.h 0BOE>MP VLR AFA QLL. 9LR
IB>OKBA >?LRQ PE>MBP FK LKB AFJBKPFLK, QTL AFJBKPFLKP, >KA J>V?B BSBK QEOBB. 7E>Q FP QEB
@FO@RJCBOBK@B LC > @FO@IB? 4EB >OB> LC > OB@Q>KDIB? 4EB AFPQ>K@B ?BQTBBK > MLFKQ >KA > IFKB?
#LJB QL QEFKH LC FQ, TBfSB ?BBK PQRAVFKD DBLJBQOV >II >ILKD FK QEFP ?LLH, RPFKD SB@QLOP QL
ABP@OF?B QEB JLQFLK LC ?LAFBP FK #>OQBPF>K PM>@B. 4EFP PLOQ LC DBLJBQOV FP DBKBO>IIV OBCBOOBA
QL >P %R@IFAB>K DBLJBQOV, >CQBO QEB 'OBBH J>QEBJ>QF@F>K %R@IFA.
F64B?2 8.1
4EB .>QROB LC #LAB (S005)
355
&LO RP K>QROB @LABOP, TB E>SB QL >PH QEB NRBPQFLK: #>K TB ABP@OF?B LRO TLOIA TFQE
%R@IFAB>K DBLJBQOV? 4EB ,#$ P@OBBK )fJ PQ>OFKD >Q OFDEQ KLT PROB ILLHP IFHB > OB@Q>KDIB.
!KA QEB MIRJ ) >QB QEFP JLOKFKD FP @FO@RI>O. "RQ TE>Q FC ) TBOB QL ILLH CROQEBO, >KA @LKPFABO
QEB QOBBP QE>Q IFKB QEB PQOBBQ, QEB IB>SBP QE>Q E>KD LCC QELPB QOBBP, QEB IFDEQKFKD COLJ I>PQ
KFDEQfP QERKABOPQLOJ, QEB @>RIFCILTBO ) >QB CLO AFKKBO, QEB ?ILLA SBPPBIP FK JV ?LAV, >KA QEB
JLRKQ>FKP >KA @L>PQIFKBP QE>Q @LSBO I>KA ?BVLKA .BT 9LOH #FQV? -LPQ LC QEB PQRCC VLR CFKA
FK K>QROB @>KKLQ ?B ABP@OF?BA ?V QEB FAB>IFWBA DBLJBQOF@>I CLOJP LC %R@IFAB>K DBLJBQOV. 3L
FC TB T>KQ QL PQ>OQ ?RFIAFKD @LJMRQ>QFLK>I ABPFDKP TFQE M>QQBOKP ?BVLKA QEB PFJMIB PE>MBP
?FFCJM?(), L?=N(), >KA FCH?(), FQfP QFJB CLO RP QL IB>OK >?LRQ QEB @LK@BMQP ?BEFKA >KA
QB@EKFNRBP CLO PFJRI>QFKD QEB DBLJBQOV LC K>QROB: CO>@Q>IP.
8.1 What Is a Fractal? 8.1 What Is a Fractal?
4EB QBOJ 1=,.?,7 1=,.?,7 (COLJ QEB ,>QFK :F57HIG, JB>KFKD g?OLHBKh) T>P @LFKBA ?V QEB
J>QEBJ>QF@F>K "BKLFQ ->KABI?OLQ FK 1975. )K EFP PBJFK>I TLOH g4EB &O>@Q>I 'BLJBQOV LC
.>QROB,h EB ABCFKBP > CO>@Q>I >P g> OLRDE LO CO>DJBKQBA DBLJBQOF@ PE>MB QE>Q @>K ?B PMIFQ
FKQL M>OQP, B>@E LC TEF@E FP (>Q IB>PQ >MMOLUFJ>QBIV) > OBAR@BA-PFWB @LMV LC QEB TELIB.h
,BQfP FIIRPQO>QB QEFP ABCFKFQFLK TFQE QTL PFJMIB BU>JMIBP. &FOPQ, IBQfP QEFKH >?LRQ > QOBB
?O>K@EFKD PQOR@QROB (CLO TEF@E TBfII TOFQB QEB @LAB I>QBO):
F64B?2 8.2: &;2 <3 A52 :<@A D299-8;<D; .;1 ?20<4;6G./92 3?.0A.9 =.AA2?;@ 6@ ;.:21 3<? B2;<6A
$.;129/?<A 56:@293. G2;2?.A6;4 A52 $.;129/?<A @2A 6;C<9C2@ A2@A6;4 A52 =?<=2?A62@ <3 0<:=92E
;B:/2?@ .3A2? A52F .?2 =.@@21 A5?<B45 .; 6A2?.A6C2 3B;0A6<;. D< A52F A2;1 A< 6;36;6AF? D< A52F @A.F
/<B;121? ,5692 . 3.@06;.A6;4 :.A52:.A60.9 16@0B@@6<;, A56@ I2@0.=2-A6:2J .94<?6A5: 6@ . 92@@
=?.0A60.9 :2A5<1 3<? 42;2?.A6;4 3?.0A.9@ A5.; A52 ?20B?@6C2 A205;6>B2@ D2K99 2E.:6;2 6; A56@ 05.=A2?.
<D2C2?, .; 2E.:=92 3<? 42;2?.A6;4 A52 $.;129/?<A @2A 6@ 6;09B121 6; A52 0<12 2E.:=92@.
#E>MQBO 8. &O>@Q>IP
356
.LQF@B ELT QEB QOBB FK &FDROB 8.3 E>P > PFKDIB OLLQ TFQE QTL ?O>K@EBP @LKKB@QBA >Q FQP BKA.
%>@E LKB LC QELPB ?O>K@EBP E>P QTL ?O>K@EBP >Q FQP BKA >KA QELPB ?O>K@EBP E>SB QTL
?O>K@EBP >KA PL LK >KA PL CLOQE. 7E>Q FC TB TBOB QL MIR@H LKB ?O>K@E COLJ QEB QOBB >KA
BU>JFKB FQ LK FQP LTK?
,LLHFKD @ILPBIV >Q > DFSBK PB@QFLK LC QEB QOBB, TB CFKA QE>Q QEB PE>MB LC QEFP ?O>K@E OBPBJ?IBP
QEB QOBB FQPBIC. 4EFP FP HKLTK >P >071->4847,=4?D >071->4847,=4?D; >P ->KABI?OLQ PQ>QBA, B>@E M>OQ FP > gOBAR@BA-
PFWB @LMV LC QEB TELIB.h
4EB >?LSB QOBB FP MBOCB@QIV PVJJBQOF@>I >KA QEB M>OQP >OB, FK C>@Q, BU>@Q OBMIF@>P LC QEB TELIB.
(LTBSBO, CO>@Q>IP AL KLQ E>SB QL ?B MBOCB@QIV PBIC-PFJFI>O. ,BQfP Q>HB > ILLH >Q > DO>ME LC QEB
PQL@H J>OHBQ (>A>MQBA COLJ >@QR>I !MMIB PQL@H A>Q>).
!KA LKB JLOB.
F64B?2 8.3
F64B?2 8.4
F64B?2 8.5: G?.=5 A
4EB .>QROB LC #LAB (S005)
357
)K QEBPB DO>MEP, QEB U->UFP FP QFJB >KA QEB V->UFP FP QEB PQL@HfP S>IRB. )QfP KLQ >K >@@FABKQ QE>Q
) LJFQQBA QEB I>?BIP, ELTBSBO. 'O>MEP LC PQL@H J>OHBQ A>Q> >OB BU>JMIBP LC CO>@Q>IP ?B@>RPB
QEBV ILLH QEB P>JB >Q >KV P@>IB. !OB QEBPB DO>MEP LC QEB PQL@H LSBO LKB VB>O? /KB A>V?
/KB ELRO? 4EBOBfP KL T>V CLO VLR QL HKLT TFQELRQ > I>?BI. ()K@FABKQ>IIV, DO>ME ! PELTP PFU
JLKQEPf TLOQE LC A>Q> >KA DO>ME " WLLJP FKQL > QFKV M>OQ LC DO>ME !, PELTFKD PFU ELROP.)
4EFP FP >K BU>JMIB LC > >?:.3,>?4. >?:.3,>?4. CO>@Q>I, JB>KFKD QE>Q FQ FP ?RFIQ LRQ LC MOL?>?FIFQFBP >KA
O>KALJKBPP. 5KIFHB QEB ABQBOJFKFPQF@ QOBB-?O>K@EFKD PQOR@QROB, FQ FP PQ>QFPQF@>IIV PBIC-PFJFI>O.
!P TB DL QEOLRDE QEB BU>JMIBP FK QEFP @E>MQBO, TB TFII ILLH >Q ?LQE ABQBOJFKFPQF@ >KA
PQL@E>PQF@ QB@EKFNRBP CLO DBKBO>QFKD CO>@Q>I M>QQBOKP.
7EFIB PBIC-PFJFI>OFQV FP > HBV QO>FQ LC CO>@Q>IP, FQfP FJMLOQ>KQ QL OB>IFWB QE>Q PBIC-PFJFI>OFQV >ILKB
ALBP KLQ J>HB > CO>@Q>I. !CQBO >II, > IFKB FP PBIC-PFJFI>O. ! IFKB ILLHP QEB P>JB >Q >KV P@>IB,
>KA @>K ?B QELRDEQ LC >P @LJMOFPFKD ILQP LC IFQQIB IFKBP. "RQ FQfP KLQ > CO>@Q>I. &O>@Q>IP >OB
@E>O>@QBOFWBA ?V E>SFKD > CFKB PQOR@QROB >Q PJ>II P@>IBP (HBBM WLLJFKD FKQL QEB PQL@H J>OHBQ
DO>ME >KA VLRfII @LKQFKRB QL CFKA CIR@QR>QFLKP) >KA @>KKLQ ?B ABP@OF?BA TFQE %R@IFAB>K
DBLJBQOV. )C VLR @>K P>V g)QfP > IFKB!h QEBK FQfP KLQ > CO>@Q>I.
!KLQEBO CRKA>JBKQ>I @LJMLKBKQ LC CO>@Q>I DBLJBQOV FP OB@ROPFLK. &O>@Q>IP >II E>SB >
OB@ROPFSB ABCFKFQFLK. 7BfII PQ>OQ TFQE OB@ROPFLK ?BCLOB ABSBILMFKD QB@EKFNRBP >KA @LAB
BU>JMIBP CLO ?RFIAFKD CO>@Q>I M>QQBOKP FK 0OL@BPPFKD.
F64B?2 8.6: G?.=5 B
F64B?2 8.7
8.2 Recursion 8.2 Recursion
,BQfP ?BDFK LRO AFP@RPPFLK LC OB@ROPFLK ?V BU>JFKFKD QEB CFOPQ >MMB>O>K@B LC CO>@Q>IP FK
JLABOK J>QEBJ>QF@P. )K 1883, 'BOJ>K J>QEBJ>QF@F>K 'BLODB #>KQLO ABSBILMBA PFJMIB
ORIBP QL DBKBO>QB >K FKCFKFQB PBQ:
#E>MQBO 8. &O>@Q>IP
358
4EBOB FP > CBBA?>@H ILLM >Q TLOH EBOB. 4>HB > PFKDIB IFKB >KA ?OB>H FQ FKQL QTL. 4EBK OBQROK QL
QELPB QTL IFKBP >KA >MMIV QEB P>JB ORIB, ?OB>HFKD B>@E IFKB FKQL QTL, >KA KLT TBfOB IBCQ TFQE
CLRO. 4EBK OBQROK QL QELPB CLRO IFKBP >KA >MMIV QEB ORIB. .LT VLRfSB DLQ BFDEQ. 4EFP MOL@BPP FP
HKLTK >P =0.@=>4:9 =0.@=>4:9: QEB OBMB>QBA >MMIF@>QFLK LC > ORIB QL PR@@BPPFSB OBPRIQP. #>KQLO T>P
FKQBOBPQBA FK TE>Q E>MMBKP TEBK VLR >MMIV QEBPB ORIBP >K FKCFKFQB KRJ?BO LC QFJBP. 7B,
ELTBSBO, >OB TLOHFKD FK > CFKFQB MFUBI PM>@B >KA @>K JLPQIV FDKLOB QEB NRBPQFLKP >KA
M>O>ALUBP QE>Q >OFPB COLJ FKCFKFQB OB@ROPFLK. 7B TFII FKPQB>A @LKPQOR@Q LRO @LAB FK PR@E > T>V
QE>Q TB AL KLQ >MMIV QEB ORIBP CLOBSBO (TEF@E TLRIA @>RPB LRO MOLDO>J QL COBBWB).
"BCLOB TB FJMIBJBKQ QEB #>KQLO PBQ, IBQfP Q>HB > ILLH >Q TE>Q FQ JB>KP QL E>SB OB@ROPFLK FK
@LAB. (BOBfP PLJBQEFKD TBfOB RPBA QL ALFKD >II QEB QFJBd@>IIFKD > CRK@QFLK FKPFAB >KLQEBO
CRK@QFLK.
7E>Q TLRIA E>MMBK FC TB @>IIBA QEB CRK@QFLK TB >OB ABCFKFKD TFQEFK QEB CRK@QFLK FQPBIC? #>K
MIG?$OH=NCIH() @>II MIG?$OH=NCIH()?
)K C>@Q, QEFP FP KLQ LKIV >IILTBA, ?RQ FQfP NRFQB @LJJLK (>KA BPPBKQF>I QL ELT TB TFII FJMIBJBKQ
QEB #>KQLO PBQ). &RK@QFLKP QE>Q @>II QEBJPBISBP >OB F97IFG=J9 >KA DLLA CLO PLISFKD @BOQ>FK
MOL?IBJP. &LO BU>JMIB, @BOQ>FK J>QEBJ>QF@>I @>I@RI>QFLKP >OB FJMIBJBKQBA OB@ROPFSBIV; QEB
JLPQ @LJJLK BU>JMIB FP :57HCF=5@.
4EB C>@QLOF>I LC >KV KRJ?BO K, RPR>IIV TOFQQBK >P K!, FP ABCFKBA >P:
D! = D * D W 1 * . . . . * 3 * 2 * 1
0! = 1
(BOB TBfII TOFQB > CRK@QFLK FK 0OL@BPPFKD QE>Q RPBP > @IL ILLM QL @>I@RI>QB C>@QLOF>I:
F64B?2 8.8: *52 C.;A<? @2A
PIC> MIG?$OH=NCIH() U
Calling the function background() in the
definition of someFunction()
<;=EALIOH>(0);
W
PIC> MIG?$OH=NCIH() U
MIG?$OH=NCIH();
W
CHN @;=NILC;F(CHN H) U
CHN @ = 1;
4EB .>QROB LC #LAB (S005)
359
5MLK @ILPB BU>JFK>QFLK, VLRfII KLQF@B PLJBQEFKD FKQBOBPQFKD >?LRQ ELT C>@QLOF>I TLOHP. ,BQfP
ILLH >Q 4! >KA 3!
4! = 4 * 3 * 2 * 1
3! = 3 * 2 * 1
?30=01:=0. . . ?30=01:=0. . .
4! = 4 * 3!
)K JLOB DBKBO>I QBOJP, CLO >KV MLPFQFSB FKQBDBO K:
D! = D * (D-1)!
1! = 1
7OFQQBK LRQ:
4EB :57HCF=5@ LC H FP ABCFKBA >P H QFJBP QEB :57HCF=5@ LC H-1.
4EB ABCFKFQFLK LC 1,.?:=4,7 1,.?:=4,7 FK@IRABP 1,.?:=4,7 1,.?:=4,7?! )QfP HFKA LC IFHB ABCFKFKD gQFOBA" >P gQEB CBBIFKD
VLR DBQ TEBK VLR >OB QFOBA.h 4EFP @LK@BMQ LC PBIC-OBCBOBK@B FK CRK@QFLKP FP >K BU>JMIB LC
OB@ROPFLK. !KA TB @>K RPB FQ QL TOFQB > C>@QLOF>I CRK@QFLK QE>Q @>IIP FQPBIC.
)Q J>V ILLH @O>WV, ?RQ FQ TLOHP. (BOB >OB QEB PQBMP QE>Q E>MMBK TEBK @;=NILC;F(4) FP
@>IIBA.
Using a regular loop to compute factorial @IL (CHN C = 0; C < H; C++) U
@ = @ * (C+1);
W
L?NOLH @;
W
CHN @;=NILC;F(CHN H) U
C@ (H == 1) U
L?NOLH 1;
W ?FM? U
L?NOLH H * @;=NILC;F(H-1);
W
W
#E>MQBO 8. &O>@Q>IP
360
7B @>K >MMIV QEB P>JB MOFK@FMIB QL DO>MEF@P TFQE FKQBOBPQFKD OBPRIQP, >P TB TFII PBB FK J>KV
BU>JMIBP QEOLRDELRQ QEFP @E>MQBO. 4>HB > ILLH >Q QEFP OB@ROPFSB CRK@QFLK.
Example 8.1: Recursive Circles I
>L;Q!CL=F?() AO>TP >K BIIFMPB ?>PBA LK > PBQ LC M>O>JBQBOP QE>Q FQ OB@BFSBP >P >ODRJBKQP. )Q
QEBK @>IIP FQPBIC TFQE QELPB P>JB M>O>JBQBOP, >AGRPQFKD QEBJ PIFDEQIV. 4EB OBPRIQ FP > PBOFBP LC
@FO@IBP, B>@E LC TEF@E FP AO>TK FKPFAB QEB MOBSFLRP @FO@IB.
F64B?2 8.9
PIC> >L;Q!CL=F?(CHN R, CHN S, @FI;N L;>COM) U
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 2) U
L;>COM *= 0.75@;
The drawCircle() function is calling itself
recursively.
>L;Q!CL=F?(R, S, L;>COM);
W
W
4EB .>QROB LC #LAB (S005)
361
.LQF@B QE>Q QEB >?LSB CRK@QFLK LKIV OB@ROPFSBIV @>IIP FQPBIC FC QEB O>AFRP FP DOB>QBO QE>K 2. 4EFP
FP > @OR@F>I MLFKQ. !P TFQE FQBO>QFLK, 5@@ F97IFG=J9 :IB7H=CBG AIGH <5J9 5B 9L=H 7CB8=H=CB! 9LR
IFHBIV >OB >IOB>AV >T>OB QE>Q >II @IL >KA QBCF? ILLMP JRPQ FK@IRAB > ?LLIB>K BUMOBPPFLK
QE>Q BSBKQR>IIV BS>IR>QBP QL C>IPB, QERP BUFQFKD QEB ILLM. 7FQELRQ LKB, QEB MOLDO>J TLRIA
@O>PE, @>RDEQ FKPFAB LC >K FKCFKFQB ILLM. 4EB P>JB @>K ?B P>FA >?LRQ OB@ROPFLK. )C > OB@ROPFSB
CRK@QFLK @>IIP FQPBIC CLOBSBO >KA BSBO, VLRfII ?B JLPQ IFHBIV ?B QOB>QBA QL > KF@B COLWBK P@OBBK.
4EFP @FO@IBP BU>JMIB FP O>QEBO QOFSF>I; FQ @LRIA B>PFIV ?B >@EFBSBA QEOLRDE PFJMIB FQBO>QFLK.
(LTBSBO, CLO P@BK>OFLP FK TEF@E > CRK@QFLK @>IIP FQPBIC JLOB QE>K LK@B, OB@ROPFLK ?B@LJBP
TLKABOCRIIV BIBD>KQ.
,BQfP J>HB >L;Q!CL=F?() > ?FQ JLOB @LJMIBU. &LO BSBOV @FO@IB AFPMI>VBA, AO>T > @FO@IB E>IC
FQP PFWB QL QEB IBCQ >KA OFDEQ LC QE>Q @FO@IB.
Example 8.2: Recursion twice
PIC> M?NOJ() U
MCT?(400,400);
MGIINB();
W
PIC> >L;Q() U
<;=EALIOH>(255);
>L;Q!CL=F?(QC>NB/2,B?CABN/2,200);
W
PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U
MNLIE?(0);
HI$CFF();
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 2) U
drawCircle() calls itself twice, creating a
branching effect. For every circle, a
smaller circle is drawn to the left and the
right.
>L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2);
W
W
#E>MQBO 8. &O>@Q>IP
362
7FQE GRPQ > IFQQIB JLOB @LAB, TB @LRIA >IPL >AA > @FO@IB >?LSB >KA ?BILT B>@E @FO@IB.
Example 8.3: Recursion four times
4OV OBMOLAR@FKD QEFP PHBQ@E TFQE FQBO>QFLK FKPQB>A LC OB@ROPFLKd) A>OB VLR!
PIC> >L;Q!CL=F?(@FI;N R, @FI;N S, @FI;N L;>COM) U
?FFCJM?(R, S, L;>COM, L;>COM);
C@(L;>COM > 8) U
>L;Q!CL=F?(R + L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R - L;>COM/2, S, L;>COM/2);
>L;Q!CL=F?(R, S + L;>COM/2, L;>COM/2);
>L;Q!CL=F?(R, S - L;>COM/2, L;>COM/2);
W
W
8.3 The Cantor Set with a Recursive Function 8.3 The Cantor Set with a Recursive Function
.LT TBfOB OB>AV QL SFPR>IFWB QEB #>KQLO PBQ FK 0OL@BPPFKD RPFKD > OB@ROPFSB CRK@QFLK. 7EBOB
AL TB ?BDFK? 7BII, TB HKLT QE>Q QEB #>KQLO PBQ ?BDFKP TFQE > IFKB. 3L IBQfP PQ>OQ QEBOB >KA
TOFQB > CRK@QFLK QE>Q AO>TP > IFKB.
4EB >?LSB =;HNIL() CRK@QFLK AO>TP > IFKB QE>Q PQ>OQP >Q MFUBI @LLOAFK>QB (L,M) TFQE > IBKDQE LC
F?H. (4EB IFKB FP AO>TK ELOFWLKQ>IIV EBOB, ?RQ QEFP FP >K >O?FQO>OV AB@FPFLK.) 3L FC TB @>IIBA QE>Q
CRK@QFLK, P>VFKD:
TBfA DBQ QEB CLIILTFKD:
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
W
=;HNIL(10, 20, QC>NB-20);
4EB .>QROB LC #LAB (S005)
363
.LT, QEB #>KQLO ORIB QBIIP RP QL BO>PB QEB
JFAAIB QEFOA LC QE>Q IFKB, TEF@E IB>SBP RP
TFQE QTL IFKBP, LKB COLJ QEB ?BDFKKFKD LC
QEB IFKB QL QEB LKB-QEFOA J>OH, >KA LKB COLJ
QEB QTL-QEFOAP J>OH QL QEB BKA LC QEB IFKB.
7B @>K KLT >AA QTL JLOB IFKBP LC @LAB QL
AO>T QEB PB@LKA M>FO LC IFKBP, JLSFKD QEB
V-IL@>QFLK ALTK > ?RK@E LC MFUBIP PL QE>Q
TB @>K PBB QEB OBPRIQ ?BILT QEB LOFDFK>I
IFKB.
7EFIB QEFP FP > CFKB PQ>OQ, PR@E > J>KR>I >MMOL>@E LC @>IIFKD FCH?() CLO B>@E IFKB FP KLQ TE>Q
TB T>KQ. )Q TFII DBQ RKTFBIAV SBOV NRF@HIV, >P TBfA KBBA CLRO, QEBK BFDEQ, QEBK PFUQBBK @>IIP QL
FCH?(). 9BP, > @IL ILLM FP LRO RPR>I T>V >OLRKA PR@E > MOL?IBJ, ?RQ DFSB QE>Q > QOV >KA
VLRfII PBB QE>Q TLOHFKD LRQ QEB J>QE CLO B>@E FQBO>QFLK NRF@HIV MOLSBP FKLOAFK>QBIV
@LJMIF@>QBA. (BOB FP TEBOB OB@ROPFLK @LJBP >KA OBP@RBP RP.
4>HB > ILLH >Q TEBOB TB AO>T QE>Q CFOPQ IFKB COLJ QEB PQ>OQ QL QEB LKB-QEFOA J>OH.
)KPQB>A LC @>IIFKD QEB FCH?() CRK@QFLK AFOB@QIV, TB @>K PFJMIV @>II QEB =;HNIL() CRK@QFLK
FQPBIC. !CQBO >II, TE>Q ALBP QEB =;HNIL() CRK@QFLK AL? )Q AO>TP > IFKB >Q >K (L,M) IL@>QFLK TFQE >
DFSBK IBKDQE! !KA PL:
F64B?2 8.10
F64B?2 8.11
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
S += 20;
From start to 1/3rd B?D;(N,O,N+B;D/3,O);
From 2/3rd to end B?D;(N+B;D*2/3,O,N+B;D,O);
W
F64B?2 8.12
FCH?(R,S,R+F?H/3,S);
FCH?(R,S,R+F?H/3,S); <?=IG?M -------> =;HNIL(R,S,F?H/3);
#E>MQBO 8. &O>@Q>IP
364
!KA CLO QEB PB@LKA IFKB:
,B>SFKD RP TFQE:
!KA PFK@B QEB =;HNIL() CRK@QFLK FP @>IIBA OB@ROPFSBIV, QEB P>JB ORIB TFII ?B >MMIFBA QL QEB KBUQ
IFKBP >KA QL QEB KBUQ >KA QL QEB KBUQ >P =;HNIL() @>IIP FQPBIC >D>FK >KA >D>FK! .LT, ALKfQ DL
>KA ORK QEFP @LAB VBQ. 7BfOB JFPPFKD QE>Q @OR@F>I BIBJBKQ: >K BUFQ @LKAFQFLK. 7BfII T>KQ QL
J>HB PROB TB PQLM >Q PLJB MLFKQdCLO BU>JMIB, FC QEB IBKDQE LC QEB IFKB BSBO FP IBPP QE>K 1
MFUBI.
Example 8.4: Cantor set
FCH?(R+F?H*2/3,S,R+F?H,S); <?=IG?M -------> =;HNIL(R+F?H*2/3,S,F?H/3);
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
FCH?(R,S,R+F?H,S);
S += 20;
=;HNIL(R,S,F?H/3);
=;HNIL(R+F?H*2/3,S,F?H/3);
W
PIC> =;HNIL(@FI;N R, @FI;N S, @FI;N F?H) U
Stop at 1 pixel! C@ (F?H >= 1) U
FCH?(R,S,R+F?H,S);
S += 20;
=;HNIL(R,S,F?H/3);
=;HNIL(R+F?H*2/3,S,F?H/3);
W
W
4EB .>QROB LC #LAB (S005)
365
%UBO@FPB: 5PFKD >L;Q!CL=F?() >KA QEB #>KQLO PBQ >P JLABIP, DBKBO>QB VLRO LTK
M>QQBOK TFQE OB@ROPFLK. (BOB FP > P@OBBKPELQ LC LKB QE>Q RPBP IFKBP.
Exercise 8.1 Exercise 8.1
8.4 The Koch Curve and the ArrayList Technique 8.4 The Koch Curve and the ArrayList Technique
7OFQFKD > CRK@QFLK QE>Q OB@ROPFSBIV @>IIP FQPBIC FP LKB QB@EKFNRB CLO DBKBO>QFKD > CO>@Q>I
M>QQBOK LK P@OBBK. (LTBSBO, TE>Q FC VLR T>KQBA QEB IFKBP FK QEB >?LSB #>KQLO PBQ QL BUFPQ >P
FKAFSFAR>I L?GB@QP QE>Q @LRIA ?B JLSBA FKABMBKABKQIV? 4EB OB@ROPFSB CRK@QFLK FP PFJMIB >KA
BIBD>KQ, ?RQ FQ ALBP KLQ >IILT VLR QL AL JR@E ?BPFABP PFJMIV DBKBO>QFKD QEB M>QQBOK FQPBIC.
(LTBSBO, QEBOB FP >KLQEBO T>V TB @>K >MMIV OB@ROPFLK FK @LJ?FK>QFLK TFQE >K ALL;S*CMN
QE>Q TFII >IILT RP QL KLQ LKIV DBKBO>QB > CO>@Q>I M>QQBOK, ?RQ HBBM QO>@H LC >II FQP FKAFSFAR>I
M>OQP >P L?GB@QP.
4L ABJLKPQO>QB QEFP QB@EKFNRB, IBQfP ILLH >Q >KLQEBO C>JLRP CO>@Q>I M>QQBOK, AFP@LSBOBA FK
1904 ?V 3TBAFPE J>QEBJ>QF@F>K (BIDB SLK +L@E. (BOB >OB QEB ORIBP. (.LQB QE>Q FQ PQ>OQP QEB
P>JB T>V >P QEB #>KQLO PBQ, TFQE > PFKDIB IFKB.)
#E>MQBO 8. &O>@Q>IP
366
4EB OBPRIQ ILLHP IFHB:
The Monster Curve The Monster Curve
4EB +L@E @ROSB >KA LQEBO CO>@Q>I M>QQBOKP >OB LCQBK @>IIBA gJ>QEBJ>QF@>I JLKPQBOP.h
4EFP FP ARB QL >K LAA M>O>ALU QE>Q BJBODBP TEBK VLR >MMIV QEB OB@ROPFSB ABCFKFQFLK >K
FKCFKFQB KRJ?BO LC QFJBP. )C QEB IBKDQE LC QEB LOFDFK>I PQ>OQFKD IFKB FP LKB, QEB CFOPQ
FQBO>QFLK LC QEB +L@E @ROSB TFII VFBIA > IFKB LC IBKDQE CLRO-QEFOAP (B>@E PBDJBKQ FP LKB-
QEFOA QEB IBKDQE LC QEB PQ>OQFKD IFKB). $L FQ >D>FK >KA VLR DBQ > IBKDQE LC PFUQBBK-KFKQEP.
!P VLR FQBO>QB QLT>OAP FKCFKFQV, QEB IBKDQE LC QEB +L@E @ROSB >MMOL>@EBP FKCFKFQV. 9BQ FQ
CFQP FK QEB QFKV CFKFQB PM>@B MOLSFABA OFDEQ EBOB LK QEFP M>MBO (LO P@OBBK)!
3FK@B TB >OB TLOHFKD FK QEB 0OL@BPPFKD I>KA LC CFKFQB MFUBIP, QEFP QEBLOBQF@>I M>O>ALU
TLKfQ ?B > C>@QLO CLO RP. 7BfII E>SB QL IFJFQ QEB KRJ?BO LC QFJBP TB OB@ROPFSBIV >MMIV
QEB +L@E ORIBP PL QE>Q LRO MOLDO>J TLKfQ ORK LRQ LC JBJLOV LO @O>PE.
7B @LRIA MOL@BBA FK QEB P>JB J>KKBO >P TB AFA TFQE QEB #>KQLO PBQ, >KA TOFQB > OB@ROPFSB
CRK@QFLK QE>Q FQBO>QFSBIV >MMIFBP QEB +L@E ORIBP LSBO >KA LSBO. .BSBOQEBIBPP, TB >OB DLFKD QL
Q>@HIB QEFP MOL?IBJ FK > AFCCBOBKQ J>KKBO ?V QOB>QFKD B>@E PBDJBKQ LC QEB +L@E @ROSB >P >K
FKAFSFAR>I L?GB@Q. 4EFP TFII LMBK RM PLJB ABPFDK MLPPF?FIFQFBP. &LO BU>JMIB, FC B>@E PBDJBKQ FP
F64B?2 8.13
F64B?2 8.14
4EB .>QROB LC #LAB (S005)
367
>K L?GB@Q, TB @LRIA >IILT B>@E PBDJBKQ QL JLSB FKABMBKABKQIV COLJ FQP LOFDFK>I IL@>QFLK
>KA M>OQF@FM>QB FK > MEVPF@P PFJRI>QFLK. )K >AAFQFLK, TB @LRIA RPB > O>KALJ @LILO, IFKB
QEF@HKBPP, BQ@. QL AFPMI>V B>@E PBDJBKQ AFCCBOBKQIV.
)K LOABO QL >@@LJMIFPE LRO DL>I LC QOB>QFKD B>@E PBDJBKQ >P >K FKAFSFAR>I L?GB@Q, TB JRPQ
CFOPQ AB@FAB TE>Q QEFP L?GB@Q PELRIA ?B FK QEB CFOPQ MI>@B. 7E>Q A>Q> PELRIA FQ PQLOB? 7E>Q
CRK@QFLKP PELRIA FQ E>SB?
4EB +L@E @ROSB FP > PBOFBP LC @LKKB@QBA IFKBP, >KA PL TB TFII QEFKH LC B>@E PBDJBKQ >P >
g+L@E,FKB.h %>@E +L@E IFKB E>P > PQ>OQ MLFKQ (g>h) >KA >K BKA MLFKQ (g?h). 4EBPB MLFKQP >OB
.3?=NIL L?GB@QP, >KA QEB IFKB FP AO>TK TFQE 0OL@BPPFKDfP FCH?() CRK@QFLK.
.LT QE>Q TB E>SB LRO )I=B*CH? @I>PP, TB @>K DBQ PQ>OQBA LK QEB J>FK MOLDO>J. 7BfII KBBA
> A>Q> PQOR@QROB QL HBBM QO>@H LC TE>Q TFII BSBKQR>IIV ?B@LJB J>KV )I=B*CH? L?GB@QP, >KA
>K ALL;S*CMN (PBB #E>MQBO 4 CLO > OBSFBT LC ALL;S*CMNP) TFII AL GRPQ CFKB.
)K M?NOJ(), TBfII T>KQ QL @OB>QB QEB ALL;S*CMN >KA >AA QEB CFOPQ IFKB PBDJBKQ QL FQ, > IFKB
QE>Q PQOBQ@EBP COLJ 0 QL QEB TFAQE LC QEB PHBQ@E.
=F;MM )I=B*CH? U
A line between two points: start and end .3?=NIL MN;LN;
.3?=NIL ?H>;
)I=B*CH?(.3?=NIL ;, .3?=NIL <) U
MN;LN = ;.A?N();
?H> = <.A?N();
W
PIC> >CMJF;S() U
MNLIE?(0);
Draw the line from PVector start to end. FCH?(MN;LN.R, MN;LN.S, ?H>.R, ?H>.S);
W
W
ALL;S*CMN<)I=B*CH?> FCH?M;
PIC> M?NOJ() U
MCT?(600, 300);
Create the ArrayList. FCH?M = H?Q ALL;S*CMN<)I=B*CH?>();
Left side of window .3?=NIL MN;LN = H?Q .3?=NIL(0, 200);
Right side of window .3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200);
#E>MQBO 8. &O>@Q>IP
368
4EBK FK >L;Q(), >II )I=B*CH? L?GB@QP (GRPQ LKB OFDEQ KLT) @>K ?B AFPMI>VBA FK > ILLM.
4EFP FP LRO CLRKA>QFLK. ,BQfP OBSFBT TE>Q TB E>SB PL C>O:
i :.3!490 .7,>>: :.3!490 .7,>>: ! @I>PP QL HBBM QO>@H LC > IFKB COLJ MLFKQ ! QL ".
i A==,D!4>?: A==,D!4>?: ! IFPQ LC >II )I=B*CH? L?GB@QP.
7FQE QEB >?LSB BIBJBKQP, ELT >KA TEBOB AL TB >MMIV +L@E ORIBP >KA MOFK@FMIBP LC OB@ROPFLK?
2BJBJ?BO QEB '>JB LC ,FCB @BIIRI>O >RQLJ>Q>? )K QE>Q PFJRI>QFLK, TB >IT>VP HBMQ QO>@H LC QTL
DBKBO>QFLKP: @ROOBKQ >KA KBUQ. 7EBK TB TBOB CFKFPEBA @LJMRQFKD QEB KBUQ DBKBO>QFLK, KBUQ
?B@>JB @ROOBKQ >KA TB JLSBA LK QL @LJMRQFKD QEB KBT KBUQ DBKBO>QFLK. 7B >OB DLFKD QL
>MMIV > PFJFI>O QB@EKFNRB EBOB. 7B E>SB >K ALL;S*CMN QE>Q HBBMP QO>@H LC QEB @ROOBKQ PBQ LC
)I=B*CH? L?GB@QP (>Q QEB PQ>OQ LC QEB MOLDO>J, QEBOB FP LKIV LKB). 7B TFII KBBA > PB@LKA
ALL;S*CMN (IBQfP @>II FQ gKBUQh) TEBOB TB TFII MI>@B >II QEB KBT )I=B*CH? L?GB@QP QE>Q >OB
DBKBO>QBA COLJ >MMIVFKD QEB +L@E ORIBP. &LO BSBOV )I=B*CH? L?GB@Q FK QEB @ROOBKQ ALL;S*CMN,
CLRO KBT )I=B*CH? L?GB@QP >OB >AABA QL QEB KBUQ ALL;S*CMN. 7EBK TBfOB ALKB, QEB KBUQ
ALL;S*CMN ?B@LJBP QEB @ROOBKQ LKB.
The first KochLine object FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>));
W
PIC> >L;Q() U
<;=EALIOH>(255);
@IL ()I=B*CH? F : FCH?M) U
F.>CMJF;S();
W
W
F64B?2 8.15
4EB .>QROB LC #LAB (S005)
369
(BOBfP ELT QEB @LAB TFII ILLH:
"V @>IIFKD A?H?L;N?() LSBO >KA LSBO >D>FK (CLO BU>JMIB, B>@E QFJB QEB JLRPB FP MOBPPBA),
TB OB@ROPFSBIV >MMIV QEB +L@E @ROSB ORIBP QL QEB BUFPQFKD PBQ LC )I=B*CH? L?GB@QP. /C
@LROPB, QEB >?LSB LJFQP QEB OB>I gTLOHh EBOB, TEF@E FP CFDROFKD LRQ QELPB ORIBP. (LT AL TB
?OB>H LKB IFKB PBDJBKQ FKQL CLRO >P ABP@OF?BA ?V QEB ORIBP? 7EFIB QEFP @>K ?B >@@LJMIFPEBA
TFQE PLJB PFJMIB >OFQEJBQF@ >KA QOFDLKLJBQOV, PFK@B LRO )I=B*CH? L?GB@Q RPBP .3?=NIL, QEFP
FP > KF@B LMMLOQRKFQV CLO RP QL MO>@QF@B LRO SB@QLO J>QE. ,BQfP BPQ>?IFPE ELT J>KV MLFKQP TB
KBBA QL @LJMRQB CLO B>@E )I=B*CH? L?GB@Q.
!P VLR @>K PBB COLJ QEB >?LSB CFDROB, TB KBBA CFSB MLFKQP (>, ?, @, A, >KA B) QL DBKBO>QB QEB
KBT )I=B*CH? L?GB@QP >KA J>HB QEB KBT IFKB PBDJBKQP (>?, @?, @A, >KA AB).
7EBOB AL TB DBQ QEBPB MLFKQP? 3FK@B TB E>SB > )I=B*CH? L?GB@Q, TEV KLQ >PH QEB
)I=B*CH? L?GB@Q QL @LJMRQB >II QEBPB MLFKQP CLO RP?
PIC> A?H?L;N?() U
Create the next ArrayList... ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>();
...for every current line. @IL ()I=B*CH? F : FCH?M) U
Add four new lines. (We need to figure out
how to compute the locations of these
lines!)
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
H?RN.;>>(H?Q )I=B*CH?(???,???));
W
The new ArrayList is now the one we care
about!
FCH?M = H?RN;
W
F64B?2 8.16
H?RN.;>>(H?Q )I=B*CH?(;,<));
H?RN.;>>(H?Q )I=B*CH?(<,=));
H?RN.;>>(H?Q )I=B*CH?(=,>));
H?RN.;>>(H?Q )I=B*CH?(>,?));
PIC> A?H?L;N?() U
ALL;S*CMN H?RN = H?Q ALL;S*CMN<)I=B*CH?>();
@IL ()I=B*CH? F : FCH?M) U
#E>MQBO 8. &O>@Q>IP
370
.LT TB GRPQ KBBA QL TOFQB CFSB KBT CRK@QFLKP FK QEB )I=B*CH? @I>PP, B>@E LKB OBQROKFKD >
.3?=NIL >@@LOAFKD QL &FDROB 8.16 (PBB M>DB 370) >?LSB. ,BQfP HKL@H LCC EI=BA() >KA EI=B#()
CFOPQ, TEF@E >OB PFJMIV QEB PQ>OQ >KA BKA MLFKQP LC QEB LOFDFK>I IFKB.
.LT IBQfP JLSB LK QL MLFKQP " >KA $. " FP LKB-QEFOA LC QEB T>V >ILKD QEB IFKB PBDJBKQ >KA $ FP
QTL-QEFOAP. (BOB TB @>K J>HB > .3?=NIL QE>Q MLFKQP COLJ PQ>OQ QL BKA >KA PEOFKH FQ QL LKB-QEFOA
QEB IBKDQE CLO " >KA QTL-QEFOAP QEB IBKDQE CLO $ QL CFKA QEBPB MLFKQP.
The KochLine object has five functions,
each of which return a PVector according to
the Koch rules.
.3?=NIL ; = F.EI=BA();
.3?=NIL < = F.EI=B();
.3?=NIL = = F.EI=B!();
.3?=NIL > = F.EI=B"();
.3?=NIL ? = F.EI=B#();
H?RN.;>>(H?Q )I=B*CH?(;, <));
H?RN.;>>(H?Q )I=B*CH?(<, =));
H?RN.;>>(H?Q )I=B*CH?(=, >));
H?RN.;>>(H?Q )I=B*CH?(>, ?));
W
FCH?M = H?RN;
W
.3?=NIL EI=BA() U
Note the use of get(), which returns a copy
of the PVector. As was noted in Chapter 6,
section 14, we want to avoid making copies
whenever possible, but here we will need a
new PVector in case we want the segments
to move independently of each other.
L?NOLH MN;LN.A?N();
W
.3?=NIL EI=B#() U
L?NOLH ?H>.A?N();
W
F64B?2 8.17
.3?=NIL EI=B() U
PVector from start to end .3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
One-third the length P.>CP(3);
4EB .>QROB LC #LAB (S005)
371
4EB I>PQ MLFKQ, #, FP QEB JLPQ AFCCF@RIQ LKB QL CFKA. (LTBSBO, FC VLR OB@>II QE>Q QEB >KDIBP LC >K
BNRFI>QBO>I QOF>KDIB >OB >II PFUQV ABDOBBP, QEFP J>HBP FQ > IFQQIB ?FQ B>PFBO. )C TB HKLT ELT QL
CFKA MLFKQ " TFQE > .3?=NIL LKB-QEFOA QEB IBKDQE LC QEB IFKB, TE>Q FC TB TBOB QL OLQ>QB QE>Q
P>JB .3?=NIL PFUQV ABDOBBP >KA JLSB >ILKD QE>Q SB@QLO COLJ MLFKQ "? 7BfA ?B >Q MLFKQ #!
0RQQFKD FQ >II QLDBQEBO, FC TB @>II A?H?L;N?() CFSB QFJBP FK M?NOJ(), TBfII PBB QEB CLIILTFKD
OBPRIQ.
Add that PVector to the beginning of the
line to find the new point.
P.;>>(MN;LN);
L?NOLH P;
W
.3?=NIL EI=B"() U
.3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
Same thing here, only we need to move
two-thirds along the line instead of one-
third.
P.GOFN(2/3.0);
P.;>>(MN;LN);
L?NOLH P;
W
F64B?2 8.18
.3?=NIL EI=B!() U
Start at the beginning. .3?=NIL ; = MN;LN.A?N();
.3?=NIL P = .3?=NIL.MO<(?H>, MN;LN);
Move 1/3rd of the way to point B. P.>CP(3);
;.;>>(P);
Rotate above the line 60 degrees. P.LIN;N?(-L;>C;HM(60));
Move along that vector to point C. ;.;>>(P);
L?NOLH ;;
W
#E>MQBO 8. &O>@Q>IP
372
Example 8.5: Koch curve
ALL;S*CMN<)I=B*CH?> FCH?M;
PIC> M?NOJ() U
MCT?(600, 300);
<;=EALIOH>(255);
FCH?M = H?Q ALL;S*CMN<)I=B*CH?>();
.3?=NIL MN;LN = H?Q .3?=NIL(0, 200);
.3?=NIL ?H> = H?Q .3?=NIL(QC>NB, 200);
FCH?M.;>>(H?Q )I=B*CH?(MN;LN, ?H>));
Arbitrarily apply the Koch rules five times. @IL (CHN C = 0; C < 5; C++) U
A?H?L;N?();
W
W
$O>T QEB +L@E PKLTCI>HB (LO PLJB LQEBO
S>OF>QFLK LC QEB +L@E @ROSB).
Exercise 8.2 Exercise 8.2
4EB .>QROB LC #LAB (S005)
373
4OV >KFJ>QFKD QEB +L@E @ROSB. &LO BU>JMIB, @>K VLR AO>T FQ COLJ IBCQ QL OFDEQ? #>K VLR
S>OV QEB SFPR>I ABPFDK LC QEB IFKB PBDJBKQP? #>K VLR JLSB QEB IFKB PBDJBKQP RPFKD
QB@EKFNRBP COLJ B>OIFBO @E>MQBOP? 7E>Q FC B>@E IFKB PBDJBKQ TBOB J>AB FKQL > PMOFKD
(QLUF@IF?P) LO GLFKQ ("LU2$)?
Exercise 8.3 Exercise 8.3
2BTOFQB QEB #>KQLO PBQ BU>JMIB RPFKD L?GB@QP >KA >K ALL;S*CMN.
Exercise 8.4 Exercise 8.4
$O>T QEB 3FBOMF\PHF QOF>KDIB (>P PBBK FK 7LICO>J BIBJBKQ>OV #!) RPFKD OB@ROPFLK.
Exercise 8.5 Exercise 8.5
8.5 Trees 8.5 Trees
4EB CO>@Q>IP TB E>SB BU>JFKBA FK QEFP @E>MQBO PL C>O >OB ABQBOJFKFPQF@, JB>KFKD QEBV E>SB
KL O>KALJKBPP >KA TFII >IT>VP MOLAR@B QEB FABKQF@>I LRQ@LJB B>@E QFJB QEBV >OB ORK. 4EBV
>OB BU@BIIBKQ ABJLKPQO>QFLKP LC @I>PPF@ CO>@Q>IP >KA QEB MOLDO>JJFKD QB@EKFNRBP ?BEFKA
AO>TFKD QEBJ, ?RQ >OB QLL MOB@FPB QL CBBI B5HIF5@. )K QEFP KBUQ M>OQ LC QEB @E>MQBO, ) T>KQ QL
BU>JFKB PLJB QB@EKFNRBP ?BEFKA DBKBO>QFKD > PQL@E>PQF@ (LO KLK-ABQBOJFKFPQF@) CO>@Q>I. 4EB
BU>JMIB TBfII RPB FP > ?O>K@EFKD QOBB. ,BQfP CFOPQ T>IH QEOLRDE QEB PQBMP QL @OB>QB >
ABQBOJFKFPQF@ SBOPFLK. (BOB >OB LRO MOLAR@QFLK ORIBP:
#E>MQBO 8. &O>@Q>IP
374
!D>FK, TB E>SB > KF@B CO>@Q>I TFQE > OB@ROPFSB ABCFKFQFLK: ! ?O>K@E FP > IFKB TFQE QTL ?O>K@EBP
@LKKB@QBA QL FQ.
4EB M>OQ QE>Q FP > ?FQ JLOB AFCCF@RIQ QE>K LRO MOBSFLRP CO>@Q>IP IFBP FK QEB RPB LC QEB TLOA FCH5H9
FK QEB CO>@Q>IfP ORIBP. %>@E KBT ?O>K@E JRPQ OLQ>QB OBI>QFSB QL QEB MOBSFLRP ?O>K@E, TEF@E FP
OLQ>QBA OBI>QFSB QL >II FQP MOBSFLRP ?O>K@EBP. ,R@HFIV CLO RP, 0OL@BPPFKD E>P > JB@E>KFPJ QL
HBBM QO>@H LC OLQ>QFLKP CLO RPdQEB ?=,9>1:=8,?4:9 8,?=4C ?=,9>1:=8,?4:9 8,?=4C. )C VLR >OBKfQ C>JFIF>O TFQE QEB
CRK@QFLKP JOMB+;NLCR() >KA JIJ+;NLCR(), ) PRDDBPQ VLR OB>A QEB LKIFKB 0OL@BPPFKD QRQLOF>I
2$ 4O>KPCLOJ>QFLKP (EQQM://MOL@BPPFKD.LOD/IB>OKFKD/QO>KPCLOJ2A/), TEF@E TFII @LSBO QEB
@LK@BMQP VLRfII KBBA CLO QEFP M>OQF@RI>O BU>JMIB.
,BQfP ?BDFK ?V AO>TFKD > PFKDIB ?O>K@E, QEB QORKH LC QEB QOBB. 3FK@B TB >OB DLFKD QL FKSLISB QEB
LIN;N?() CRK@QFLK, TBfII KBBA QL J>HB PROB TB >OB @LKQFKRLRPIV QO>KPI>QFKD >ILKD QEB
?O>K@EBP TEFIB TB AO>T QEB QOBB. !KA PFK@B QEB OLLQ PQ>OQP >Q QEB ?LQQLJ LC QEB TFKALT (PBB
>?LSB), QEB CFOPQ PQBM OBNRFOBP QO>KPI>QFKD QL QE>Q PMLQ:
F64B?2 8.19
NL;HMF;N?(QC>NB/2,B?CABN);
4EB .>QROB LC #LAB (S005)
375
jCLIILTBA ?V AO>TFKD > IFKB RMT>OAP
(&FDROB 8.20):
/K@B TBfSB CFKFPEBA QEB OLLQ, TB GRPQ KBBA QL QO>KPI>QB QL QEB BKA >KA OLQ>QB FK LOABO QL
AO>T QEB KBUQ ?O>K@E. (%SBKQR>IIV, TBfOB DLFKD QL KBBA QL M>@H>DB RM TE>Q TBfOB ALFKD
OFDEQ KLT FKQL > OB@ROPFSB CRK@QFLK, ?RQ IBQfP PLOQ LRQ QEB PQBMP CFOPQ.)
2BJBJ?BO, TEBK TB OLQ>QB FK 0OL@BPPFKD, TB >OB >IT>VP OLQ>QFKD >OLRKA QEB MLFKQ LC LOFDFK,
PL EBOB QEB MLFKQ LC LOFDFK JRPQ >IT>VP ?B QO>KPI>QBA QL QEB BKA LC LRO @ROOBKQ ?O>K@E.
.LT QE>Q TB E>SB > ?O>K@E DLFKD QL QEB OFDEQ, TB KBBA LKB DLFKD QL QEB IBCQ. 7B @>K RPB
JOMB+;NLCR() QL P>SB QEB QO>KPCLOJ>QFLK PQ>QB ?BCLOB TB OLQ>QB, IBQQFKD RP @>II
JIJ+;NLCR() QL OBPQLOB QE>Q PQ>QB >KA AO>T QEB ?O>K@E QL QEB IBCQ. ,BQfP ILLH >Q >II QEB @LAB
QLDBQEBO.
F64B?2 8.20
FCH?(0,0,0,-100);
F64B?2 8.21
NL;HMF;N?(0,-100);
LIN;N?(.'/6);
FCH?(0,0,0,-100);
#E>MQBO 8. &O>@Q>IP
376
)C VLR QEFKH LC B>@E @>II QL QEB CRK@QFLK FCH?() >P > g?O>K@E,h VLR @>K PBB COLJ QEB @LAB
>?LSB QE>Q TB E>SB FJMIBJBKQBA LRO ABCFKFQFLK LC ?O>K@EFKD >P > IFKB QE>Q E>P QTL IFKBP
@LKKB@QBA QL FQP BKA. 7B @LRIA HBBM >AAFKD JLOB >KA JLOB @>IIP QL FCH?() CLO JLOB >KA
JLOB ?O>K@EBP, ?RQ GRPQ >P TFQE QEB #>KQLO PBQ >KA +L@E @ROSB, LRO @LAB TLRIA ?B@LJB
FK@OBAF?IV @LJMIF@>QBA >KA RKTFBIAV. )KPQB>A, TB @>K RPB QEB >?LSB ILDF@ >P LRO CLRKA>QFLK
CLO TOFQFKD > OB@ROPFSB CRK@QFLK, OBMI>@FKD QEB AFOB@Q @>IIP QL FCH?() TFQE LRO LTK CRK@QFLK
@>IIBA <L;H=B(). ,BQfP Q>HB > ILLH.
Example 8.6: Recursive tree
F64B?2 8.22 F64B?2 8.23
NL;HMF;N?(QC>NB/2,B?CABN);
The root FCH?(0,0,0,-100);
NL;HMF;N?(0,-100);
JOMB+;NLCR();
LIN;N?(.'/6);
Branch to the right FCH?(0,0,0,-100);
JIJ+;NLCR();
LIN;N?(-.'/6);
Branch to the left FCH?(0,0,0,-100);
PIC> <L;H=B() U
Draw the branch itself. FCH?(0, 0, 0, -100);
Translate to the end. NL;HMF;N?(0, -100);
JOMB+;NLCR();
4EB .>QROB LC #LAB (S005)
377
.LQF@B ELT FK QEB >?LSB @LAB TB RPB JOMB+;NLCR() >KA JIJ+;NLCR() >OLRKA B>@E
PR?PBNRBKQ @>II QL <L;H=B(). 4EFP FP LKB LC QELPB BIBD>KQ @LAB PLIRQFLKP QE>Q CBBIP >IJLPQ
IFHB J>DF@. %>@E @>II QL <L;H=B() Q>HBP > JLJBKQ QL OBJBJ?BO QEB IL@>QFLK LC QE>Q
M>OQF@RI>O ?O>K@E. )C VLR QROK VLROPBIC FKQL 0OL@BPPFKD CLO > JLJBKQ >KA QOV QL CLIILT QEB
OB@ROPFSB CRK@QFLK TFQE MBK@FI >KA M>MBO, VLRfII KLQF@B QE>Q FQ AO>TP >II LC QEB ?O>K@EBP QL QEB
OFDEQ CFOPQ. 7EBK FQ DBQP QL QEB BKA, JIJ+;NLCR() TFII MLM RP ?>@H >ILKD >II LC QEB ?O>K@EBP
TBfSB AO>TK >KA PQ>OQ PBKAFKD ?O>K@EBP LRQ QL QEB IBCQ.
9LR J>V E>SB KLQF@BA QE>Q QEB OB@ROPFSB CRK@QFLK TB GRPQ TOLQB TLRIA KLQ >@QR>IIV AO>T QEB
>?LSB QOBB. !CQBO >II, FQ E>P KL BUFQ @LKAFQFLK >KA TLRIA DBQ PQR@H FK FKCFKFQB OB@ROPFSB @>IIP QL
FQPBIC. 9LRfII >IPL MOL?>?IV KLQF@B QE>Q QEB ?O>K@EBP LC QEB QOBB DBQ PELOQBO >Q B>@E IBSBI.
,BQfP ILLH >Q ELT TB @>K PEOFKH QEB IBKDQE LC QEB IFKBP >P QEB QOBB FP AO>TK, >KA PQLM
?O>K@EFKD LK@B QEB IFKBP E>SB ?B@LJB QLL PELOQ.
Rotate to the right and branch again. LIN;N?(.'/6);
<L;H=B();
JIJ+;NLCR();
JOMB+;NLCR();
Rotate to the left and branch again. LIN;N?(-.'/6);
<L;H=B();
JIJ+;NLCR();
W
%JRI>QB QEB 0OL@BPPFKD @LAB FK %U>JMIB 8.6 (PBB M>DB 377) >KA KRJ?BO QEB
?O>K@EBP FK QEB >?LSB AF>DO>J FK QEB LOABO QE>Q 0OL@BPPFKD TLRIA >@QR>IIV AO>T B>@E
LKB.
Exercise 8.6 Exercise 8.6
#E>MQBO 8. &O>@Q>IP
378
7BfSB >IPL FK@IRABA > S>OF>?IB CLO QEBQ> QE>Q >IILTP RP, TEBK TOFQFKD QEB OBPQ LC QEB @LAB FK
M?NOJ() >KA >L;Q(), QL S>OV QEB ?O>K@EFKD >KDIB >@@LOAFKD QL, P>V, QEB GIOM?5 IL@>QFLK.
Example 8.7: Recursive tree
Each branch now receives its length as an
argument.
PIC> <L;H=B(@FI;N F?H) U
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
Each branchs length shrinks by two-thirds. F?H *= 0.66;
C@ (F?H > 2) U
JOMB+;NLCR();
LIN;N?(NB?N;);
Subsequent calls to branch() include the
length argument.
<L;H=B(F?H);
JIJ+;NLCR();
JOMB+;NLCR();
LIN;N?(-NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
W
W
@FI;N NB?N;;
PIC> M?NOJ() U
MCT?(300, 200);
W
PIC> >L;Q() U
<;=EALIOH>(255);
4EB .>QROB LC #LAB (S005)
379
4EB OB@ROPFSB QOBB CO>@Q>I FP > KF@B BU>JMIB LC > P@BK>OFL FK TEF@E >AAFKD > IFQQIB ?FQ LC
O>KALJKBPP @>K J>HB QEB QOBB ILLH JLOB K>QRO>I. 4>HB > ILLH LRQPFAB >KA VLRfII KLQF@B QE>Q
?O>K@E IBKDQEP >KA >KDIBP S>OV COLJ ?O>K@E QL ?O>K@E, KLQ QL JBKQFLK QEB C>@Q QE>Q ?O>K@EBP
ALKfQ >II E>SB BU>@QIV QEB P>JB KRJ?BO LC PJ>IIBO ?O>K@EBP. &FOPQ, IBQfP PBB TE>Q E>MMBKP
Pick an angle according to the mouse
location.
NB?N; = G;J(GIOM?5,0,QC>NB,0,.'/2);
The first branch starts at the bottom of the
window.
NL;HMF;N?(QC>NB/2, B?CABN);
MNLIE?(0);
<L;H=B(60);
W
6>OV QEB MNLIE?4?CABN() CLO B>@E ?O>K@E. ->HB QEB OLLQ QEF@H >KA B>@E PR?PBNRBKQ
?O>K@E QEFKKBO.
Exercise 8.7 Exercise 8.7
4EB QOBB PQOR@QROB @>K >IPL ?B DBKBO>QBA RPFKD QEB ALL;S*CMN QB@EKFNRB
ABJLKPQO>QBA TFQE QEB +L@E @ROSB. 2B@OB>QB QEB QOBB RPFKD > L;H=B L?GB@Q >KA >K
ALL;S*CMN QL HBBM QO>@H LC QEB ?O>K@EBP. (FKQ: VLRfII T>KQ QL HBBM QO>@H LC QEB ?O>K@E
AFOB@QFLKP >KA IBKDQEP RPFKD SB@QLO J>QE FKPQB>A LC 0OL@BPPFKD QO>KPCLOJ>QFLKP.
Exercise 8.8 Exercise 8.8
/K@B VLR E>SB QEB QOBB ?RFIQ TFQE >K ALL;S*CMN LC L;H=B L?GB@QP, >KFJ>QB QEB QOBBfP
DOLTQE. #>K VLR AO>T IB>SBP >Q QEB BKA LC QEB ?O>K@EBP?
Exercise 8.9 Exercise 8.9
#E>MQBO 8. &O>@Q>IP
380
TEBK TB PFJMIV S>OV QEB >KDIB >KA IBKDQE. 4EFP FP > MOBQQV B>PV LKB, DFSBK QE>Q TB @>K GRPQ
>PH 0OL@BPPFKD CLO > O>KALJ KRJ?BO B>@E QFJB TB AO>T QEB QOBB.
)K QEB >?LSB CRK@QFLK, TB >IT>VP @>II <L;H=B() QTF@B. "RQ TEV KLQ MF@H > O>KALJ KRJ?BO LC
?O>K@EBP >KA @>II <L;H=B() QE>Q KRJ?BO LC QFJBP?
PIC> <L;H=B(@FI;N F?H) U
Start by picking a random angle for each
branch.
@FI;N NB?N; = L;H>IG(0,.'/3);
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
F?H *= 0.66;
C@ (F?H > 2) U
JOMB+;NLCR();
LIN;N?(NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
JOMB+;NLCR();
LIN;N?(-NB?N;);
<L;H=B(F?H);
JIJ+;NLCR();
W
W
4EB .>QROB LC #LAB (S005)
381
Example 8.8: Stochastic tree
PIC> <L;H=B(@FI;N F?H) U
FCH?(0, 0, 0, -F?H);
NL;HMF;N?(0, -F?H);
C@ (F?H > 2) U
Call branch() a random number of times. CHN H = CHN(L;H>IG(1,4));
@IL (CHN C = 0; C < H; C++) U
Each branch gets its own random angle. @FI;N NB?N; = L;H>IG(-.'/2, .'/2);
JOMB+;NLCR();
LIN;N?(NB?N;);
<L;H=B(B);
JIJ+;NLCR();
W
W
3BQ QEB >KDIBP LC QEB ?O>K@EBP LC QEB QOBB >@@LOAFKD QL 0BOIFK KLFPB S>IRBP. !AGRPQ QEB
KLFPB S>IRBP LSBO QFJB QL >KFJ>QB QEB QOBB. 3BB FC VLR @>K DBQ FQ QL >MMB>O >P FC FQ FP
?ILTFKD FK QEB TFKA.
Exercise 8.10 Exercise 8.10
5PB QLUF@IF?P QL PFJRI>QB QOBB MEVPF@P. %>@E ?O>K@E LC QEB QOBB PELRIA ?B QTL M>OQF@IBP
@LKKB@QBA TFQE > PMOFKD. (LT @>K VLR DBQ QEB QOBB QL PQ>KA RM >KA KLQ C>II ALTK?
Exercise 8.11 Exercise 8.11
8.6 L-systems 8.6 L-systems
)K 1968, (RKD>OF>K ?LQ>KFPQ !OFPQFA ,FKABKJ>VBO ABSBILMBA > DO>JJ>O-?>PBA PVPQBJ QL
JLABI QEB DOLTQE M>QQBOKP LC MI>KQP. ,-PVPQBJP (PELOQ CLO ,FKABKJ>VBO PVPQBJP) @>K ?B RPBA
QL DBKBO>QB >II LC QEB OB@ROPFSB CO>@Q>I M>QQBOKP TBfSB PBBK PL C>O FK QEFP @E>MQBO. 7B ALKfQ
KBBA ,-PVPQBJP QL AL QEB HFKA LC TLOH TBfOB ALFKD EBOB; ELTBSBO, QEBV >OB FK@OBAF?IV RPBCRI
?B@>RPB QEBV MOLSFAB > JB@E>KFPJ CLO HBBMFKD QO>@H LC CO>@Q>I PQOR@QROBP QE>Q OBNRFOB
@LJMIBU >KA JRIQF-C>@BQBA MOLAR@QFLK ORIBP.
#E>MQBO 8. &O>@Q>IP
382
)K LOABO QL @OB>QB >K BU>JMIB QE>Q FJMIBJBKQP ,-PVPQBJP FK 0OL@BPPFKD, TB >OB DLFKD QL E>SB
QL ?B @LJCLOQ>?IB TFQE TLOHFKD TFQE (>) OB@ROPFLK, (?) QO>KPCLOJ>QFLK J>QOF@BP, >KA (@) PQOFKDP.
3L C>O TBfSB TLOHBA TFQE OB@ROPFLK >KA QO>KPCLOJ>QFLKP, ?RQ PQOFKDP >OB KBT EBOB. 7B TFII
>PPRJB QEB ?>PF@P, ?RQ FC QE>Q FP KLQ @LJCLOQ>?IB CLO VLR, ) TLRIA PRDDBPQ Q>HFKD > ILLH >Q QEB
0OL@BPPFKD QRQLOF>I 3QOFKDP >KA $O>TFKD 4BUQ (EQQM://TTT.MOL@BPPFKD.LOD/IB>OKFKD/QBUQ/).
!K ,-PVPQBJ FKSLISBP QEOBB J>FK @LJMLKBKQP:
i A7;3,-0?. A7;3,-0?. !K ,-PVPQBJfP >IME>?BQ FP @LJMOFPBA LC QEB S>IFA @E>O>@QBOP QE>Q @>K ?B
FK@IRABA. &LO BU>JMIB, TB @LRIA P>V QEB >IME>?BQ FP g!"#,h JB>KFKD QE>Q >KV S>IFA
gPBKQBK@Bh (> PQOFKD LC @E>O>@QBOP) FK >K ,-PVPQBJ @>K LKIV FK@IRAB QEBPB QEOBB
@E>O>@QBOP.
i AC4:8. AC4:8. 4EB >UFLJ FP > PBKQBK@B (J>AB RM TFQE @E>O>@QBOP COLJ QEB >IME>?BQ) QE>Q
ABP@OF?BP QEB FKFQF>I PQ>QB LC QEB PVPQBJ. &LO BU>JMIB, TFQE QEB >IME>?BQ g!"#,h PLJB
BU>JMIB >UFLJP >OB g!!!h LO g"h LO g!#"!".h
i &@70>. &@70>. 4EB ORIBP LC >K ,-PVPQBJ >OB >MMIFBA QL QEB >UFLJ >KA QEBK >MMIFBA
OB@ROPFSBIV, DBKBO>QFKD KBT PBKQBK@BP LSBO >KA LSBO >D>FK. !K ,-PVPQBJ ORIB
FK@IRABP QTL PBKQBK@BP, > gMOBAB@BPPLOh >KA > gPR@@BPPLO.h &LO BU>JMIB, TFQE QEB
2RIB g! d> !"h, TEBKBSBO >K g!h FP CLRKA FK > PQOFKD, FQ FP OBMI>@BA TFQE g!".h
,BQfP ?BDFK TFQE > SBOV PFJMIB ,-PVPQBJ. (4EFP FP, FK C>@Q, ,FKABKJ>VBOfP LOFDFK>I ,-PVPQBJ CLO
JLABIFKD QEB DOLTQE LC >ID>B.)
ABF>78;J: A B
AN?EC: A
,KB;I: (A X AB) (B X A)
!P TFQE LRO OB@ROPFSB CO>@Q>I PE>MBP, TB @>K
@LKPFABO B>@E PR@@BPPFSB >MMIF@>QFLK LC QEB
,-PVPQBJ ORIBP QL ?B > DBKBO>QFLK.
'BKBO>QFLK 0 FP, ?V ABCFKFQFLK, QEB >UFLJ.
,BQfP ILLH >Q ELT TB JFDEQ @OB>QB QEBPB
DBKBO>QFLKP TFQE @LAB. 7BfII PQ>OQ ?V RPFKD >
0NLCHA L?GB@Q QL PQLOB QEB >UFLJ.
!KA LK@B >D>FK, GRPQ >P TB AFA TFQE QEB '>JB LC ,FCB >KA QEB +L@E @ROSB ALL;S*CMN
BU>JMIBP, TB TFII KBBA >K BKQFOBIV PBM>O>QB PQOFKD QL HBBM QO>@H LC QEB gKBUQh DBKBO>QFLK.
F64B?2 8.24: A;1 @< <; .;1 @< 3<?A5...
0NLCHA =OLL?HN = "A";
4EB .>QROB LC #LAB (S005)
383
.LT FQfP QFJB QL >MMIV QEB ORIBP QL QEB @ROOBKQ DBKBO>QFLK >KA MI>@B QEB OBPRIQP FK QEB KBUQ.
!KA TEBK TBfOB ALKB, @ROOBKQ @>K ?B@LJB KBUQ.
4L ?B PROB QEFP FP TLOHFKD, IBQfP M>@H>DB FQ FKQL > CRK@QFLK >KA >KA @>II FQ BSBOV QFJB QEB
JLRPB FP MOBPPBA.
Example 8.9: Simple L-system sentence generation
0NLCHA H?RN = "";
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
Production rule A --> AB C@ (= == 'A') U
H?RN += "A";
Production rule B --> A W ?FM? C@ (= == '') U
H?RN += "A";
W
W
=OLL?HN = H?RN;
Start with an axiom. 0NLCHA =OLL?HN = "A";
Lets keep track of how many generations. CHN =IOHN = 0;
PIC> M?NOJ() U
JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN);
W
PIC> >L;Q() U
W
PIC> GIOM?.L?MM?>() U
0NLCHA H?RN = "";
#E>MQBO 8. &O>@Q>IP
384
3FK@B QEB ORIBP >OB >MMIFBA OB@ROPFSBIV QL B>@E DBKBO>QFLK, QEB IBKDQE LC QEB PQOFKD DOLTP
BUMLKBKQF>IIV. "V DBKBO>QFLK #11, QEB PBKQBK@B FP 233 @E>O>@QBOP ILKD; ?V DBKBO>QFLK #22, FQ FP
LSBO 46,000 @E>O>@QBOP ILKD. 4EB *>S> 0NLCHA @I>PP, TEFIB @LKSBKFBKQ QL RPB, FP > DOLPPIV
FKBCCF@FBKQ A>Q> PQOR@QROB CLO @LK@>QBK>QFKD I>ODB PQOFKDP. ! 0NLCHA L?GB@Q FP gFJJRQ>?IB,h
TEF@E JB>KP LK@B QEB L?GB@Q FP @OB>QBA FQ @>K KBSBO ?B @E>KDBA. 7EBKBSBO VLR >AA LK QL QEB
BKA LC > 0NLCHA L?GB@Q, *>S> E>P QL J>HB > ?O>KA KBT 0NLCHA L?GB@Q (BSBK FC VLR >OB RPFKD
QEB P>JB S>OF>?IB K>JB).
)K JLPQ @>PBP, QEFP FP CFKB, ?RQ TEV ARMIF@>QB > 46,000-@E>O>@QBO PQOFKD FC VLR ALKfQ E>SB QL?
&LO ?BQQBO BCCF@FBK@V FK LRO ,-PVPQBJ BU>JMIBP, TBfII RPB QEB 0NLCHAO@@?L @I>PP, TEF@E FP
LMQFJFWBA CLO QEFP QVMB LC Q>PH >KA @>K B>PFIV ?B @LKSBOQBA FKQL > PQOFKD >CQBO @LK@>QBK>QFLK FP
@LJMIBQB.
9LR J>V CFKA VLROPBIC TLKABOFKD OFDEQ >?LRQ KLT: TE>Q BU>@QIV FP QEB MLFKQ LC >II QEFP? !CQBO >II,
FPKfQ QEFP > @E>MQBO >?LRQ 8F5K=B; CO>@Q>I M>QQBOKP? 9BP, QEB OB@ROPFSB K>QROB LC QEB ,-PVPQBJ
PBKQBK@B PQOR@QROB PBBJP OBIBS>KQ QL QEB AFP@RPPFLK, ?RQ ELT BU>@QIV ALBP QEFP JLABI MI>KQ
DOLTQE FK > SFPR>I T>V?
Traverse the current String and make the
new one.
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
C@ (= == 'A') U
H?RN += "A";
W ?FM? C@ (= == '') U
H?RN += "A";
W
W
=OLL?HN = H?RN;
=IOHN++;
JLCHNFH("%?H?L;NCIH " + =IOHN + ": " + =OLL?HN);
W
0NLCHA M = "<F;B";
M += ";>> MIG? GIL? MNO@@";
A StringBuffer for the next sentence 0NLCHAO@@?L H?RN = H?Q 0NLCHAO@@?L();
@IL (CHN C = 0; C < =OLL?HN.F?HANB(); C++) U
=B;L = = =OLL?HN.=B;LAN(C);
C@ (= == 'A') U
append() instead of += H?RN.;JJ?H>("A");
W ?FM? C@ (= == '') U
H?RN.;JJ?H>("A");
W
W
StringBuffer can easily be converted back to
a String.
=OLL?HN = H?RN.NI0NLCHA();
4EB .>QROB LC #LAB (S005)
385
7E>Q TBfSB IBCQ RKP>FA RKQFI KLT FP QE>Q BJ?BAABA FKQL QEBPB ,-PVPQBJ PBKQBK@BP >OB
FKPQOR@QFLKP CLO AO>TFKD. ,BQfP PBB ELT QEFP TLOHP TFQE >KLQEBO BU>JMIB.
ABF>78;J: A B
AN?EC: A
,KB;I: (A X ABA) (B X BBB)
4L OB>A > PBKQBK@B, TBfII QO>KPI>QB FQ FK QEB CLIILTFKD T>V:
A: H7M 7 B?D; <EHM7H:.
B: (EL; <EHM7H: M?J>EKJ :H7M?D=.
,BQfP ILLH >Q QEB PBKQBK@B LC B>@E DBKBO>QFLK >KA FQP SFPR>I LRQMRQ.
#;D;H7J?ED 0: A
#;D;H7J?ED 1: ABA
#;D;H7J?ED 2: ABABBBABA
#;D;H7J?ED 3: ABABBBABABBBBBBBBBABABBBABA
,LLH C>JFIF>O? 4EFP FP QEB #>KQLO PBQ DBKBO>QBA TFQE >K ,-PVPQBJ.
4EB CLIILTFKD >IME>?BQ FP LCQBK RPBA TFQE ,-PVPQBJP: g&'+-;<h, JB>KFKD:
": H7M 7 B?D; 7D: CEL; <EHM7H:
#: (EL; <EHM7H: (M?J>EKJ :H7M?D= 7 B?D;)
+: .KHD H?=>J
-: .KHD B;<J
4: -7L; 9KHH;DJ BE97J?ED
5: ,;IJEH; FH;L?EKI BE97J?ED
4EFP QVMB LC AO>TFKD CO>JBTLOH FP LCQBK OBCBOOBA QL >P g4ROQIB DO>MEF@Ph (COLJ QEB LIA A>VP
LC ,/'/ MOLDO>JJFKD). )J>DFKB > QROQIB PFQQFKD LK VLRO @LJMRQBO P@OBBK QL TEF@E VLR @LRIA
FPPRB > PJ>II PBQ LC @LJJ>KAP: QROK IBCQ, QROK OFDEQ, AO>T > IFKB, BQ@. 0OL@BPPFKD FPKfQ PBQ RM
QL LMBO>QB QEFP T>V ?V ABC>RIQ, ?RQ ?V RPFKD NL;HMF;N?(), LIN;N?(), >KA FCH?(), TB @>K
BJRI>QB > 4ROQIB DO>MEF@P BKDFKB C>FOIV B>PFIV.
F64B?2 8.25
#E>MQBO 8. &O>@Q>IP
386
(BOBfP ELT TB TLRIA QO>KPI>QB QEB >?LSB ,-PVPQBJ >IME>?BQ FKQL 0OL@BPPFKD @LAB.
": B?D;(0,0,0,B;D); JH7DIB7J;(0,B;D);
#: JH7DIB7J;(0,B;D);
+: HEJ7J;(7D=B;);
-: HEJ7J;(-7D=B;);
4: FKI>(7JH?N();
5: FEF(7JH?N();
!PPRJFKD TB E>SB > PBKQBK@B DBKBO>QBA COLJ QEB ,-PVPQBJ, TB @>K T>IH QEOLRDE QEB
PBKQBK@B @E>O>@QBO ?V @E>O>@QBO >KA @>II QEB >MMOLMOF>QB CRK@QFLK >P LRQIFKBA >?LSB.
4EB KBUQ BU>JMIB TFII AO>T > JLOB BI>?LO>QB PQOR@QROB TFQE QEB CLIILTFKD ,-PVPQBJ.
ABF>78;J: "#+-45
AN?EC: "
,KB;I: " -X ""+4+"-"-"5-4-"+"+"5
4EB BU>JMIB >S>FI>?IB CLO ALTKIL>A LK QEB ?LLHfP TB?PFQB Q>HBP >II LC QEB ,-PVPQBJ @LAB
MOLSFABA FK QEFP PB@QFLK >KA LOD>KFWBP FQ FKQL QEOBB @I>PPBP:
i ,KB;: ! @I>PP QE>Q PQLOBP QEB MOBAB@BPPLO >KA PR@@BPPLO PQOFKDP CLO >K ,-PVPQBJ ORIB.
i '-OIJ;C: ! @I>PP QL FQBO>QB > KBT ,-PVPQBJ DBKBO>QFLK (>P ABJLKPQO>QBA TFQE QEB
0NLCHAO@@?L QB@EKFNRB).
i .KHJB;: ! @I>PP QL J>K>DB OB>AFKD QEB ,-PVPQBJ PBKQBK@B >KA CLIILTFKD FQP
FKPQOR@QFLKP QL AO>T LK QEB P@OBBK.
@IL (CHN C = 0; C < M?HN?H=?.F?HANB(); C++) U
Looking at each character one at a time =B;L = = M?HN?H=?.=B;LAN(C);
Performing the correct task for each
character. This could also be written with a
case statement, which might be nicer to
look at, but leaving it as an if/else if
structure helps readers not familiar with
case statements.
C@ (= == '$') U
FCH?(0,0,F?H,0);
NL;HMF;N?(F?H,0);
W ?FM? C@ (= == '$') U
NL;HMF;N?(F?H,0);
W ?FM? C@ (= == '+') U
LIN;N?(NB?N;);
W ?FM? C@ (= == '-') U
LIN;N?(-NB?N;);
W ?FM? C@ (= == '8') U
JOMB+;NLCR();
W ?FM? C@ (= == '9') U
JIJ+;NLCR();
W
W
4EB .>QROB LC #LAB (S005)
387
7B TLKfQ TOFQB LRQ QEBPB @I>PPBP EBOB PFK@B QEBV PFJMIV ARMIF@>QB QEB @LAB TBfSB >IOB>AV
TLOHBA LRQ FK QEFP @E>MQBO. (LTBSBO, IBQfP PBB ELT QEBV >OB MRQ QLDBQEBO FK QEB J>FK Q>?.
Example 8.10: LSystem
*0SMN?G FMSM;
1OLNF? NOLNF?;
PIC> M?NOJ() U
MCT?(600,600);
A ruleset is an array of Rule objects. /OF?89 LOF?M?N = H?Q /OF?819;
LOF?M?N809 = H?Q /OF?('$',"$$+8+$-$-$9-8-$+$+$9");
The L-system is created with an axiom and
a ruleset.
FMSM = H?Q *0SMN?G("$",LOF?M?N);
NOLNF? = H?Q 1OLNF?(FMSM.A?N0?HN?H=?(),QC>NB/4,L;>C;HM(25));
W
PIC> >L;Q() U
<;=EALIOH>(255);
The Turtle graphics renderer is given a
sentence, a starting length, and an angle
for rotations.
Start at the bottom of the window and draw. NL;HMF;N?(QC>NB/2,B?CABN);
NOLNF?.L?H>?L();
W
PIC> GIOM?.L?MM?>() U
Generate a new sentence when the mouse
is pressed.
FMSM.A?H?L;N?();
NOLNF?.M?N1I"I(FMSM.A?N0?HN?H=?());
The length shrinks each generation. NOLNF?.=B;HA?*?H(0.5);
W
#E>MQBO 8. &O>@Q>IP
388
The Ecosystem Project The Ecosystem Project
Step 8 Exercise:
Incorporate fractals into your ecosystem. Some possibilities:
Add plant-like creatures to the ecosystem environment.
Lets say one of your plants is similar to a tree. Can you add leaves or
flowers to the end of the branches? What if the leaves can fall off the tree
(depending on a wind force)? What if you add fruit that can be picked and
eaten by the creatures?
Design a creature with a fractal pattern.
Use an L-system to generate instructions for how a creature should move
or behave.
5PB >K ,-PVPQBJ >P > PBQ LC FKPQOR@QFLKP CLO @OB>QFKD L?GB@QP PQLOBA FK >K ALL;S*CMN.
5PB QOFDLKLJBQOV >KA SB@QLO J>QE QL MBOCLOJ QEB OLQ>QFLKP FKPQB>A LC J>QOFU
QO>KPCLOJ>QFLKP (JR@E IFHB TB AFA FK QEB +L@E @ROSB BU>JMIB).
Exercise 8.12 Exercise 8.12
4EB PBJFK>I TLOH FK ,-PVPQBJP >KA MI>KQ PQOR@QROBP, /<9 A@;CF=H<A=7 B95IHM C: ,@5BHG
?V 0OWBJVP[>T 0ORPFKHFBTF@W >KA !OFPQFA ,FKABKJ>VBO, T>P MR?IFPEBA FK 1990. )Q FP
>S>FI>?IB CLO COBB FK FQP BKQFOBQV LKIFKB (EQQM://>IDLOFQEJF@?LQ>KV.LOD/M>MBOP/#>?LM).
#E>MQBO 1 ABP@OF?BP J>KV PLMEFPQF@>QBA ,-PVPQBJP TFQE >AAFQFLK>I AO>TFKD ORIBP >KA
>S>FI>?IB >IME>?BQ @E>O>@QBOP. )K >AAFQFLK, FQ ABP@OF?BP PBSBO>I JBQELAP CLO DBKBO>QFKD
PQL@E>PQF@ ,-PVPQBJP. %UM>KA QEB ,-PVPQBJ BU>JMIB QL FK@IRAB LKB LO JLOB >AAFQFLK>I
CB>QROBP ABP@OF?BA ?V 0ORPFKHFBTF@W >KA ,FKABKJ>VBO.
Exercise 8.13 Exercise 8.13
)K QEFP @E>MQBO, TB BJME>PFWBA RPFKD CO>@Q>I >IDLOFQEJP CLO DBKBO>QFKD SFPR>I M>QQBOKP.
(LTBSBO, CO>@Q>IP @>K ?B CLRKA FK LQEBO @OB>QFSB JBAFRJP. &LO BU>JMIB, CO>@Q>I M>QQBOKP
>OB BSFABKQ FK *LE>KK 3B?>PQF>K ">@EfP #BIIL 3RFQB KL. 3. 4EB PQOR@QROB LC $>SFA &LPQBO
7>II>@BfP KLSBI %B:=B=H9 &9GH T>P FKPMFOBA ?V CO>@Q>IP. #LKPFABO RPFKD QEB BU>JMIBP FK
QEFP @E>MQBO QL DBKBO>QB >RAFL LO QBUQ.
Exercise 8.14 Exercise 8.14
4EB .>QROB LC #LAB (S005)
389
Chapter 9. The Chapter 9. The
Evolution of Code Evolution of Code
I*52 3.0A A5.A 9632 2C<9C21 <BA <3 ;2.?9F ;<A56;4, @<:2 10 /6996<; F2.?@
.3A2? A52 B;6C2?@2 2C<9C21 <BA <3 96A2?.99F ;<A56;4, 6@ . 3.0A @< @A.442?6;4
A5.A ! D<B91 /2 :.1 A< .AA2:=A D<?1@ A< 1< 6A 7B@A602.J
Richard Dawkins
,BQfP Q>HB > JLJBKQ QL QEFKH ?>@H QL > PFJMIBO QFJB, TEBK VLR TOLQB VLRO CFOPQ 0OL@BPPFKD
PHBQ@EBP >KA IFCB T>P COBB >KA B>PV. 7E>Q FP LKB LC MOLDO>JJFKDfP CRKA>JBKQ>I @LK@BMQP
QE>Q VLR IFHBIV RPBA FK QELPB CFOPQ PHBQ@EBP >KA @LKQFKRB QL RPB LSBO >KA LSBO >D>FK?
15F=56@9G. 6>OF>?IBP >IILT VLR QL P>SB A>Q> >KA OBRPB QE>Q A>Q> TEFIB > MOLDO>J ORKP. 4EFP,
LC @LROPB, FP KLQEFKD KBT QL RP. )K C>@Q, TB E>SB JLSBA C>O ?BVLKA > PHBQ@E TFQE GRPQ LKB LO
QTL S>OF>?IBP >KA LK QL JLOB @LJMIBU A>Q> PQOR@QROBPdS>OF>?IBP J>AB COLJ @RPQLJ QVMBP
(L?GB@QP) QE>Q FK@IRAB ?LQE A>Q> >KA CRK@QFLK>IFQV. 7BfSB J>AB LRO LTK IFQQIB TLOIAP LC
JLSBOP >KA M>OQF@IBP >KA SBEF@IBP >KA @BIIP >KA QOBBP.
)K B>@E >KA BSBOV BU>JMIB FK QEFP ?LLH, QEB S>OF>?IBP LC QEBPB L?GB@QP E>SB QL ?B FKFQF>IFWBA.
0BOE>MP VLR J>AB > TELIB ?RK@E LC M>OQF@IBP TFQE O>KALJ @LILOP >KA PFWBP LO > IFPQ LC
SBEF@IBP >II PQ>OQFKD >Q QEB P>JB R,S IL@>QFLK LK P@OBBK. "RQ FKPQB>A LC >@QFKD >P gFKQBIIFDBKQ
ABPFDKBOPh >KA >PPFDKFKD QEB MOLMBOQFBP LC LRO L?GB@QP QEOLRDE O>KALJKBPP LO QELRDEQCRI
@LKPFABO>QFLK, TB @>K IBQ > MOL@BPP CLRKA FK K>QROBd9JC@IH=CBdAB@FAB CLO RP.
#>K TB QEFKH LC QEB S>OF>?IBP LC >K L?GB@Q >P FQP $.!? #>K L?GB@QP J>HB LQEBO L?GB@QP >KA
M>PP ALTK QEBFO $.! QL > KBT DBKBO>QFLK? #>K LRO PFJRI>QFLK BSLISB?
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
390
4EB >KPTBO QL >II QEBPB NRBPQFLKP FP VBP. !CQBO >II, TB TLRIAKfQ ?B >?IB QL C>@B LROPBISBP FK QEB
JFOOLO >P K>QROB-LC-@LABOP TFQELRQ Q>@HIFKD > PFJRI>QFLK LC LKB LC QEB JLPQ MLTBOCRI
>IDLOFQEJF@ MOL@BPPBP CLRKA FK K>QROB FQPBIC. 4EFP @E>MQBO FP ABAF@>QBA QL BU>JFKFKD QEB
MOFK@FMIBP ?BEFKA ?FLILDF@>I BSLIRQFLK >KA CFKAFKD T>VP QL >MMIV QELPB MOFK@FMIBP FK @LAB.
9.1 Genetic Algorithms: Inspired by Actual Events 9.1 Genetic Algorithms: Inspired by Actual Events
)QfP FJMLOQ>KQ CLO RP QL @I>OFCV QEB DL>IP LC QEFP @E>MQBO. 7B TFII KLQ DL FKQL ABMQE >?LRQ QEB
P@FBK@B LC DBKBQF@P >KA BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. 7B TLKfQ ?B J>HFKD 0RKKBQQ
PNR>OBP (PLOOV QL AFP>MMLFKQ) >KA QEBOB TFII ?B KL AFP@RPPFLK LC KR@IBLQFABP, MOLQBFK PVKQEBPFP,
2.!, >KA LQEBO QLMF@P OBI>QBA QL QEB >@QR>I ?FLILDF@>I MOL@BPPBP LC BSLIRQFLK. )KPQB>A, TB >OB
DLFKD QL ILLH >Q QEB @LOB MOFK@FMIBP ?BEFKA $>OTFKF>K BSLIRQFLK>OV QEBLOV >KA ABSBILM > PBQ LC
>IDLOFQEJP =BGD=F98 ?V QEBPB MOFK@FMIBP. 7B ALKfQ @>OB PL JR@E >?LRQ >K >@@RO>QB PFJRI>QFLK
LC BSLIRQFLK; O>QEBO, TB @>OB >?LRQ JBQELAP CLO >MMIVFKD BSLIRQFLK>OV PQO>QBDFBP FK PLCQT>OB.
4EFP FP KLQ QL P>V QE>Q > MOLGB@Q TFQE JLOB P@FBKQFCF@ ABMQE TLRIAKfQ E>SB S>IRB, >KA )
BK@LRO>DB OB>ABOP TFQE > M>OQF@RI>O FKQBOBPQ FK QEFP QLMF@ QL BUMILOB MLPPF?FIFQFBP CLO BUM>KAFKD
QEB BU>JMIBP MOLSFABA TFQE >AAFQFLK>I BSLIRQFLK>OV CB>QROBP. .BSBOQEBIBPP, CLO QEB P>HB LC
HBBMFKD QEFKDP J>K>DB>?IB, TBfOB DLFKD QL PQF@H QL QEB ?>PF@P, TEF@E TFII ?B MIBKQV @LJMIBU
>KA BU@FQFKD.
4EB QBOJ gDBKBQF@ >IDLOFQEJh OBCBOP QL > PMB@FCF@ >IDLOFQEJ FJMIBJBKQBA FK > PMB@FCF@ T>V QL
PLISB PMB@FCF@ PLOQP LC MOL?IBJP. 7EFIB QEB CLOJ>I DBKBQF@ >IDLOFQEJ FQPBIC TFII PBOSB >P QEB
CLRKA>QFLK CLO QEB BU>JMIBP TB @OB>QB FK QEFP @E>MQBO, TB KBBAKfQ TLOOV >?LRQ FJMIBJBKQFKD
QEB >IDLOFQEJ TFQE MBOCB@Q >@@RO>@V, DFSBK QE>Q TB >OB ILLHFKD CLO @OB>QFSB RPBP LC
BSLIRQFLK>OV QEBLOFBP FK LRO @LAB. 4EFP @E>MQBO TFII ?B ?OLHBK ALTK FKQL QEB CLIILTFKD QEOBB
M>OQP (TFQE QEB J>GLOFQV LC QEB QFJB PMBKQ LK QEB CFOPQ).
1. (=,/4?4:9,7 G090?4. A72:=4?38. (=,/4?4:9,7 G090?4. A72:=4?38. 7BfII ?BDFK TFQE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B
DBKBQF@ >IDLOFQEJ. 4EFP >IDLOFQEJ T>P ABSBILMBA QL PLISB MOL?IBJP FK TEF@E QEB
PLIRQFLK PM>@B FP PL S>PQ QE>Q > g?ORQB CLO@Bh >IDLOFQEJ TLRIA PFJMIV Q>HB QLL ILKD.
(BOBfP >K BU>JMIB: )fJ QEFKHFKD LC > KRJ?BO. ! KRJ?BO ?BQTBBK LKB >KA LKB ?FIIFLK.
(LT ILKD TFII FQ Q>HB CLO VLR QL DRBPP FQ? 3LISFKD > MOL?IBJ TFQE g?ORQB CLO@Bh OBCBOP
QL QEB MOL@BPP LC @EB@HFKD BSBOV MLPPF?IB PLIRQFLK. )P FQ LKB? )P FQ QTL? )P FQ QEOBB? )P
FQ CLRO? !KA PL >KA >KA PL CLOQE. 4ELRDE IR@H ALBP MI>V > C>@QLO EBOB, TFQE ?ORQB
CLO@B TB TLRIA LCQBK CFKA LROPBISBP M>QFBKQIV T>FQFKD CLO VB>OP TEFIB VLR @LRKQ QL
LKB ?FIIFLK. (LTBSBO, TE>Q FC ) @LRIA QBII VLR FC >K >KPTBO VLR D>SB T>P DLLA LO ?>A?
7>OJ LO @LIA? 6BOV T>OJ? (LQ? 3RMBO, PRMBO @LIA? )C VLR @LRIA BS>IR>QB ELT gCFQh >
DRBPP FP, VLR @LRIA MF@H LQEBO KRJ?BOP @ILPBO QL QE>Q DRBPP >KA >OOFSB >Q QEB >KPTBO
JLOB NRF@HIV. 9LRO >KPTBO @LRIA BSLISB.
2. I9?0=,.?4A0 '070.?4:9. I9?0=,.?4A0 '070.?4:9. /K@B TB BPQ>?IFPE QEB QO>AFQFLK>I @LJMRQBO P@FBK@B >IDLOFQEJ,
TBfII ILLH >Q LQEBO >MMIF@>QFLKP LC DBKBQF@ >IDLOFQEJP FK QEB SFPR>I >OQP. )KQBO>@QFSB
PBIB@QFLK OBCBOP QL QEB MOL@BPP LC BSLISFKD PLJBQEFKD (LCQBK >K @LJMRQBO-DBKBO>QBA
FJ>DB) QEOLRDE RPBO FKQBO>@QFLK. ,BQfP P>V VLR T>IH FKQL > JRPBRJ D>IIBOV >KA PBB
4EB .>QROB LC #LAB (S005)
391
QBK M>FKQFKDP. 7FQE FKQBO>@QFSB PBIB@QFLK, VLR TLRIA MF@H VLRO C>SLOFQBP >KA >IILT
>K >IDLOFQEJF@ MOL@BPP QL DBKBO>QB (LO gBSLISBh) KBT M>FKQFKDP ?>PBA LK VLRO
MOBCBOBK@BP.
3. E.:>D>?08 '48@7,?4:9. E.:>D>?08 '48@7,?4:9. 4EB QO>AFQFLK>I @LJMRQBO P@FBK@B DBKBQF@ >IDLOFQEJ >KA
FKQBO>@QFSB PBIB@QFLK QB@EKFNRB >OB TE>Q VLR TFII IFHBIV CFKA FC VLR PB>O@E LKIFKB LO
OB>A > QBUQ?LLH >?LRQ >OQFCF@F>I FKQBIIFDBK@B. "RQ >P TBfII PLLK PBB, QEBV ALKfQ OB>IIV
PFJRI>QB QEB MOL@BPP LC BSLIRQFLK >P FQ E>MMBKP FK QEB OB>I TLOIA. )K QEFP @E>MQBO, )
T>KQ QL >IPL BUMILOB QB@EKFNRBP CLO PFJRI>QFKD QEB MOL@BPP LC BSLIRQFLK FK >K
B@LPVPQBJ LC MPBRAL-IFSFKD ?BFKDP. (LT @>K LRO L?GB@QP QE>Q JLSB >?LRQ QEB
P@OBBK JBBQ B>@E LQEBO, J>QB, >KA M>PP QEBFO DBKBP LK QL > KBT DBKBO>QFLK? 4EFP
TLRIA >MMIV AFOB@QIV QL QEB %@LPVPQBJ 0OLGB@Q LRQIFKBA >Q QEB BKA LC B>@E @E>MQBO.
9.2 Why Use Genetic Algorithms? 9.2 Why Use Genetic Algorithms?
7EFIB @LJMRQBO PFJRI>QFLKP LC BSLIRQFLK>OV MOL@BPPBP A>QB ?>@H QL QEB 1950P, JR@E LC TE>Q
TB QEFKH LC >P DBKBQF@ >IDLOFQEJP (>IPL HKLTK >P g'!Ph) QLA>V T>P ABSBILMBA ?V *LEK
(LII>KA, > MOLCBPPLO >Q QEB 5KFSBOPFQV LC -F@EFD>K, TELPB ?LLH A85DH5H=CB =B *5HIF5@ 5B8
AFH=:=7=5@ .MGH9AG MFLKBBOBA '! OBPB>O@E. 4LA>V, JLOB DBKBQF@ >IDLOFQEJP >OB M>OQ LC >
TFABO CFBIA LC OBPB>O@E, LCQBK OBCBOOBA QL >P "%SLIRQFLK>OV #LJMRQFKD."
4L EBIM FIIRPQO>QB QEB QO>AFQFLK>I DBKBQF@ >IDLOFQEJ, TB >OB DLFKD QL PQ>OQ TFQE JLKHBVP. .L,
KLQ LRO BSLIRQFLK>OV >K@BPQLOP. 7BfOB DLFKD QL PQ>OQ TFQE PLJB CF@QFLK>I JLKHBVP QE>Q ?>KD
>T>V LK HBV?L>OAP TFQE QEB DL>I LC QVMFKD LRQ QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB.
F64B?2 9.1
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
392
4EB gFKCFKFQB JLKHBV QEBLOBJh FP PQ>QBA >P CLIILTP: ! JLKHBV EFQQFKD HBVP O>KALJIV LK >
QVMBTOFQBO TFII BSBKQR>IIV QVMB QEB @LJMIBQB TLOHP LC 3E>HBPMB>OB (DFSBK >K FKCFKFQB >JLRKQ LC
QFJB). 4EB MOL?IBJ TFQE QEFP QEBLOV FP QE>Q QEB MOL?>?FIFQV LC P>FA JLKHBV >@QR>IIV QVMFKD
3E>HBPMB>OB FP PL ILT QE>Q BSBK FC QE>Q JLKHBV PQ>OQBA >Q QEB "FD ">KD, FQfP RK?BIFBS>?IV
RKIFHBIV TBfA BSBK E>SB $5A@9H >Q QEFP MLFKQ.
,BQfP @LKPFABO > JLKHBV K>JBA 'BLODB. 'BLODB QVMBP LK > OBAR@BA QVMBTOFQBO @LKQ>FKFKD
LKIV QTBKQV-PBSBK @E>O>@QBOP: QTBKQV-PFU IBQQBOP >KA LKB PM>@B ?>O. 3L QEB MOL?>?FIFQV LC
'BLODB EFQQFKD >KV DFSBK HBV FP LKB FK QTBKQV-PBSBK.
,BQfP @LKPFABO QEB MEO>PB gQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKh (TBfOB PFJMIFCVFKD FQ COLJ QEB
LOFDFK>I g4L ?B, LO KLQ QL ?B: QE>Q FP QEB NRBPQFLKh). 4EB MEO>PB FP 39 @E>O>@QBOP ILKD. )C 'BLODB
PQ>OQP QVMFKD, QEB @E>K@B EBfII DBQ QEB CFOPQ @E>O>@QBO OFDEQ FP 1 FK 27. 3FK@B QEB MOL?>?FIFQV EBfII
DBQ QEB PB@LKA @E>O>@QBO OFDEQ FP >IPL 1 FK 27, EB E>P > 1 FK 27*27 @E>K@B LC I>KAFKD QEB CFOPQ
QTL @E>O>@QBOP FK @LOOB@Q LOABOdTEF@E CLIILTP AFOB@QIV COLJ LRO AFP@RPPFLK LC "BSBKQ
MOL?>?FIFQV" FK QEB )KQOLAR@QFLK (PBB M>DB 0). 4EBOBCLOB, QEB MOL?>?FIFQV QE>Q 'BLODB TFII QVMB
QEB CRII MEO>PB FP:
(1/27) JRIQFMIFBA ?V FQPBIC 39 QFJBP, F.B. (1/27)
39
TEF@E BNR>IP > 1 FK
66,555,937,033,867,822,607,895,549,241,096,482,953,017,615,834,735,226,163 @E>K@B LC
DBQQFKD FQ OFDEQ!
.BBAIBPP QL P>V, BSBK EFQQFKD GRPQ QEFP LKB MEO>PB, KLQ QL JBKQFLK >K BKQFOB MI>V, FP EFDEIV
RKIFHBIV. %SBK FC 'BLODB FP > @LJMRQBO PFJRI>QFLK >KA @>K QVMB LKB JFIIFLK O>KALJ MEO>PBP
MBO PB@LKA, CLO 'BLODB QL E>SB > 99% MOL?>?FIFQV LC BSBKQR>IIV DBQQFKD FQ OFDEQ, EB TLRIA E>SB
QL QVMB CLO 9,719,096,182,010,563,073,125,591,133,903,305,625,605,017 VB>OP. (.LQB QE>Q QEB
>DB LC QEB RKFSBOPB FP BPQFJ>QBA QL ?B > JBOB 13,750,000,000 VB>OP.)
4EB MLFKQ LC >II QEBPB RKC>QELJ>?IV I>ODB KRJ?BOP FP KLQ QL DFSB VLR > EB>A>@EB, ?RQ QL
ABJLKPQO>QB QE>Q > ?ORQB CLO@B >IDLOFQEJ (QVMFKD BSBOV MLPPF?IB O>KALJ MEO>PB) FP KLQ >
OB>PLK>?IB PQO>QBDV CLO >OOFSFKD O>KALJIV >Q gQL ?B LO KLQ QL ?B QE>Q FP QEB NRBPQFLKh. %KQBO
DBKBQF@ >IDLOFQEJP, TEF@E TFII PELT QE>Q TB @>K PQFII PQ>OQ TFQE O>KALJ MEO>PBP >KA CFKA QEB
PLIRQFLK QEOLRDE PFJRI>QBA BSLIRQFLK.
.LT, FQfP TLOQE KLQFKD QE>Q QEFP MOL?IBJ (5FF=J9 5H H<9 D<F5G9 THC 69 CF BCH HC 69 H<5H =G H<9
EI9GH=CBU) FP > OFAF@RILRP LKB. 3FK@B TB HKLT QEB >KPTBO, >II TB KBBA QL AL FP QVMB FQ. (BOBfP >
0OL@BPPFKD PHBQ@E QE>Q PLISBP QEB MOL?IBJ.
.BSBOQEBIBPP, QEB MLFKQ EBOB FP QE>Q PLISFKD > MOL?IBJ TFQE > HKLTK >KPTBO >IILTP RP QL B>PFIV
QBPQ LRO @LAB. /K@B TBfSB PR@@BPPCRIIV PLISBA QEB MOL?IBJ, TB @>K CBBI JLOB @LKCFABKQ FK
RPFKD DBKBQF@ >IDLOFQEJP QL AL PLJB >@QR>I RPBCRI TLOH: PLISFKD MOL?IBJP TFQE RKHKLTK
>KPTBOP. 3L QEFP CFOPQ BU>JMIB PBOSBP KL OB>I MROMLPB LQEBO QE>K QL ABJLKPQO>QB ELT DBKBQF@
MNLCHA M = "1I <? IL HIN NI <? NB;N CM NB? KO?MNCIH";
JLCHNFH(M);
4EB .>QROB LC #LAB (S005)
393
>IDLOFQEJP TLOH. )C TB QBPQ QEB '! OBPRIQP >D>FKPQ QEB HKLTK >KPTBO >KA DBQ gQL ?B LO KLQ QL
?Bh, QEBK TBfSB PR@@BBABA FK TOFQFKD LRO DBKBQF@ >IDLOFQEJ.
#OB>QB > PHBQ@E QE>Q DBKBO>QBP O>KALJ PQOFKDP. 7BfII KBBA QL HKLT ELT QL AL QEFP FK
LOABO QL FJMIBJBKQ QEB DBKBQF@ >IDLOFQEJ BU>JMIB QE>Q TFII PELOQIV CLIILT. (LT ILKD
ALBP FQ Q>HB CLO 0OL@BPPFKD QL O>KALJIV DBKBO>QB QEB PQOFKD g@>Qh? (LT @LRIA VLR
>A>MQ QEFP QL DBKBO>QB > O>KALJ ABPFDK RPFKD 0OL@BPPFKDfP PE>MB-AO>TFKD CRK@QFLKP?
Exercise 9.1 Exercise 9.1
9.3 Darwinian Natural Selection 9.3 Darwinian Natural Selection
"BCLOB TB ?BDFK T>IHFKD QEOLRDE QEB DBKBQF@ >IDLOFQEJ, IBQfP Q>HB > JLJBKQ QL ABP@OF?B
QEOBB @LOB MOFK@FMIBP LC $>OTFKF>K BSLIRQFLK QE>Q TFII ?B OBNRFOBA >P TB FJMIBJBKQ LRO
PFJRI>QFLK. )K LOABO CLO K>QRO>I PBIB@QFLK QL L@@RO >P FQ ALBP FK K>QROB, >II QEOBB LC QEBPB
BIBJBKQP JRPQ ?B MOBPBKQ.
1. H0=0/4?D. H0=0/4?D. 4EBOB JRPQ ?B > MOL@BPP FK MI>@B ?V TEF@E @EFIAOBK OB@BFSB QEB
MOLMBOQFBP LC QEBFO M>OBKQP. )C @OB>QROBP IFSB ILKD BKLRDE QL OBMOLAR@B, QEBK QEBFO
QO>FQP >OB M>PPBA ALTK QL QEBFO @EFIAOBK FK QEB KBUQ DBKBO>QFLK LC @OB>QROBP.
2. *,=4,?4:9. *,=4,?4:9. 4EBOB JRPQ ?B > S>OFBQV LC QO>FQP MOBPBKQ FK QEB MLMRI>QFLK LO > JB>KP
TFQE TEF@E QL FKQOLAR@B S>OF>QFLK. &LO BU>JMIB, IBQfP P>V QEBOB FP > MLMRI>QFLK LC
?BBQIBP FK TEF@E >II QEB ?BBQIBP >OB BU>@QIV QEB P>JB: P>JB @LILO, P>JB PFWB, P>JB
TFKDPM>K, P>JB BSBOVQEFKD. 7FQELRQ >KV S>OFBQV FK QEB MLMRI>QFLK, QEB @EFIAOBK TFII
>IT>VP ?B FABKQF@>I QL QEB M>OBKQP >KA QL B>@E LQEBO. .BT @LJ?FK>QFLKP LC QO>FQP
@>K KBSBO L@@RO >KA KLQEFKD @>K BSLISB.
3. '070.?4:9. '070.?4:9. 4EBOB JRPQ ?B > JB@E>KFPJ ?V TEF@E PLJB JBJ?BOP LC > MLMRI>QFLK
E>SB QEB LMMLOQRKFQV QL ?B M>OBKQP >KA M>PP ALTK QEBFO DBKBQF@ FKCLOJ>QFLK >KA
PLJB AL KLQ. 4EFP FP QVMF@>IIV OBCBOOBA QL >P gPROSFS>I LC QEB CFQQBPQ.h &LO BU>JMIB,
IBQfP P>V > MLMRI>QFLK LC D>WBIIBP FP @E>PBA ?V IFLKP BSBOV A>V. 4EB C>PQBO D>WBIIBP
>OB JLOB IFHBIV QL BP@>MB QEB IFLKP >KA >OB QEBOBCLOB JLOB IFHBIV QL IFSB ILKDBO >KA
E>SB > @E>K@B QL OBMOLAR@B >KA M>PP QEBFO DBKBP ALTK QL QEBFO @EFIAOBK. 4EB QBOJ
:=HH9GH, ELTBSBO, @>K ?B > ?FQ JFPIB>AFKD. 'BKBO>IIV, TB QEFKH LC FQ >P JB>KFKD
?FDDBO, C>PQBO, LO PQOLKDBO. 7EFIB QEFP J>V ?B QEB @>PB FK PLJB FKPQ>K@BP, K>QRO>I
PBIB@QFLK LMBO>QBP LK QEB MOFK@FMIB QE>Q PLJB QO>FQP >OB ?BQQBO >A>MQBA CLO QEB
@OB>QROBfP BKSFOLKJBKQ >KA QEBOBCLOB MOLAR@B > DOB>QBO IFHBIFELLA LC PROSFSFKD
>KA OBMOLAR@FKD. )Q E>P KLQEFKD QL AL TFQE > DFSBK @OB>QROB ?BFKD g?BQQBOh (>CQBO >II,
QEFP FP > PR?GB@QFSB QBOJ) LO JLOB gMEVPF@>IIV CFQ.h )K QEB @>PB LC LRO QVMFKD
JLKHBVP, CLO BU>JMIB, > JLOB gCFQh JLKHBV FP LKB QE>Q E>P QVMBA > MEO>PB @ILPBO
QL gQL ?B LO KLQ QL ?Bh.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
394
.BUQ )fA IFHB QL T>IH QEOLRDE QEB K>OO>QFSB LC QEB DBKBQF@ >IDLOFQEJ. 7BfII AL QEFP FK QEB @LKQBUQ
LC QEB QVMFKD JLKHBV. 4EB >IDLOFQEJ FQPBIC TFII ?B AFSFABA FKQL QTL M>OQP: > PBQ LC @LKAFQFLKP CLO
FKFQF>IFW>QFLK (F.B. 0OL@BPPFKDfP M?NOJ()) >KA QEB PQBMP QE>Q >OB OBMB>QBA LSBO >KA LSBO >D>FK
(F.B. 0OL@BPPFKDfP >L;Q()) RKQFI TB >OOFSB >Q QEB @LOOB@Q >KPTBO.
9.4 The Genetic Algorithm, Part I: Creating a 9.4 The Genetic Algorithm, Part I: Creating a
Population Population
)K QEB @LKQBUQ LC QEB QVMFKD JLKHBV BU>JMIB, TB TFII @OB>QB > MLMRI>QFLK LC MEO>PBP. (.LQB
QE>Q TB >OB RPFKD QEB QBOJ gMEO>PBh O>QEBO ILLPBIV, JB>KFKD > PQOFKD LC @E>O>@QBOP.) 4EFP ?BDP
QEB NRBPQFLK: (LT AL TB @OB>QB QEFP MLMRI>QFLK? (BOB FP TEBOB QEB $>OTFKF>K MOFK@FMIB LC
A,=4,?4:9 A,=4,?4:9 >MMIFBP. ,BQfP P>V, CLO PFJMIF@FQV, QE>Q TB >OB QOVFKD QL BSLISB QEB MEO>PB g@>Qh >KA
QE>Q TB E>SB > MLMRI>QFLK LC QEOBB MEO>PBP.
>K=
H?:
MED
3ROB, QEBOB FP S>OFBQV FK QEB QEOBB MEO>PBP >?LSB, ?RQ QOV QL JFU >KA J>Q@E QEB @E>O>@QBOP
BSBOV TEF@E T>V >KA VLR TFII KBSBO DBQ 75H. 4EBOB FP KLQ 9BCI;< S>OFBQV EBOB QL BSLISB QEB
LMQFJ>I PLIRQFLK. (LTBSBO, FC TB E>A > MLMRI>QFLK LC QELRP>KAP LC MEO>PBP, >II DBKBO>QBA
O>KALJIV, @E>K@BP >OB QE>Q >Q IB>PQ LKB JBJ?BO LC QEB MLMRI>QFLK TFII E>SB > 7 >P QEB CFOPQ
@E>O>@QBO, LKB TFII E>SB >K 5 >P QEB PB@LKA, >KA LKB > H >P QEB QEFOA. ! I>ODB MLMRI>QFLK TFII
JLPQ IFHBIV DFSB RP BKLRDE S>OFBQV QL DBKBO>QB QEB ABPFOBA MEO>PB (>KA FK 0>OQ 2 LC QEB
>IDLOFQEJ, TBfII E>SB >KLQEBO LMMLOQRKFQV QL FKQOLAR@B BSBK JLOB S>OF>QFLK FK @>PB QEBOB FPKfQ
BKLRDE FK QEB CFOPQ MI>@B). 3L TB @>K ?B JLOB PMB@FCF@ FK ABP@OF?FKD 3QBM 1 >KA P>V:
#OB>QB > MLMRI>QFLK LC O>KALJIV DBKBO>QBA BIBJBKQP.
4EFP ?OFKDP RM >KLQEBO FJMLOQ>KQ NRBPQFLK. 7E>Q FP QEB BIBJBKQ FQPBIC? !P TB JLSB QEOLRDE
QEB BU>JMIBP FK QEFP @E>MQBO, TBfII PBB PBSBO>I AFCCBOBKQ P@BK>OFLP; TB JFDEQ E>SB > MLMRI>QFLK
LC FJ>DBP LO > MLMRI>QFLK LC SBEF@IBP Y I> #E>MQBO 6 (PBB M>DB 308). 4EB HBV, >KA QEB M>OQ QE>Q
FP KBT CLO RP FK QEFP @E>MQBO, FP QE>Q B>@E JBJ?BO LC QEB MLMRI>QFLK E>P > SFOQR>I g$.!,h > PBQ
LC MOLMBOQFBP (TB @>K @>II QEBJ gDBKBPh) QE>Q ABP@OF?B ELT > DFSBK BIBJBKQ ILLHP LO ?BE>SBP.
)K QEB @>PB LC QEB QVMFKD JLKHBV, CLO BU>JMIB, QEB $.! FP PFJMIV > PQOFKD LC @E>O>@QBOP.
)K QEB CFBIA LC DBKBQF@P, QEBOB FP >K FJMLOQ>KQ AFPQFK@QFLK ?BQTBBK QEB @LK@BMQP ;9BCHMD9 >KA
D<9BCHMD9. 4EB >@QR>I DBKBQF@ @LABdFK LRO @>PB, QEB AFDFQ>I FKCLOJ>QFLK FQPBICdFP >K BIBJBKQfP
209:?D;0 209:?D;0. 4EFP FP TE>Q DBQP M>PPBA ALTK COLJ DBKBO>QFLK QL DBKBO>QFLK. 4EB ;309:?D;0 ;309:?D;0,
ELTBSBO, FP QEB BUMOBPPFLK LC QE>Q A>Q>. 4EFP AFPQFK@QFLK FP HBV QL ELT VLR TFII RPB DBKBQF@
>IDLOFQEJP FK VLRO LTK TLOH. 7E>Q >OB QEB L?GB@QP FK VLRO TLOIA? (LT TFII VLR ABPFDK QEB
4EB .>QROB LC #LAB (S005)
395
DBKLQVMB CLO VLRO L?GB@QP (QEB A>Q> PQOR@QROB QL PQLOB B>@E L?GB@QfP MOLMBOQFBP) >P TBII >P
QEB MEBKLQVMB (TE>Q >OB MCI RPFKD QEBPB S>OF>?IBP QL BUMOBPP?) 7B AL QEFP >II QEB QFJB FK
DO>MEF@P MOLDO>JJFKD. 4EB PFJMIBPQ BU>JMIB FP MOL?>?IV @LILO.
Genotype Genotype Phenotype Phenotype
FKQ @ = 255;
FKQ @ = 127;
FKQ @ = 0;
!P TB @>K PBB, QEB DBKLQVMB FP QEB AFDFQ>I FKCLOJ>QFLK. %>@E @LILO FP > S>OF>?IB QE>Q PQLOBP >K
FKQBDBO >KA TB @ELLPB QL BUMOBPP QE>Q FKQBDBO >P > @LILO. "RQ ELT TB @ELLPB QL BUMOBPP QEB
A>Q> FP >O?FQO>OV. )K > AFCCBOBKQ >MMOL>@E, TB @LRIA E>SB RPBA QEB FKQBDBO QL ABP@OF?B QEB
IBKDQE LC > IFKB, QEB TBFDEQ LC > CLO@B, BQ@.
Same Genotype Same Genotype Different Phenotype (line length) Different Phenotype (line length)
FKQ @ = 255;
FKQ @ = 127;
FKQ @ = 0;
4EB KF@B QEFKD >?LRQ LRO JLKHBV-QVMFKD BU>JMIB FP QE>Q QEBOB FP KL AFCCBOBK@B ?BQTBBK
DBKLQVMB >KA MEBKLQVMB. 4EB $.! A>Q> FQPBIC FP > PQOFKD LC @E>O>@QBOP >KA QEB BUMOBPPFLK LC
QE>Q A>Q> FP QE>Q SBOV PQOFKD.
3L, TB @>K CFK>IIV BKA QEB AFP@RPPFLK LC QEFP CFOPQ PQBM >KA ?B JLOB PMB@FCF@ TFQE FQP
ABP@OFMQFLK, P>VFKD:
#OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
396
9.5 The Genetic Algorithm, Part II: Selection 9.5 The Genetic Algorithm, Part II: Selection
(BOB FP TEBOB TB >MMIV QEB $>OTFKF>K MOFK@FMIB LC G9@97H=CB. 7B KBBA QL BS>IR>QB QEB
MLMRI>QFLK >KA ABQBOJFKB TEF@E JBJ?BOP >OB CFQ QL ?B PBIB@QBA >P M>OBKQP CLO QEB KBUQ
DBKBO>QFLK. 4EB MOL@BPP LC PBIB@QFLK @>K ?B AFSFABA FKQL QTL PQBMP.
1) EA,7@,?0 14?90>>. 1) EA,7@,?0 14?90>>.
&LO LRO DBKBQF@ >IDLOFQEJ QL CRK@QFLK MOLMBOIV, TB TFII KBBA QL ABPFDK TE>Q FP OBCBOOBA QL >P >
14?90>> 1@9.?4:9 14?90>> 1@9.?4:9. 4EB CRK@QFLK TFII MOLAR@B > KRJBOF@ P@LOB QL ABP@OF?B QEB CFQKBPP LC > DFSBK
JBJ?BO LC QEB MLMRI>QFLK. 4EFP, LC @LROPB, FP KLQ ELT QEB OB>I TLOIA TLOHP >Q >II. #OB>QROBP >OB
KLQ DFSBK > P@LOB; QEBV PFJMIV PROSFSB LO KLQ. "RQ FK QEB @>PB LC QEB QO>AFQFLK>I DBKBQF@
>IDLOFQEJ, TEBOB TB >OB QOVFKD QL BSLISB >K LMQFJ>I PLIRQFLK QL > MOL?IBJ, TB KBBA QL ?B >?IB
QL KRJBOF@>IIV BS>IR>QB >KV DFSBK MLPPF?IB PLIRQFLK.
,BQfP BU>JFKB LRO @ROOBKQ BU>JMIB, QEB QVMFKD JLKHBV. !D>FK, IBQfP PFJMIFCV QEB P@BK>OFL >KA
P>V TB >OB >QQBJMQFKD QL BSLISB QEB TLOA g@>Qh. 7B E>SB QEOBB JBJ?BOP LC QEB MLMRI>QFLK:
<IH, 75F, >KA 6CL. C5F FP L?SFLRPIV QEB JLPQ CFQ, DFSBK QE>Q FQ E>P QTL @LOOB@Q @E>O>@QBOP, <IH E>P
LKIV LKB, >KA 6CL E>P WBOL. !KA QEBOB FQ FP, LRO CFQKBPP CRK@QFLK:
<?JD;II = J>; DKC8;H E< 9EHH;9J 9>7H79J;HI
DNA DNA Fitness Fitness
ERQ 1
@>O 2
?LU 0
7B TFII BSBKQR>IIV T>KQ QL ILLH >Q BU>JMIBP TFQE JLOB PLMEFPQF@>QBA CFQKBPP CRK@QFLKP, ?RQ QEFP
FP > DLLA MI>@B QL PQ>OQ.
2) C=0,?0 , 8,?492 ;::7. 2) C=0,?0 , 8,?492 ;::7.
/K@B QEB CFQKBPP E>P ?BBK @>I@RI>QBA CLO >II JBJ?BOP LC QEB MLMRI>QFLK, TB @>K QEBK PBIB@Q
TEF@E JBJ?BOP >OB CFQ QL ?B@LJB M>OBKQP >KA MI>@B QEBJ FK > J>QFKD MLLI. 4EBOB >OB PBSBO>I
AFCCBOBKQ >MMOL>@EBP TB @LRIA Q>HB EBOB. &LO BU>JMIB, TB @LRIA BJMILV TE>Q FP HKLTK >P QEB
074?4>? 074?4>? JBQELA >KA P>V, g7EF@E QTL JBJ?BOP LC QEB MLMRI>QFLK P@LOBA QEB EFDEBPQ? 9LR QTL
TFII J>HB >II QEB @EFIAOBK CLO QEB KBUQ DBKBO>QFLK.h 4EFP FP MOL?>?IV LKB LC QEB B>PFBO JBQELAP
QL MOLDO>J; ELTBSBO, FQ CIFBP FK QEB C>@B LC QEB MOFK@FMIB LC S>OF>QFLK. )C QTL JBJ?BOP LC QEB
MLMRI>QFLK (LRQ LC MBOE>MP QELRP>KAP) >OB QEB LKIV LKBP >S>FI>?IB QL OBMOLAR@B, QEB KBUQ
DBKBO>QFLK TFII E>SB IFQQIB S>OFBQV >KA QEFP J>V PQRKQ QEB BSLIRQFLK>OV MOL@BPP. 7B @LRIA
FKPQB>A J>HB > J>QFKD MLLI LRQ LC > I>ODBO KRJ?BOdCLO BU>JMIB, QEB QLM 50% LC QEB
4EB .>QROB LC #LAB (S005)
397
MLMRI>QFLK, 500 LRQ LC 1,000. 4EFP FP >IPL GRPQ >P B>PV QL MOLDO>J, ?RQ FQ TFII KLQ MOLAR@B
LMQFJ>I OBPRIQP. )K QEFP @>PB, QEB EFDE-P@LOFKD QLM BIBJBKQP TLRIA E>SB QEB P>JB @E>K@B LC
?BFKD PBIB@QBA >P > M>OBKQ >P QEB LKBP QLT>OA QEB JFAAIB. !KA TEV PELRIA BIBJBKQ KRJ?BO
500 E>SB > PLIFA PELQ LC OBMOLAR@FKD, TEFIB BIBJBKQ KRJ?BO 501 E>P KL PELQ?
! ?BQQBO PLIRQFLK CLO QEB J>QFKD MLLI FP QL RPB > ;=:-,-474>?4. ;=:-,-474>?4. JBQELA, TEF@E TBfII @>II QEB
gTEBBI LC CLOQRKBh (>IPL HKLTK >P QEB gOLRIBQQB TEBBIh). 4L FIIRPQO>QB QEFP JBQELA, IBQfP
@LKPFABO > PFJMIB BU>JMIB TEBOB TB E>SB > MLMRI>QFLK LC CFSB BIBJBKQP, B>@E TFQE > CFQKBPP
P@LOB.
Element Element Fitness Fitness
! 3
" 4
# 0.5
$ 1.5
% 1
4EB CFOPQ QEFKD TBfII T>KQ QL AL FP 9:=8,74E0 9:=8,74E0 >II QEB P@LOBP. 2BJBJ?BO KLOJ>IFWFKD > SB@QLO?
4E>Q FKSLISBA Q>HFKD >K SB@QLO >KA PQ>KA>OAFWFKD FQP IBKDQE, PBQQFKD FQ QL 1. 7EBK TB
KLOJ>IFWB > PBQ LC CFQKBPP P@LOBP, TB >OB PQ>KA>OAFWFKD QEBFO O>KDB QL ?BQTBBK 0 >KA 1, >P >
MBO@BKQ>DB LC QLQ>I CFQKBPP. ,BQfP >AA RM >II QEB CFQKBPP P@LOBP.
QLQ>I CFQKBPP = 3 + 4 + 0.5 + 1.5 + 1 = 10
4EBK IBQfP AFSFAB B>@E P@LOB ?V QEB QLQ>I CFQKBPP, DFSFKD RP QEB KLOJ>IFWBA CFQKBPP.
Element Element Fitness Fitness Normalized Fitness Normalized Fitness
Expressed as a Expressed as a
Percentage Percentage
! 3 0.3 30%
" 4 0.4 40%
# 0.5 0.05 5%
$ 1.5 0.15 15%
% 1 0.1 10%
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
398
.LT FQfP QFJB CLO QEB TEBBI LC CLOQRKB.
3MFK QEB TEBBI >KA VLRfII KLQF@B QE>Q %IBJBKQ " E>P QEB EFDEBPQ @E>K@B LC ?BFKD PBIB@QBA,
CLIILTBA ?V !, QEBK $, QEBK %, >KA CFK>IIV #. 4EFP MOL?>?FIFQV-?>PBA PBIB@QFLK >@@LOAFKD QL
CFQKBPP FP >K BU@BIIBKQ >MMOL>@E. /KB, FQ DR>O>KQBBP QE>Q QEB EFDEBPQ-P@LOFKD BIBJBKQP TFII ?B
JLPQ IFHBIV QL OBMOLAR@B. 4TL, FQ ALBP KLQ BKQFOBIV BIFJFK>QB >KV S>OF>QFLK COLJ QEB MLMRI>QFLK.
5KIFHB TFQE QEB BIFQFPQ JBQELA, BSBK QEB ILTBPQ-P@LOFKD BIBJBKQ (FK QEFP @>PB #) E>P > @E>K@B
QL M>PP FQP FKCLOJ>QFLK ALTK QL QEB KBUQ DBKBO>QFLK. )QfP NRFQB MLPPF?IB (>KA LCQBK QEB @>PB)
QE>Q BSBK ILT-P@LOFKD BIBJBKQP E>SB > QFKV KRDDBQ LC DBKBQF@ @LAB QE>Q FP QORIV RPBCRI >KA
PELRIA KLQ BKQFOBIV ?B BIFJFK>QBA COLJ QEB MLMRI>QFLK. &LO BU>JMIB, FK QEB @>PB LC BSLISFKD gQL
?B LO KLQ QL ?Bh, TB JFDEQ E>SB QEB CLIILTFKD BIBJBKQP.
A: JE 8; EH DEJ JE =E
B: JE 8; EH DEJ JE F?
C: NNNNNNNNNNNNNNNN8;
!P VLR @>K PBB, BIBJBKQP ! >KA " >OB @IB>OIV QEB JLPQ CFQ >KA TLRIA E>SB QEB EFDEBPQ P@LOB.
"RQ KBFQEBO @LKQ>FKP QEB @LOOB@Q @E>O>@QBOP CLO QEB BKA LC QEB MEO>PB. %IBJBKQ #, BSBK QELRDE
FQ TLRIA OB@BFSB > SBOV ILT P@LOB, E>MMBKP QL E>SB QEB DBKBQF@ A>Q> CLO QEB BKA LC QEB MEO>PB.
!KA PL TEFIB TB TLRIA T>KQ ! >KA " QL ?B MF@HBA QL DBKBO>QB QEB J>GLOFQV LC QEB KBUQ
DBKBO>QFLK, TB TLRIA PQFII T>KQ # QL E>SB > PJ>II @E>K@B QL M>OQF@FM>QB FK QEB OBMOLAR@QFSB
MOL@BPP.
F64B?2 9.2
9.6 The Genetic Algorithm, Part III: Reproduction 9.6 The Genetic Algorithm, Part III: Reproduction
.LT QE>Q TB E>SB > PQO>QBDV CLO MF@HFKD M>OBKQP, TB KBBA QL CFDROB LRQ ELT QL RPB
F9DFC8I7H=CB QL J>HB QEB MLMRI>QFLKfP KBUQ DBKBO>QFLK, HBBMFKD FK JFKA QEB $>OTFKF>K
MOFK@FMIB LC EBOBAFQVdQE>Q @EFIAOBK FKEBOFQ MOLMBOQFBP COLJ QEBFO M>OBKQP. !D>FK, QEBOB >OB >
KRJ?BO LC AFCCBOBKQ QB@EKFNRBP TB @LRIA BJMILV EBOB. &LO BU>JMIB, LKB OB>PLK>?IB (>KA B>PV
QL MOLDO>J) PQO>QBDV FP >PBUR>I OBMOLAR@QFLK, JB>KFKD TB MF@H GRPQ LKB M>OBKQ >KA @OB>QB >
4EB .>QROB LC #LAB (S005)
399
@EFIA QE>Q FP >K BU>@Q @LMV LC QE>Q M>OBKQ. 4EB PQ>KA>OA >MMOL>@E TFQE DBKBQF@ >IDLOFQEJP,
ELTBSBO, FP QL MF@H QTL M>OBKQP >KA @OB>QB > @EFIA >@@LOAFKD QL QEB CLIILTFKD PQBMP.
1) C=:>>:A0=. 1) C=:>>:A0=.
#OLPPLSBO FKSLISBP @OB>QFKD > @EFIA LRQ LC QEB DBKBQF@ @LAB LC QTL M>OBKQP. )K QEB @>PB LC
QEB JLKHBV-QVMFKD BU>JMIB, IBQfP >PPRJB TBfSB MF@HBA QTL MEO>PBP COLJ QEB J>QFKD MLLI
(>P LRQIFKBA FK LRO PBIB@QFLK PQBM).
+7H;DJ A: "*,&
+7H;DJ B: +'A3
)QfP KLT RM QL RP QL J>HB > @EFIA MEO>PB COLJ QEBPB QTL. 0BOE>MP QEB JLPQ L?SFLRP T>V
(IBQfP @>II QEFP QEB 50/50 JBQELA) TLRIA ?B QL Q>HB QEB CFOPQ QTL @E>O>@QBOP COLJ ! >KA QEB
PB@LKA QTL COLJ ", IB>SFKD RP TFQE:
! S>OF>QFLK LC QEFP QB@EKFNRB FP QL MF@H > O>KALJ JFAMLFKQ. )K LQEBO TLOAP, TB ALKfQ E>SB QL
MF@H BU>@QIV E>IC LC QEB @LAB COLJ B>@E M>OBKQ. 7B @LRIA PLJBQFJBP BKA RM TFQE &,!9, >KA
PLJBQFJBP TFQE &/29. 4EFP FP MOBCBO>?IB QL QEB 50/50 >MMOL>@E, PFK@B TB FK@OB>PB QEB
S>OFBQV LC MLPPF?FIFQFBP CLO QEB KBUQ DBKBO>QFLK.
F64B?2 9.3
F64B?2 9.4: '6086;4 . ?.;1<: :61=<6;A
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
400
!KLQEBO MLPPF?FIFQV FP QL O>KALJIV PBIB@Q > M>OBKQ CLO B>@E @E>O>@QBO FK QEB @EFIA PQOFKD. 9LR
@>K QEFKH LC QEFP >P CIFMMFKD > @LFK CLRO QFJBP: EB>AP Q>HB COLJ M>OBKQ !, Q>FIP COLJ M>OBKQ ".
(BOB TB @LRIA BKA RM TFQE J>KV AFCCBOBKQ OBPRIQP PR@E >P: 0,29, &,2+, &,29, &/29, BQ@.
4EFP PQO>QBDV TFII MOLAR@B BPPBKQF>IIV QEB P>JB OBPRIQP >P QEB O>KALJ JFAMLFKQ JBQELA;
ELTBSBO, FC QEB LOABO LC QEB DBKBQF@ FKCLOJ>QFLK MI>VP PLJB OLIB FK BUMOBPPFKD QEB MEBKLQVMB,
VLR J>V MOBCBO LKB PLIRQFLK LSBO QEB LQEBO.
2) "@?,?4:9. 2) "@?,?4:9.
/K@B QEB @EFIA $.! E>P ?BBK @OB>QBA SF> @OLPPLSBO, TB >MMIV LKB CFK>I MOL@BPP ?BCLOB >AAFKD
QEB @EFIA QL QEB KBUQ DBKBO>QFLKd8@?,?4:9 8@?,?4:9. -RQ>QFLK FP >K LMQFLK>I PQBM, >P QEBOB >OB PLJB
@>PBP FK TEF@E FQ FP RKKB@BPP>OV. (LTBSBO, FQ BUFPQP ?B@>RPB LC QEB $>OTFKF>K MOFK@FMIB LC
S>OF>QFLK. 7B @OB>QBA >K FKFQF>I MLMRI>QFLK O>KALJIV, J>HFKD PROB QE>Q TB PQ>OQ TFQE > S>OFBQV LC
BIBJBKQP. (LTBSBO, QEBOB @>K LKIV ?B PL JR@E S>OFBQV TEBK PBBAFKD QEB CFOPQ DBKBO>QFLK, >KA
JRQ>QFLK >IILTP RP QL FKQOLAR@B >AAFQFLK>I S>OFBQV QEOLRDELRQ QEB BSLIRQFLK>OV MOL@BPP FQPBIC.
-RQ>QFLK FP ABP@OF?BA FK QBOJP LC > F5H9. !
DFSBK DBKBQF@ >IDLOFQEJ JFDEQ E>SB >
JRQ>QFLK O>QB LC 5% LO 1% LO 0.1%, BQ@. ,BQfP
>PPRJB TB GRPQ CFKFPEBA TFQE @OLPPLSBO >KA
BKABA RM TFQE QEB @EFIA &/29. )C TB E>SB >
JRQ>QFLK O>QB LC 1%, QEFP JB>KP QE>Q CLO B>@E
@E>O>@QBO FK QEB MEO>PB DBKBO>QBA COLJ
@OLPPLSBO, QEBOB FP > 1% @E>K@B QE>Q FQ TFII
JRQ>QB. 7E>Q ALBP FQ JB>K CLO > @E>O>@QBO QL
JRQ>QB? )K QEFP @>PB, TB ABCFKB JRQ>QFLK >P
MF@HFKD > KBT O>KALJ @E>O>@QBO. ! 1%
MOL?>?FIFQV FP C>FOIV ILT, >KA JLPQ LC QEB QFJB JRQ>QFLK TFII KLQ L@@RO >Q >II FK > CLRO-@E>O>@QBO
PQOFKD (96% LC QEB QFJB QL ?B JLOB MOB@FPB). (LTBSBO, TEBK FQ ALBP, QEB JRQ>QBA @E>O>@QBO FP
OBMI>@BA TFQE > O>KALJIV DBKBO>QBA LKB (PBB &FDROB 9.6).
F64B?2 9.5: C<6;-396==6;4 .==?<.05
F64B?2 9.6
4EB .>QROB LC #LAB (S005)
401
!P TBfII PBB FK PLJB LC QEB BU>JMIBP, QEB JRQ>QFLK O>QB @>K DOB>QIV >CCB@Q QEB ?BE>SFLO LC
QEB PVPQBJ. #BOQ>FKIV, > SBOV EFDE JRQ>QFLK O>QB (PR@E >P, P>V, 80%) TLRIA KBD>QB QEB
BSLIRQFLK>OV MOL@BPP FQPBIC. )C QEB J>GLOFQV LC > @EFIAfP DBKBP >OB DBKBO>QBA O>KALJIV, QEBK
TB @>KKLQ DR>O>KQBB QE>Q QEB JLOB gCFQh DBKBP L@@RO TFQE DOB>QBO COBNRBK@V TFQE B>@E
PR@@BPPFSB DBKBO>QFLK.
4EB MOL@BPP LC PBIB@QFLK (MF@HFKD QTL M>OBKQP) >KA OBMOLAR@QFLK (@OLPPLSBO >KA JRQ>QFLK) FP
>MMIFBA LSBO >KA LSBO >D>FK , QFJBP RKQFI TB E>SB > KBT MLMRI>QFLK LC , BIBJBKQP. !Q QEFP
MLFKQ, QEB KBT MLMRI>QFLK LC @EFIAOBK ?B@LJBP QEB @ROOBKQ MLMRI>QFLK >KA TB ILLM ?>@H QL
BS>IR>QB CFQKBPP >KA MBOCLOJ PBIB@QFLK >KA OBMOLAR@QFLK >D>FK.
.LT QE>Q TB E>SB ABP@OF?BA >II QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ FK ABQ>FI, FQfP QFJB QL
QO>KPI>QB QEBPB PQBMP FKQL 0OL@BPPFKD @LAB. "B@>RPB QEB MOBSFLRP ABP@OFMQFLK T>P > ?FQ
ILKDTFKABA, IBQfP ILLH >Q >K LSBOSFBT LC QEB >IDLOFQEJ CFOPQ. 7BfII QEBK @LSBO B>@E LC QEB
QEOBB PQBMP FK FQP LTK PB@QFLK, TLOHFKD LRQ QEB @LAB.
'E()%: 'E()%:
3QBM 1: I94?4,74E0 I94?4,74E0. #OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE O>KALJIV DBKBO>QBA $.!.
!$$%: !$$%:
3QBM 2: '070.?4:9 '070.?4:9. %S>IR>QB QEB CFQKBPP LC B>@E BIBJBKQ LC QEB MLMRI>QFLK >KA ?RFIA > J>QFKD
MLLI.
3QBM 3: &0;=:/@.?4:9 &0;=:/@.?4:9. 2BMB>Q . QFJBP:
>) 0F@H QTL M>OBKQP TFQE MOL?>?FIFQV >@@LOAFKD QL OBI>QFSB CFQKBPP.
?) #OLPPLSBOd@OB>QB > g@EFIAh ?V @LJ?FKFKD QEB $.! LC QEBPB QTL M>OBKQP.
@) -RQ>QFLKdJRQ>QB QEB @EFIAfP $.! ?>PBA LK > DFSBK MOL?>?FIFQV.
A) !AA QEB KBT @EFIA QL > KBT MLMRI>QFLK.
3QBM 4. 2BMI>@B QEB LIA MLMRI>QFLK TFQE QEB KBT MLMRI>QFLK >KA OBQROK QL 3QBM 2.
9.7 Code for Creating the Population 9.7 Code for Creating the Population
Step 1: Initialize Population Step 1: Initialize Population
)C TBfOB DLFKD QL @OB>QB > MLMRI>QFLK, TB KBBA > A>Q> PQOR@QROB QL PQLOB > IFPQ LC JBJ?BOP LC
QEB MLMRI>QFLK. )K JLPQ @>PBP (PR@E >P LRO QVMFKD-JLKHBV BU>JMIB), QEB KRJ?BO LC BIBJBKQP
FK QEB MLMRI>QFLK @>K ?B CFUBA, >KA PL TB RPB >K >OO>V. (,>QBO TBfII PBB BU>JMIBP QE>Q
FKSLISB > DOLTFKD/PEOFKHFKD MLMRI>QFLK >KA TBfII RPB >K ALL;S*CMN.) "RQ >K >OO>V LC TE>Q?
7B KBBA >K L?GB@Q QE>Q PQLOBP QEB DBKBQF@ FKCLOJ>QFLK CLO > JBJ?BO LC QEB MLMRI>QFLK. ,BQfP
@>II FQ D#A D#A.
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
402
4EB MLMRI>QFLK TFII QEBK ?B >K >OO>V LC ",A L?GB@QP.
"RQ TE>Q PQRCC DLBP FK QEB ",A @I>PP? &LO > QVMFKD JLKHBV, FQP $.! FP QEB O>KALJ MEO>PB FQ
QVMBP, > PQOFKD LC @E>O>@QBOP.
7EFIB QEFP FP MBOCB@QIV OB>PLK>?IB CLO QEFP M>OQF@RI>O BU>JMIB, TBfOB KLQ DLFKD QL RPB >K >@QR>I
0NLCHA L?GB@Q >P QEB DBKBQF@ @LAB. )KPQB>A, TBfII RPB >K >OO>V LC @E>O>@QBOP.
"V RPFKD >K >OO>V, TBfII ?B >?IB QL BUQBKA >II QEB @LAB TB TOFQB FKQL LQEBO BU>JMIBP. &LO
BU>JMIB, QEB $.! LC > @OB>QROB FK > MEVPF@P PVPQBJ JFDEQ ?B >K >OO>V LC .3?=NILPdLO CLO >K
FJ>DB, >K >OO>V LC FKQBDBOP (2'" @LILOP). 7B @>K ABP@OF?B >KV PBQ LC MOLMBOQFBP FK >K >OO>V,
>KA BSBK QELRDE > PQOFKD FP @LKSBKFBKQ CLO QEFP M>OQF@RI>O PHBQ@E, >K >OO>V TFII PBOSB >P > ?BQQBO
CLRKA>QFLK CLO CRQROB BSLIRQFLK>OV BU>JMIBP.
/RO DBKBQF@ >IDLOFQEJ AF@Q>QBP QE>Q TB @OB>QB > MLMRI>QFLK LC . BIBJBKQP, B>@E TFQE F5B8CA@M
;9B9F5H98 *A. 4EBOBCLOB, FK QEB L?GB@QfP @LKPQOR@QLO, TB O>KALJIV @OB>QB B>@E @E>O>@QBO LC
QEB >OO>V.
.LT QE>Q TB E>SB QEB @LKPQOR@QLO, TB @>K OBQROK QL M?NOJ() >KA FKFQF>IFWB B>@E ",A L?GB@Q FK
QEB MLMRI>QFLK >OO>V.
=F;MM ",A U
W
A population of 100 DNA objects ",A89 JIJOF;NCIH = H?Q ",A81009;
=F;MM ",A U
0NLCHA JBL;M?;
W
=F;MM ",A U
Each "gene" is one element of the array. We
need 18 genes because to be or not to be
is 18 characters long.
=B;L89 A?H?M = H?Q =B;L8189;
W
=F;MM ",A U
=B;L89 A?H?M = H?Q =B;L8189;
",A() U
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Picking randomly from a range of characters
with ASCII values between 32 and 128. For
more about ASCII: http://en.wikipedia.org/
wiki/ASCII
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
4EB .>QROB LC #LAB (S005)
403
/RO ",A @I>PP FP KLQ >Q >II @LJMIBQB. 7BfII KBBA QL >AA CRK@QFLKP QL FQ QL MBOCLOJ >II QEB LQEBO
Q>PHP FK LRO DBKBQF@ >IDLOFQEJ, TEF@E TBfII AL >P TB T>IH QEOLRDE PQBMP 2 >KA 3.
",A89 JIJOF;NCIH = H?Q ",A81009;
PIC> M?NOJ() U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
Initializing each member of the population JIJOF;NCIH8C9 = H?Q ",A();
W
W
Step 2: Selection Step 2: Selection
3QBM 2 OB>AP, T!J5@I5H9 H<9 :=HB9GG C: 957< 9@9A9BH C: H<9 DCDI@5H=CB 5B8 6I=@8 5 A5H=B;
DCC@.U ,BQfP CFOPQ BS>IR>QB B>@E L?GB@QfP CFQKBPP. %>OIFBO TB PQ>QBA QE>Q LKB MLPPF?IB CFQKBPP
CRK@QFLK CLO LRO QVMBA MEO>PBP FP QEB QLQ>I KRJ?BO LC @LOOB@Q @E>O>@QBOP. ,BQfP OBSFPB QEFP
CFQKBPP CRK@QFLK > IFQQIB ?FQ >KA PQ>QB FQ >P QEB MBO@BKQ>DB LC @LOOB@Q @E>O>@QBOPdF.B., QEB QLQ>I
KRJ?BO LC @LOOB@Q @E>O>@QBOP AFSFABA ?V QEB QLQ>I @E>O>@QBOP.
"?JD;II = .EJ7B # C>7H79J;HI CEHH;9J/.EJ7B # C>7H79J;HI
7EBOB PELRIA TB @>I@RI>QB QEB CFQKBPP? 3FK@B QEB ",A @I>PP @LKQ>FKP QEB DBKBQF@ FKCLOJ>QFLK
(QEB MEO>PB TB TFII QBPQ >D>FKPQ QEB Q>ODBQ MEO>PB), TB @>K TOFQB > CRK@QFLK FKPFAB QEB ",A
@I>PP FQPBIC QL P@LOB FQP LTK CFQKBPP. ,BQfP >PPRJB TB E>SB > Q>ODBQ MEO>PB:
7B @>K KLT @LJM>OB B>@E gDBKBh >D>FKPQ QEB @LOOBPMLKAFKD @E>O>@QBO FK QEB Q>ODBQ MEO>PB,
FK@OBJBKQFKD > @LRKQBO B>@E QFJB TB DBQ > @LOOB@Q @E>O>@QBO.
0NLCHA N;LA?N = "NI <? IL HIN NI <?";
=F;MM ",A U
We are adding another variable to the DNA
class to track fitness.
@FI;N @CNH?MM;
Function to score fitness PIC> @CNH?MM () U
CHN M=IL? = 0;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Is the character correct? C@ (A?H?M8C9 == N;LA?N.=B;LAN(C)) U
If so, increment the score. M=IL?++;
W
W
Fitness is the percentage correct. @CNH?MM = @FI;N(M=IL?)/N;LA?N.F?HANB();
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
404
)K QEB J>FK Q>?fP >L;Q(), QEB SBOV CFOPQ PQBM TBfII Q>HB FP QL @>II QEB CFQKBPP CRK@QFLK CLO B>@E
JBJ?BO LC QEB MLMRI>QFLK.
!CQBO TB E>SB >II QEB CFQKBPP P@LOBP, TB @>K ?RFIA QEB gJ>QFKD MLLIh QE>Q TBfII KBBA CLO QEB
OBMOLAR@QFLK PQBM. 4EB J>QFKD MLLI FP > A>Q> PQOR@QROB COLJ TEF@E TBfII @LKQFKRLRPIV MF@H QTL
M>OBKQP. 2B@>IIFKD LRO ABP@OFMQFLK LC QEB PBIB@QFLK MOL@BPP, TB T>KQ QL MF@H M>OBKQP TFQE
MOL?>?FIFQFBP @>I@RI>QBA >@@LOAFKD QL CFQKBPP. )K LQEBO TLOAP, QEB JBJ?BOP LC QEB MLMRI>QFLK
QE>Q E>SB QEB EFDEBPQ CFQKBPP P@LOBP PELRIA ?B JLPQ IFHBIV QL ?B MF@HBA; QELPB TFQE QEB ILTBPQ
P@LOBP, QEB IB>PQ IFHBIV.
)K QEB )KQOLAR@QFLK (PBB M>DB 7), TB @LSBOBA QEB ?>PF@P LC MOL?>?FIFQV >KA DBKBO>QFKD > @RPQLJ
AFPQOF?RQFLK LC O>KALJ KRJ?BOP. 7BfOB DLFKD QL RPB QELPB QB@EKFNRBP QL >PPFDK > MOL?>?FIFQV
QL B>@E JBJ?BO LC QEB MLMRI>QFLK, MF@HFKD M>OBKQP ?V PMFKKFKD QEB gTEBBI LC CLOQRKB.h ,BQfP
ILLH >Q &FDROB 9.2 >D>FK.
)Q JFDEQ ?B CRK QL AL PLJBQEFKD OFAF@RILRP >KA >@QR>IIV MOLDO>J > PFJRI>QFLK LC > PMFKKFKD
TEBBI >P ABMF@QBA >?LSB. "RQ QEFP FP NRFQB RKKB@BPP>OV.
PIC> >L;Q() U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9.@CNH?MM();
W
F64B?2 9.2 (.4.6;)
4EB .>QROB LC #LAB (S005)
405
)KPQB>A TB @>K MF@H COLJ QEB CFSB LMQFLKP
(!"#$%) >@@LOAFKD QL QEBFO MOL?>?FIFQFBP ?V
CFIIFKD >K ALL;S*CMN TFQE JRIQFMIB FKPQ>K@BP
LC B>@E M>OBKQ. )K LQEBO TLOAP, IBQfP P>V VLR
E>A > ?R@HBQ LC TLLABK IBQQBOPd30 !P, 40
"P, 5 #P, 15 $P, >KA 10 %P.
)C VLR MF@H > O>KALJ IBQQBO LRQ LC QE>Q
?R@HBQ, QEBOBfP > 30% @E>K@B VLRfII DBQ >K
!, > 5% @E>K@B VLRfII DBQ > #, >KA PL LK.
&LO RP, QE>Q ?R@HBQ FP >K ALL;S*CMN, >KA
B>@E TLLABK IBQQBO FP > MLQBKQF>I M>OBKQ.
7B >AA B>@E M>OBKQ QL QEB ALL;S*CMN .
KRJ?BO LC QFJBP TEBOB . FP BNR>I QL FQP
MBO@BKQ>DB P@LOB.
F64B?2 9.7
Start with an empty mating pool. ALL;S*CMN<",A> G;NCHA.IIF = H?Q
ALL;S*CMN<",A>();
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
n is equal to fitness times 100, which
leaves us with an integer between 0 and
100.
CHN H = CHN(JIJOF;NCIH8C9.@CNH?MM * 100);
@IL (CHN D = 0; D < H; D++) U
Add each member of the population to the
mating pool N times.
G;NCHA.IIF.;>>(JIJOF;NCIH8C9);
W
W
/KB LC QEB LQEBO JBQELAP TB RPBA QL DBKBO>QB > @RPQLJ AFPQOF?RQFLK LC O>KALJ
KRJ?BOP FP @>IIBA QEB -LKQB #>OIL JBQELA. 4EFP QB@EKFNRB FKSLISBA MF@HFKD QTL
O>KALJ KRJ?BOP, TFQE QEB PB@LKA KRJ?BO >@QFKD >P > NR>IFCVFKD KRJ?BO >KA
ABQBOJFKFKD FC QEB CFOPQ O>KALJ KRJ?BO PELRIA ?B HBMQ LO QEOLTK >T>V. 2BTOFQB QEB
>?LSB J>QFKD MLLI >IDLOFQEJ QL RPB QEB -LKQB #>OIL JBQELA FKPQB>A.
Exercise 9.2 Exercise 9.2
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
406
)K PLJB @>PBP, QEB TEBBI LC CLOQRKB >IDLOFQEJ TFII E>SB >K BUQO>LOAFK>OFIV EFDE
MOBCBOBK@B CLO PLJB BIBJBKQP LSBO LQEBOP. 4>HB QEB CLIILTFKD MOL?>?FIFQFBP:
A: 98%
B: 1%
C: 1%
4EFP FP PLJBQFJBP RKABPFO>?IB DFSBK ELT FQ TFII AB@OB>PB QEB >JLRKQ LC S>OFBQV FK QEFP
PVPQBJ. ! PLIRQFLK QL QEFP MOL?IBJ FP QL OBMI>@B QEB @>I@RI>QBA CFQKBPP P@LOBP TFQE QEB
LOAFK>IP LC P@LOFKD (JB>KFKD QEBFO O>KH).
A: 50% (3/6)
B: 33% (2/6)
C: 17% (1/6)
2BTOFQB QEB J>QFKD MLLI >IDLOFQEJ QL RPB QEFP JBQELA FKPQB>A.
Exercise 9.3 Exercise 9.3
Step 3: Reproduction Step 3: Reproduction
7FQE QEB J>QFKD MLLI OB>AV QL DL, FQfP QFJB QL J>HB PLJB ?>?FBP. 4EB CFOPQ PQBM FP QL MF@H QTL
M>OBKQP. !D>FK, FQfP PLJBTE>Q LC >K >O?FQO>OV AB@FPFLK QL MF@H QTL M>OBKQP. )Q @BOQ>FKIV JFOOLOP
ERJ>K OBMOLAR@QFLK >KA FP QEB PQ>KA>OA JB>KP FK QEB QO>AFQFLK>I '!, ?RQ FK QBOJP LC VLRO
TLOH, QEBOB OB>IIV >OBKfQ >KV OBPQOF@QFLKP EBOB. 9LR @LRIA @ELLPB QL MBOCLOJ g>PBUR>Ih
OBMOLAR@QFLK TFQE LKB M>OBKQ, LO @LJB RM TFQE > P@EBJB CLO MF@HFKD QEOBB LO CLRO M>OBKQP COLJ
TEF@E QL DBKBO>QB @EFIA $.!. &LO QEFP @LAB ABJLKPQO>QFLK, TBfII PQF@H QL QTL M>OBKQP >KA @>II
QEBJ J;L?HNA >KA J;L?HN.
&FOPQ QEFKD TB KBBA >OB QTL O>KALJ FKAF@BP FKQL QEB J>QFKD MLLIdO>KALJ KRJ?BOP ?BQTBBK 0
>KA QEB PFWB LC QEB ALL;S*CMN.
7B @>K RPB QEBPB FKAF@BP QL OBQOFBSB >K >@QR>I $.! FKPQ>K@B COLJ QEB J>QFKD MLLI.
"B@>RPB TB E>SB JRIQFMIB FKPQ>K@BP LC QEB P>JB ",A L?GB@QP FK QEB J>QFKD MLLI (KLQ QL
JBKQFLK QE>Q TB @LRIA MF@H QEB P>JB O>KALJ KRJ?BO QTF@B), FQfP MLPPF?IB QE>Q J;L?HNA >KA
J;L?HN @LRIA ?B QEB P>JB ",A L?GB@Q. )C TB T>KQBA QL ?B PQOF@Q, TB @LRIA TOFQB PLJB @LAB QL
CHN ; = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
CHN < = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
",A J;L?HNA = G;NCHA.IIF.A?N(;);
",A J;L?HN = G;NCHA.IIF.A?N(<);
4EB .>QROB LC #LAB (S005)
407
BKPROB QE>Q TB E>SBKfQ MF@HBA QEB P>JB M>OBKQ QTF@B, ?RQ TB TLRIA D>FK SBOV IFQQIB
BCCF@FBK@V CLO >II QE>Q BUQO> @LAB. 3QFII, FQeP TLOQE QOVFKD QEFP >P >K BUBO@FPB.
/K@B TB E>SB QEB QTL M>OBKQP, TB @>K MBOCLOJ .=:>>:A0= .=:>>:A0= QL DBKBO>QB QEB @EFIA $.!,
CLIILTBA ?V 8@?,?4:9 8@?,?4:9.
/C @LROPB, QEB CRK@QFLKP =LIMMIP?L() >KA GON;N?() ALKfQ J>DF@>IIV BUFPQ FK LRO ",A @I>PP;
TB E>SB QL TOFQB QEBJ. 4EB T>V TB @>IIBA =LIMMIP?L() >?LSB FKAF@>QBP QE>Q QEB CRK@QFLK
OB@BFSBP >K FKPQ>K@B LC $.! >P >K >ODRJBKQ >KA OBQROKP > KBT FKPQ>K@B LC $.!, QEB @EFIA.
4EB >?LSB @OLPPLSBO CRK@QFLK RPBP QEB gO>KALJ JFAMLFKQh JBQELA LC @OLPPLSBO, FK TEF@E
QEB CFOPQ PB@QFLK LC DBKBP FP Q>HBK COLJ M>OBKQ ! >KA QEB PB@LKA PB@QFLK COLJ M>OBKQ ".
!AA @LAB QL QEB >?LSB QL DR>O>KQBB QE>Q VLR E>SB MF@HBA QTL RKFNRB gM>OBKQP.h
Exercise 9.4 Exercise 9.4
A function for crossover ",A =BCF> = J;L?HNA.=LIMMIP?L(J;L?HN);
A function for mutation =BCF>.GON;N?();
The function receives one argument (DNA)
and returns DNA.
",A =LIMMIP?L(",A J;LNH?L) U
The child is a new instance of DNA. Note
that the DNA is generated randomly in the
constructor, but we will overwrite it below
with DNA from parents.
",A =BCF> = H?Q ",A();
Picking a random midpoint in the genes
array
CHN GC>JICHN = CHN(L;H>IG(A?H?M.F?HANB));
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Before midpoint copy genes from one
parent, after midpoint copy genes from the
other parent
C@ (C > GC>JICHN) =BCF>.A?H?M8C9 = A?H?M8C9;
?FM? =BCF>.A?H?M8C9 = J;LNH?L.A?H?M8C9;
W
Return the new child DNA L?NOLH =BCF>;
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
408
4EB JRQ>QB() CRK@QFLK FP BSBK PFJMIBO QL TOFQB QE>K @OLPPLSBO(). !II TB KBBA QL AL FP ILLM
QEOLRDE QEB >OO>V LC DBKBP >KA CLO B>@E O>KALJIV MF@H > KBT @E>O>@QBO >@@LOAFKD QL QEB
JRQ>QFLK O>QB. 7FQE > JRQ>QFLK O>QB LC 1%, CLO BU>JMIB, TB TLRIA MF@H > KBT @E>O>@QBO LKB
QFJB LRQ LC > ERKAOBA.
4EB BKQFOB CRK@QFLK QEBOBCLOB OB>AP:
2BTOFQB QEB @OLPPLSBO CRK@QFLK QL RPB QEB g@LFK CIFMMFKDh JBQELA FKPQB>A, FK TEF@E B>@E
DBKB E>P > 50% @E>K@B LC @LJFKD COLJ M>OBKQ ! >KA > 50% @E>K@B LC @LJFKD COLJ
M>OBKQ ".
Exercise 9.5 Exercise 9.5
@FI;N GON;NCIH/;N? = 0.01;
C@ (L;H>IG(1) < GON;NCIH/;N?) U
Any code here would be executed 1% of the
time.
W
PIC> GON;N?() U
Looking at each gene in the array @IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (L;H>IG(1) < GON;NCIH/;N?) U
Mutation, a new random character A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
9.8 Genetic Algorithms: Putting It All Together 9.8 Genetic Algorithms: Putting It All Together
9LR J>V E>SB KLQF@BA QE>Q TBfSB BPPBKQF>IIV T>IHBA QEOLRDE QEB PQBMP LC QEB DBKBQF@
>IDLOFQEJ QTF@B, LK@B ABP@OF?FKD FQ FK K>OO>QFSB CLOJ >KA >KLQEBO QFJB TFQE @LAB PKFMMBQP
FJMIBJBKQFKD B>@E LC QEB PQBMP. 7E>Q )fA IFHB QL AL FK QEFP PB@QFLK FP @LKABKPB QEB MOBSFLRP
QTL PB@QFLKP FKQL LKB M>DB, TFQE QEB >IDLOFQEJ ABP@OF?BA FK GRPQ QEOBB PQBMP >KA QEB
@LOOBPMLKAFKD @LAB >ILKDPFAB.
4EB .>QROB LC #LAB (S005)
409
Example 9.1: Genetic algorithm: Evolving Shakespeare
Variables we need for our GA
Mutation rate @FI;N GON;NCIH/;N?;
Population total CHN NIN;F.IJOF;NCIH = 150;
Population array ",A89 JIJOF;NCIH;
Mating pool ArrayList ALL;S*CMN<",A> G;NCHA.IIF;
Target phrase 0NLCHA N;LA?N;
PIC> M?NOJ() U
MCT?(200, 200);
Initializing target phrase and mutation rate N;LA?N = "NI <? IL HIN NI <?";
GON;NCIH/;N? = 0.01;
Step 1: Initialize Population JIJOF;NCIH = H?Q ",A8NIN;F.IJOF;NCIH9;
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9 = H?Q ",A();
W
W
PIC> >L;Q() U
Step 2: Selection
Step 2a: Calculate fitness. @IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
JIJOF;NCIH8C9.@CNH?MM();
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
410
4EB J>FK Q>? MOB@FPBIV JFOOLOP QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ. (LTBSBO, JLPQ LC QEB
CRK@QFLK>IFQV @>IIBA RMLK FP >@QR>IIV MOBPBKQ FK QEB ",A @I>PP FQPBIC.
Step 2b: Build mating pool. ALL;S*CMN<",A> G;NCHA.IIF = H?Q ALL;S*CMN<",A>();
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
Add each member n times according to its
fitness score.
CHN H = CHN(JIJOF;NCIH8C9.@CNH?MM * 100);
@IL (CHN D = 0; D < H; D++) U
G;NCHA.IIF.;>>(JIJOF;NCIH8C9);
W
W
Step 3: Reproduction @IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
CHN ; = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
CHN < = CHN(L;H>IG(G;NCHA.IIF.MCT?()));
",A J;LNH?LA = G;NCHA.IIF.A?N(;);
",A J;LNH?L = G;NCHA.IIF.A?N(<);
Step 3a: Crossover ",A =BCF> = J;LNH?LA.=LIMMIP?L(J;LNH?L);
Step 3b: Mutation =BCF>.GON;N?(GON;NCIH/;N?);
Note that we are overwriting the population
with the new children. When draw() loops,
we will perform all the same steps with the
new population of children.
JIJOF;NCIH8C9 = =BCF>;
W
W
=F;MM ",A U
=B;L89 A?H?M;
@FI;N @CNH?MM;
Create DNA randomly. ",A() U
A?H?M = H?Q =B;L8N;LA?N.F?HANB()9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
Calculate fitness. PIC> @CNH?MM() U
CHN M=IL? = 0;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (A?H?M8C9 == N;LA?N.=B;LAN(C)) U
M=IL?++;
W
W
@CNH?MM = @FI;N(M=IL?)/N;LA?N.F?HANB();
W
4EB .>QROB LC #LAB (S005)
411
Crossover ",A =LIMMIP?L(",A J;LNH?L) U
",A =BCF> = H?Q ",A(A?H?M.F?HANB);
CHN GC>JICHN = CHN(L;H>IG(A?H?M.F?HANB));
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (C > GC>JICHN) =BCF>.A?H?M8C9 = A?H?M8C9;
?FM? =BCF>.A?H?M8C9 = J;LNH?L.A?H?M8C9;
W
L?NOLH =BCF>;
W
Mutation PIC> GON;N?(@FI;N GON;NCIH/;N?) U
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
C@ (L;H>IG(1) < GON;NCIH/;N?) U
A?H?M8C9 = (=B;L) L;H>IG(32,128);
W
W
W
Convert to StringPHENOTYPE. 0NLCHA A?N.BL;M?() U
L?NOLH H?Q 0NLCHA(A?H?M);
W
W
!AA CB>QROBP QL QEB >?LSB BU>JMIB QL OBMLOQ JLOB FKCLOJ>QFLK >?LRQ QEB MOLDOBPP LC
QEB DBKBQF@ >IDLOFQEJ FQPBIC. &LO BU>JMIB, PELT QEB MEO>PB @ILPBPQ QL QEB Q>ODBQ B>@E
DBKBO>QFLK, >P TBII >P OBMLOQ LK QEB KRJ?BO LC DBKBO>QFLKP, >SBO>DB CFQKBPP, BQ@. 3QLM
QEB DBKBQF@ >IDLOFQEJ LK@B FQ E>P PLISBA QEB MEO>PB. #LKPFABO TOFQFKD > .IJOF;NCIH
@I>PP QL J>K>DB QEB '! (FKPQB>A LC FK@IRAFKD >II QEB @LAB FK AO>T()).
Exercise 9.6 Exercise 9.6
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
412
9.9 Genetic Algorithms: Make Them Your Own 9.9 Genetic Algorithms: Make Them Your Own
4EB KF@B QEFKD >?LRQ RPFKD DBKBQF@ >IDLOFQEJP FK > MOLGB@Q FP QE>Q BU>JMIB @LAB @>K B>PFIV ?B
MLOQBA COLJ >MMIF@>QFLK QL >MMIF@>QFLK. 4EB @LOB JB@E>KF@P LC PBIB@QFLK >KA OBMOLAR@QFLK
ALKfQ KBBA QL @E>KDB. 4EBOB >OB, ELTBSBO, QEOBB HBV @LJMLKBKQP QL DBKBQF@ >IDLOFQEJP QE>Q
VLR, QEB ABSBILMBO, TFII E>SB QL @RPQLJFWB CLO B>@E RPB. 4EFP FP @OR@F>I QL JLSFKD ?BVLKA
QOFSF>I ABJLKPQO>QFLKP LC BSLIRQFLK>OV PFJRI>QFLKP (>P FK QEB 3E>HBPMB>OB BU>JMIB) QL @OB>QFSB
RPBP FK MOLGB@QP QE>Q VLR J>HB FK 0OL@BPPFKD >KA LQEBO @OB>QFSB MOLDO>JJFKD BKSFOLKJBKQP.
Key #1: Varying the variables Key #1: Varying the variables
4EBOB >OBKfQ > ILQ LC S>OF>?IBP QL QEB DBKBQF@ >IDLOFQEJ FQPBIC. )K C>@Q, FC VLR ILLH >Q QEB MOBSFLRP
BU>JMIBfP @LAB, VLRfII PBB LKIV QTL DIL?>I S>OF>?IBP (KLQ FK@IRAFKD QEB >OO>VP >KA ALL;S*CMNP
QL PQLOB QEB MLMRI>QFLK >KA J>QFKD MLLI).
4EBPB QTL S>OF>?IBP @>K DOB>QIV >CCB@Q QEB ?BE>SFLO LC QEB PVPQBJ, >KA FQfP KLQ PR@E > DLLA
FAB> QL >O?FQO>OFIV >PPFDK QEBJ S>IRBP (QELRDE QTB>HFKD QEBJ QEOLRDE QOF>I >KA BOOLO FP >
MBOCB@QIV OB>PLK>?IB T>V QL >OOFSB >Q LMQFJ>I S>IRBP).
4EB S>IRBP ) @ELPB CLO QEB 3E>HBPMB>OB ABJLKPQO>QFLK TBOB MF@HBA QL SFOQR>IIV DR>O>KQBB QE>Q
QEB DBKBQF@ >IDLOFQEJ TLRIA PLISB CLO QEB MEO>PB, ?RQ KLQ QLL NRF@HIV (>MMOLUFJ>QBIV 1,000
DBKBO>QFLKP LK >SBO>DB) PL >P QL ABJLKPQO>QB QEB MOL@BPP LSBO > OB>PLK>?IB MBOFLA LC QFJB. !
JR@E I>ODBO MLMRI>QFLK, ELTBSBO, TLRIA VFBIA C>PQBO OBPRIQP (FC QEB DL>I TBOB >IDLOFQEJF@
BCCF@FBK@V O>QEBO QE>K ABJLKPQO>QFLK). (BOB FP > Q>?IB LC PLJB OBPRIQP.
Total Population Total Population Mutation Rate Mutation Rate
Number of Number of
Generations until Generations until
Phrase Solved Phrase Solved
Total Time (in Total Time (in
seconds) until Phrase seconds) until Phrase
Solved Solved
150 1% 1089 18.8
300 1% 448 8.2
1,000 1% 71 1.8
50,000 1% 27 4.3
.LQF@B ELT FK@OB>PFKD QEB MLMRI>QFLK PFWB AO>PQF@>IIV OBAR@BP QEB KRJ?BO LC DBKBO>QFLKP
KBBABA QL PLISB CLO QEB MEO>PB. (LTBSBO, FQ ALBPKfQ KB@BPP>OFIV OBAR@B QEB >JLRKQ LC QFJB.
/K@B LRO MLMRI>QFLK ?>IILLKP QL CFCQV QELRP>KA BIBJBKQP, QEB PHBQ@E ORKP PILTIV, DFSBK QEB
@FI;N GON;NCIH/;N? = 0.01;
CHN NIN;F.IJOF;NCIH = 150;
4EB .>QROB LC #LAB (S005)
413
>JLRKQ LC QFJB OBNRFOBA QL MOL@BPP CFQKBPP >KA ?RFIA > J>QFKD MLLI LRQ LC PL J>KV BIBJBKQP.
(4EBOB >OB, LC @LROPB, LMQFJFW>QFLKP QE>Q @LRIA ?B J>AB PELRIA VLR OBNRFOB PR@E > I>ODB
MLMRI>QFLK.)
)K >AAFQFLK QL QEB MLMRI>QFLK PFWB, QEB JRQ>QFLK O>QB @>K DOB>QIV >CCB@Q MBOCLOJ>K@B.
Total Population Total Population Mutation Rate Mutation Rate
Number of Number of
Generations until Generations until
Phrase Solved Phrase Solved
Total Time (in Total Time (in
seconds) until seconds) until
Phrase Solved Phrase Solved
1,000 0% 37 LO KBSBO? 1.2 LO KBSBO?
1,000 1% 71 1.8
1,000 2% 60 1.6
1,000 10% KBSBO? KBSBO?
7FQELRQ >KV JRQ>QFLK >Q >II (0%), VLR GRPQ E>SB QL DBQ IR@HV. )C >II QEB @LOOB@Q @E>O>@QBOP >OB
MOBPBKQ PLJBTEBOB FK PLJB JBJ?BO LC QEB FKFQF>I MLMRI>QFLK, VLRfII BSLISB QEB MEO>PB SBOV
NRF@HIV. )C KLQ, QEBOB FP KL T>V CLO QEB PHBQ@E QL BSBO OB>@E QEB BU>@Q MEO>PB. 2RK FQ > CBT
QFJBP >KA VLRfII PBB ?LQE FKPQ>K@BP. )K >AAFQFLK, LK@B QEB JRQ>QFLK O>QB DBQP EFDE BKLRDE
(10%, CLO BU>JMIB), QEBOB FP PL JR@E O>KALJKBPP FKSLISBA (1 LRQ LC BSBOV 10 IBQQBOP FP
O>KALJ FK B>@E KBT @EFIA) QE>Q QEB PFJRI>QFLK FP MOBQQV JR@E ?>@H QL > O>KALJ QVMFKD
JLKHBV. )K QEBLOV, FQ TFII BSBKQR>IIV PLISB QEB MEO>PB, ?RQ VLR J>V ?B T>FQFKD JR@E, JR@E
ILKDBO QE>K FP OB>PLK>?IB.
Key #2: The fitness function Key #2: The fitness function
0I>VFKD >OLRKA TFQE QEB JRQ>QFLK O>QB LO MLMRI>QFLK QLQ>I FP MOBQQV B>PV >KA FKSLISBP IFQQIB
JLOB QE>K QVMFKD KRJ?BOP FK VLRO PHBQ@E. 4EB OB>I E>OA TLOH LC > ABSBILMFKD > DBKBQF@
>IDLOFQEJ FP FK TOFQFKD > CFQKBPP CRK@QFLK. )C VLR @>KKLQ ABCFKB VLRO MOL?IBJfP DL>IP >KA
BS>IR>QB KRJBOF@>IIV ELT TBII QELPB DL>IP E>SB ?BBK >@EFBSBA, QEBK VLR TFII KLQ E>SB
PR@@BPPCRI BSLIRQFLK FK VLRO PFJRI>QFLK.
"BCLOB TB QEFKH >?LRQ LQEBO P@BK>OFLP TFQE LQEBO CFQKBPP CRK@QFLKP, IBQfP ILLH >Q CI>TP FK LRO
3E>HBPMB>OB>K CFQKBPP CRK@QFLK. #LKPFABO PLISFKD CLO > MEO>PB QE>Q FP KLQ KFKBQBBK
@E>O>@QBOP ILKD, ?RQ LKB QELRP>KA. .LT, IBQfP P>V QEBOB >OB QTL JBJ?BOP LC QEB MLMRI>QFLK,
LKB TFQE 800 @E>O>@QBOP @LOOB@Q >KA LKB TFQE 801. (BOB >OB QEBFO CFQKBPP P@LOBP:
0EO>PB !: 800 @E>O>@QBOP @LOOB@Q CFQKBPP = 80%
0EO>PB ": 801 @E>O>@QBOP @LOOB@Q CFQKBPP = 80.1%
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
414
4EBOB >OB > @LRMIB LC MOL?IBJP EBOB. &FOPQ, TB >OB >AAFKD BIBJBKQP QL QEB J>QFKD MLLI .
KRJ?BOP LC QFJBP, TEBOB . BNR>IP CFQKBPP JRIQFMIFBA ?V LKB ERKAOBA. /?GB@QP @>K LKIV ?B
>AABA QL >K ALL;S*CMN > TELIB KRJ?BO LC QFJBP, >KA PL ! >KA " TFII ?LQE ?B >AABA 80 QFJBP,
DFSFKD QEBJ >K BNR>I MOL?>?FIFQV LC ?BFKD PBIB@QBA. %SBK TFQE >K FJMOLSBA PLIRQFLK QE>Q Q>HBP
CIL>QFKD MLFKQ MOL?>?FIFQFBP FKQL >@@LRKQ, 80.1% FP LKIV > QBBKV QFKV ?FQ EFDEBO QE>K 80%. "RQ
DBQQFKD 801 @E>O>@QBOP OFDEQ FP > TELIB ILQ ?BQQBO QE>K 800 FK QEB BSLIRQFLK>OV P@BK>OFL. 7B
OB>IIV T>KQ QL J>HB QE>Q >AAFQFLK>I @E>O>@QBO @LRKQ. 7B T>KQ QEB CFQKBPP P@LOB CLO 801
@E>O>@QBOP QL ?B BUMLKBKQF>IIV ?BQQBO QE>K QEB P@LOB CLO 800.
4L MRQ FQ >KLQEBO T>V, IBQfP DO>ME QEB CFQKBPP CRK@QFLK.
4EFP FP > IFKB>O DO>ME; >P QEB KRJ?BO LC @E>O>@QBOP DLBP RM, PL ALBP QEB CFQKBPP P@LOB.
(LTBSBO, TE>Q FC QEB CFQKBPP FK@OB>PBA BUMLKBKQF>IIV >P QEB KRJ?BO LC @LOOB@Q @E>O>@QBOP
FK@OB>PBA? /RO DO>ME @LRIA QEBK ILLH PLJBQEFKD IFHB:
4EB JLOB @LOOB@Q @E>O>@QBOP, QEB BSBK DOB>QBO QEB CFQKBPP. 7B @>K >@EFBSB QEFP QVMB LC OBPRIQ
FK > KRJ?BO LC AFCCBOBKQ T>VP. &LO BU>JMIB, TB @LRIA P>V:
<?JD;II = (DKC8;H E< 9EHH;9J 9>7H79J;HI) * (DKC8;H E< 9EHH;9J 9>7H79J;HI)
,BQfP P>V TB E>SB QTL JBJ?BOP LC QEB MLMRI>QFLK, LKB TFQE CFSB @LOOB@Q @E>O>@QBOP >KA LKB
TFQE PFU. 4EB KRJ?BO 6 FP > 20% FK@OB>PB LSBO QEB KRJ?BO 5. ,BQfP ILLH >Q QEB CFQKBPP P@LOBP
PNR>OBA.
F64B?2 9.8
F64B?2 9.9
4EB .>QROB LC #LAB (S005)
415
Characters correct Characters correct Fitness Fitness
5 25
6 36
4EB CFQKBPP P@LOBP FK@OB>PB BUMLKBKQF>IIV OBI>QFSB QL QEB KRJ?BO LC @LOOB@Q @E>O>@QBOP. 36 FP
> 44% FK@OB>PB LSBO 25.
(BOBfP >KLQEBO CLOJRI>.
<?JD;II = 2
(DKC8;H E< 9EHH;9J 9>7H79J;HI)
Characters correct Characters correct Fitness Fitness
1 2
2 4
3 8
4 16
(BOB, QEB CFQKBPP P@LOBP FK@OB>PB >Q > C>PQBO O>QB, ALR?IFKD TFQE B>@E >AAFQFLK>I @LOOB@Q
@E>O>@QBO.
7EFIB QEFP O>QEBO PMB@FCF@ AFP@RPPFLK LC BUMLKBKQF>I SP. IFKB>O CFQKBPP CRK@QFLKP FP >K
FJMLOQ>KQ ABQ>FI FK QEB ABPFDK LC > DLLA CFQKBPP CRK@QFLK, ) ALKfQ T>KQ RP QL JFPP QEB JLOB
FJMLOQ>KQ MLFKQ EBOB: 9G=;B MCIF CKB :=HB9GG :IB7H=CB! ) PBOFLRPIV ALR?Q QE>Q >KV MOLGB@Q
VLR RKABOQ>HB FK 0OL@BPPFKD TFQE DBKBQF@ >IDLOFQEJP TFII >@QR>IIV FKSLISB @LRKQFKD QEB
@LOOB@Q KRJ?BO LC @E>O>@QBOP FK > PQOFKD. )K QEB @LKQBUQ LC QEFP ?LLH, FQfP JLOB IFHBIV VLR TFII
?B ILLHFKD QL BSLISB > @OB>QROB QE>Q FP M>OQ LC > MEVPF@P PVPQBJ. 0BOE>MP VLR >OB ILLHFKD QL
LMQFJFWB QEB TBFDEQP LC PQBBOFKD ?BE>SFLOP PL > @OB>QROB @>K ?BPQ BP@>MB > MOBA>QLO LO
>SLFA >K L?PQ>@IB LO J>HB FQ QEOLRDE > J>WB. 9LR E>SB QL >PH VLROPBIC TE>Q VLRfOB ELMFKD
QL BS>IR>QB.
2BTOFQB QEB CFQKBPP CRK@QFLK QL FK@OB>PB BUMLKBKQF>IIV >@@LOAFKD QL QEB KRJ?BO LC
@LOOB@Q @E>O>@QBOP. .LQB QE>Q VLR TFII >IPL E>SB QL KLOJ>IFWB QEB CFQKBPP S>IRBP QL >
O>KDB ?BQTBBK 0 >KA 1 PL QEBV @>K ?B >AABA QL QEB J>QFKD MLLI > OB>PLK>?IB
KRJ?BO LC QFJBP.
Exercise 9.7 Exercise 9.7
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
416
,BQfP @LKPFABO > O>@FKD PFJRI>QFLK FK TEF@E > SBEF@IB FP BSLISFKD > ABPFDK LMQFJFWBA CLO PMBBA.
<?JD;II = JEJ7B DKC8;H E< <H7C;I H;GK?H;: <EH L;>?9B; JE H;79> J7H=;J
(LT >?LRQ > @>KKLK QE>Q FP BSLISFKD QEB LMQFJ>I T>V QL PELLQ > Q>ODBQ?
<?JD;II = 97DDED87BB :?IJ7D9; JE J7H=;J
4EB ABPFDK LC @LJMRQBO-@LKQOLIIBA MI>VBOP FK > D>JB FP >IPL > @LJJLK P@BK>OFL. ,BQfP P>V VLR
>OB MOLDO>JJFKD > PL@@BO D>JB FK TEF@E QEB RPBO FP QEB DL>IFB. 4EB OBPQ LC QEB MI>VBOP >OB
@LKQOLIIBA ?V VLRO MOLDO>J >KA E>SB > PBQ LC M>O>JBQBOP QE>Q ABQBOJFKB ELT QEBV HF@H > ?>II
QLT>OAP QEB DL>I. 7E>Q TLRIA QEB CFQKBPP P@LOB CLO >KV DFSBK MI>VBO ?B?
<?JD;II = JEJ7B =E7BI I9EH;:
4EFP, L?SFLRPIV, FP > PFJMIFPQF@ Q>HB LK QEB D>JB LC PL@@BO, ?RQ FQ FIIRPQO>QBP QEB MLFKQ. 4EB JLOB
DL>IP > MI>VBO P@LOBP, QEB EFDEBO FQP CFQKBPP, >KA QEB JLOB IFHBIV FQP DBKBQF@ FKCLOJ>QFLK TFII
>MMB>O FK QEB KBUQ D>JB. %SBK TFQE > CFQKBPP CRK@QFLK >P PFJMIB >P QEB LKB ABP@OF?BA EBOB,
QEFP P@BK>OFL FP ABJLKPQO>QFKD PLJBQEFKD SBOV MLTBOCRIdQEB >A>MQ>?FIFQV LC > PVPQBJ. )C QEB
MI>VBOP @LKQFKRB QL BSLISB COLJ D>JB QL D>JB QL D>JB, TEBK > KBT <IA5B RPBO BKQBOP QEB
D>JB TFQE > @LJMIBQBIV AFCCBOBKQ PQO>QBDV, QEB PVPQBJ TFII NRF@HIV AFP@LSBO QE>Q QEB CFQKBPP
P@LOBP >OB DLFKD ALTK >KA BSLISB > KBT LMQFJ>I PQO>QBDV. )Q TFII >A>MQ. ($LKfQ TLOOV, QEBOB FP
SBOV IFQQIB A>KDBO FK QEFP OBPRIQFKD FK PBKQFBKQ OL?LQP QE>Q TFII BKPI>SB >II ERJ>KP.)
)K QEB BKA, FC VLR AL KLQ E>SB > CFQKBPP CRK@QFLK QE>Q BCCB@QFSBIV BS>IR>QBP QEB MBOCLOJ>K@B LC
QEB FKAFSFAR>I BIBJBKQP LC VLRO MLMRI>QFLK, VLR TFII KLQ E>SB >KV BSLIRQFLK. !KA QEB CFQKBPP
CRK@QFLK COLJ LKB BU>JMIB TFII IFHBIV KLQ >MMIV QL > QLQ>IIV AFCCBOBKQ MOLGB@Q. 3L QEFP FP QEB M>OQ
TEBOB VLR DBQ QL PEFKB. 9LR E>SB QL ABPFDK > CRK@QFLK, PLJBQFJBP COLJ P@O>Q@E, QE>Q TLOHP CLO
VLRO M>OQF@RI>O MOLGB@Q. !KA TEBOB AL VLR AL QEFP? !II VLR E>SB QL BAFQ >OB QELPB CBT IFKBP LC
@LAB FKPFAB QEB CRK@QFLK QE>Q @LJMRQBP QEB CFQKBPP S>OF>?IB.
PIC> @CNH?MM() U
????????????
????????????
@CNH?MM = ??????????
W
Key #3: Genotype and Phenotype Key #3: Genotype and Phenotype
4EB CFK>I HBV QL ABPFDKFKD VLRO LTK DBKBQF@ >IDLOFQEJ OBI>QBP QL ELT VLR @ELLPB QL BK@LAB
QEB MOLMBOQFBP LC VLRO PVPQBJ. 7E>Q >OB VLR QOVFKD QL BUMOBPP, >KA ELT @>K VLR QO>KPI>QB QE>Q
BUMOBPPFLK FKQL > ?RK@E LC KRJ?BOP? 7E>Q FP QEB DBKLQVMB >KA MEBKLQVMB?
7EBK Q>IHFKD >?LRQ QEB CFQKBPP CRK@QFLK, TB E>MMFIV >PPRJBA TB @LRIA @OB>QB @LJMRQBO-
@LKQOLIIBA HF@HBOP QE>Q B>@E E>A > gPBQ LC M>O>JBQBOP QE>Q ABQBOJFKB ELT QEBV HF@H > ?>II
QLT>OAP QEB DL>I.h (LTBSBO, TE>Q QELPB M>O>JBQBOP >OB >KA ELT VLR @ELLPB QL BK@LAB QEBJ
FP RM QL VLR.
4EB .>QROB LC #LAB (S005)
417
7B PQ>OQBA TFQE QEB 3E>HBPMB>OB BU>JMIB ?B@>RPB LC ELT B>PV FQ T>P QL ABPFDK ?LQE QEB
DBKLQVMB (>K >OO>V LC @E>O>@QBOP) >KA FQP BUMOBPPFLK, QEB MEBKLQVMB (QEB PQOFKD AO>TK FK QEB
TFKALT).
4EB DLLA KBTP FPd>KA TB EFKQBA >Q QEFP >Q QEB PQ>OQ LC QEFP @E>MQBOdVLRfSB OB>IIV ?BBK
ALFKD QEFP >II >ILKD. !KVQFJB VLR TOFQB > @I>PP FK 0OL@BPPFKD, VLR J>HB > TELIB ?RK@E LC
S>OF>?IBP.
!II TB KBBA QL AL QL BSLISB QELPB M>O>JBQBOP FP QL QROK QEBJ FKQL >K >OO>V, PL QE>Q QEB >OO>V
@>K ?B RPBA TFQE >II LC QEB CRK@QFLKPd=LIMMIP?L(), GON;N?(), BQ@.dCLRKA FK QEB ",A @I>PP.
/KB @LJJLK PLIRQFLK FP QL RPB >K >OO>V LC CIL>QFKD MLFKQ KRJ?BOP ?BQTBBK 0 >KA 1.
.LQF@B ELT TBfSB KLT MRQ QEB DBKBQF@ A>Q> (DBKLQVMB) >KA FQP BUMOBPPFLK (MEBKLQVMB) FKQL
QTL PBM>O>QB @I>PPBP. 4EB ",A @I>PP FP QEB DBKLQVMB >KA QEB 6BEF@IB @I>PP RPBP > ",A L?GB@Q
QL AOFSB FQP ?BE>SFLOP >KA BUMOBPP QE>Q A>Q> SFPR>IIVdFQ FP QEB MEBKLQVMB. 4EB QTL @>K ?B
IFKHBA ?V @OB>QFKD > ",A FKPQ>K@B FKPFAB QEB SBEF@IB @I>PP FQPBIC.
=F;MM 3?BC=F? U
@FI;N G;RMJ??>;
@FI;N G;R@IL=?;
@FI;N MCT?;
@FI;N M?J;L;NCIH4?CABN;
// ?N=.
=F;MM ",A U
An array of floats @FI;N89 A?H?M;
",A(CHN HOG) U
A?H?M = H?Q @FI;N8HOG9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Always pick a number between 0 and 1. A?H?M8C9 = @FI;N(1);
W
W
=F;MM 3?BC=F? U
A DNA object embedded into the Vehicle
class
",A >H;;
@FI;N G;RMJ??>;
@FI;N G;R@IL=?;
@FI;N MCT?;
@FI;N M?J;L;NCIH4?CABN;
Etc.
3?BC=F?() U
",A = H?Q ",A(4);
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
418
/C @LROPB, VLR JLPQ IFHBIV ALKfQ T>KQ >II VLRO S>OF>?IBP QL E>SB > O>KDB ?BQTBBK 0 >KA 1. "RQ
O>QEBO QE>K QOV QL OBJBJ?BO ELT QL >AGRPQ QELPB O>KDBP FK QEB ",A @I>PP FQPBIC, FQfP B>PFBO QL
MRII QEB DBKBQF@ FKCLOJ>QFLK COLJ QEB $.! L?GB@Q >KA RPB 0OL@BPPFKDfP G;J() CRK@QFLK QL
@E>KDB QEB O>KDB. &LO BU>JMIB, FC VLR T>KQ > PFWB S>OF>?IB ?BQTBBK 10 >KA 72, VLR TLRIA P>V:
)K LQEBO @>PBP, VLR TFII T>KQ QL ABPFDK > DBKLQVMB QE>Q FP >K >OO>V LC L?GB@QP. #LKPFABO QEB
ABPFDK LC > OL@HBQ TFQE > PBOFBP LC gQEORPQBOh BKDFKBP. 9LR @LRIA ABP@OF?B B>@E QEORPQBO TFQE >
.3?=NIL QE>Q LRQIFKBP FQP AFOB@QFLK >KA OBI>QFSB PQOBKDQE.
4EB MEBKLQVMB TLRIA ?B > /I=E?N @I>PP QE>Q M>OQF@FM>QBP FK > MEVPF@P PVPQBJ.
7E>QfP DOB>Q >?LRQ QEFP QB@EKFNRB LC AFSFAFKD QEB DBKLQVMB >KA MEBKLQVMB FKQL PBM>O>QB
@I>PPBP (",A >KA /I=E?N CLO BU>JMIB) FP QE>Q TEBK FQ @LJBP QFJB QL ?RFIA >II LC QEB @LAB, VLRfII
KLQF@B QE>Q QEB $.! @I>PP TB ABSBILMBA B>OIFBO OBJ>FKP FKQ>@Q. 4EB LKIV QEFKD QE>Q @E>KDBP FP
QEB >OO>VfP A>Q> QVMB (@FI;N, .3?=NIL, BQ@.) >KA QEB BUMOBPPFLK LC QE>Q A>Q> FK QEB MEBKLQVMB
@I>PP.
)K QEB KBUQ PB@QFLK, TBfII CLIILT QEFP FAB> > ?FQ CROQEBO >KA T>IH QEOLRDE QEB KB@BPP>OV PQBMP CLO
>K BU>JMIB QE>Q FKSLISBP JLSFKD ?LAFBP >KA >K >OO>V LC .3?=NILP >P $.!.
Using the genes to set variables G;RMJ??> = >H;.A?H?M809;
G;R@IL=? = >H;.A?H?M819;
MCT? = >H;.A?H?M829;
M?J;L;NCIH4?CABN = >H;.A?H?M839;
Etc. W
MCT? = G;J(>H;.A?H?M829,0,1,10,72);
=F;MM ",A U
The genotype is an array of PVectors. .3?=NIL89 A?H?M;
",A(CHN HOG) U
A?H?M = H?Q @FI;N8HOG9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A PVector pointing in a random direction A?H?M8C9 = .3?=NIL.L;H>IG2"();
And scaled randomly A?H?M8C9.GOFN(L;H>IG(10));
W
W
=F;MM /I=E?N U
",A >H;;
// ?N=.
4EB .>QROB LC #LAB (S005)
419
9.10 Evolving Forces: Smart Rockets 9.10 Evolving Forces: Smart Rockets
7B MF@HBA QEB OL@HBQ FAB> CLO > PMB@FCF@ OB>PLK. )K 2009, *BO 4ELOM (EQQM://?IMOKQ.@LJ)
OBIB>PBA > DBKBQF@ >IDLOFQEJP BU>JMIB LK EFP ?ILD BKQFQIBA g3J>OQ 2L@HBQP.h *BO MLFKQP LRQ
QE>Q .!3! RPBP BSLIRQFLK>OV @LJMRQFKD QB@EKFNRBP QL PLISB >II PLOQP LC MOL?IBJP, COLJ
P>QBIIFQB >KQBKK> ABPFDK QL OL@HBQ CFOFKD M>QQBOKP. 4EFP FKPMFOBA EFJ QL @OB>QB > &I>PE
ABJLKPQO>QFLK LC BSLISFKD OL@HBQP. (BOB FP > ABP@OFMQFLK LC QEB P@BK>OFL:
! MLMRI>QFLK LC OL@HBQP I>RK@EBP COLJ QEB ?LQQLJ LC QEB P@OBBK TFQE QEB DL>I LC EFQQFKD >
Q>ODBQ >Q QEB QLM LC QEB P@OBBK (TFQE L?PQ>@IBP ?IL@HFKD > PQO>FDEQ IFKB M>QE).
%>@E OL@HBQ FP BNRFMMBA TFQE CFSB QEORPQBOP
LC S>OF>?IB PQOBKDQE >KA AFOB@QFLK. 4EB
QEORPQBOP ALKfQ CFOB >II >Q LK@B >KA
@LKQFKRLRPIV; O>QEBO, QEBV CFOB LKB >Q > QFJB
FK > @RPQLJ PBNRBK@B.
)K QEFP PB@QFLK, TBfOB DLFKD QL BSLISB LRO
LTK PFJMIFCFBA 3J>OQ 2L@HBQP, FKPMFOBA ?V
*BO 4ELOMfP. 7EBK TB DBQ QL QEB BKA LC QEB
PB@QFLK, TBfII IB>SB FJMIBJBKQFKD PLJB LC
*BOfP >AAFQFLK>I >AS>K@BA CB>QROBP >P >K
BUBO@FPB.
/RO OL@HBQP TFII E>SB LKIV LKB QEORPQBO, >KA QEFP QEORPQBO TFII ?B >?IB QL CFOB FK >KV AFOB@QFLK
TFQE >KV PQOBKDQE CLO BSBOV CO>JB LC >KFJ>QFLK. 4EFP FPKfQ M>OQF@RI>OIV OB>IFPQF@, ?RQ FQ TFII
J>HB ?RFIAFKD LRQ QEB CO>JBTLOH > IFQQIB B>PFBO. (7B @>K >IT>VP J>HB QEB OL@HBQ >KA FQP
QEORPQBOP JLOB >AS>K@BA >KA OB>IFPQF@ I>QBO.)
,BQfP PQ>OQ ?V Q>HFKD LRO ?>PF@ +IP?L @I>PP COLJ #E>MQBO 2 BU>JMIBP >KA OBK>JFKD FQ /I=E?N.
F64B?2 9.10
F64B?2 9.11
=F;MM /I=E?N U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
420
5PFKD QEB >?LSB CO>JBTLOH, TB @>K FJMIBJBKQ LRO PJ>OQ OL@HBQ ?V P>VFKD QE>Q CLO BSBOV CO>JB
LC >KFJ>QFLK, TB @>II ;JJFS$IL=?() TFQE > KBT CLO@B. 4EB gQEORPQBOh >MMIFBP > PFKDIB CLO@B QL
QEB OL@HBQ B>@E QFJB QEOLRDE >L;Q().
#LKPFABOFKD QEFP BU>JMIB, IBQfP DL QEOLRDE QEB QEOBB HBVP QL MOLDO>JJFKD LRO LTK @RPQLJ
DBKBQF@ >IDLOFQEJ BU>JMIB >P LRQIFKBA FK QEB MOBSFLRP PB@QFLK.
Key #1: Population size and mutation rate Key #1: Population size and mutation rate
7B @>K >@QR>IIV ELIA LCC LK QEFP CFOPQ HBV CLO QEB JLJBKQ. /RO PQO>QBDV TFII ?B QL MF@H PLJB
OB>PLK>?IB KRJ?BOP (> MLMRI>QFLK LC 100 OL@HBQP, JRQ>QFLK O>QB LC 1%) >KA ?RFIA LRQ QEB
PVPQBJ, MI>VFKD TFQE QEBPB KRJ?BOP LK@B TB E>SB LRO PHBQ@E RM >KA ORKKFKD.
Key #2: The fitness function Key #2: The fitness function
7B PQ>QBA QEB DL>I LC > OL@HBQ OB>@EFKD > Q>ODBQ. )K LQEBO TLOAP, QEB @ILPBO > OL@HBQ DBQP QL
QEB Q>ODBQ, QEB EFDEBO QEB CFQKBPP. &FQKBPP FP FKSBOPBIV MOLMLOQFLK>I QL AFPQ>K@B: QEB PJ>IIBO QEB
AFPQ>K@B, QEB DOB>QBO QEB CFQKBPP; QEB DOB>QBO QEB AFPQ>K@B, QEB PJ>IIBO QEB CFQKBPP.
,BQfP >PPRJB TB E>SB > .3?=NIL Q>ODBQ.
4EFP FP MBOE>MP QEB PFJMIBPQ CFQKBPP CRK@QFLK TB @LRIA TOFQB. "V RPFKD LKB AFSFABA ?V AFPQ>K@B,
I>ODB AFPQ>K@BP ?B@LJB PJ>II KRJ?BOP >KA PJ>II AFPQ>K@BP ?B@LJB I>ODB.
A rocket has three vectors: location,
velocity, acceleration.
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
Accumulating forces into acceleration
(Newtons 2nd law)
PIC> ;JJFS$IL=?(.3?=NIL @) U
;==?F?L;NCIH.;>>(@);
W
Our simple physics model (Euler integration) PIC> OJ>;N?() U
Velocity changes according to acceleration. P?FI=CNS.;>>(;==?F?L;NCIH);
Location changes according to velocity. FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
W
PIC> @CNH?MM() U
How close did we get? @FI;N > = .3?=NIL.>CMN(FI=;NCIH,N;LA?N);
Fitness is inversely proportional to distance. @CNH?MM = 1/>;
W
4EB .>QROB LC #LAB (S005)
421
distance distance 1 / distance 1 / distance
300 1 / 300 = 0.0033
100 1 / 100 = 0.01
5 1 / 5 = 0.2
1 1 / 1 = 1.0
0.1 1 / 0.1 = 10
!KA FC TB T>KQBA QL RPB LRO BUMLKBKQF>I QOF@H COLJ QEB MOBSFLRP PB@QFLK, TB @LRIA RPB LKB
AFSFABA ?V AFPQ>K@B PNR>OBA.
distance distance 1 / distance 1 / distance
(1 / distance) (1 / distance)
22
300 1 / 400 = 0.0025 0.00000625
100 1 / 100 = 0.01 0.0001
5 1 / 5 = 0.2 0.04
1 1 / 1 = 1.0 1.0
0.1 1 / 0.1 = 10 100
4EBOB >OB PBSBO>I >AAFQFLK>I FJMOLSBJBKQP TBfII T>KQ QL J>HB QL QEB CFQKBPP CRK@QFLK, ?RQ
QEFP PFJMIB LKB FP > DLLA PQ>OQ.
+BV #3: 'BKLQVMB >KA 0EBKLQVMB
7B PQ>QBA QE>Q B>@E OL@HBQ E>P > QEORPQBO QE>Q CFOBP FK > S>OF>?IB AFOB@QFLK TFQE > S>OF>?IB
J>DKFQRAB FK B>@E CO>JB. !KA PL TB KBBA > .3?=NIL CLO B>@E CO>JB LC >KFJ>QFLK. /RO
DBKLQVMB, QEB A>Q> OBNRFOBA QL BK@LAB QEB OL@HBQfP ?BE>SFLO, FP QEBOBCLOB >K >OO>V LC
.3?=NILP.
PIC> @CNH?MM() U
@FI;N > = .3?=NIL.>CMN(FI=;NCIH,N;LA?N);
Squaring 1 divided by distance @CNH?MM = JIQ(1/>,2);
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
422
4EB E>MMV KBTP EBOB FP QE>Q TB ALKfQ OB>IIV E>SB QL AL >KVQEFKD BIPB QL QEB ",A @I>PP. !II LC
QEB CRK@QFLK>IFQV TB ABSBILMBA CLO QEB QVMFKD JLKHBV (@OLPPLSBO >KA JRQ>QFLK) >MMIFBP EBOB.
4EB LKB AFCCBOBK@B TB AL E>SB QL @LKPFABO FP ELT TB FKFQF>IFWB QEB >OO>V LC DBKBP. 7FQE QEB
QVMFKD JLKHBV, TB E>A >K >OO>V LC @E>O>@QBOP >KA MF@HBA > O>KALJ @E>O>@QBO CLO B>@E
BIBJBKQ LC QEB >OO>V. (BOB TBfII AL BU>@QIV QEB P>JB QEFKD >KA FKFQF>IFWB > $.! PBNRBK@B >P >K
>OO>V LC O>KALJ .3?=NILP. .LT, VLRO FKPQFK@Q FK @OB>QFKD > O>KALJ .3?=NIL JFDEQ ?B >P
CLIILTP:
4EFP FP MBOCB@QIV CFKB >KA TFII IFHBIV AL QEB
QOF@H. (LTBSBO, FC TB TBOB QL AO>T BSBOV
PFKDIB MLPPF?IB SB@QLO TB JFDEQ MF@H, QEB
OBPRIQ TLRIA CFII > PNR>OB (PBB &FDROB 9.12). )K
QEFP @>PB, FQ MOL?>?IV ALBPKfQ J>QQBO, ?RQ
QEBOB FP > PIFDEQ ?F>P QL AF>DLK>IP EBOB DFSBK
QE>Q > .3?=NIL COLJ QEB @BKQBO LC > PNR>OB
QL > @LOKBO FP ILKDBO QE>K > MROBIV SBOQF@>I LO
ELOFWLKQ>I LKB.
7E>Q TLRIA ?B ?BQQBO EBOB FP QL MF@H >
O>KALJ >KDIB >KA J>HB > .3?=NIL LC IBKDQE
LKB COLJ QE>Q >KDIB, DFSFKD RP > @FO@IB (PBB
&FDROB 9.13). 4EFP @LRIA ?B B>PFIV ALKB TFQE
> NRF@H MLI>O QL #>OQBPF>K @LKSBOPFLK (PBB
M>DB 112), ?RQ > NRF@HBO M>QE QL QEB OBPRIQ FP
GRPQ QL RPB .3?=NIL'P L;H>IG2"().
! .3?=NIL LC IBKDQE LKB FP >@QR>IIV DLFKD QL ?B NRFQB > I>ODB CLO@B. 2BJBJ?BO, CLO@BP >OB
>MMIFBA QL >@@BIBO>QFLK, TEF@E >@@RJRI>QBP FKQL SBIL@FQV QEFOQV QFJBP MBO PB@LKA. 3L, CLO QEFP
BU>JMIB, TB @>K >IPL >AA LKB JLOB S>OF>?IB QL QEB ",A @I>PP: > J>UFJRJ CLO@B QE>Q P@>IBP >II
QEB .3?=NILP. 4EFP TFII @LKQOLI QEB QEORPQBO MLTBO.
=F;MM ",A U
.3?=NIL89 A?H?M;
.3?=NIL P = H?Q .3?=NIL(L;H>IG(-1,1),L;H>IG(-1,1));
F64B?2 9.12
F64B?2 9.13
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Making a PVector from a random angle A?H?M8C9 = .3?=NIL.L;H>IG2"();
W
4EB .>QROB LC #LAB (S005)
423
.LQF@B >IPL QE>Q TB @OB>QBA >K >OO>V LC .3?=NILP TFQE IBKDQE IFCBQFJB. 7B KBBA > .3?=NIL
CLO B>@E CO>JB LC QEB OL@HBQfP IFCB, >KA QEB >?LSB >PPRJBP QEB BUFPQBK@B LC > DIL?>I S>OF>?IB
IFCBQFJB QE>Q PQLOBP QEB QLQ>I KRJ?BO LC CO>JBP FK B>@E DBKBO>QFLKfP IFCB @V@IB.
4EB BUMOBPPFLK LC QEFP >OO>V LC .3?=NILP, QEB MEBKLQVMB, FP > /I=E?N @I>PP JLABIBA LK LRO
?>PF@ .3?=NIL >KA CLO@BP BU>JMIBP COLJ #E>MQBO 2. !II TB KBBA QL AL FP >AA >K FKPQ>K@B LC
> ",A L?GB@Q QL QEB @I>PP. 4EB CFQKBPP S>OF>?IB TFII >IPL IFSB EBOB. /KIV QEB /I=E?N L?GB@Q
HKLTP ELT QL @LJMRQB FQP AFPQ>K@B QL QEB Q>ODBQ, >KA QEBOBCLOB QEB CFQKBPP CRK@QFLK TFII IFSB
EBOB FK QEB MEBKLQVMB >P TBII.
7E>Q >OB TB RPFKD QEB $.! CLO? 7B >OB J>O@EFKD QEOLRDE QEB >OO>V LC .3?=NILP >KA
>MMIVFKD QEBJ LKB >Q > QFJB >P > CLO@B QL QEB OL@HBQ. 4L AL QEFP, TBfII >IPL E>SB QL >AA >K
FKQBDBO QE>Q >@QP >P > @LRKQBO QL T>IH QEOLRDE QEB >OO>V.
=F;MM ",A U
The genetic sequence is an array of
PVectors.
.3?=NIL89 A?H?M;
How strong can the thrusters be? @FI;N G;R@IL=? = 0.1;
",A() U
We need a PVector for every frame of the
rockets life.
A?H?M = H?Q .3?=NIL8FC@?NCG?9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = .3?=NIL.L;H>IG2"();
Scaling the PVectors randomly, but no
stronger than maximum force
A?H?M8C9.GOFN(L;H>IG(0, G;R@IL=?));
W
W
=F;MM /I=E?N U
A Rocket has DNA. ",A >H;;
A Rocket has fitness. @FI;N @CNH?MM;
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
CHN A?H?!IOHN?L = 0;
PIC> LOH() U
Apply a force from the genes array. ;JJFS$IL=?(>H;.A?H?M8A?H?!IOHN?L9);
Go to the next force in the genes array. A?H?!IOHN?L++;
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
424
Update the Rockets physics. OJ>;N?();
W
9.11 Smart Rockets: Putting It All Together 9.11 Smart Rockets: Putting It All Together
7B KLT E>SB LRO ",A @I>PP (DBKLQVMB) >KA LRO /I=E?N @I>PP (MEBKLQVMB). 4EB I>PQ MFB@B LC
QEB MRWWIB FP > .IJOF;NCIH @I>PP, TEF@E J>K>DBP >K >OO>V LC OL@HBQP >KA E>P QEB CRK@QFLK>IFQV
CLO PBIB@QFLK >KA OBMOLAR@QFLK. !D>FK, QEB E>MMV KBTP EBOB FP QE>Q TB ?>OBIV E>SB QL @E>KDB
>KVQEFKD COLJ QEB 3E>HBPMB>OB JLKHBV BU>JMIB. 4EB MOL@BPP CLO ?RFIAFKD > J>QFKD MLLI >KA
DBKBO>QFKD > KBT >OO>V LC @EFIA OL@HBQP FP BU>@QIV QEB P>JB >P TE>Q TB AFA TFQE LRO MLMRI>QFLK
LC PQOFKDP.
4EBOB FP LKB C>FOIV PFDKFCF@>KQ @E>KDB, ELTBSBO. 7FQE QVMFKD JLKHBVP, > O>KALJ MEO>PB T>P
BS>IR>QBA >P PLLK >P FQ T>P @OB>QBA. 4EB PQOFKD LC @E>O>@QBOP E>A KL IFCBPM>K; FQ BUFPQBA MROBIV
CLO QEB MROMLPB LC @>I@RI>QFKD FQP CFQKBPP >KA QEBK TB JLSBA LK. 4EB OL@HBQP, ELTBSBO, KBBA
QL IFSB CLO > MBOFLA LC QFJB ?BCLOB QEBV @>K ?B BS>IR>QBA; QEBV KBBA QL ?B DFSBK > @E>K@B QL
J>HB QEBFO >QQBJMQ >Q OB>@EFKD QEB Q>ODBQ. 4EBOBCLOB, TB KBBA QL >AA LKB JLOB CRK@QFLK QL QEB
.IJOF;NCIH @I>PP QE>Q ORKP QEB MEVPF@P PFJRI>QFLK FQPBIC. 4EFP FP FABKQF@>I QL TE>Q TB AFA FK QEB
LOH() CRK@QFLK LC > M>OQF@IB PVPQBJdRMA>QB >II QEB M>OQF@IB IL@>QFLKP >KA AO>T QEBJ.
&FK>IIV, TBfOB OB>AV CLO M?NOJ() >KA >L;Q(). (BOB FK QEB J>FK Q>?, LRO MOFJ>OV OBPMLKPF?FIFQV FP
QL FJMIBJBKQ QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ FK QEB >MMOLMOF>QB LOABO ?V @>IIFKD QEB
CRK@QFLKP FK QEB .IJOF;NCIH @I>PP.
=F;MM .IJOF;NCIH U
Population has variables to keep track of
mutation rate, current population array,
mating pool, and number of generations.
@FI;N GON;NCIH/;N?;
/I=E?N89 JIJOF;NCIH;
ALL;S*CMN</I=E?N> G;NCHA.IIF;
CHN A?H?L;NCIHM;
These functions havent changed, so no
need to go through the code again.
PIC> @CNH?MM() UW
PIC> M?F?=NCIH() UW
PIC> L?JLI>O=NCIH() UW
PIC> FCP? () U
@IL (CHN C = 0; C < JIJOF;NCIH.F?HANB; C++) U
The run function takes care of the forces,
updating the rockets location, and
displaying it.
JIJOF;NCIH8C9.LOH();
W
W
4EB .>QROB LC #LAB (S005)
425
(LTBSBO, RKIFHB QEB 3E>HBPMB>OB BU>JMIB, TB ALKfQ T>KQ QL AL QEFP BSBOV CO>JB. 2>QEBO,
LRO PQBMP TLOH >P CLIILTP:
1. #OB>QB > MLMRI>QFLK LC OL@HBQP
2. ,BQ QEB OL@HBQP IFSB CLO . CO>JBP
3. %SLISB QEB KBUQ DBKBO>QFLK
3BIB@QFLK
2BMOLAR@QFLK
4. 2BQROK QL 3QBM #2
Example 9.2: Simple Smart Rockets
JIJOF;NCIH.@CNH?MM();
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
How many frames does a generation live
for?
CHN FC@?NCG?;
What frame are we on? CHN FC@?!IOHN?L;
The population .IJOF;NCIH JIJOF;NCIH;
PIC> M?NOJ() U
MCT?(640, 480);
FC@?NCG? = 500;
FC@?!IOHN?L = 0;
@FI;N GON;NCIH/;N? = 0.01;
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
426
4EB >?LSB BU>JMIB TLOHP, ?RQ FQ FPKfQ M>OQF@RI>OIV FKQBOBPQFKD. !CQBO >II, QEB OL@HBQP PFJMIV
BSLISB QL E>SFKD $.! TFQE > ?RK@E LC SB@QLOP QE>Q MLFKQ PQO>FDEQ RMT>OAP. )K QEB KBUQ
BU>JMIB, TBfOB DLFKD QL Q>IH QEOLRDE QTL PRDDBPQBA FJMOLSBJBKQP CLO QEB BU>JMIB >KA
MOLSFAB @LAB PKFMMBQP QE>Q FJMIBJBKQ QEBPB FJMOLSBJBKQP.
)JMOLSBJBKQ #1: /?PQ>@IBP
!AAFKD L?PQ>@IBP QE>Q QEB OL@HBQP JRPQ >SLFA TFII J>HB QEB PVPQBJ JLOB @LJMIBU >KA
ABJLKPQO>QB QEB MLTBO LC QEB BSLIRQFLK>OV >IDLOFQEJ JLOB BCCB@QFSBIV. 7B @>K J>HB
OB@Q>KDRI>O, PQ>QFLK>OV L?PQ>@IBP C>FOIV B>PFIV ?V @OB>QFKD > @I>PP QE>Q PQLOBP > IL@>QFLK >KA
AFJBKPFLKP.
Step 1: Create the population. Here is where
we could play with the mutation rate and
population size.
JIJOF;NCIH = H?Q .IJOF;NCIH(GON;NCIH/;N?, 50);
W
PIC> >L;Q() U
<;=EALIOH>(255);
The revised genetic algorithm C@ (FC@?!IOHN?L < FC@?NCG?) U
Step 2: The rockets live their life until
lifeCounter reaches lifetime.
JIJOF;NCIH.FCP?();
FC@?!IOHN?L++;
W ?FM? U
When lifetime is reached, reset lifeCounter
and evolve the next generation (Steps 3 and
4, selection and reproduction).
FC@?!IOHN?L = 0;
JIJOF;NCIH.@CNH?MM();
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
W
W
4EB .>QROB LC #LAB (S005)
427
Example 9.3: Smart Rockets
7B @>K >IPL TOFQB > =IHN;CHM() CRK@QFLK QE>Q TFII L?NOLH NLO? LO L?NOLH @;FM? QL
ABQBOJFKB FC > OL@HBQ E>P EFQ QEB L?PQ>@IB.
!PPRJFKD TB J>HB >K ALL;S*CMN LC L?PQ>@IBP, TB @>K QEBK E>SB B>@E OL@HBQ @EB@H QL PBB
FC FQ E>P @LIIFABA TFQE >K L?PQ>@IB >KA PBQ > <IIF?;H CI>D QL ?B QORB FC FQ ALBP, >AAFKD >
CRK@QFLK QL QEB OL@HBQ @I>PP.
)C QEB OL@HBQ EFQP >K L?PQ>@IB, TB @ELLPB QL PQLM FQ COLJ RMA>QFKD FQP IL@>QFLK.
!KA TB >IPL E>SB >K LMMLOQRKFQV QL >AGRPQ QEB OL@HBQfP CFQKBPP. 7B @LKPFABO FQ QL ?B MOBQQV
QBOOF?IB FC QEB OL@HBQ EFQP >K L?PQ>@IB, >KA PL FQP CFQKBPP PELRIA ?B DOB>QIV OBAR@BA.
=F;MM -<MN;=F? U
An obstacle is a location (top left corner of
rectangle) with a width and height.
.3?=NIL FI=;NCIH;
@FI;N Q,B;
<IIF?;H =IHN;CHM(.3?=NIL P) U
C@ (P.R > FI=;NCIH.R && P.R < FI=;NCIH.R + Q && P.S > FI=;NCIH.S && P.S <
FI=;NCIH.S + B) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
This new function lives in the rocket class
and checks if a rocket has hit an obstacle.
PIC> I<MN;=F?M() U
@IL (-<MN;=F? I<M : I<MN;=F?M) U
C@ (I<M.=IHN;CHM(FI=;NCIH)) U
MNIJJ?> = NLO?;
W
W
W
PIC> LOH() U
Only run the rocket if it doesnt hit an
obstacle.
?< (!IJEFF;:) Q
;JJFS$IL=?(>H;.A?H?M8A?H?!IOHN?L9);
A?H?!IOHN?L = (A?H?!IOHN?L + 1) % >H;.A?H?M.F?HANB;
OJ>;N?();
E8IJ79B;I();
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
428
)JMOLSBJBKQ #2: %SLISB OB>@EFKD QEB Q>ODBQ C>PQBO
)C VLR ILLH @ILPBIV >Q LRO CFOPQ 3J>OQ 2L@HBQP BU>JMIB, VLRfII KLQF@B QE>Q QEB OL@HBQP >OB KLQ
OBT>OABA CLO DBQQFKD QL QEB Q>ODBQ C>PQBO. 4EB LKIV S>OF>?IB FK QEBFO CFQKBPP @>I@RI>QFLK FP QEB
AFPQ>K@B QL QEB Q>ODBQ >Q QEB BKA LC QEB DBKBO>QFLKfP IFCB. )K C>@Q, FK QEB BSBKQ QE>Q QEB OL@HBQP
DBQ SBOV @ILPB QL QEB Q>ODBQ ?RQ LSBOPELLQ FQ >KA CIV M>PQ, QEBV J>V >@QR>IIV ?B MBK>IFWBA CLO
DBQQFKD QL QEB Q>ODBQ C>PQBO. 3ILT >KA PQB>AV TFKP QEB O>@B FK QEFP @>PB.
7B @LRIA FJMOLSB QEB >IDLOFQEJ QL LMQFJFWB CLO PMBBA > KRJ?BO LC T>VP. &FOPQ, FKPQB>A LC
RPFKD QEB AFPQ>K@B QL QEB Q>ODBQ >Q QEB BKA LC QEB DBKBO>QFLK, TB @LRIA RPB QEB AFPQ>K@B QE>Q FP
QEB @ILPBPQ QL QEB Q>ODBQ >Q >KV MLFKQ AROFKD QEB OL@HBQfP IFCB. 7B TLRIA @>II QEFP QEB OL@HBQfP
gOB@LOAh AFPQ>K@B. (!II LC QEB @LAB PKFMMBQP FK QEFP PB@QFLK IFSB FKPFAB QEB /I=E?N @I>PP.)
)K >AAFQFLK, > OL@HBQ PELRIA ?B OBT>OABA >@@LOAFKD QL ELT NRF@HIV FQ OB>@EBP QEB Q>ODBQ. 4EB
C>PQBO FQ OB>@EBP QEB Q>ODBQ, QEB EFDEBO QEB CFQKBPP. 4EB PILTBO, QEB ILTBO. 4L >@@LJMIFPE QEFP,
TB @>K FK@OBJBKQ > @LRKQBO BSBOV @V@IB LC QEB OL@HBQfP IFCB RKQFI FQ OB>@EBP QEB Q>ODBQ. !Q QEB
BKA LC FQP IFCB, QEB @LRKQBO TFII BNR>I QEB >JLRKQ LC QFJB QEB OL@HBQ QLLH QL OB>@E QE>Q Q>ODBQ.
&FQKBPP FP >IPL FKSBOPBIV MOLMLOQFLK>I QL @CHCMB1CG?, >KA PL TB @>K FJMOLSB LRO CFQKBPP
CRK@QFLK >P CLIILTP:
PIC> @CNH?MM() U
@FI;N > = >CMN(FI=;NCIH.R, FI=;NCIH.S, N;LA?N.FI=;NCIH.R, N;LA?N.FI=;NCIH.S);
@CNH?MM = JIQ(1/>, 2);
?< (IJEFF;:) <?JD;II *= 0.1;
W
PIC> =B?=E1;LA?N() U
@FI;N > = >CMN(FI=;NCIH.R, FI=;NCIH.S, N;LA?N.FI=;NCIH.R, N;LA?N.FI=;NCIH.S);
Every frame, we check its distance and see
if its closer than the record distance. If it is,
we have a new record.
C@ (> < L?=IL>"CMN) L?=IL>"CMN = >;
If the object reaches the target, set a
boolean flag to true.
C@ (N;LA?N.=IHN;CHM(FI=;NCIH)) U
BCN1;LA?N = NLO?;
W ?FM? C@ (!BCN1;LA?N) U
As long as we havent yet reached the
target, keep incrementing the counter.
@CHCMB1CG?++;
W
W
PIC> @CNH?MM() U
Finish time and record distance! @CNH?MM = (1/(@CHCMB1CG?*L?=IL>"CMN));
4EB .>QROB LC #LAB (S005)
429
4EBPB FJMOLSBJBKQP >OB ?LQE FK@LOMLO>QBA FKQL QEB @LAB CLO %U>JMIB 9.3: 3J>OQ 2L@HBQP.
/KB LC QEB JLOB C>JLRP FJMIBJBKQ>QFLKP LC DBKBQF@ >IDLOFQEJP FK @LJMRQBO DO>MEF@P FP +>OI
3FJPfP g%SLISBA 6FOQR>I #OB>QROBP.h )K 3FJPfP TLOH, > MLMRI>QFLK LC AFDFQ>I @OB>QROBP (FK >
PFJRI>QBA MEVPF@P BKSFOLKJBKQ) FP BS>IR>QBA CLO QEB @OB>QROBP' >?FIFQV QL MBOCLOJ Q>PHP, PR@E
>P PTFJJFKD, ORKKFKD, GRJMFKD, CLIILTFKD, >KA @LJMBQFKD CLO > DOBBK @R?B.
Make it exponential. @CNH?MM = JIQ(@CNH?MM, 2);
Fitness goes way down if you hit an
obstacle.
C@ (MNIJJ?>) @CNH?MM *= 0.1;
You are rewarded for reaching the target. C@ (BCN1;LA?N) @CNH?MM *= 2;
W
#OB>QB > JLOB @LJMIBU L?PQ>@IB @LROPB. !P VLR J>HB FQ JLOB AFCCF@RIQ CLO QEB OL@HBQP
QL OB>@E QEB Q>ODBQ, AL VLR KBBA QL FJMOLSB LQEBO >PMB@QP LC QEB '!dCLO BU>JMIB,
QEB CFQKBPP CRK@QFLK?
Exercise 9.8 Exercise 9.8
)JMIBJBKQ QEB OL@HBQ CFOFKD M>QQBOK LC *BO 4ELOMfP 3J>OQ 2L@HBQP. %>@E OL@HBQ LKIV
DBQP CFSB QEORPQBOP (LC >KV AFOB@QFLK >KA PQOBKDQE) QE>Q CLIILT > CFOFKD PBNRBK@B (LC
>O?FQO>OV IBKDQE). *BOfP PFJRI>QFLK (EQQM://TTT.?IMOKQ.@LJ/PJ>OQOL@HBQP/) >IPL DFSBP QEB
OL@HBQP > CFKFQB >JLRKQ LC CRBI.
Exercise 9.9 Exercise 9.9
6FPR>IFWB QEB OL@HBQP AFCCBOBKQIV. #>K VLR AO>T > IFKB CLO QEB PELOQBPQ M>QE QL QEB
Q>ODBQ? #>K VLR >AA M>OQF@IB PVPQBJP QE>Q >@Q >P PJLHB FK QEB AFOB@QFLK LC QEB OL@HBQ
QEORPQBOP?
Exercise 9.10 Exercise 9.10
!KLQEBO T>V QL >@EFBSB > PFJFI>O OBPRIQ FP QL BSLISB > CILT CFBIA. #>K VLR J>HB QEB
DBKLQVMB LC > OL@HBQ > CILT CFBIA LC .3?=NILP?
Exercise 9.11 Exercise 9.11
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
430
/KB LC QEB FKKLS>QFLKP FK 3FJPfP TLOH FP > KLAB-?>PBA DBKLQVMB. )K LQEBO TLOAP, QEB
@OB>QROBfP $.! FP KLQ > IFKB>O IFPQ LC .3?=NILP LO KRJ?BOP, ?RQ > J>M LC KLABP. (&LO >K
BU>JMIB LC QEFP, Q>HB > ILLH >Q %UBO@FPB 5.15 (PBB M>DB 256), QLUF@IF?P' &LO@B $FOB@QBA 'O>ME.)
4EB MEBKLQVMB FP QEB @OB>QROBfP ABPFDK FQPBIC, > KBQTLOH LC IFJ?P @LKKB@QBA TFQE JRP@IBP.
5PFKD QLUF@IF?P LO "LU2$ >P QEB MEVPF@P
JLABI, @>K VLR @OB>QB > PFJMIFCFBA 2$
SBOPFLK LC 3FJPfP @OB>QROBP? &LO >
IBKDQEFBO ABP@OFMQFLK LC 3FJPfP
QB@EKFNRBP, ) PRDDBPQ VLR T>Q@E QEB
SFABL >KA OB>A 3FJPfP M>MBO 6FOQR>I
#OB>QROBP (EQQM://TTT.H>OIPFJP.@LJ/
BSLISBA-SFOQR>I-@OB>QROBP.EQJI). )K
>AAFQFLK, VLR @>K CFKA > PFJFI>O BU>JMIB
QE>Q RPBP "LU2$ QL BSLISB > g@>Oh:
"LU#>O2$ (EQQM://?LU@>O2A.@LJ/).
Exercise 9.12 Exercise 9.12
9.12 Interactive Selection 9.12 Interactive Selection
)K >AAFQFLK QL %SLISBA 6FOQR>I #OB>QROBP, 3FJP FP >IPL TBII HKLTK CLO EFP JRPBRJ FKPQ>II>QFLK
#5@5D5;CG. /OFDFK>IIV FKPQ>IIBA FK QEB )KQBO@LJJRKF@>QFLK #BKQBO FK 4LHVL FK 1997, QEB
FKPQ>II>QFLK @LKPFPQP LC QTBISB JLKFQLOP AFPMI>VFKD @LJMRQBO-DBKBO>QBA FJ>DBP. 4EBPB FJ>DBP
BSLISB LSBO QFJB, CLIILTFKD QEB DBKBQF@ >IDLOFQEJ PQBMP LC PBIB@QFLK >KA OBMOLAR@QFLK. 4EB
FKKLS>QFLK EBOB FP KLQ QEB RPB LC QEB DBKBQF@ >IDLOFQEJ FQPBIC, ?RQ O>QEBO QEB PQO>QBDV ?BEFKA
QEB CFQKBPP CRK@QFLK. )K COLKQ LC B>@E JLKFQLO FP > PBKPLO LK QEB CILLO QE>Q @>K ABQB@Q QEB
MOBPBK@B LC > RPBO SFBTFKD QEB P@OBBK. 4EB CFQKBPP LC >K FJ>DB FP QFBA QL QEB IBKDQE LC QFJB
QE>Q SFBTBOP ILLH >Q QEB FJ>DB. 4EFP FP HKLTK >P =BH9F57H=J9 G9@97H=CB, > DBKBQF@ >IDLOFQEJ TFQE
CFQKBPP S>IRBP >PPFDKBA ?V RPBOP.
4EFKH LC >II QEB O>QFKD PVPQBJP VLRfSB BSBO RPBA. #LRIA VLR BSLISB QEB MBOCB@Q JLSFB ?V
P@LOFKD >II CFIJP >@@LOAFKD QL VLRO .BQCIFU O>QFKDP? 4EB MBOCB@Q PFKDBO >@@LOAFKD QL !JBOF@>K
)ALI SLQFKD?
4EB .>QROB LC #LAB (S005)
431
4L FIIRPQO>QB QEFP QB@EKFNRB, TBfOB DLFKD QL
?RFIA > MLMRI>QFLK LC PFJMIB C>@BP. %>@E
C>@B TFII E>SB > PBQ LC MOLMBOQFBP: EB>A
PFWB, EB>A @LILO, BVB IL@>QFLK, BVB PFWB,
JLRQE @LILO, JLRQE IL@>QFLK, JLRQE TFAQE,
>KA JLRQE EBFDEQ.
4EB C>@BfP $.! (DBKLQVMB) FP >K >OO>V LC
CIL>QFKD MLFKQ KRJ?BOP ?BQTBBK 0 >KA 1,
TFQE > PFKDIB S>IRB CLO B>@E MOLMBOQV.
4EB MEBKLQVMB FP > $;=? @I>PP QE>Q FK@IRABP >K FKPQ>K@B LC > ",A L?GB@Q.
7EBK FQ @LJBP QFJB QL AO>T QEB C>@B LK P@OBBK, TB @>K RPB 0OL@BPPFKDfP G;J() CRK@QFLK QL
@LKSBOQ >KV DBKB S>IRB QL QEB >MMOLMOF>QB O>KDB CLO MFUBI AFJBKPFLKP LO @LILO S>IRBP. ()K
QEFP @>PB, TB >OB >IPL RPFKD =IFIL+I>?() QL PBQ QEB 2'" O>KDBP ?BQTBBK 0 >KA 1.)
F64B?2 9.14
=F;MM ",A U
@FI;N89 A?H?M;
We need 20 numbers to draw the face. CHN F?H = 20;
",A() U
A?H?M = H?Q @FI;N8F?H9;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
Each gene is a random float between 0
and 1.
A?H?M8C9 = L;H>IG(0,1);
W
W
=F;MM $;=? U
",A >H;;
@FI;N @CNH?MM;
PIC> >CMJF;S() U
Using map() to convert the genes to a
range for drawing the face.
@FI;N L = G;J(>H;.A?H?M809,0,1,0,70);
=IFIL = = =IFIL(>H;.A?H?M819,>H;.A?H?M829,>H;.A?H?M839);
@FI;N ?S?:S = G;J(>H;.A?H?M849,0,1,0,5);
@FI;N ?S?:R = G;J(>H;.A?H?M859,0,1,0,10);
@FI;N ?S?:MCT? = G;J(>H;.A?H?M859,0,1,0,10);
=IFIL ?S?=IFIL = =IFIL(>H;.A?H?M849,>H;.A?H?M859,>H;.A?H?M869);
=IFIL GIONB!IFIL = =IFIL(>H;.A?H?M879,>H;.A?H?M889,>H;.A?H?M899);
@FI;N GIONB:S = G;J(>H;.A?H?M859,0,1,0,25);
@FI;N GIONB:R = G;J(>H;.A?H?M859,0,1,-25,25);
@FI;N GIONBQ = G;J(>H;.A?H?M859,0,1,0,50);
@FI;N GIONBB = G;J(>H;.A?H?M859,0,1,0,10);
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
432
3L C>O, TBfOB KLQ OB>IIV ALFKD >KVQEFKD KBT. 4EFP FP TE>Q TBfSB ALKB FK BSBOV '! BU>JMIB PL
C>O. 7E>QfP KBT FP QE>Q TB >OB KLQ DLFKD QL TOFQB > @CNH?MM() CRK@QFLK FK TEF@E QEB P@LOB FP
@LJMRQBA ?>PBA LK > J>QE CLOJRI>. )KPQB>A, TB >OB DLFKD QL >PH QEB RPBO QL >PPFDK QEB
CFQKBPP.
.LT, ELT ?BPQ QL >PH > RPBO QL >PPFDK CFQKBPP FP OB>IIV JLOB LC >K FKQBO>@QFLK ABPFDK MOL?IBJ,
>KA FQ FPKfQ OB>IIV TFQEFK QEB P@LMB LC QEFP ?LLH. 3L TBfOB KLQ DLFKD QL I>RK@E FKQL >K BI>?LO>QB
AFP@RPPFLK LC ELT QL MOLDO>J PIFABOP LO ?RFIA VLRO LTK E>OAT>OB AF>IP LO ?RFIA > 7B? >MM CLO
RPBOP QL PR?JFQ LKIFKB P@LOBP. (LT VLR @ELLPB QL >@NRFOB CFQKBPP P@LOBP FP OB>IIV RM QL VLR
>KA QEB M>OQF@RI>O >MMIF@>QFLK VLR >OB ABSBILMFKD.
&LO QEFP PFJMIB ABJLKPQO>QFLK, TBfII FK@OB>PB CFQKBPP TEBKBSBO > RPBO OLIIP QEB JLRPB LSBO >
C>@B. 4EB KBUQ DBKBO>QFLK FP @OB>QBA TEBK QEB RPBO MOBPPBP > ?RQQLK TFQE >K gBSLISB KBUQ
DBKBO>QFLKh I>?BI.
,BQfP ILLH >Q ELT QEB PQBMP LC QEB DBKBQF@ >IDLOFQEJ >OB >MMIFBA FK QEB J>FK Q>?, KLQFKD ELT
CFQKBPP FP >PPFDKBA >@@LOAFKD QL JLRPB FKQBO>@QFLK >KA QEB KBUQ DBKBO>QFLK FP @OB>QBA LK >
?RQQLK MOBPP. 4EB OBPQ LC QEB @LAB CLO @EB@HFKD JLRPB IL@>QFLKP, ?RQQLK FKQBO>@QFLKP, BQ@. @>K
?B CLRKA FK QEB >@@LJM>KVFKD BU>JMIB @LAB.
Example 9.4: Interactive selection
.IJOF;NCIH JIJOF;NCIH;
ONNIH <ONNIH;
PIC> M?NOJ() U
MCT?(780,200);
@FI;N GON;NCIH/;N? = 0.05;
JIJOF;NCIH = H?Q .IJOF;NCIH(GON;NCIH/;N?,10);
<ONNIH = H?Q ONNIH(15,150,160,20, "?PIFP? H?Q A?H?L;NCIH");
W
PIC> >L;Q() U
JIJOF;NCIH.>CMJF;S();
4EB .>QROB LC #LAB (S005)
433
4EFP BU>JMIB, FQ PELRIA ?B KLQBA, FP OB>IIV GRPQ > ABJLKPQO>QFLK LC QEB FAB> LC FKQBO>@QFSB
PBIB@QFLK >KA ALBP KLQ >@EFBSB > M>OQF@RI>OIV JB>KFKDCRI OBPRIQ. &LO LKB, TB AFAKfQ Q>HB
JR@E @>OB FK QEB SFPR>I ABPFDK LC QEB C>@BP; QEBV >OB GRPQ > CBT PFJMIB PE>MBP TFQE PFWBP >KA
@LILOP. 3FJP, CLO BU>JMIB, RPBA JLOB BI>?LO>QB J>QEBJ>QF@>I CRK@QFLKP >P EFP FJ>DBPf
DBKLQVMB. 9LR JFDEQ >IPL @LKPFABO > SB@QLO-?>PBA >MMOL>@E, FK TEF@E > ABPFDKfP DBKLQVMB
FP > PBQ LC MLFKQP >KA/LO M>QEP.
4EB JLOB PFDKFCF@>KQ MOL?IBJ EBOB, ELTBSBO, FP LKB LC QFJB. )K QEB K>QRO>I TLOIA, BSLIRQFLK
L@@ROP LSBO JFIIFLKP LC VB>OP. )K QEB @LJMRQBO PFJRI>QFLK TLOIA LC LRO MOBSFLRP BU>JMIBP,
TB TBOB >?IB QL BSLISB ?BE>SFLOP OBI>QFSBIV NRF@HIV ?B@>RPB TB TBOB MOLAR@FKD KBT
DBKBO>QFLKP >IDLOFQEJF@>IIV. )K QEB 3E>HBPMB>OB JLKHBV BU>JMIB, > KBT DBKBO>QFLK T>P
?LOK FK B>@E CO>JB LC >KFJ>QFLK (>MMOLUFJ>QBIV PFUQV MBO PB@LKA). 3FK@B QEB CFQKBPP S>IRBP
TBOB @LJMRQBA >@@LOAFKD QL > J>QE CLOJRI>, TB @LRIA >IPL E>SB E>A >O?FQO>OFIV I>ODB
MLMRI>QFLKP QE>Q FK@OB>PBA QEB PMBBA LC BSLIRQFLK. )K QEB @>PB LC FKQBO>@QFSB PBIB@QFLK,
ELTBSBO, TB E>SB QL PFQ >KA T>FQ CLO > RPBO QL O>QB B>@E >KA BSBOV JBJ?BO LC QEB
MLMRI>QFLK ?BCLOB TB @>K DBQ QL QEB KBUQ DBKBO>QFLK. ! I>ODB MLMRI>QFLK TLRIA ?B
RKOB>PLK>?IV QBAFLRP QL AB>I TFQEdKLQ QL JBKQFLK, ELT J>KV DBKBO>QFLKP @LRIA VLR PQ>KA
QL PFQ QEOLRDE?
4EBOB >OB @BOQ>FKIV @IBSBO PLIRQFLKP >OLRKA QEFP. 3FJPfP '>I>M>DLP BUEF?FQ @LK@B>IBA QEB
O>QFKD MOL@BPP COLJ QEB RPBOP, >P FQ L@@ROOBA QEOLRDE QEB KLOJ>I ?BE>SFLO LC ILLHFKD >Q
>OQTLOH FK > JRPBRJ PBQQFKD. "RFIAFKD > 7B? >MMIF@>QFLK QE>Q TLRIA >IILT J>KV RPBOP QL O>QB
> MLMRI>QFLK FK > AFPQOF?RQBA C>PEFLK FP >IPL > DLLA PQO>QBDV CLO >@EFBSFKD J>KV O>QFKDP CLO
I>ODB MLMRI>QFLKP NRF@HIV.
)K QEB BKA, QEB HBV QL > PR@@BPPCRI FKQBO>@QFSB PBIB@QFLK PVPQBJ ?LFIP ALTK QL QEB P>JB HBVP
TB MOBSFLRPIV BPQ>?IFPEBA. 7E>Q FP QEB DBKLQVMB >KA MEBKLQVMB? !KA ELT AL VLR @>I@RI>QB
CFQKBPP, TEF@E FK QEFP @>PB TB @>K OBSFPB QL P>V: g7E>Q FP VLRO PQO>QBDV CLO >PPFDKFKD CFQKBPP
>@@LOAFKD QL RPBO FKQBO>@QFLK?h
The mouse location is passed to the
population, which will score each face
according to rollover time.
FEFKB7J?ED.HEBBEL;H(CEKI;2,CEKI;3);
<ONNIH.>CMJF;S();
W
PIC> GIOM?.L?MM?>() U
When a button is pressed, the new
generation is created via selection and
reproduction.
?< (8KJJED.9B?9A;:(CEKI;2,CEKI;3)) Q
JIJOF;NCIH.M?F?=NCIH();
JIJOF;NCIH.L?JLI>O=NCIH();
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
434
"RFIA VLRO LTK FKQBO>@QFSB PBIB@QFLK MOLGB@Q. )K >AAFQFLK QL > SFPR>I ABPFDK, @LKPFABO
BSLISFKD PLRKAPdCLO BU>JMIB, > PELOQ PBNRBK@B LC QLKBP. #>K VLR ABSFPB > PQO>QBDV,
PR@E >P > 7B? >MMIF@>QFLK LO MEVPF@>I PBKPLO PVPQBJ, QL >@NRFOB O>QFKDP COLJ J>KV
RPBOP LSBO QFJB?
Exercise 9.14 Exercise 9.14
9.13 Ecosystem Simulation 9.13 Ecosystem Simulation
9LR J>V E>SB KLQF@BA PLJBQEFKD > ?FQ LAA >?LRQ BSBOV PFKDIB BSLIRQFLK>OV PVPQBJ TBfSB ?RFIQ
PL C>O FK QEFP @E>MQBO. !CQBO >II, FK QEB OB>I TLOIA, > MLMRI>QFLK LC ?>?FBP FPKfQ ?LOK >II >Q QEB
P>JB QFJB. 4ELPB ?>?FBP ALKfQ QEBK DOLT RM >KA >II OBMOLAR@B >Q BU>@QIV QEB P>JB QFJB, QEBK
FKPQ>KQIV AFB QL IB>SB QEB MLMRI>QFLK PFWB MBOCB@QIV PQ>?IB. 4E>Q TLRIA ?B OFAF@RILRP. .LQ QL
JBKQFLK QEB C>@Q QE>Q QEBOB FP @BOQ>FKIV KL LKB ORKKFKD >OLRKA QEB CLOBPQ TFQE > @>I@RI>QLO
@ORK@EFKD KRJ?BOP >KA >PPFDKFKD CFQKBPP S>IRBP QL >II QEB @OB>QROBP.
)K QEB OB>I TLOIA, TB ALKfQ OB>IIV E>SB gPROSFS>I LC QEB CFQQBPQh; TB E>SB gPROSFS>I LC QEB
PROSFSLOP.h 4EFKDP QE>Q E>MMBK QL IFSB ILKDBO, CLO TE>QBSBO OB>PLK, E>SB > DOB>QBO @E>K@B LC
OBMOLAR@FKD. ">?FBP >OB ?LOK, QEBV IFSB CLO > TEFIB, J>V?B QEBV QEBJPBISBP E>SB ?>?FBP,
J>V?B QEBV ALKfQ, >KA QEBK QEBV AFB.
9LR TLKfQ KB@BPP>OFIV CFKA PFJRI>QFLKP LC gOB>I-TLOIAh BSLIRQFLK FK >OQFCF@F>I FKQBIIFDBK@B
QBUQ?LLHP. 'BKBQF@ >IDLOFQEJP >OB DBKBO>IIV RPBA FK QEB JLOB CLOJ>I J>KKBO TB LRQIFKBA FK
QEFP @E>MQBO. (LTBSBO, PFK@B TB >OB OB>AFKD QEFP ?LLH QL ABSBILM PFJRI>QFLKP LC K>QRO>I
PVPQBJP, FQfP TLOQE ILLHFKD >Q PLJB T>VP FK TEF@E TB JFDEQ RPB > DBKBQF@ >IDLOFQEJ QL ?RFIA
PLJBQEFKD QE>Q OBPBJ?IBP > IFSFKD gB@LPVPQBJh, JR@E IFHB QEB LKB TBfSB ABP@OF?BA FK QEB
BUBO@FPBP >Q QEB BKA LC B>@E @E>MQBO.
,BQfP ?BDFK ?V ABSBILMFKD > SBOV PFJMIB P@BK>OFL. 7BfII @OB>QB > @OB>QROB @>IIBA > "?ILLM," >
@FO@IB QE>Q JLSBP >?LRQ QEB P@OBBK >@@LOAFKD QL 0BOIFK KLFPB. 4EB @OB>QROB TFII E>SB > O>AFRP
>KA > J>UFJRJ PMBBA. 4EB ?FDDBO FQ FP, QEB PILTBO FQ JLSBP; QEB PJ>IIBO, QEB C>PQBO.
=F;MM FIIJ U
A location .3?=NIL FI=;NCIH;
Variables for size and speed @FI;N L;
@FI;N G;RMJ??>;
4EB .>QROB LC #LAB (S005)
435
4EB >?LSB FP JFPPFKD > CBT ABQ>FIP (PR@E >P FKFQF>IFWFKD QEB S>OF>?IBP FK QEB @LKPQOR@QLO), ?RQ
VLR DBQ QEB FAB>.
&LO QEFP BU>JMIB, TBfII T>KQ QL PQLOB QEB MLMRI>QFLK LC ?ILLMP FK >K ALL;S*CMN, O>QEBO QE>K
>K >OO>V, >P TB BUMB@Q QEB MLMRI>QFLK QL DOLT >KA PEOFKH >@@LOAFKD QL ELT LCQBK ?ILLMP AFB
LO >OB ?LOK. 7B @>K PQLOB QEFP ALL;S*CMN FK > @I>PP @>IIBA 4ILF>, TEF@E TFII J>K>DB >II QEB
BIBJBKQP LC QEB ?ILLMPf TLOIA.
3L C>O, TE>Q TB E>SB FP GRPQ > OBE>PEFKD LC LRO M>OQF@IB PVPQBJ BU>JMIB COLJ #E>MQBO 5. 7B
E>SB >K BKQFQV (FIIJ) QE>Q JLSBP >OLRKA QEB TFKALT >KA > @I>PP (4ILF>) QE>Q J>K>DBP >
S>OF>?IB NR>KQFQV LC QEBPB BKQFQFBP. 4L QROK QEFP FKQL > PVPQBJ QE>Q BSLISBP, TB KBBA QL >AA
QTL >AAFQFLK>I CB>QROBP QL LRO TLOIA:
i B7::;> /40. B7::;> /40.
i B7::;> ,=0 -:=9. B7::;> ,=0 -:=9.
Some variables for Perlin noise
calculations
@FI;N RI@@, SI@@;
PIC> OJ>;N?() U
@FI;N PR = G;J(HICM?(RI@@),0,1,-G;RMJ??>,G;RMJ??>);
@FI;N PS = G;J(HICM?(SI@@),0,1,-G;RMJ??>,G;RMJ??>);
A little Perlin noise algorithm to calculate a
velocity
.3?=NIL P?FI=CNS = H?Q .3?=NIL(PR,PS);
RI@@ += 0.01;
SI@@ += 0.01;
The bloop moves. FI=;NCIH.;>>(P?FI=CNS);
W
A bloop is a circle. PIC> >CMJF;S() U
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, L, L);
W
W
=F;MM 4ILF> U
A list of bloops ALL;S*CMN<FIIJ> <FIIJM;
4ILF>(CHN HOG) U
<FIIJM = H?Q ALL;S*CMN<FIIJ>();
@IL (CHN C = 0; C < HOG; C++) U
Making an initial population of bloops <FIIJM.;>>(H?Q FIIJ());
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
436
"ILLMP AVFKD FP LRO OBMI>@BJBKQ CLO > CFQKBPP CRK@QFLK, QEB MOL@BPP LC gPBIB@QFLK.h )C > ?ILLM
AFBP, FQ @>KKLQ ?B PBIB@QBA QL ?B > M>OBKQ, ?B@>RPB FQ PFJMIV KL ILKDBO BUFPQP! /KB T>V TB @>K
?RFIA > JB@E>KFPJ QL BKPROB ?ILLM AB>QEP FK LRO TLOIA FP ?V >AAFKD > B?;FNB S>OF>?IB QL QEB
FIIJ @I>PP.
)K B>@E CO>JB LC >KFJ>QFLK, > ?ILLM ILPBP PLJB EB>IQE.
)C EB>IQE AOLMP ?BILT 0, QEB ?ILLM AFBP.
4EFP FP > DLLA CFOPQ PQBM, ?RQ TB E>SBKfQ OB>IIV >@EFBSBA >KVQEFKD. !CQBO >II, FC >II ?ILLMP PQ>OQ
TFQE 100 EB>IQE MLFKQP >KA ILPB 1 MLFKQ MBO CO>JB, QEBK >II ?ILLMP TFII IFSB CLO QEB BU>@Q P>JB
>JLRKQ LC QFJB >KA AFB QLDBQEBO. )C BSBOV PFKDIB ?ILLM IFSBP QEB P>JB >JLRKQ LC QFJB, QEBV >II
E>SB BNR>I @E>K@BP LC OBMOLAR@FKD >KA QEBOBCLOB KLQEFKD TFII BSLISB.
4EBOB >OB J>KV T>VP TB @LRIA >@EFBSB S>OF>?IB IFCBPM>KP TFQE > JLOB PLMEFPQF@>QBA TLOIA. &LO
BU>JMIB, TB @LRIA FKQOLAR@B MOBA>QLOP QE>Q B>Q ?ILLMP. 0BOE>MP QEB C>PQBO ?ILLMP TLRIA ?B
>?IB QL BP@>MB ?BFKD B>QBK JLOB B>PFIV, >KA QEBOBCLOB LRO TLOIA TLRIA BSLISB QL E>SB C>PQBO
>KA C>PQBO ?ILLMP. !KLQEBO LMQFLK TLRIA ?B QL FKQOLAR@B CLLA. 7EBK > ?ILLM B>QP CLLA, FQ
FK@OB>PBP FQP EB>IQE MLFKQP, >KA QEBOBCLOB BUQBKAP FQP IFCB.
,BQfP >PPRJB TB E>SB >K ALL;S*CMN LC .3?=NIL IL@>QFLKP CLO CLLA, K>JBA gCLLA.h 7B @LRIA
QBPQ B>@E ?ILLMfP MOLUFJFQV QL B>@E CLLA IL@>QFLK. )C QEB ?ILLM FP @ILPB BKLRDE, FQ B>QP QEB CLLA
(TEF@E FP QEBK OBJLSBA COLJ QEB TLOIA) >KA FK@OB>PBP FQP EB>IQE.
=F;MM FIIJ U
A bloop is born with 100 health points. @FI;N B?;FNB = 100;
PIC> OJ>;N?() U
All that other stuff for movement
Death is always looming! B?;FNB -= 1;
W
We add a function to the Bloop class to test
if the bloop is alive or dead.
<IIF?;H >?;>() U
C@ (B?;FNB < 0.0) U
L?NOLH NLO?;
W ?FM? U
L?NOLH @;FM?;
W
W
PIC> ?;N() U
@IL (CHN C = @II>.MCT?()-1; C >= 0; C--) U
.3?=NIL @II>*I=;NCIH = @II>.A?N(C);
@FI;N > = .3?=NIL.>CMN(FI=;NCIH, @II>*I=;NCIH);
4EB .>QROB LC #LAB (S005)
437
.LT TB E>SB > P@BK>OFL FK TEF@E ?ILLMP QE>Q B>Q JLOB CLLA IFSB ILKDBO >KA E>SB > DOB>QBO
IFHBIFELLA LC OBMOLAR@FKD. 4EBOBCLOB, TB BUMB@Q QE>Q LRO PVPQBJ TLRIA BSLISB ?ILLMP TFQE
>K LMQFJ>I >?FIFQV QL CFKA >KA B>Q CLLA.
.LT QE>Q TB E>SB ?RFIQ LRO TLOIA, FQfP QFJB QL >AA QEB @LJMLKBKQP OBNRFOBA CLO BSLIRQFLK.
&FOPQ TB PELRIA BPQ>?IFPE LRO DBKLQVMB >KA MEBKLQVMB.
Is the Bloop close to the food? C@ (> < L/2) U
If so, it gets 100 more health points. B?;FNB += 100;
The food is no longer available for other
Bloops.
@II>.L?GIP?(C);
W
W
W
Genotype and Phenotype Genotype and Phenotype
4EB >?FIFQV CLO > ?ILLM QL CFKA CLLA FP QFBA QL QTL S>OF>?IBPdPFWB >KA PMBBA. "FDDBO ?ILLMP
TFII CFKA CLLA JLOB B>PFIV PFJMIV ?B@>RPB QEBFO PFWB TFII >IILT QEBJ QL FKQBOPB@Q TFQE CLLA
IL@>QFLKP JLOB LCQBK. !KA C>PQBO ?ILLMP TFII CFKA JLOB CLLA ?B@>RPB QEBV @>K @LSBO JLOB
DOLRKA FK > PELOQBO MBOFLA LC QFJB.
3FK@B PFWB >KA PMBBA >OB FKSBOPBIV OBI>QBA
(I>ODB ?ILLMP >OB PILT, PJ>II ?ILLMP >OB
C>PQ), TB LKIV KBBA > DBKLQVMB TFQE >
PFKDIB KRJ?BO.
F64B?2 9.15
=F;MM ",A U
@FI;N89 A?H?M;
",A() U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
438
4EB MEBKLQVMB QEBK FP QEB ?ILLM FQPBIC, TELPB PFWB >KA PMBBA FP >PPFDKBA ?V >AAFKD >K
FKPQ>K@B LC > ",A L?GB@Q QL QEB FIIJ @I>PP.
.LQF@B QE>Q TFQE G;RMJ??>, QEB O>KDB FP J>MMBA QL ?BQTBBK 15 >KA 0, JB>KFKD > ?ILLM TFQE >
DBKB S>IRB LC 0 JLSBP >Q > PMBBA LC 15 >KA > ?ILLM TFQE > DBKB S>IRB LC 1 ALBPKfQ JLSB >Q >II
(PMBBA LC 0).
It may seem absurd to use an array when all
we have is a single value, but we stick with
an array in case we want to make more
sophisticated bloops later.
A?H?M = H?Q @FI;N819;
@IL (CHN C = 0; C < A?H?M.F?HANB; C++) U
A?H?M8C9 = L;H>IG(0,1);
W
W
=F;MM FIIJ U
.3?=NIL FI=;NCIH;
@FI;N B?;FNB;
A bloop now has DNA. ",A >H;;
@FI;N L;
@FI;N G;RMJ??>;
FIIJ(",A >H;:) U
FI=;NCIH = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
B?;FNB = 200;
>H; = >H;:;
maxspeed and r (radius) are mapped to
values according to the DNA.
G;RMJ??> = G;J(>H;.A?H?M809, 0, 1, 15, 0);
L = G;J(>H;.A?H?M809, 0, 1, 0, 50);
W
Selection and Reproduction Selection and Reproduction
.LT QE>Q TB E>SB QEB DBKLQVMB >KA MEBKLQVMB, TB KBBA QL JLSB LK QL ABSFPFKD > JB>KP CLO
?ILLMP QL ?B PBIB@QBA >P M>OBKQP. 7B PQ>QBA ?BCLOB QE>Q QEB ILKDBO > ?ILLM IFSBP, QEB JLOB
@E>K@BP FQ E>P QL OBMOLAR@B. 4EB IBKDQE LC IFCB FP QEB ?ILLMfP CFQKBPP.
/KB LMQFLK TLRIA ?B QL P>V QE>Q TEBKBSBO QTL ?ILLMP @LJB FKQL @LKQ>@Q TFQE B>@E LQEBO, QEBV
J>HB > KBT ?ILLM. 4EB ILKDBO > ?ILLM IFSBP, QEB JLOB IFHBIV FQ FP QL @LJB FKQL @LKQ>@Q TFQE
>KLQEBO ?ILLM. (4EFP TLRIA >IPL >CCB@Q QEB BSLIRQFLK>OV LRQ@LJB DFSBK QE>Q, FK >AAFQFLK QL
B>QFKD CLLA, QEBFO >?FIFQV QL CFKA LQEBO ?ILLMP FP > C>@QLO FK QEB IFHBIFELLA LC E>SFKD > ?>?V.)
! PFJMIBO LMQFLK TLRIA ?B QL E>SB g>PBUR>Ih OBMOLAR@QFLK, JB>KFKD > ?ILLM ALBP KLQ OBNRFOB
> M>OQKBO. )Q @>K, >Q >KV JLJBKQ, J>HB > @ILKB LC FQPBIC, >KLQEBO ?ILLM TFQE QEB P>JB DBKBQF@
J>HBRM. )C TB PQ>QB QEFP PBIB@QFLK >IDLOFQEJ >P CLIILTP:
A? ,9D 24A09 8:809?, , -7::; 3,> , 1% .3,9.0 :1 =0;=:/@.492. A? ,9D 24A09 8:809?, , -7::; 3,> , 1% .3,9.0 :1 =0;=:/@.492.
4EB .>QROB LC #LAB (S005)
439
jQEBK QEB ILKDBO > ?ILLM IFSBP, QEB JLOB IFHBIV FQ TFII J>HB >Q IB>PQ LKB @EFIA. 4EFP FP
BNRFS>IBKQ QL P>VFKD QEB JLOB QFJBP VLR MI>V QEB ILQQBOV, QEB DOB>QBO QEB IFHBIFELLA VLRfII TFK
(QELRDE )fJ PLOOV QL P>V VLRO @E>K@BP LC QE>Q >OB PQFII BPPBKQF>IIV WBOL).
4L FJMIBJBKQ QEFP PBIB@QFLK >IDLOFQEJ, TB @>K TOFQB > CRK@QFLK FK QEB FIIJ @I>PP QE>Q MF@HP
> O>KALJ KRJ?BO BSBOV CO>JB. )C QEB KRJ?BO FP IBPP QE>K 0.01 (1%), > KBT ?ILLM FP ?LOK.
(LT ALBP > ?ILLM OBMOLAR@B? )K LRO MOBSFLRP BU>JMIBP, QEB OBMOLAR@QFLK MOL@BPP FKSLISBA
@>IIFKD QEB =LIMMIP?L() CRK@QFLK FK QEB ",A @I>PP >KA J>HFKD > KBT L?GB@Q COLJ QEB KBTIV
J>AB $.!. (BOB, PFK@B TB >OB J>HFKD > @EFIA COLJ > PFKDIB M>OBKQ, TBfII @>II > CRK@QFLK
@>IIBA =IJS() FKPQB>A.
.LQB >IPL QE>Q TBfSB OBAR@BA QEB MOL?>?FIFQV LC OBMOLAR@FKD COLJ 1% QL 0.05%. 4EFP S>IRB
J>HBP NRFQB > AFCCBOBK@B; TFQE > EFDE MOL?>?FIFQV LC OBMOLAR@FKD, QEB PVPQBJ TFII NRF@HIV QBKA
QLT>OAP LSBOMLMRI>QFLK. 4LL ILT > MOL?>?FIFQV, >KA BSBOVQEFKD TFII IFHBIV NRF@HIV AFB LRQ.
7OFQFKD QEB =IJS() CRK@QFLK FKQL QEB ",A @I>PP FP B>PV PFK@B 0OL@BPPFKD FK@IRABP > CRK@QFLK
;LL;S=IJS() QE>Q @LMFBP QEB @LKQBKQP LC LKB >OO>V FKQL >KLQEBO.
This function will return a new bloop, the
child.
FIIJ L?JLI>O=?() U
A 1% chance of executing the code in this
conditional, i.e. a 1% chance of
reproducing
C@ (L;H>IG(1) < 0.01) U
// +;E? NB? FIIJ <;<S
W
W
FIIJ L?JLI>O=?() U
C@ (L;H>IG(1) < 0.0005) U
Make a copy of the DNA. )A 9>?B:)A = :D7.9EFO();
1% mutation rate 9>?B:)A.CKJ7J;(0.01);
Make a new bloop at the same location
with the new DNA.
H;JKHD D;M BBEEF(BE97J?ED, 9>?B:)A);
S ;BI; Q
If the bloop does not reproduce, return null. H;JKHD DKBB;
W
W
=F;MM ",A U
This copy() function replaces crossover() in
this example.
",A =IJS() U
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
440
.LT QE>Q TB E>SB >II QEB MFB@BP FK MI>@B CLO PBIB@QFLK >KA OBMOLAR@QFLK, TB @>K CFK>IFWB QEB
4ILF> @I>PP QE>Q J>K>DBP QEB IFPQ LC >II FIIJ L?GB@QP (>P TBII >P > $II> L?GB@Q, TEF@E FQPBIC FP
> IFPQ LC .3?=NIL IL@>QFLKP CLO CLLA).
"BCLOB VLR ORK QEB BU>JMIB, Q>HB > JLJBKQ QL DRBPP TE>Q PFWB >KA PMBBA LC ?ILLMP QEB
PVPQBJ TFII BSLISB QLT>OAP. 7BfII AFP@RPP CLIILTFKD QEB @LAB.
Example 9.5: Evolution ecosystem
Make a new array the same length and copy
its contents.
@FI;N89 H?QA?H?M = H?Q @FI;N8A?H?M.F?HANB9;
;LL;S=IJS(A?H?M,H?QA?H?M);
L?NOLH H?Q ",A(H?QA?H?M);
W
W
4ILF> QILF>;
setup() and draw() do nothing more than
create and run a World object.
PIC> M?NOJ() U
MCT?(600,400);
QILF> = H?Q 4ILF>(20);
W
PIC> >L;Q() U
<;=EALIOH>(255);
QILF>.LOH();
W
=F;MM 4ILF> U
The World object keeps track of the
population bloops as well as the food.
ALL;S*CMN<FIIJ> <FIIJM;
$II> @II>;
4ILF>(CHN HOG) U
@II> = H?Q $II>(HOG);
<FIIJM = H?Q ALL;S*CMN<FIIJ>();
4EB .>QROB LC #LAB (S005)
441
)C VLR DRBPPBA JBAFRJ-PFWBA ?ILLMP TFQE JBAFRJ PMBBA, VLR TBOB OFDEQ. 7FQE QEB ABPFDK LC
QEFP PVPQBJ, ?ILLMP QE>Q >OB I>ODB >OB PFJMIV QLL PILT QL CFKA CLLA. !KA ?ILLMP QE>Q >OB C>PQ
>OB QLL PJ>II QL CFKA CLLA. 4EB LKBP QE>Q >OB >?IB QL IFSB QEB ILKDBPQ QBKA QL ?B FK QEB
JFAAIB, I>ODB BKLRDE >KA C>PQ BKLRDE QL CFKA CLLA (?RQ KLQ QLL I>ODB LO QLL C>PQ). 4EBOB >OB
>IPL PLJB >KLJ>IFBP. &LO BU>JMIB, FC FQ PL E>MMBKP QE>Q > ?RK@E LC I>ODB ?ILLMP BKA RM FK
QEB P>JB IL@>QFLK (>KA ?>OBIV JLSB ?B@>RPB QEBV >OB PL I>ODB), QEBV J>V >II AFB LRQ
PRAABKIV, IB>SFKD > ILQ LC CLLA CLO LKB I>ODB ?ILLM TEL E>MMBKP QL ?B QEBOB QL B>Q >KA
>IILTFKD > JFKF-MLMRI>QFLK LC I>ODB ?ILLMP QL PRPQ>FK QEBJPBISBP CLO > MBOFLA LC QFJB FK LKB
IL@>QFLK.
4EFP BU>JMIB FP O>QEBO PFJMIFPQF@ DFSBK FQP PFKDIB DBKB >KA >PBUR>I OBMOLAR@QFLK. (BOB >OB
PLJB PRDDBPQFLKP CLO ELT VLR JFDEQ >MMIV QEB ?ILLM BU>JMIB FK > JLOB BI>?LO>QB
B@LPVPQBJ PFJRI>QFLK.
Creating the population @IL (CHN C = 0; C < HOG; C++) U
.3?=NIL FI=;NCIH = H?Q .3?=NIL(L;H>IG(QC>NB),L;H>IG(B?CABN));
",A >H; = H?Q ",A();
<FIIJM.;>>(H?Q FIIJ(F,>H;));
W
W
PIC> LOH() U
@II>.LOH();
@IL (CHN C = <FIIJM.MCT?()-1; C >= 0; C--) U
The bloops live their life. FIIJ < = <FIIJM.A?N(C);
<.LOH();
<.?;N(@II>);
If one dies, it is removed from the
population and food is added at its
location.
C@ (<.>?;>()) U
<FIIJM.L?GIP?(C);
@II>.;>>(<.FI=;NCIH);
W
Here is where each living bloop has a
chance to reproduce. As long as a child is
made (i.e. not null) it is added to the
population.
FIIJ =BCF> = <.L?JLI>O=?();
C@ (=BCF> != HOFF) <FIIJM.;>>(=BCF>);
W
W
W
#E>MQBO 9. 4EB %SLIRQFLK LC #LAB
442
The Ecosystem Project The Ecosystem Project
Step 9 Exercise:
Add evolution to your ecosystem, building from the examples in this chapter.
Add a population of predators to your ecosystem. Biological evolution
between predators and prey (or parasites and hosts) is often referred to as
an arms race, in which the creatures continuously adapt and counter-
adapt to each other. Can you achieve this behavior in a system of multiple
creatures?
How would you implement crossover and mutation between two parents in
an ecosystem modeled after the bloops? Try implementing an algorithm so
that two creatures meet and mate when within a certain proximity. Can
you make creatures with gender?
Try using the weights of multiple steering forces as a creatures DNA. Can
you create a scenario in which creatures evolve to cooperate with each
other?
One of the greatest challenges in ecosystem simulations is achieving a
nice balance. You will likely find that most of your attempts result in either
mass overpopulation (followed by mass extinction) or simply mass
extinction straight away. What techniques can you employ to achieve
balance? Consider using the genetic algorithm itself to evolve optimal
parameters for an ecosystem.
4EB .>QROB LC #LAB (S005)
443
Chapter 10. Neural Chapter 10. Neural
Networks Networks
I-<B 0.;KA =?<02@@ :2 D6A5 . ;<?:.9 /?.6;.J
Charlie Sheen
7BfOB >Q QEB BKA LC LRO PQLOV. 4EFP FP QEB I>PQ LCCF@F>I @E>MQBO LC QEFP ?LLH (QELRDE ) BKSFPFLK
>AAFQFLK>I PRMMIBJBKQ>I J>QBOF>I CLO QEB TB?PFQB >KA MBOE>MP KBT @E>MQBOP FK QEB CRQROB).
7B ?BD>K TFQE FK>KFJ>QB L?GB@QP IFSFKD FK > TLOIA LC CLO@BP >KA D>SB QELPB L?GB@QP ABPFOBP,
>RQLKLJV, >KA QEB >?FIFQV QL Q>HB >@QFLK >@@LOAFKD QL > PVPQBJ LC ORIBP. .BUQ, TB >IILTBA
QELPB L?GB@QP QL IFSB FK > MLMRI>QFLK >KA BSLISB LSBO QFJB. .LT TB >PH: 7E>Q FP B>@E
L?GB@QfP AB@FPFLK-J>HFKD MOL@BPP? (LT @>K FQ >AGRPQ FQP @ELF@BP ?V IB>OKFKD LSBO QFJB? #>K
> @LJMRQ>QFLK>I BKQFQV MOL@BPP FQP BKSFOLKJBKQ >KA DBKBO>QB > AB@FPFLK?
4EB ERJ>K ?O>FK @>K ?B ABP@OF?BA >P > ?FLILDF@>I KBRO>I KBQTLOHd>K FKQBO@LKKB@QBA TB?
LC KBROLKP QO>KPJFQQFKD BI>?LO>QB M>QQBOKP LC BIB@QOF@>I PFDK>IP. $BKAOFQBP OB@BFSB FKMRQ
PFDK>IP >KA, ?>PBA LK QELPB FKMRQP, CFOB >K LRQMRQ PFDK>I SF> >K >ULK. /O PLJBQEFKD IFHB QE>Q.
(LT QEB ERJ>K ?O>FK >@QR>IIV TLOHP FP >K BI>?LO>QB >KA @LJMIBU JVPQBOV, LKB QE>Q TB
@BOQ>FKIV >OB KLQ DLFKD QL >QQBJMQ QL Q>@HIB FK OFDLOLRP ABQ>FI FK QEFP @E>MQBO.
#E>MQBO 10. .BRO>I .BQTLOHP
444
4EB DLLA KBTP FP QE>Q ABSBILMFKD BKD>DFKD >KFJ>QBA PVPQBJP TFQE @LAB ALBP KLQ OBNRFOB
P@FBKQFCF@ OFDLO LO >@@RO>@V, >P TBfSB IB>OKBA QEOLRDELRQ QEFP ?LLH. 7B @>K PFJMIV ?B FKPMFOBA
?V QEB FAB> LC ?O>FK CRK@QFLK.
)K QEFP @E>MQBO, TBfII ?BDFK TFQE > @LK@BMQR>I LSBOSFBT LC QEB MOLMBOQFBP >KA CB>QROBP LC KBRO>I
KBQTLOHP >KA ?RFIA QEB PFJMIBPQ MLPPF?IB BU>JMIB LC LKB (> KBQTLOH QE>Q @LKPFPQP LC > PFKDIB
KBROLK). !CQBOT>OAP, TBfII BU>JFKB PQO>QBDFBP CLO @OB>QFKD > g"O>FKh L?GB@Q QE>Q @>K ?B FKPBOQBA
FKQL LRO 3?BC=F? @I>PP >KA RPBA QL ABQBOJFKB PQBBOFKD. &FK>IIV, TBfII >IPL ILLH >Q QB@EKFNRBP
CLO SFPR>IFWFKD >KA >KFJ>QFKD > KBQTLOH LC KBROLKP.
F64B?2 10.1
10.1 Artificial Neural Networks: Introduction and 10.1 Artificial Neural Networks: Introduction and
Application Application
#LJMRQBO P@FBKQFPQP E>SB ILKD ?BBK FKPMFOBA ?V QEB ERJ>K ?O>FK. )K 1943, 7>OOBK 3.
-@#RIIL@E, > KBROLP@FBKQFPQ, >KA 7>IQBO 0FQQP, > ILDF@F>K, ABSBILMBA QEB CFOPQ @LK@BMQR>I JLABI
LC >K >OQFCF@F>I KBRO>I KBQTLOH. )K QEBFO M>MBO, "! ILDF@>I @>I@RIRP LC QEB FAB>P FJJFKBKQ FK
KBOSLRP >@QFSFQV,h QEBV ABP@OF?B QEB @LK@BMQ LC > KBROLK, > PFKDIB @BII IFSFKD FK > KBQTLOH LC
@BIIP QE>Q OB@BFSBP FKMRQP, MOL@BPPBP QELPB FKMRQP, >KA DBKBO>QBP >K LRQMRQ.
4EBFO TLOH, >KA QEB TLOH LC J>KV P@FBKQFPQP >KA OBPB>O@EBOP QE>Q CLIILTBA, T>P KLQ JB>KQ QL
>@@RO>QBIV ABP@OF?B ELT QEB ?FLILDF@>I ?O>FK TLOHP. 2>QEBO, >K >OQFCF@F>I KBRO>I KBQTLOH (TEF@E
TB TFII KLT PFJMIV OBCBO QL >P > gKBRO>I KBQTLOHh) T>P ABPFDKBA >P > @LJMRQ>QFLK>I JLABI
?>PBA LK QEB ?O>FK QL PLISB @BOQ>FK HFKAP LC MOL?IBJP.
)QfP MOL?>?IV MOBQQV L?SFLRP QL VLR QE>Q QEBOB >OB MOL?IBJP QE>Q >OB FK@OBAF?IV PFJMIB CLO >
@LJMRQBO QL PLISB, ?RQ AFCCF@RIQ CLO VLR. 4>HB QEB PNR>OB OLLQ LC 964,324, CLO BU>JMIB. ! NRF@H
IFKB LC @LAB MOLAR@BP QEB S>IRB 982, > KRJ?BO 0OL@BPPFKD @LJMRQBA FK IBPP QE>K >
JFIIFPB@LKA. 4EBOB >OB, LK QEB LQEBO E>KA, MOL?IBJP QE>Q >OB FK@OBAF?IV PFJMIB CLO VLR LO JB QL
PLISB, ?RQ KLQ PL B>PV CLO > @LJMRQBO. 3ELT >KV QLAAIBO > MF@QROB LC > HFQQBK LO MRMMV >KA
QEBVfII ?B >?IB QL QBII VLR SBOV NRF@HIV TEF@E LKB FP TEF@E. 3>V EBIIL >KA PE>HB JV E>KA LKB
JLOKFKD >KA VLR PELRIA ?B >?IB QL MF@H JB LRQ LC > @OLTA LC MBLMIB QEB KBUQ A>V. "RQ KBBA >
J>@EFKB QL MBOCLOJ LKB LC QEBPB Q>PHP? 3@FBKQFPQP E>SB >IOB>AV PMBKQ BKQFOB @>OBBOP
OBPB>O@EFKD >KA FJMIBJBKQFKD @LJMIBU PLIRQFLKP.
4EB .>QROB LC #LAB (S005)
445
4EB JLPQ @LJJLK >MMIF@>QFLK LC KBRO>I KBQTLOHP FK @LJMRQFKD QLA>V FP QL MBOCLOJ LKB LC
QEBPB gB>PV-CLO->-ERJ>K, AFCCF@RIQ-CLO->-J>@EFKBh Q>PHP, LCQBK OBCBOOBA QL >P M>QQBOK
OB@LDKFQFLK. !MMIF@>QFLKP O>KDB COLJ LMQF@>I @E>O>@QBO OB@LDKFQFLK (QROKFKD MOFKQBA LO
E>KATOFQQBK P@>KP FKQL AFDFQ>I QBUQ) QL C>@F>I OB@LDKFQFLK. 7B ALKfQ E>SB QEB QFJB LO KBBA QL
RPB PLJB LC QEBPB JLOB BI>?LO>QB >OQFCF@F>I FKQBIIFDBK@B >IDLOFQEJP EBOB, ?RQ FC VLR >OB
FKQBOBPQBA FK OBPB>O@EFKD KBRO>I KBQTLOHP, )fA OB@LJJBKA QEB ?LLHP AFH=:=7=5@ %BH9@@=;9B79: A
)C89FB ADDFC57< ?V 3QR>OQ *. 2RPPBII >KA 0BQBO .LOSFD >KA A% :CF #5A9 9J9@CD9FG ?V
$>SFA -. "LROD >KA 'IBKK 3BBJ>KK.
! KBRO>I KBQTLOH FP > g@LKKB@QFLKFPQh
@LJMRQ>QFLK>I PVPQBJ. 4EB @LJMRQ>QFLK>I
PVPQBJP TB TOFQB >OB MOL@BARO>I; >
MOLDO>J PQ>OQP >Q QEB CFOPQ IFKB LC @LAB,
BUB@RQBP FQ, >KA DLBP LK QL QEB KBUQ,
CLIILTFKD FKPQOR@QFLKP FK > IFKB>O C>PEFLK. !
QORB KBRO>I KBQTLOH ALBP KLQ CLIILT > IFKB>O
M>QE. 2>QEBO, FKCLOJ>QFLK FP MOL@BPPBA
@LIIB@QFSBIV, FK M>O>IIBI QEOLRDELRQ >
KBQTLOH LC KLABP (QEB KLABP, FK QEFP @>PB,
?BFKD KBROLKP).
(BOB TB E>SB VBQ >KLQEBO BU>JMIB LC >
@LJMIBU PVPQBJ, JR@E IFHB QEB LKBP TB
BU>JFKBA FK #E>MQBOP 6, 7, >KA 8. 4EB
FKAFSFAR>I BIBJBKQP LC QEB KBQTLOH, QEB KBROLKP, >OB PFJMIB. 4EBV OB>A >K FKMRQ, MOL@BPP FQ,
>KA DBKBO>QB >K LRQMRQ. ! KBQTLOH LC J>KV KBROLKP, ELTBSBO, @>K BUEF?FQ FK@OBAF?IV OF@E
>KA FKQBIIFDBKQ ?BE>SFLOP.
/KB LC QEB HBV BIBJBKQP LC > KBRO>I KBQTLOH FP FQP >?FIFQV QL @95FB. ! KBRO>I KBQTLOH FP KLQ
GRPQ > @LJMIBU PVPQBJ, ?RQ > @LJMIBU ,/,;?4A0 ,/,;?4A0 PVPQBJ, JB>KFKD FQ @>K @E>KDB FQP FKQBOK>I
PQOR@QROB ?>PBA LK QEB FKCLOJ>QFLK CILTFKD QEOLRDE FQ. 4VMF@>IIV, QEFP FP >@EFBSBA QEOLRDE QEB
>AGRPQFKD LC K9=;<HG. )K QEB AF>DO>J >?LSB, B>@E IFKB OBMOBPBKQP > @LKKB@QFLK ?BQTBBK QTL
KBROLKP >KA FKAF@>QBP QEB M>QET>V CLO QEB CILT LC FKCLOJ>QFLK. %>@E @LKKB@QFLK E>P >
B0423? B0423?, > KRJ?BO QE>Q @LKQOLIP QEB PFDK>I ?BQTBBK QEB QTL KBROLKP. )C QEB KBQTLOH
DBKBO>QBP > gDLLAh LRQMRQ (TEF@E TBfII ABCFKB I>QBO), QEBOB FP KL KBBA QL >AGRPQ QEB TBFDEQP.
(LTBSBO, FC QEB KBQTLOH DBKBO>QBP > gMLLOh LRQMRQd>K BOOLO, PL QL PMB>HdQEBK QEB PVPQBJ
>A>MQP, >IQBOFKD QEB TBFDEQP FK LOABO QL FJMOLSB PR?PBNRBKQ OBPRIQP.
4EBOB >OB PBSBO>I PQO>QBDFBP CLO IB>OKFKD, >KA TBfII BU>JFKB QTL LC QEBJ FK QEFP @E>MQBO.
i '@;0=A4>0/ !0,=9492 '@;0=A4>0/ !0,=9492 d%PPBKQF>IIV, > PQO>QBDV QE>Q FKSLISBP > QB>@EBO QE>Q FP
PJ>OQBO QE>K QEB KBQTLOH FQPBIC. &LO BU>JMIB, IBQfP Q>HB QEB C>@F>I OB@LDKFQFLK
BU>JMIB. 4EB QB>@EBO PELTP QEB KBQTLOH > ?RK@E LC C>@BP, >KA QEB QB>@EBO
>IOB>AV HKLTP QEB K>JB >PPL@F>QBA TFQE B>@E C>@B. 4EB KBQTLOH J>HBP FQP
DRBPPBP, QEBK QEB QB>@EBO MOLSFABP QEB KBQTLOH TFQE QEB >KPTBOP. 4EB KBQTLOH
@>K QEBK @LJM>OB FQP >KPTBOP QL QEB HKLTK g@LOOB@Qh LKBP >KA J>HB >AGRPQJBKQP
F64B?2 10.2
#E>MQBO 10. .BRO>I .BQTLOHP
446
>@@LOAFKD QL FQP BOOLOP. /RO CFOPQ KBRO>I KBQTLOH FK QEB KBUQ PB@QFLK TFII CLIILT QEFP
JLABI.
i )9>@;0=A4>0/ !0,=9492 )9>@;0=A4>0/ !0,=9492 d2BNRFOBA TEBK QEBOB FPKfQ >K BU>JMIB A>Q> PBQ TFQE HKLTK
>KPTBOP. )J>DFKB PB>O@EFKD CLO > EFAABK M>QQBOK FK > A>Q> PBQ. !K >MMIF@>QFLK LC QEFP
FP @IRPQBOFKD, F.B. AFSFAFKD > PBQ LC BIBJBKQP FKQL DOLRMP >@@LOAFKD QL PLJB RKHKLTK
M>QQBOK. 7B TLKfQ ?B ILLHFKD >Q >KV BU>JMIBP LC RKPRMBOSFPBA IB>OKFKD FK QEFP
@E>MQBO, >P QEFP PQO>QBDV FP IBPP OBIBS>KQ CLO LRO BU>JMIBP.
i &0491:=.0809? !0,=9492 &0491:=.0809? !0,=9492 d! PQO>QBDV ?RFIQ LK L?PBOS>QFLK. 4EFKH LC > IFQQIB JLRPB
ORKKFKD QEOLRDE > J>WB. )C FQ QROKP IBCQ, FQ DBQP > MFB@B LC @EBBPB; FC FQ QROKP OFDEQ, FQ
OB@BFSBP > IFQQIB PEL@H. ($LKfQ TLOOV, QEFP FP GRPQ > MOBQBKA JLRPB.) 0OBPRJ>?IV, QEB
JLRPB TFII IB>OK LSBO QFJB QL QROK IBCQ. )QP KBRO>I KBQTLOH J>HBP > AB@FPFLK TFQE >K
LRQ@LJB (QROK IBCQ LO OFDEQ) >KA L?PBOSBP FQP BKSFOLKJBKQ (VRJ LO LR@E). )C QEB
L?PBOS>QFLK FP KBD>QFSB, QEB KBQTLOH @>K >AGRPQ FQP TBFDEQP FK LOABO QL J>HB >
AFCCBOBKQ AB@FPFLK QEB KBUQ QFJB. 2BFKCLO@BJBKQ IB>OKFKD FP @LJJLK FK OL?LQF@P. !Q
QFJB N, QEB OL?LQ MBOCLOJP > Q>PH >KA L?PBOSBP QEB OBPRIQP. $FA FQ @O>PE FKQL > T>II LO
C>II LCC > Q>?IB? /O FP FQ RKE>OJBA? 7BfII ILLH >Q OBFKCLO@BJBKQ IB>OKFKD FK QEB @LKQBUQ
LC LRO PFJRI>QBA PQBBOFKD SBEF@IBP.
4EFP >?FIFQV LC > KBRO>I KBQTLOH QL IB>OK, QL J>HB >AGRPQJBKQP QL FQP PQOR@QROB LSBO QFJB, FP TE>Q
J>HBP FQ PL RPBCRI FK QEB CFBIA LC >OQFCF@F>I FKQBIIFDBK@B. (BOB >OB PLJB PQ>KA>OA RPBP LC KBRO>I
KBQTLOHP FK PLCQT>OB QLA>V.
i %,??0=9 &0.:294?4:9 %,??0=9 &0.:294?4:9 d7BfSB JBKQFLKBA QEFP PBSBO>I QFJBP >IOB>AV >KA FQfP MOL?>?IV
QEB JLPQ @LJJLK >MMIF@>QFLK. %U>JMIBP >OB C>@F>I OB@LDKFQFLK, LMQF@>I @E>O>@QBO
OB@LDKFQFLK, BQ@.
i (480 '0=40> %=0/4.?4:9 (480 '0=40> %=0/4.?4:9 d.BRO>I KBQTLOHP @>K ?B RPBA QL J>HB MOBAF@QFLKP. 7FII QEB
PQL@H OFPB LO C>II QLJLOOLT? 7FII FQ O>FK LO ?B PRKKV?
i '429,7 %=:.0>>492 '429,7 %=:.0>>492 d#L@EIB>O FJMI>KQP >KA EB>OFKD >FAP KBBA QL CFIQBO LRQ
RKKB@BPP>OV KLFPB >KA >JMIFCV QEB FJMLOQ>KQ PLRKAP. .BRO>I KBQTLOHP @>K ?B
QO>FKBA QL MOL@BPP >K >RAFL PFDK>I >KA CFIQBO FQ >MMOLMOF>QBIV.
i C:9?=:7 C:9?=:7 d9LR J>V E>SB OB>A >?LRQ OB@BKQ OBPB>O@E >AS>K@BP FK PBIC-AOFSFKD @>OP.
.BRO>I KBQTLOHP >OB LCQBK RPBA QL J>K>DB PQBBOFKD AB@FPFLKP LC MEVPF@>I SBEF@IBP
(LO PFJRI>QBA LKBP).
i ':1? '09>:=> ':1? '09>:=> d! PLCQ PBKPLO OBCBOP QL QEB MOL@BPP LC >K>IVWFKD > @LIIB@QFLK LC J>KV
JB>PROBJBKQP. ! QEBOJLJBQBO @>K QBII VLR QEB QBJMBO>QROB LC QEB >FO, ?RQ TE>Q FC
VLR >IPL HKBT QEB ERJFAFQV, ?>OLJBQOF@ MOBPPROB, ABTMLFKQ, >FO NR>IFQV, >FO ABKPFQV,
BQ@.? .BRO>I KBQTLOHP @>K ?B BJMILVBA QL MOL@BPP QEB FKMRQ A>Q> COLJ J>KV
FKAFSFAR>I PBKPLOP >KA BS>IR>QB QEBJ >P > TELIB.
i A9:8,7D D0?0.?4:9 A9:8,7D D0?0.?4:9 d"B@>RPB KBRO>I KBQTLOHP >OB PL DLLA >Q OB@LDKFWFKD M>QQBOKP,
QEBV @>K >IPL ?B QO>FKBA QL DBKBO>QB >K LRQMRQ TEBK PLJBQEFKD L@@ROP QE>Q ALBPKfQ
4EB .>QROB LC #LAB (S005)
447
CFQ QEB M>QQBOK. 4EFKH LC > KBRO>I KBQTLOH JLKFQLOFKD VLRO A>FIV OLRQFKB LSBO > ILKD
MBOFLA LC QFJB. !CQBO IB>OKFKD QEB M>QQBOKP LC VLRO ?BE>SFLO, FQ @LRIA >IBOQ VLR TEBK
PLJBQEFKD FP >JFPP.
4EFP FP ?V KL JB>KP > @LJMOBEBKPFSB IFPQ LC >MMIF@>QFLKP LC KBRO>I KBQTLOHP. "RQ ELMBCRIIV FQ
DFSBP VLR >K LSBO>II PBKPB LC QEB CB>QROBP >KA MLPPF?FIFQFBP. 4EB QEFKD FP, KBRO>I KBQTLOHP
>OB @LJMIF@>QBA >KA AFCCF@RIQ. 4EBV FKSLISB >II PLOQP LC C>K@V J>QEBJ>QF@P. 7EFIB QEFP FP >II
C>P@FK>QFKD (>KA FK@OBAF?IV FJMLOQ>KQ QL P@FBKQFCF@ OBPB>O@E), > ILQ LC QEB QB@EKFNRBP >OB KLQ
SBOV MO>@QF@>I FK QEB TLOIA LC ?RFIAFKD FKQBO>@QFSB, >KFJ>QBA 0OL@BPPFKD PHBQ@EBP. .LQ QL
JBKQFLK QE>Q FK LOABO QL @LSBO >II QEFP J>QBOF>I, TB TLRIA KBBA >KLQEBO ?LLHdLO JLOB IFHBIV,
> PBOFBP LC ?LLHP.
3L FKPQB>A, TBfII ?BDFK LRO I>PQ EROO>E FK QEB K>QROB LC @LAB TFQE QEB PFJMIBPQ LC >II KBRO>I
KBQTLOHP, FK >K BCCLOQ QL RKABOPQ>KA ELT QEB LSBO>II @LK@BMQP >OB >MMIFBA FK @LAB. 4EBK
TBfII ILLH >Q PLJB 0OL@BPPFKD PHBQ@EBP QE>Q DBKBO>QB SFPR>I OBPRIQP FKPMFOBA ?V QEBPB
@LK@BMQP.
10.2 The Perceptron 10.2 The Perceptron
)KSBKQBA FK 1957 ?V &O>KH 2LPBK?I>QQ >Q QEB #LOKBII !BOLK>RQF@>I ,>?LO>QLOV, > MBO@BMQOLK FP
QEB PFJMIBPQ KBRO>I KBQTLOH MLPPF?IB: > @LJMRQ>QFLK>I JLABI LC > PFKDIB KBROLK. !
MBO@BMQOLK @LKPFPQP LC LKB LO JLOB FKMRQP, > MOL@BPPLO, >KA > PFKDIB LRQMRQ.
! MBO@BMQOLK CLIILTP QEB gCBBA-CLOT>OAh JLABI, JB>KFKD FKMRQP >OB PBKQ FKQL QEB KBROLK,
>OB MOL@BPPBA, >KA OBPRIQ FK >K LRQMRQ. )K QEB AF>DO>J >?LSB, QEFP JB>KP QEB KBQTLOH (LKB
KBROLK) OB>AP COLJ IBCQ QL OFDEQ: FKMRQP @LJB FK, LRQMRQ DLBP LRQ.
,BQfP CLIILT B>@E LC QEBPB PQBMP FK JLOB ABQ>FI.
3QBM 1: 2B@BFSB FKMRQP.
3>V TB E>SB > MBO@BMQOLK TFQE QTL FKMRQPdIBQfP @>II QEBJ L1 >KA L2.
F64B?2 10.3: *52 =2?02=A?<;
#E>MQBO 10. .BRO>I .BQTLOHP
448
%DFKJ 0: N1 = 12
%DFKJ 1: N2 = 4
3QBM 2: 7BFDEQ FKMRQP.
%>@E FKMRQ QE>Q FP PBKQ FKQL QEB KBROLK JRPQ CFOPQ ?B TBFDEQBA, F.B. JRIQFMIFBA ?V PLJB S>IRB
(LCQBK > KRJ?BO ?BQTBBK -1 >KA 1). 7EBK @OB>QFKD > MBO@BMQOLK, TBfII QVMF@>IIV ?BDFK ?V
>PPFDKFKD O>KALJ TBFDEQP. (BOB, IBQfP DFSB QEB FKMRQP QEB CLIILTFKD TBFDEQP:
1;?=>J 0: 0.5
1;?=>J 1: -1
7B Q>HB B>@E FKMRQ >KA JRIQFMIV FQ ?V FQP TBFDEQ.
%DFKJ 0 * 1;?=>J 0 Y 12 * 0.5 = 6
%DFKJ 1 * 1;?=>J 1 Y 4 * -1 = -4
3QBM 3: 3RJ FKMRQP.
4EB TBFDEQBA FKMRQP >OB QEBK PRJJBA.
-KC = 6 + -4 = 2
3QBM 4: 'BKBO>QB LRQMRQ.
4EB LRQMRQ LC > MBO@BMQOLK FP DBKBO>QBA ?V M>PPFKD QE>Q PRJ QEOLRDE >K >@QFS>QFLK CRK@QFLK. )K
QEB @>PB LC > PFJMIB ?FK>OV LRQMRQ, QEB >@QFS>QFLK CRK@QFLK FP TE>Q QBIIP QEB MBO@BMQOLK TEBQEBO
QL gCFOBh LO KLQ. 9LR @>K BKSFPFLK >K ,%$ @LKKB@QBA QL QEB LRQMRQ PFDK>I: FC FQ CFOBP, QEB IFDEQ
DLBP LK; FC KLQ, FQ PQ>VP LCC.
!@QFS>QFLK CRK@QFLKP @>K DBQ > IFQQIB ?FQ E>FOV. )C VLR PQ>OQ OB>AFKD LKB LC QELPB >OQFCF@F>I
FKQBIIFDBK@B QBUQ?LLHP ILLHFKD CLO JLOB FKCL >?LRQ >@QFS>QFLK CRK@QFLKP, VLR J>V PLLK CFKA
VLROPBIC OB>@EFKD CLO > @>I@RIRP QBUQ?LLH. (LTBSBO, TFQE LRO COFBKA QEB PFJMIB MBO@BMQOLK,
TBfOB DLFKD QL AL PLJBQEFKD OB>IIV B>PV. ,BQfP J>HB QEB >@QFS>QFLK CRK@QFLK QEB PFDK LC QEB
PRJ. )K LQEBO TLOAP, FC QEB PRJ FP > MLPFQFSB KRJ?BO, QEB LRQMRQ FP 1; FC FQ FP KBD>QFSB, QEB LRQMRQ
FP -1.
*KJFKJ = I?=D(IKC) Y I?=D(2) Y +1
4EB .>QROB LC #LAB (S005)
449
,BQfP OBSFBT >KA @LKABKPB QEBPB PQBMP PL TB @>K FJMIBJBKQ QEBJ TFQE > @LAB PKFMMBQ.
(30 %0=.0;?=:9 A72:=4?38: (30 %0=.0;?=:9 A72:=4?38:
1. &LO BSBOV FKMRQ, JRIQFMIV QE>Q FKMRQ ?V FQP TBFDEQ.
2. 3RJ >II LC QEB TBFDEQBA FKMRQP.
3. #LJMRQB QEB LRQMRQ LC QEB MBO@BMQOLK ?>PBA LK QE>Q PRJ M>PPBA QEOLRDE >K
>@QFS>QFLK CRK@QFLK (QEB PFDK LC QEB PRJ).
,BQfP >PPRJB TB E>SB QTL >OO>VP LC KRJ?BOP, QEB FKMRQP >KA QEB TBFDEQP. &LO BU>JMIB:
g&LO BSBOV FKMRQh FJMIFBP > ILLM QE>Q JRIQFMIFBP B>@E FKMRQ ?V FQP @LOOBPMLKAFKD TBFDEQ.
3FK@B TB KBBA QEB PRJ, TB @>K >AA RM QEB OBPRIQP FK QE>Q SBOV ILLM.
/K@B TB E>SB QEB PRJ TB @>K @LJMRQB QEB LRQMRQ.
@FI;N89 CHJONM = U12 , 4W;
@FI;N89 Q?CABNM = U0.5,-1W;
Steps 1 and 2: Add up all the weighted
inputs.
@FI;N MOG = 0;
@IL (CHN C = 0; C < CHJONM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
Step 3: Passing the sum through an
activation function
@FI;N IONJON = ;=NCP;N?(MOG);
The activation function CHN ;=NCP;N?(@FI;N MOG) U
Return a 1 if positive, -1 if negative. C@ (MOG > 0) L?NOLH 1;
?FM? L?NOLH -1;
W
10.3 Simple Pattern Recognition Using a 10.3 Simple Pattern Recognition Using a
Perceptron Perceptron
.LT QE>Q TB RKABOPQ>KA QEB @LJMRQ>QFLK>I MOL@BPP LC > MBO@BMQOLK, TB @>K ILLH >Q >K
BU>JMIB LC LKB FK >@QFLK. 7B PQ>QBA QE>Q KBRO>I KBQTLOHP >OB LCQBK RPBA CLO M>QQBOK
OB@LDKFQFLK >MMIF@>QFLKP, PR@E >P C>@F>I OB@LDKFQFLK. %SBK PFJMIB MBO@BMQOLKP @>K
ABJLKPQO>QB QEB ?>PF@P LC @I>PPFCF@>QFLK, >P FK QEB CLIILTFKD BU>JMIB.
#E>MQBO 10. .BRO>I .BQTLOHP
450
#LKPFABO > IFKB FK QTL-AFJBKPFLK>I PM>@B.
0LFKQP FK QE>Q PM>@B @>K ?B @I>PPFCFBA >P
IFSFKD LK BFQEBO LKB PFAB LC QEB IFKB LO QEB
LQEBO. 7EFIB QEFP FP > PLJBTE>Q PFIIV BU>JMIB
(PFK@B QEBOB FP @IB>OIV KL KBBA CLO > KBRO>I
KBQTLOH; TB @>K ABQBOJFKB LK TEF@E PFAB >
MLFKQ IFBP TFQE PLJB PFJMIB >IDB?O>), FQ
PELTP ELT > MBO@BMQOLK @>K ?B QO>FKBA QL
OB@LDKFWB MLFKQP LK LKB PFAB SBOPRP
>KLQEBO.
,BQfP P>V > MBO@BMQOLK E>P 2 FKMRQP (QEB U-
>KA V-@LLOAFK>QBP LC > MLFKQ). 5PFKD > PFDK >@QFS>QFLK CRK@QFLK, QEB LRQMRQ TFII BFQEBO ?B -1 LO
1dF.B., QEB FKMRQ A>Q> FP @I>PPFCFBA >@@LOAFKD QL QEB PFDK LC QEB LRQMRQ. )K QEB >?LSB AF>DO>J,
TB @>K PBB ELT B>@E MLFKQ FP BFQEBO ?BILT QEB IFKB (-1) LO >?LSB (+1).
4EB MBO@BMQOLK FQPBIC @>K ?B AF>DO>JJBA >P CLIILTP:
7B @>K PBB ELT QEBOB >OB QTL FKMRQP (L >KA M), > TBFDEQ CLO B>@E FKMRQ (K9=;<H
L
>KA K9=;<H
M
),
>P TBII >P > MOL@BPPFKD KBROLK QE>Q DBKBO>QBP QEB LRQMRQ.
4EBOB FP > MOBQQV PFDKFCF@>KQ MOL?IBJ EBOB, ELTBSBO. ,BQfP @LKPFABO QEB MLFKQ (0,0). 7E>Q FC TB
PBKA QEFP MLFKQ FKQL QEB MBO@BMQOLK >P FQP FKMRQ: U = 0 >KA V = 0? 7E>Q TFII QEB PRJ LC FQP
TBFDEQBA FKMRQP ?B? .L J>QQBO TE>Q QEB TBFDEQP >OB, QEB PRJ TFII >IT>VP ?B 0! "RQ QEFP @>KfQ
?B OFDEQd>CQBO >II, QEB MLFKQ (0,0) @LRIA @BOQ>FKIV ?B >?LSB LO ?BILT S>OFLRP IFKBP FK LRO QTL-
AFJBKPFLK>I TLOIA.
4L >SLFA QEFP AFIBJJ>, LRO MBO@BMQOLK TFII OBNRFOB > QEFOA FKMRQ, QVMF@>IIV OBCBOOBA QL >P > -4,> -4,>
FKMRQ. ! ?F>P FKMRQ >IT>VP E>P QEB S>IRB LC 1 >KA FP >IPL TBFDEQBA. (BOB FP LRO MBO@BMQOLK TFQE
QEB >AAFQFLK LC QEB ?F>P:
F64B?2 10.4
F64B?2 10.5
4EB .>QROB LC #LAB (S005)
451
,BQfP DL ?>@H QL QEB MLFKQ (0,0). (BOB >OB LRO FKMRQP:
0 * M;?=>J <EH N = 0
0 * M;?=>J <EH O = 0
1 * M;?=>J <EH 8?7I = M;?=>J <EH 8?7I
4EB LRQMRQ FP QEB PRJ LC QEB >?LSB QEOBB S>IRBP, 0 MIRP 0 MIRP QEB ?F>PfP TBFDEQ. 4EBOBCLOB,
QEB ?F>P, LK FQP LTK, >KPTBOP QEB NRBPQFLK >P QL TEBOB (0,0) FP FK OBI>QFLK QL QEB IFKB. )C QEB
?F>PfP TBFDEQ FP MLPFQFSB, (0,0) FP >?LSB QEB IFKB; KBD>QFSB, FQ FP ?BILT. )Q g?F>PBPh QEB
MBO@BMQOLKfP RKABOPQ>KAFKD LC QEB IFKBfP MLPFQFLK OBI>QFSB QL (0,0).
F64B?2 10.6
10.4 Coding the Perceptron 10.4 Coding the Perceptron
7BfOB KLT OB>AV QL >PPBJ?IB QEB @LAB CLO > .?L=?JNLIH @I>PP. 4EB LKIV A>Q> QEB
MBO@BMQOLK KBBAP QL QO>@H >OB QEB FKMRQ TBFDEQP, >KA TB @LRIA RPB >K >OO>V LC CIL>QP QL PQLOB
QEBPB.
4EB @LKPQOR@QLO @LRIA OB@BFSB >K >ODRJBKQ FKAF@>QFKD QEB KRJ?BO LC FKMRQP (FK QEFP @>PB
QEOBB: U, V, >KA > ?F>P) >KA PFWB QEB >OO>V >@@LOAFKDIV.
! MBO@BMQOLK KBBAP QL ?B >?IB QL OB@BFSB FKMRQP >KA DBKBO>QB >K LRQMRQ. 7B @>K M>@H>DB
QEBPB OBNRFOBJBKQP FKQL > CRK@QFLK @>IIBA @??>@ILQ;L>(). )K QEFP BU>JMIB, TBfII E>SB QEB
=F;MM .?L=?JNLIH U
@FI;N89 Q?CABNM;
.?L=?JNLIH(CHN H) U
Q?CABNM = H?Q @FI;N8H9;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
The weights are picked randomly to start. Q?CABNM8C9 = L;H>IG(-1,1);
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
452
MBO@BMQOLK OB@BFSB FQP FKMRQP >P >K >OO>V (TEF@E PELRIA ?B QEB P>JB IBKDQE >P QEB >OO>V LC
TBFDEQP) >KA OBQROK QEB LRQMRQ >P >K FKQBDBO.
0OBPRJ>?IV, TB @LRIA KLT @OB>QB > .?L=?JNLIH L?GB@Q >KA >PH FQ QL J>HB > DRBPP CLO >KV
DFSBK MLFKQ.
$FA QEB MBO@BMQOLK DBQ FQ OFDEQ? !Q QEFP MLFKQ, QEB MBO@BMQOLK E>P KL ?BQQBO QE>K > 50/50
@E>K@B LC >OOFSFKD >Q QEB OFDEQ >KPTBO. 2BJBJ?BO, TEBK TB @OB>QBA FQ, TB D>SB B>@E TBFDEQ >
O>KALJ S>IRB. ! KBRO>I KBQTLOH FPKfQ J>DF@. )QfP KLQ DLFKD QL ?B >?IB QL DRBPP >KVQEFKD
@LOOB@QIV RKIBPP TB QB>@E FQ ELT QL!
4L QO>FK > KBRO>I KBQTLOH QL >KPTBO @LOOB@QIV, TBfOB DLFKD QL BJMILV QEB JBQELA LC
GID9FJ=G98 @95FB=B; QE>Q TB ABP@OF?BA FK PB@QFLK 10.1 (PBB M>DB 445).
7FQE QEFP JBQELA, QEB KBQTLOH FP MOLSFABA TFQE FKMRQP CLO TEF@E QEBOB FP > HKLTK >KPTBO. 4EFP
T>V QEB KBQTLOH @>K CFKA LRQ FC FQ E>P J>AB > @LOOB@Q DRBPP. )C FQfP FK@LOOB@Q, QEB KBQTLOH @>K
IB>OK COLJ FQP JFPQ>HB >KA >AGRPQ FQP TBFDEQP. 4EB MOL@BPP FP >P CLIILTP:
1. 0OLSFAB QEB MBO@BMQOLK TFQE FKMRQP CLO TEF@E QEBOB FP > HKLTK >KPTBO.
CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
@FI;N MOG = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
Result is the sign of the sum, -1 or +1. Here
the perceptron is making a guess. Is it on
one side of the line or the other?
L?NOLH ;=NCP;N?(MOG);
W
F64B?2 10.7
Create the Perceptron. .?L=?JNLIH J = H?Q .?L=?JNLIH(3);
The input is 3 values: x,y and bias. @FI;N89 JICHN = U50,-12,1W;
The answer! CHN L?MOFN = J.@??>@ILQ;L>(JICHN);
4EB .>QROB LC #LAB (S005)
453
2. !PH QEB MBO@BMQOLK QL DRBPP >K >KPTBO.
3. #LJMRQB QEB BOOLO. ($FA FQ DBQ QEB >KPTBO OFDEQ LO TOLKD?)
4. !AGRPQ >II QEB TBFDEQP >@@LOAFKD QL QEB BOOLO.
5. 2BQROK QL 3QBM 1 >KA OBMB>Q!
3QBMP 1 QEOLRDE 4 @>K ?B M>@H>DBA FKQL > CRK@QFLK. "BCLOB TB @>K TOFQB QEB BKQFOB CRK@QFLK,
ELTBSBO, TB KBBA QL BU>JFKB 3QBMP 3 >KA 4 FK JLOB ABQ>FI. (LT AL TB ABCFKB QEB
MBO@BMQOLKfP BOOLO? !KA ELT PELRIA TB >AGRPQ QEB TBFDEQP >@@LOAFKD QL QEFP BOOLO?
4EB MBO@BMQOLKfP BOOLO @>K ?B ABCFKBA >P QEB AFCCBOBK@B ?BQTBBK QEB ABPFOBA >KPTBO >KA FQP
DRBPP.
!,,*, = !-%,! */.+/. - #/!-- */.+/.
4EB >?LSB CLOJRI> J>V ILLH C>JFIF>O QL VLR. )K #E>MQBO 6 (PBB M>DB 263), TB @LJMRQBA >
PQBBOFKD CLO@B >P QEB AFCCBOBK@B ?BQTBBK LRO ABPFOBA SBIL@FQV >KA LRO @ROOBKQ SBIL@FQV.
-.!!,%)# = !-%,! 0!'*C%.3 - C/,,!). 0!'*C%.3
4EFP T>P >IPL >K BOOLO @>I@RI>QFLK. 4EB @ROOBKQ SBIL@FQV >@QP >P > DRBPP >KA QEB BOOLO (QEB
PQBBOFKD CLO@B) QBIIP RP ELT QL >AGRPQ QEB SBIL@FQV FK QEB OFDEQ AFOB@QFLK. )K > JLJBKQ, TBfII
PBB ELT >AGRPQFKD QEB SBEF@IBfP SBIL@FQV QL CLIILT > Q>ODBQ FP GRPQ IFHB >AGRPQFKD QEB TBFDEQP
LC > KBRO>I KBQTLOH QL >OOFSB >Q QEB OFDEQ >KPTBO.
)K QEB @>PB LC QEB MBO@BMQOLK, QEB LRQMRQ E>P LKIV QTL MLPPF?IB S>IRBP: +1 +1 LO -1 -1. 4EFP JB>KP
QEBOB >OB LKIV QEOBB MLPPF?IB BOOLOP.
)C QEB MBO@BMQOLK DRBPPBP QEB @LOOB@Q >KPTBO, QEBK QEB DRBPP BNR>IP QEB ABPFOBA LRQMRQ >KA
QEB BOOLO FP 0. )C QEB @LOOB@Q >KPTBO FP -1 >KA TBfSB DRBPPBA +1, QEBK QEB BOOLO FP -2. )C QEB
@LOOB@Q >KPTBO FP +1 >KA TBfSB DRBPPBA -1, QEBK QEB BOOLO FP +2.
Desired Desired Guess Guess Error Error
-1 -1 0
-1 +1 -2
+1 -1 +2
+1 +1 0
#E>MQBO 10. .BRO>I .BQTLOHP
454
4EB BOOLO FP QEB ABQBOJFKFKD C>@QLO FK ELT QEB MBO@BMQOLKfP TBFDEQP PELRIA ?B >AGRPQBA. &LO
>KV DFSBK TBFDEQ, TE>Q TB >OB ILLHFKD QL @>I@RI>QB FP QEB @E>KDB FK TBFDEQ, LCQBK @>IIBA
OK9=;<H (LO gABIQ>h TBFDEQ, ABIQ> ?BFKD QEB 'OBBH IBQQBO ]).
)!1 1!%#$. = 1!%#$. + U1!%#$.
]TBFDEQ FP @>I@RI>QBA >P QEB BOOLO JRIQFMIFBA ?V QEB FKMRQ.
U1!%#$. = !,,*, * %)+/.
4EBOBCLOB:
)!1 1!%#$. = 1!%#$. + !,,*, * %)+/.
4L RKABOPQ>KA TEV QEFP TLOHP, TB @>K >D>FK OBQROK QL PQBBOFKD (PBB M>DB 263). ! PQBBOFKD
CLO@B FP BPPBKQF>IIV >K BOOLO FK SBIL@FQV. )C TB >MMIV QE>Q CLO@B >P LRO >@@BIBO>QFLK (]SBIL@FQV),
QEBK TB >AGRPQ LRO SBIL@FQV QL JLSB FK QEB @LOOB@Q AFOB@QFLK. 4EFP FP TE>Q TB T>KQ QL AL TFQE
LRO KBRO>I KBQTLOHfP TBFDEQP. 7B T>KQ QL >AGRPQ QEBJ FK QEB OFDEQ AFOB@QFLK, >P ABCFKBA ?V QEB
BOOLO.
7FQE PQBBOFKD, ELTBSBO, TB E>A >K >AAFQFLK>I S>OF>?IB QE>Q @LKQOLIIBA QEB SBEF@IBfP >?FIFQV QL
PQBBO: QEB A5L=AIA :CF79. 7FQE > EFDE J>UFJRJ CLO@B, QEB SBEF@IB T>P >?IB QL >@@BIBO>QB >KA
QROK SBOV NRF@HIV; TFQE > ILTBO CLO@B, QEB SBEF@IB TLRIA Q>HB ILKDBO QL >AGRPQ FQP SBIL@FQV. 4EB
KBRO>I KBQTLOH TFII BJMILV > PFJFI>O PQO>QBDV TFQE > S>OF>?IB @>IIBA QEB gIB>OKFKD @LKPQ>KQ.h
7BfII >AA FK QEB IB>OKFKD @LKPQ>KQ >P CLIILTP:
)!1 1!%#$. = 1!%#$. + !,,*, * %)+/. * '!A,)%)# C*)-.A).
.LQF@B QE>Q > EFDE IB>OKFKD @LKPQ>KQ JB>KP QEB TBFDEQ TFII @E>KDB JLOB AO>PQF@>IIV. 4EFP J>V
EBIM RP >OOFSB >Q > PLIRQFLK JLOB NRF@HIV, ?RQ TFQE PR@E I>ODB @E>KDBP FK TBFDEQ FQfP MLPPF?IB
TB TFII LSBOPELLQ QEB LMQFJ>I TBFDEQP. 7FQE > PJ>II IB>OKFKD @LKPQ>KQ, QEB TBFDEQP TFII ?B
>AGRPQBA PILTIV, OBNRFOFKD JLOB QO>FKFKD QFJB ?RQ >IILTFKD QEB KBQTLOH QL J>HB SBOV PJ>II
>AGRPQJBKQP QE>Q @LRIA FJMOLSB QEB KBQTLOHfP LSBO>II >@@RO>@V.
!PPRJFKD QEB >AAFQFLK LC > S>OF>?IB = CLO QEB IB>OKFKD @LKPQ>KQ, TB @>K KLT TOFQB > QO>FKFKD
CRK@QFLK CLO QEB MBO@BMQOLK CLIILTFKD QEB >?LSB PQBMP.
A new variable is introduced to control the
learning rate.
@FI;N = = 0.01;
Step 1: Provide the inputs and known
answer. These are passed in as arguments
to train().
PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
Step 2: Guess according to those inputs. CHN AO?MM = @??>@ILQ;L>(CHJONM);
Step 3: Compute the error (difference
between answer and guess).
@FI;N ?LLIL = >?MCL?> - AO?MM;
4EB .>QROB LC #LAB (S005)
455
7B @>K KLT PBB QEB .?L=?JNLIH @I>PP >P > TELIB.
4L QO>FK QEB MBO@BMQOLK, TB KBBA > PBQ LC FKMRQP TFQE > HKLTK >KPTBO. 7B @LRIA M>@H>DB
QEFP RM FK > @I>PP IFHB PL:
Step 4: Adjust all the weights according to
the error and learning constant.
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
=F;MM .?L=?JNLIH U
The Perceptron stores its weights and
learning constants.
@FI;N89 Q?CABNM;
@FI;N = = 0.01;
.?L=?JNLIH(CHN H) U
Q?CABNM = H?Q @FI;N8H9;
Weights start off random. @IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 = L;H>IG(-1,1);
W
W
Return an output based on inputs. CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
@FI;N MOG = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
MOG += CHJONM8C9*Q?CABNM8C9;
W
L?NOLH ;=NCP;N?(MOG);
W
Output is a +1 or -1. CHN ;=NCP;N?(@FI;N MOG) U
C@ (MOG > 0) L?NOLH 1;
?FM? L?NOLH -1;
W
Train the network against known data. PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
CHN AO?MM = @??>@ILQ;L>(CHJONM);
@FI;N ?LLIL = >?MCL?> - AO?MM;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
W
=F;MM 1L;CH?L U
#E>MQBO 10. .BRO>I .BQTLOHP
456
.LT QEB NRBPQFLK ?B@LJBP, ELT AL TB MF@H > MLFKQ >KA HKLT TEBQEBO FQ FP >?LSB LO ?BILT >
IFKB? ,BQfP PQ>OQ TFQE QEB CLOJRI> CLO > IFKB, TEBOB S FP @>I@RI>QBA >P > CRK@QFLK LC R:
O = <(N)
)K DBKBOF@ QBOJP, > IFKB @>K ?B ABP@OF?BA >P:
O = 7N + 8
(BOBfP > PMB@FCF@ BU>JMIB:
O = 2*N + 1
7B @>K QEBK TOFQB > 0OL@BPPFKD CRK@QFLK TFQE QEFP FK JFKA.
3L, FC TB J>HB RM > MLFKQ:
(LT AL TB HKLT FC QEFP MLFKQ FP >?LSB LO ?BILT QEB IFKB? 4EB IFKB CRK@QFLK @(R) DFSBP RP QEB S
S>IRB LK QEB IFKB CLO QE>Q R MLPFQFLK. ,BQfP @>II QE>Q SFCH?.
)C QEB S S>IRB TB >OB BU>JFKFKD FP >?LSB QEB IFKB, FQ TFII ?B IBPP QE>K SFCH?.
A "Trainer" object stores the inputs and the
correct answer.
@FI;N89 CHJONM;
CHN ;HMQ?L;
1L;CH?L(@FI;N R, @FI;N S, CHN ;) U
CHJONM = H?Q @FI;N839;
CHJONM809 = R;
CHJONM819 = S;
Note that the Trainer has the bias input built
into its array.
CHJONM829 = 1;
;HMQ?L = ;;
W
W
A function to calculate y based on x along a
line
@FI;N @(@FI;N R) U
L?NOLH 2*R+1;
W
@FI;N R = L;H>IG(QC>NB);
@FI;N S = L;H>IG(B?CABN);
The y position on the line @FI;N SFCH? = @(R);
4EB .>QROB LC #LAB (S005)
457
7B @>K QEBK J>HB > 1L;CH?L L?GB@Q TFQE QEB FKMRQP >KA QEB @LOOB@Q >KPTBO.
!PPRJFKD TB E>A > .?L=?JNLIH L?GB@Q JNLIH, TB @LRIA QEBK QO>FK FQ ?V PBKAFKD QEB FKMRQP
>ILKD TFQE QEB HKLTK >KPTBO.
.LT, FQfP FJMLOQ>KQ QL OBJBJ?BO QE>Q QEFP FP GRPQ > ABJLKPQO>QFLK. 2BJBJ?BO LRO
3E>HBPMB>OB-QVMFKD JLKHBVP (PBB M>DB 392)? 7B >PHBA LRO DBKBQF@ >IDLOFQEJ QL PLISB CLO
gQL ?B LO KLQ QL ?Bhd>K >KPTBO TB >IOB>AV HKBT. 7B AFA QEFP QL J>HB PROB LRO DBKBQF@
>IDLOFQEJ TLOHBA MOLMBOIV. 4EB P>JB OB>PLKFKD >MMIFBP QL QEFP BU>JMIB. 7B ALKfQ KBBA >
MBO@BMQOLK QL QBII RP TEBQEBO > MLFKQ FP >?LSB LO ?BILT > IFKB; TB @>K AL QE>Q TFQE PFJMIB
J>QE. 7B >OB RPFKD QEFP P@BK>OFL, LKB QE>Q TB @>K B>PFIV PLISB TFQELRQ > MBO@BMQOLK, QL
ABJLKPQO>QB QEB MBO@BMQOLKfP >IDLOFQEJ >P TBII >P B>PFIV @LKCFOJ QE>Q FQ FP TLOHFKD MOLMBOIV.
,BQfP ILLH >Q ELT QEB MBO@BMQOLK TLOHP TFQE >K >OO>V LC J>KV QO>FKFKD MLFKQP.
F64B?2 10.8
C@ (S < SFCH?) U
The answer is -1 if y is above the line. ;HMQ?L = -1;
W ?FM? U
;HMQ?L = 1;
W
1L;CH?L N = H?Q 1L;CH?L(R, S, ;HMQ?L);
JNLIH.NL;CH(N.CHJONM,N.;HMQ?L);
#E>MQBO 10. .BRO>I .BQTLOHP
458
Example 10.1: The Perceptron
The Perceptron .?L=?JNLIH JNLIH;
2,000 training points 1L;CH?L89 NL;CHCHA = H?Q 1L;CH?L820009;
CHN =IOHN = 0;
The formula for a line @FI;N @(@FI;N R) U
L?NOLH 2*R+1;
W
PIC> M?NOJ() U
MCT?(400, 400);
JNLIH = H?Q .?L=?JNLIH(3);
Make 2,000 training points. @IL (CHN C = 0; C < NL;CHCHA.F?HANB; C++) U
@FI;N R = L;H>IG(-QC>NB/2,QC>NB/2);
@FI;N S = L;H>IG(-B?CABN/2,B?CABN/2);
Is the correct answer 1 or -1? CHN ;HMQ?L = 1;
C@ (S < @(R)) ;HMQ?L = -1;
NL;CHCHA8C9 = H?Q 1L;CH?L(R, S, ;HMQ?L);
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
NL;HMF;N?(QC>NB/2,B?CABN/2);
JNLIH.NL;CH(NL;CHCHA8=IOHN9.CHJONM, NL;CHCHA8=IOHN9.;HMQ?L);
For animation, we are training one point at a
time.
=IOHN = (=IOHN + 1) % NL;CHCHA.F?HANB;
@IL (CHN C = 0; C < =IOHN; C++) U
MNLIE?(0);
CHN AO?MM = JNLIH.@??>@ILQ;L>(NL;CHCHA8C9.CHJONM);
4EB .>QROB LC #LAB (S005)
459
Show the classificationno fill for -1,
black for +1.
C@ (AO?MM > 0) HI$CFF();
?FM? @CFF(0);
?FFCJM?(NL;CHCHA8C9.CHJONM809, NL;CHCHA8C9.CHJONM819, 8, 8);
W
W
)KPQB>A LC RPFKD QEB PRMBOSFPBA IB>OKFKD JLABI >?LSB, @>K VLR QO>FK QEB KBRO>I
KBQTLOH QL CFKA QEB OFDEQ TBFDEQP ?V RPFKD > DBKBQF@ >IDLOFQEJ?
Exercise 10.1 Exercise 10.1
6FPR>IFWB QEB MBO@BMQOLK FQPBIC. $O>T QEB FKMRQP, QEB MOL@BPPFKD KLAB, >KA QEB LRQMRQ.
Exercise 10.2 Exercise 10.2
10.5 A Steering Perceptron 10.5 A Steering Perceptron
7EFIB @I>PPFCVFKD MLFKQP >@@LOAFKD QL QEBFO MLPFQFLK >?LSB LO ?BILT > IFKB T>P > RPBCRI
ABJLKPQO>QFLK LC QEB MBO@BMQOLK FK >@QFLK, FQ ALBPKfQ E>SB JR@E MO>@QF@>I OBIBS>K@B QL QEB
LQEBO BU>JMIBP QEOLRDELRQ QEFP ?LLH. )K QEFP PB@QFLK, TBfII Q>HB QEB @LK@BMQP LC > MBO@BMQOLK
(>OO>V LC FKMRQP, PFKDIB LRQMRQ), >MMIV FQ QL PQBBOFKD ?BE>SFLOP, >KA ABJLKPQO>QB
OBFKCLO@BJBKQ IB>OKFKD >ILKD QEB T>V.
7B >OB KLT DLFKD QL Q>HB PFDKFCF@>KQ @OB>QFSB IF@BKPB TFQE QEB @LK@BMQ LC > KBRO>I KBQTLOH.
4EFP TFII >IILT RP QL PQF@H TFQE QEB ?>PF@P >KA >SLFA PLJB LC QEB EFDEIV @LJMIBU >IDLOFQEJP
>PPL@F>QBA TFQE JLOB PLMEFPQF@>QBA KBRO>I KBQTLOHP. (BOB TBfOB KLQ PL @LK@BOKBA TFQE
CLIILTFKD ORIBP LRQIFKBA FK >OQFCF@F>I FKQBIIFDBK@B QBUQ?LLHPdTBfOB GRPQ ELMFKD QL J>HB
PLJBQEFKD FKQBOBPQFKD >KA ?O>FK-IFHB.
2BJBJ?BO LRO DLLA COFBKA QEB 3?BC=F? @I>PP? 9LR HKLT, QE>Q LKB CLO J>HFKD L?GB@QP TFQE
> IL@>QFLK, SBIL@FQV, >KA >@@BIBO>QFLK? 4E>Q @LRIA L?BV .BTQLKfP I>TP TFQE >K
;JJFS$IL=?() CRK@QFLK >KA JLSB >OLRKA QEB TFKALT >@@LOAFKD QL > S>OFBQV LC PQBBOFKD
ORIBP?
7E>Q FC TB >AABA LKB JLOB S>OF>?IB QL LRO 3?BC=F? @I>PP?
=F;MM 3?BC=F? U
#E>MQBO 10. .BRO>I .BQTLOHP
460
(BOBfP LRO P@BK>OFL. ,BQfP P>V TB E>SB > 0OL@BPPFKD PHBQ@E TFQE >K ALL;S*CMN LC Q>ODBQP >KA
> PFKDIB SBEF@IB.
,BQfP P>V QE>Q QEB SBEF@IB PBBHP >II LC QEB Q>ODBQP. !@@LOAFKD QL QEB MOFK@FMIBP LC #E>MQBO 6, TB
TLRIA KBUQ TOFQB > CRK@QFLK QE>Q @>I@RI>QBP > PQBBOFKD CLO@B QLT>OAP B>@E Q>ODBQ, >MMIVFKD B>@E
CLO@B LKB >Q > QFJB QL QEB L?GB@QfP >@@BIBO>QFLK. !PPRJFKD QEB Q>ODBQP >OB >K ALL;S*CMN LC
.3?=NIL L?GB@QP, FQ TLRIA ILLH PLJBQEFKD IFHB:
)K #E>MQBO 6, TB >IPL BU>JFKBA ELT TB @LRIA @OB>QB JLOB AVK>JF@ PFJRI>QFLKP ?V TBFDEQFKD
B>@E PQBBOFKD CLO@B >@@LOAFKD QL PLJB ORIB. &LO BU>JMIB, TB @LRIA P>V QE>Q QEB C>OQEBO VLR
>OB COLJ > Q>ODBQ, QEB PQOLKDBO QEB CLO@B.
Giving the vehicle a brain! .?L=?JNLIH <L;CH;
.3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
//?N=...
F64B?2 10.9
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
@IL (.3?=NIL N;LA?N : N;LA?NM) U
For every target, apply a steering force
towards the target.
.3?=NIL @IL=? = M??E(N;LA?NM.A?N(C));
;JJFS$IL=?(@IL=?);
W
W
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
@IL (.3?=NIL N;LA?N : N;LA?NM) U
.3?=NIL @IL=? = M??E(N;LA?NM.A?N(C));
@FI;N > = .3?=NIL.>CMN(N;LA?N,FI=;NCIH);
@FI;N Q?CABN = G;J(>,0,QC>NB,0,5);
4EB .>QROB LC #LAB (S005)
461
"RQ TE>Q FC FKPQB>A TB @LRIA >PH LRO ?O>FK (F.B. MBO@BMQOLK) QL Q>HB FK >II QEB CLO@BP >P >K
FKMRQ, MOL@BPP QEBJ >@@LOAFKD QL TBFDEQP LC QEB MBO@BMQOLK FKMRQP, >KA DBKBO>QB >K LRQMRQ
PQBBOFKD CLO@B? 7E>Q FC TB @LRIA FKPQB>A P>V:
)K LQEBO TLOAP, FKPQB>A LC TBFDEQFKD >KA >@@RJRI>QFKD QEB CLO@BP FKPFAB LRO SBEF@IB, TB
PFJMIV M>PP >K >OO>V LC CLO@BP QL QEB SBEF@IBfP g?O>FKh L?GB@Q >KA >IILT QEB ?O>FK QL TBFDEQ
>KA PRJ QEB CLO@BP CLO RP. 4EB LRQMRQ FP QEBK >MMIFBA >P > PQBBOFKD CLO@B. 4EFP LMBKP RM >
O>KDB LC MLPPF?FIFQFBP. ! SBEF@IB @LRIA J>HB AB@FPFLKP >P QL ELT QL PQBBO LK FQP LTK, IB>OKFKD
COLJ FQP JFPQ>HBP >KA OBPMLKAFKD QL PQFJRIF FK FQP BKSFOLKJBKQ. ,BQfP PBB ELT QEFP TLOHP.
7B @>K RPB QEB IFKB @I>PPFCF@>QFLK MBO@BMQOLK >P > JLABI, TFQE LKB FJMLOQ>KQ AFCCBOBK@BdQEB
FKMRQP >OB KLQ PFKDIB KRJ?BOP, ?RQ SB@QLOP! ,BQfP ILLH >Q ELT QEB @??>@ILQ;L>() CRK@QFLK
TLOHP FK LRO SBEF@IBfP MBO@BMQOLK, >ILKDPFAB QEB LKB COLJ LRO MOBSFLRP BU>JMIB.
Vehicle PVector inputs Vehicle PVector inputs Line float inputs Line float inputs
.3?=NIL @??>@ILQ;L>(.3?=NIL89 @IL=?M) U
// 0OG CM ; .3?=NIL.
+0;9JEH IKC = D;M +0;9JEH();
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
// 3?=NIL ;>>CNCIH ;H> GOFNCJFC=;NCIH
<EH9;I4?5.CKBJ(M;?=>JI4?5);
IKC.7::(<EH9;I4?5);
W
// ,I ;=NCP;NCIH @OH=NCIH
H;JKHD IKC;
W
CHN @??>@ILQ;L>(@FI;N89 CHJONM) U
// 0OG CM ; @FI;N.
<BE7J IKC = 0;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
// 0=;F;L ;>>CNCIH ;H> GOFNCJFC=;NCIH
IKC += ?DFKJI4?5*M;?=>JI4?5;
W
// A=NCP;NCIH @OH=NCIH
H;JKHD 79J?L7J;(IKC);
W
Weighting each steering force individually @IL=?.GOFN(Q?CABN);
;JJFS$IL=?(@IL=?);
W
W
PIC> M??E(ALL;S*CMN<.3?=NIL> N;LA?NM) U
Make an array of inputs for our brain. .3?=NIL89 @IL=?M = H?Q
.3?=NIL8N;LA?NM.MCT?()9;
@IL (CHN C = 0; C < @IL=?M.F?HANB; C++) U
Fill the array with a steering force for each
target.
@IL=?M8C9 = M??E(N;LA?NM.A?N(C));
W
Ask our brain for a result and apply that as
the force!
.3?=NIL IONJON = <L;CH.JLI=?MM(@IL=?M);
;JJFS$IL=?(IONJON);
W
#E>MQBO 10. .BRO>I .BQTLOHP
462
.LQB ELT QEBPB QTL CRK@QFLKP FJMIBJBKQ KB>OIV FABKQF@>I >IDLOFQEJP, TFQE QTL AFCCBOBK@BP:
1. '@88492 %*0.?:=>. '@88492 %*0.?:=>. )KPQB>A LC > PBOFBP LC KRJ?BOP >AABA QLDBQEBO, B>@E FKMRQ FP >
.3?=NIL >KA JRPQ ?B JRIQFMIFBA ?V QEB TBFDEQ >KA >AABA QL > PRJ >@@LOAFKD QL QEB
J>QEBJ>QF@>I .3?=NIL CRK@QFLKP.
2. #: ,.?4A,?4:9 1@9.?4:9. #: ,.?4A,?4:9 1@9.?4:9. )K QEFP @>PB, TBfOB Q>HFKD QEB OBPRIQ >KA >MMIVFKD FQ AFOB@QIV
>P > PQBBOFKD CLO@B CLO QEB SBEF@IB, PL TBfOB KLQ >PHFKD CLO > PFJMIB ?LLIB>K S>IRB
QE>Q @I>PPFCFBP FQ FK LKB LC QTL @>QBDLOFBP. 2>QEBO, TBfOB >PHFKD CLO O>T LRQMRQ FQPBIC,
QEB OBPRIQFKD LSBO>II CLO@B.
/K@B QEB OBPRIQFKD PQBBOFKD CLO@B E>P ?BBK >MMIFBA, FQfP QFJB QL DFSB CBBA?>@H QL QEB ?O>FK, F.B.
F9=B:CF79A9BH @95FB=B;. 7>P QEB AB@FPFLK QL PQBBO FK QE>Q M>OQF@RI>O AFOB@QFLK > DLLA LKB LO >
?>A LKB? 0OBPRJ>?IV FC PLJB LC QEB Q>ODBQP TBOB MOBA>QLOP (OBPRIQFKD FK ?BFKD B>QBK) >KA
PLJB LC QEB Q>ODBQP TBOB CLLA (OBPRIQFKD FK DOB>QBO EB>IQE), QEB KBQTLOH TLRIA >AGRPQ FQP
TBFDEQP FK LOABO QL PQBBO >T>V COLJ QEB MOBA>QLOP >KA QLT>OAP QEB CLLA.
,BQfP Q>HB > PFJMIBO BU>JMIB, TEBOB QEB SBEF@IB PFJMIV T>KQP QL PQ>V @ILPB QL QEB @BKQBO LC QEB
TFKALT. 7BfII QO>FK QEB ?O>FK >P CLIILTP:
(BOB TB >OB M>PPFKD QEB ?O>FK > @LMV LC >II
QEB FKMRQP (TEF@E FQ TFII KBBA CLO BOOLO
@LOOB@QFLK) >P TBII >P >K L?PBOS>QFLK >?LRQ
FQP BKSFOLKJBKQ: > .3?=NIL QE>Q MLFKQP COLJ
FQP @ROOBKQ IL@>QFLK QL TEBOB FQ ABPFOBP QL ?B.
4EFP .3?=NIL BPPBKQF>IIV PBOSBP >P QEB
BOOLOdQEB ILKDBO QEB .3?=NIL, QEB TLOPB QEB SBEF@IB FP MBOCLOJFKD; QEB PELOQBO, QEB ?BQQBO.
4EB ?O>FK @>K QEBK >MMIV QEFP gBOOLOh SB@QLO (TEF@E E>P QTL BOOLO S>IRBP, LKB CLO R >KA LKB CLO
S) >P > JB>KP CLO >AGRPQFKD QEB TBFDEQP, GRPQ >P TB AFA FK QEB IFKB @I>PPFCF@>QFLK BU>JMIB.
Training the Vehicle Training the Vehicle Training the Line Classifier Training the Line Classifier
PIC> NL;CH(.3?=NIL89 @IL=?M, .3?=NIL ?LLIL) U
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += =*?LLIL.R*@IL=?M8C9.R;
Q?CABNM8C9 += =*?LLIL.S*@IL=?M8C9.S;
W
W
PIC> NL;CH(@FI;N89 CHJONM, CHN >?MCL?>) U
CHN AO?MM = @??>@ILQ;L>(CHJONM);
@FI;N ?LLIL = >?MCL?> - AO?MM;
@IL (CHN C = 0; C < Q?CABNM.F?HANB; C++) U
Q?CABNM8C9 += = * ?LLIL * CHJONM8C9;
W
W
.3?=NIL >?MCL?> = H?Q .3?=NIL(QC>NB/2,B?CABN/2);
.3?=NIL ?LLIL = .3?=NIL.MO<(>?MCL?>, FI=;NCIH);
<L;CH.NL;CH(@IL=?M,?LLIL);
F64B?2 10.10
4EB .>QROB LC #LAB (S005)
463
"B@>RPB QEB SBEF@IB L?PBOSBP FQP LTK BOOLO, QEBOB FP KL KBBA QL @>I@RI>QB LKB; TB @>K
PFJMIV OB@BFSB QEB BOOLO >P >K >ODRJBKQ. .LQF@B ELT QEB @E>KDB FK TBFDEQ FP MOL@BPPBA
QTF@B, LK@B CLO QEB BOOLO >ILKD QEB U->UFP >KA LK@B CLO QEB V->UFP.
7B @>K KLT ILLH >Q QEB 3?BC=F? @I>PP >KA PBB ELT QEB MN??L CRK@QFLK RPBP > MBO@BMQOLK QL
@LKQOLI QEB LSBO>II PQBBOFKD CLO@B. 4EB KBT @LKQBKQ COLJ QEFP @E>MQBO FP EFDEIFDEQBA.
Example 10.2: Perceptron steering
Q?CABNM8C9 += =*?LLIL.R*@IL=?M8C9.R;
Q?CABNM8C9 += =*?LLIL.S*@IL=?M8C9.S;
=F;MM 3?BC=F? U
The Vehicle now has a brain. +;H9;FJHED 8H7?D;
Same old variables for physics .3?=NIL FI=;NCIH;
.3?=NIL P?FI=CNS;
.3?=NIL ;==?F?L;NCIH;
@FI;N G;R@IL=?;
@FI;N G;RMJ??>;
The Vehicle creates a perceptron with n
inputs and a learning constant.
3?BC=F?(CHN H, @FI;N R, @FI;N S) U
8H7?D = D;M +;H9;FJHED(D,0.001);
;==?F?L;NCIH = H?Q .3?=NIL(0,0);
P?FI=CNS = H?Q .3?=NIL(0,0);
FI=;NCIH = H?Q .3?=NIL(R,S);
G;RMJ??> = 4;
G;R@IL=? = 0.1;
W
#E>MQBO 10. .BRO>I .BQTLOHP
464
Same old update() function PIC> OJ>;N?() U
P?FI=CNS.;>>(;==?F?L;NCIH);
P?FI=CNS.FCGCN(G;RMJ??>);
FI=;NCIH.;>>(P?FI=CNS);
;==?F?L;NCIH.GOFN(0);
W
Same old applyForce() function PIC> ;JJFS$IL=?(.3?=NIL @IL=?) U
;==?F?L;NCIH.;>>(@IL=?);
W
PIC> MN??L(ALL;S*CMN<.3?=NIL> N;LA?NM) U
.3?=NIL89 @IL=?M = H?Q .3?=NIL8N;LA?NM.MCT?()9;
@IL (CHN C = 0; C < @IL=?M.F?HANB; C++) U
@IL=?M8C9 = M??E(N;LA?NM.A?N(C));
W
All the steering forces are inputs. +0;9JEH H;IKBJ = 8H7?D.<;;:<EHM7H:(<EH9;I);
The result is applied. ;JJFS$IL=?(L?MOFN);
+0;9JEH :;I?H;: = D;M +0;9JEH(M?:J>/2,>;?=>J/2);
+0;9JEH ;HHEH = +0;9JEH.IK8(:;I?H;:, BE97J?ED);
8H7?D.JH7?D(<EH9;I,;HHEH);
W
The brain is trained according to the
distance to the center.
Same old seek() function .3?=NIL M??E(.3?=NIL N;LA?N) U
.3?=NIL >?MCL?> = .3?=NIL.MO<(N;LA?N,FI=;NCIH);
>?MCL?>.HILG;FCT?();
>?MCL?>.GOFN(G;RMJ??>);
.3?=NIL MN??L = .3?=NIL.MO<(>?MCL?>,P?FI=CNS);
MN??L.FCGCN(G;R@IL=?);
L?NOLH MN??L;
W
W
4EB .>QROB LC #LAB (S005)
465
6FPR>IFWB QEB TBFDEQP LC QEB KBQTLOH. 4OV J>MMFKD B>@E Q>ODBQfP @LOOBPMLKAFKD
TBFDEQ QL FQP ?OFDEQKBPP.
Exercise 10.3 Exercise 10.3
4OV AFCCBOBKQ ORIBP CLO OBFKCLO@BJBKQ IB>OKFKD. 7E>Q FC PLJB Q>ODBQP >OB ABPFO>?IB >KA
PLJB >OB RKABPFO>?IB?
Exercise 10.4 Exercise 10.4
10.6 Its a Network, Remember? 10.6 Its a Network, Remember?
9BP, > MBO@BMQOLK @>K E>SB JRIQFMIB FKMRQP, ?RQ FQ FP PQFII > ILKBIV KBROLK. 4EB MLTBO LC
KBRO>I KBQTLOHP @LJBP FK QEB KBQTLOHFKD FQPBIC. 0BO@BMQOLKP >OB, P>AIV, FK@OBAF?IV IFJFQBA FK
QEBFO >?FIFQFBP. )C VLR OB>A >K !) QBUQ?LLH, FQ TFII P>V QE>Q > MBO@BMQOLK @>K LKIV PLISB 7490,=7D 7490,=7D
>0;,=,-70 >0;,=,-70 MOL?IBJP. 7E>QfP > IFKB>OIV PBM>O>?IB MOL?IBJ? ,BQfP Q>HB > ILLH >Q LRO CFOPQ
BU>JMIB, TEF@E ABQBOJFKBA TEBQEBO MLFKQP TBOB LK LKB PFAB LC > IFKB LO QEB LQEBO.
/K QEB IBCQ LC &FDROB 10.11, TB E>SB @I>PPF@ IFKB>OIV PBM>O>?IB A>Q>. 'O>ME >II LC QEB
MLPPF?FIFQFBP; FC VLR @>K @I>PPFCV QEB A>Q> TFQE > PQO>FDEQ IFKB, QEBK FQ FP IFKB>OIV PBM>O>?IB. /K
QEB OFDEQ, ELTBSBO, FP KLK-IFKB>OIV PBM>O>?IB A>Q>. 9LR @>KfQ AO>T > PQO>FDEQ IFKB QL PBM>O>QB
QEB ?I>@H ALQP COLJ QEB DO>V LKBP.
/KB LC QEB PFJMIBPQ BU>JMIBP LC > KLK-IFKB>OIV PBM>O>?IB MOL?IBJ FP 3+-, LO gBU@IRPFSB LO.h
7BfOB >II C>JFIF>O TFQE A*. &LO A A* B QL ?B QORB, ?LQE A >KA B JRPQ ?B QORB. 7FQE +-,
BFQEBO A LO B @>K ?B QORB CLO A +- B QL BS>IR>QB >P QORB. 4EBPB >OB ?LQE IFKB>OIV PBM>O>?IB
MOL?IBJP. ,BQfP ILLH >Q QEB PLIRQFLK PM>@B, > gQORQE Q>?IB.h
F64B?2 10.11
#E>MQBO 10. .BRO>I .BQTLOHP
466
3BB ELT VLR @>K AO>T > IFKB QL PBM>O>QB QEB QORB LRQMRQP COLJ QEB C>IPB LKBP?
3+- FP QEB BNRFS>IBKQ LC +- >KA *+/ A*. )K LQEBO TLOAP, A 3+- B LKIV BS>IR>QBP QL QORB FC
LKB LC QEBJ FP QORB. )C ?LQE >OB C>IPB LO ?LQE >OB QORB, QEBK TB DBQ C>IPB. 4>HB > ILLH >Q QEB
CLIILTFKD QORQE Q>?IB.
4EFP FP KLQ IFKB>OIV PBM>O>?IB. 4OV QL AO>T > PQO>FDEQ IFKB QL PBM>O>QB QEB QORB LRQMRQP COLJ QEB
C>IPB LKBPdVLR @>KfQ!
3L MBO@BMQOLKP @>KfQ BSBK PLISB PLJBQEFKD >P PFJMIB >P 3+-. "RQ TE>Q FC TB J>AB > KBQTLOH
LRQ LC QTL MBO@BMQOLKP? )C LKB MBO@BMQOLK @>K PLISB +- >KA LKB MBO@BMQOLK @>K PLISB *+/
A*, QEBK QTL MBO@BMQOLKP @LJ?FKBA @>K PLISB 3+-.
F64B?2 10.12
F64B?2 10.13
4EB .>QROB LC #LAB (S005)
467
4EB >?LSB AF>DO>J FP HKLTK >P > AI@H=-@5M9F98 D9F79DHFCB, > KBQTLOH LC J>KV KBROLKP.
3LJB >OB FKMRQ KBROLKP >KA OB@BFSB QEB FKMRQP, PLJB >OB M>OQ LC TE>QfP @>IIBA > gEFAABKh
I>VBO (>P QEBV >OB @LKKB@QBA QL KBFQEBO QEB FKMRQP KLO QEB LRQMRQP LC QEB KBQTLOH AFOB@QIV),
>KA QEBK QEBOB >OB QEB LRQMRQ KBROLKP, COLJ TEF@E TB OB>A QEB OBPRIQP.
4O>FKFKD QEBPB KBQTLOHP FP JR@E JLOB @LJMIF@>QBA. 7FQE QEB PFJMIB MBO@BMQOLK, TB @LRIA
B>PFIV BS>IR>QB ELT QL @E>KDB QEB TBFDEQP >@@LOAFKD QL QEB BOOLO. "RQ EBOB QEBOB >OB PL
J>KV AFCCBOBKQ @LKKB@QFLKP, B>@E FK > AFCCBOBKQ I>VBO LC QEB KBQTLOH. (LT ALBP LKB HKLT
ELT JR@E B>@E KBROLK LO @LKKB@QFLK @LKQOF?RQBA QL QEB LSBO>II BOOLO LC QEB KBQTLOH?
4EB PLIRQFLK QL LMQFJFWFKD TBFDEQP LC > JRIQF-I>VBOBA KBQTLOH FP HKLTK >P -,.6;=:;,2,?4:9 -,.6;=:;,2,?4:9.
4EB LRQMRQ LC QEB KBQTLOH FP DBKBO>QBA FK QEB P>JB J>KKBO >P > MBO@BMQOLK. 4EB FKMRQP
JRIQFMIFBA ?V QEB TBFDEQP >OB PRJJBA >KA CBA CLOT>OA QEOLRDE QEB KBQTLOH. 4EB AFCCBOBK@B
EBOB FP QE>Q QEBV M>PP QEOLRDE >AAFQFLK>I I>VBOP LC KBROLKP ?BCLOB OB>@EFKD QEB LRQMRQ.
4O>FKFKD QEB KBQTLOH (F.B. >AGRPQFKD QEB TBFDEQP) >IPL FKSLISBP Q>HFKD QEB BOOLO (ABPFOBA OBPRIQ
- DRBPP). 4EB BOOLO, ELTBSBO, JRPQ ?B CBA ?>@HT>OAP QEOLRDE QEB KBQTLOH. 4EB CFK>I BOOLO
RIQFJ>QBIV >AGRPQP QEB TBFDEQP LC >II QEB @LKKB@QFLKP.
">@HMOLM>D>QFLK FP > ?FQ ?BVLKA QEB P@LMB LC QEFP ?LLH >KA FKSLISBP > C>K@FBO >@QFS>QFLK
CRK@QFLK (@>IIBA QEB PFDJLFA CRK@QFLK) >P TBII >P PLJB ?>PF@ @>I@RIRP. )C VLR >OB FKQBOBPQBA FK
ELT ?>@HMOLM>D>QFLK TLOHP, @EB@H QEB ?LLH TB?PFQB (>KA 'FQ(R? OBMLPFQLOV) CLO >K
BU>JMIB QE>Q PLISBP 3+- RPFKD > JRIQF-I>VBOBA CBBA CLOT>OA KBQTLOH TFQE ?>@HMOLM>D>QFLK.
)KPQB>A, EBOB TBfII CL@RP LK > @LAB CO>JBTLOH CLO ?RFIAFKD QEB SFPR>I >O@EFQB@QROB LC >
KBQTLOH. 7BfII J>HB ,?OLIH L?GB@QP >KA !IHH?=NCIH L?GB@QP COLJ TEF@E > ,?NQILE L?GB@Q
@>K ?B @OB>QBA >KA >KFJ>QBA QL PELT QEB CBBA CLOT>OA MOL@BPP. 4EFP TFII @ILPBIV OBPBJ?IB
PLJB LC QEB CLO@B-AFOB@QBA DO>ME BU>JMIBP TB BU>JFKBA FK #E>MQBO 5 (QLUF@IF?P).
F64B?2 10.14
10.7 Neural Network Diagram 10.7 Neural Network Diagram
/RO DL>I TFII ?B QL @OB>QB QEB CLIILTFKD PFJMIB KBQTLOH AF>DO>J:
#E>MQBO 10. .BRO>I .BQTLOHP
468
4EB MOFJ>OV ?RFIAFKD ?IL@H CLO QEFP AF>DO>J FP > KBROLK. &LO QEB MROMLPB LC QEFP BU>JMIB, QEB
,?OLIH @I>PP ABP@OF?BP >K BKQFQV TFQE >K (L,M) IL@>QFLK.
4EB ,?NQILE @I>PP @>K QEBK J>K>DB >K ALL;S*CMN LC KBROLKP, >P TBII >P E>SB FQP LTK
IL@>QFLK (PL QE>Q B>@E KBROLK FP AO>TK OBI>QFSB QL QEB KBQTLOHfP @BKQBO). 4EFP FP M>OQF@IB
PVPQBJP 101. 7B E>SB > PFKDIB BIBJBKQ (> KBROLK) >KA > KBQTLOH (> gPVPQBJh LC J>KV
KBROLKP).
F64B?2 10.15
An incredibly simple Neuron class stores
and displays the location of a single neuron.
=F;MM ,?OLIH U
.3?=NIL FI=;NCIH;
,?OLIH(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R, S);
W
PIC> >CMJF;S() U
MNLIE?(0);
@CFF(0);
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, 16, 16);
W
W
A Network is a list of neurons. =F;MM ,?NQILE U
ALL;S*CMN<,?OLIH> H?OLIHM;
.3?=NIL FI=;NCIH;
,?NQILE(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R,S);
H?OLIHM = H?Q ALL;S*CMN<,?OLIH>();
W
We can add an neuron to the network. PIC> ;>>,?OLIH(,?OLIH H) U
H?OLIHM.;>>(H);
W
4EB .>QROB LC #LAB (S005)
469
.LT TB @>K MOBQQV B>PFIV J>HB QEB AF>DO>J >?LSB.
4EB >?LSB VFBIAP:
We can draw the entire network. PIC> >CMJF;S() U
JOMB+;NLCR();
NL;HMF;N?(FI=;NCIH.R, FI=;NCIH.S);
@IL (,?OLIH H : H?OLIHM) U
H.>CMJF;S();
W
JIJ+;NLCR();
W
W
,?NQILE H?NQILE;
PIC> M?NOJ() U
MCT?(640, 360);
Make a Network. H?NQILE = H?Q ,?NQILE(QC>NB/2,B?CABN/2);
Make the Neurons. ,?OLIH ; = H?Q ,?OLIH(-200,0);
,?OLIH < = H?Q ,?OLIH(0,100);
,?OLIH = = H?Q ,?OLIH(0,-100);
,?OLIH > = H?Q ,?OLIH(200,0);
Add the Neurons to the network. H?NQILE.;>>,?OLIH(;);
H?NQILE.;>>,?OLIH(<);
H?NQILE.;>>,?OLIH(=);
H?NQILE.;>>,?OLIH(>);
W
PIC> >L;Q() U
<;=EALIOH>(255);
Show the network. H?NQILE.>CMJF;S();
W
#E>MQBO 10. .BRO>I .BQTLOHP
470
7E>QfP JFPPFKD, LC @LROPB, FP QEB @LKKB@QFLK. 7B @>K @LKPFABO > !IHH?=NCIH L?GB@Q QL ?B
J>AB RM LC QEOBB BIBJBKQP, QTL KBROLKP (COLJ ,?OLIH ; QL ,?OLIH <) >KA > Q?CABN.
/K@B TB E>SB QEB FAB> LC > !IHH?=NCIH L?GB@Q, TB @>K TOFQB > CRK@QFLK (IBQfP MRQ FQ FKPFAB QEB
,?NQILE @I>PP) QE>Q @LKKB@QP QTL KBROLKP QLDBQEBOdQEB DL>I ?BFKD QE>Q FK >AAFQFLK QL J>HFKD
QEB KBROLKP FK M?NOJ(), TB @>K >IPL @LKKB@Q QEBJ.
4EB ,?NQILE @I>PP QEBOBCLOB KBBAP > KBT CRK@QFLK @>IIBA =IHH?=N(), TEF@E J>HBP >
!IHH?=NCIH L?GB@Q ?BQTBBK QEB QTL PMB@FCFBA KBROLKP.
=F;MM !IHH?=NCIH U
A connection is between two neurons. ,?OLIH ;;
,?OLIH <;
A connection has a weight. @FI;N Q?CABN;
!IHH?=NCIH(,?OLIH @LIG, ,?OLIH NI,@FI;N Q) U
Q?CABN = Q;
; = @LIG;
< = NI;
W
A connection is drawn as a line. PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(Q?CABN*4);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
W
W
PIC> M?NOJ() U
MCT?(640, 360);
H?NQILE = H?Q ,?NQILE(QC>NB/2,B?CABN/2);
,?OLIH ; = H?Q ,?OLIH(-200,0);
,?OLIH < = H?Q ,?OLIH(0,100);
,?OLIH = = H?Q ,?OLIH(0,-100);
,?OLIH > = H?Q ,?OLIH(200,0);
Making connections between the neurons H?NQILE.=IHH?=N(;,<);
H?NQILE.=IHH?=N(;,=);
H?NQILE.=IHH?=N(<,>);
H?NQILE.=IHH?=N(=,>);
H?NQILE.;>>,?OLIH(;);
H?NQILE.;>>,?OLIH(<);
H?NQILE.;>>,?OLIH(=);
H?NQILE.;>>,?OLIH(>);
W
4EB .>QROB LC #LAB (S005)
471
0OBPRJ>?IV, TB JFDEQ QEFKH QE>Q QEB ,?NQILE PELRIA PQLOB >K ALL;S*CMN LC @LKKB@QFLKP,
GRPQ IFHB FQ PQLOBP >K ALL;S*CMN LC KBROLKP. 7EFIB RPBCRI, FK QEFP @>PB PR@E >K ALL;S*CMN FP
KLQ KB@BPP>OV >KA FP JFPPFKD >K FJMLOQ>KQ CB>QROB QE>Q TB KBBA. 5IQFJ>QBIV TB MI>K QL gCBBA
CLOT>OA" QEB KBROLKP QEOLRDE QEB KBQTLOH, PL QEB ,?OLIH L?GB@QP QEBJPBISBP JRPQ HKLT QL
TEF@E KBROLKP QEBV >OB @LKKB@QBA FK QEB gCLOT>OAh AFOB@QFLK. )K LQEBO TLOAP, B>@E KBROLK
PELRIA E>SB FQP LTK IFPQ LC !IHH?=NCIH L?GB@QP. 7EBK ; @LKKB@QP QL <, TB T>KQ ; QL PQLOB >
OBCBOBK@B LC QE>Q @LKKB@QFLK PL QE>Q FQ @>K M>PP FQP LRQMRQ QL < TEBK QEB QFJB @LJBP.
)K PLJB @>PBP, TB >IPL JFDEQ T>KQ ,?OLIH < QL HKLT >?LRQ QEFP @LKKB@QFLK, ?RQ FK QEFP
M>OQF@RI>O BU>JMIB TB >OB LKIV DLFKD QL M>PP FKCLOJ>QFLK FK LKB AFOB@QFLK.
&LO QEFP QL TLOH, TB E>SB QL >AA >K ALL;S*CMN LC @LKKB@QFLKP QL QEB ,?OLIH @I>PP. 4EBK TB
FJMIBJBKQ QEB ;>>!IHH?=NCIH() CRK@QFLK QE>Q PQLOBP QEB @LKKB@QFLK FK QE>Q ALL;S*CMN.
4EB KBROLKfP >CMJF;S() CRK@QFLK @>K AO>T QEB @LKKB@QFLKP >P TBII. !KA CFK>IIV, TB E>SB
LRO KBQTLOH AF>DO>J.
PIC> =IHH?=N(,?OLIH ;, ,?OLIH <) U
Connection has a random weight. !IHH?=NCIH = = H?Q !IHH?=NCIH(;, <,
L;H>IG(1));
// ON QB;N >I Q? >I QCNB NB? !IHH?=NCIH I<D?=N?
W
PIC> =IHH?=N(,?OLIH ;, ,?OLIH <) U
!IHH?=NCIH = = H?Q !IHH?=NCIH(;, <, L;H>IG(1));
;.;>>!IHH?=NCIH(=);
W
=F;MM ,?OLIH U
.3?=NIL FI=;NCIH;
The neuron stores its connections. ALL;S*CMN<!IHH?=NCIH> =IHH?=NCIHM;
,?OLIH(@FI;N R, @FI;N S) U
FI=;NCIH = H?Q .3?=NIL(R, S);
=IHH?=NCIHM = H?Q ALL;S*CMN<!IHH?=NCIH>();
W
Adding a connection to this neuron PIC> ;>>!IHH?=NCIH(!IHH?=NCIH =) U
=IHH?=NCIHM.;>>(=);
W
#E>MQBO 10. .BRO>I .BQTLOHP
472
Example 10.3: Neural network diagram
PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(1);
@CFF(0);
?FFCJM?(FI=;NCIH.R, FI=;NCIH.S, 16, 16);
Drawing all the connections @IL (!IHH?=NCIH = : =IHH?=NCIHM) U
=.>CMJF;S();
W
W
W
10.8 Animating Feed Forward 10.8 Animating Feed Forward
!K FKQBOBPQFKD MOL?IBJ QL @LKPFABO FP ELT QL SFPR>IFWB QEB CILT LC FKCLOJ>QFLK >P FQ QO>SBIP
QEOLRDELRQ > KBRO>I KBQTLOH. /RO KBQTLOH FP ?RFIQ LK QEB CBBA CLOT>OA JLABI, JB>KFKD QE>Q >K
FKMRQ >OOFSBP >Q QEB CFOPQ KBROLK (AO>TK LK QEB IBCQE>KA PFAB LC QEB TFKALT) >KA QEB LRQMRQ LC
QE>Q KBROLK CILTP >@OLPP QEB @LKKB@QFLKP QL QEB OFDEQ RKQFI FQ BUFQP >P LRQMRQ COLJ QEB KBQTLOH
FQPBIC.
/RO CFOPQ PQBM FP QL >AA > CRK@QFLK QL QEB KBQTLOH QL OB@BFSB QEFP FKMRQ, TEF@E TBfII J>HB >
O>KALJ KRJ?BO ?BQTBBK 0 >KA 1.
PIC> M?NOJ() U
All our old network set up code
A new function to send in an input H?NQILE.@??>@ILQ;L>(L;H>IG(1));
W
4EB .>QROB LC #LAB (S005)
473
4EB KBQTLOH, TEF@E J>K>DBP >II QEB KBROLKP, @>K @ELLPB QL TEF@E KBROLKP FQ PELRIA >MMIV
QE>Q FKMRQ. )K QEFP @>PB, TBfII AL PLJBQEFKD PFJMIB >KA GRPQ CBBA > PFKDIB FKMRQ FKQL QEB CFOPQ
KBROLK FK QEB ALL;S*CMN, TEF@E E>MMBKP QL ?B QEB IBCQ-JLPQ LKB.
7E>Q AFA TB AL? 7BII, TB J>AB FQ KB@BPP>OV QL >AA > CRK@QFLK @>IIBA @??>@ILQ;L>() FK QEB
,?OLIH @I>PP QE>Q TFII OB@BFSB QEB FKMRQ >KA MOL@BPP FQ.
)C VLR OB@>II COLJ TLOHFKD TFQE LRO MBO@BMQOLK, QEB PQ>KA>OA Q>PH QE>Q QEB MOL@BPPFKD RKFQ
MBOCLOJP FP QL PRJ RM >II LC FQP FKMRQP. 3L FC LRO ,?OLIH @I>PP >AAP > S>OF>?IB @>IIBA MOG, FQ
@>K PFJMIV >@@RJRI>QB QEB FKMRQP >P QEBV >OB OB@BFSBA.
4EB KBROLK @>K QEBK AB@FAB TEBQEBO FQ PELRIA gCFOB,h LO M>PP >K LRQMRQ QEOLRDE >KV LC FQP
@LKKB@QFLKP QL QEB KBUQ I>VBO FK QEB KBQTLOH. (BOB TB @>K @OB>QB > OB>IIV PFJMIB >@QFS>QFLK
CRK@QFLK: FC QEB PRJ FP DOB>QBO QE>K 1, CFOB!
=F;MM ,?NQILE U
A new function to feed an input into the
neuron
PIC> @??>@ILQ;L>(@FI;N CHJON) U
,?OLIH MN;LN = H?OLIHM.A?N(0);
MN;LN.@??>@ILQ;L>(CHJON);
W
=F;MM ,?OLIH
PIC> @??>@ILQ;L>(@FI;N CHJON) U
What do we do with the input?
W
=F;MM ,?OLIH
?DJ IKC = 0;
PIC> @??>@ILQ;L>(@FI;N CHJON) U
Accumulate the sums. IKC += ?DFKJ;
W
PIC> @??>@ILQ;L>(@FI;N CHJON) U
MOG += CHJON;
Activate the neuron and fire the outputs? C@ (MOG > 1) U
@CL?();
If weve fired off our output, we can reset
our sum to 0.
MOG = 0;
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
474
.LT, TE>Q AL TB AL FK QEB @CL?() CRK@QFLK? )C VLR OB@>II, B>@E KBROLK HBBMP QO>@H LC FQP
@LKKB@QFLKP QL LQEBO KBROLKP. 3L >II TB KBBA QL AL FP ILLM QEOLRDE QELPB @LKKB@QFLKP >KA
@??>@ILQ;L>() QEB KBROLKfP LRQMRQ. &LO QEFP PFJMIB BU>JMIB, TBfII GRPQ Q>HB QEB KBROLKfP MOG
S>OF>?IB >KA J>HB FQ QEB LRQMRQ.
(BOBfP TEBOB QEFKDP DBQ > IFQQIB QOF@HV. !CQBO >II, LRO GL? EBOB FP KLQ QL >@QR>IIV J>HB >
CRK@QFLKFKD KBRO>I KBQTLOH, ?RQ QL >KFJ>QB > PFJRI>QFLK LC LKB. )C QEB KBRO>I KBQTLOH TBOB GRPQ
@LKQFKRFKD FQP TLOH, FQ TLRIA FKPQ>KQIV M>PP QELPB FKMRQP (JRIQFMIFBA ?V QEB @LKKB@QFLKfP TBFDEQ)
>ILKD QL QEB @LKKB@QBA KBROLKP. 7BfA P>V PLJBQEFKD IFHB:
"RQ QEFP FP KLQ TE>Q TB T>KQ. 7E>Q TB T>KQ QL AL FP AO>T PLJBQEFKD QE>Q TB @>K PBB QO>SBIFKD
>ILKD QEB @LKKB@QFLK COLJ ,?OLIH ; QL ,?OLIH <.
,BQfP CFOPQ QEFKH >?LRQ ELT TB JFDEQ AL QE>Q. 7B HKLT QEB IL@>QFLK LC ,?OLIH ;; FQfP QEB
.3?=NIL ;.FI=;NCIH. ,?OLIH < FP IL@>QBA >Q <.FI=;NCIH. 7B KBBA QL PQ>OQ PLJBQEFKD JLSFKD
COLJ ,?OLIH ; ?V @OB>QFKD >KLQEBO .3?=NIL QE>Q TFII PQLOB QEB M>QE LC LRO QO>SBIFKD A>Q>.
/K@B TB E>SB > @LMV LC QE>Q IL@>QFLK, TB @>K RPB >KV LC QEB JLQFLK >IDLOFQEJP QE>Q TBfSB
PQRAFBA QEOLRDELRQ QEFP ?LLH QL JLSB >ILKD QEFP M>QE. (BOBdIBQfP MF@H PLJBQEFKD SBOV PFJMIB
>KA GRPQ FKQBOMLI>QB COLJ ; QL <.
!ILKD TFQE QEB @LKKB@QFLKfP IFKB, TB @>K QEBK AO>T > @FO@IB >Q QE>Q IL@>QFLK:
4EFP OBPBJ?IBP QEB CLIILTFKD:
PIC> @CL?() U
@IL (!IHH?=NCIH = : =IHH?=NCIHM) U
The Neuron sends the sum out through all
of its connections
=.@??>@ILQ;L>(MOG);
W
W
=F;MM !IHH?=NCIH U
PIC> @??>@ILQ;L>(@FI;N P;F) U
<.@??>@ILQ;L>(P;F*Q?CABN);
W
.3?=NIL M?H>?L = ;.FI=;NCIH.A?N();
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
MNLIE?(0);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
@CFF(0);
;BB?FI;(I;D:;H.N, I;D:;H.O, 8, 8);
4EB .>QROB LC #LAB (S005)
475
/+, PL QE>QfP ELT TB JFDEQ JLSB PLJBQEFKD >ILKD QEB @LKKB@QFLK. "RQ ELT AL TB HKLT
TEBK QL AL PL? 7B PQ>OQ QEFP MOL@BPP QEB JLJBKQ QEB !IHH?=NCIH L?GB@Q OB@BFSBP QEB
gCBBACLOT>OAh PFDK>I. 7B @>K HBBM QO>@H LC QEFP MOL@BPP ?V BJMILVFKD > PFJMIB <IIF?;H QL
HKLT TEBQEBO QEB @LKKB@QFLK FP PBKAFKD LO KLQ. "BCLOB, TB E>A:
.LT, FKPQB>A LC PBKAFKD QEB S>IRB LK PQO>FDEQ >T>V, TBfII QOFDDBO >K >KFJ>QFLK:
.LQF@B ELT LRO !IHH?=NCIH @I>PP KLT KBBAP QEOBB KBT S>OF>?IBP. 7B KBBA > <IIF?;H
gPBKAFKDh QE>Q PQ>OQP >P C>IPB >KA QE>Q TFII QO>@H TEBQEBO LO KLQ QEB @LKKB@QFLK FP >@QFSBIV
PBKAFKD (F.B. >KFJ>QFKD). 7B KBBA > .3?=NIL gPBKABOh CLO QEB IL@>QFLK TEBOB TBfII AO>T QEB
QO>SBIFKD ALQ. !KA PFK@B TB >OBKfQ M>PPFKD QEB LRQMRQ >ILKD QEFP FKPQ>KQ, TBfII KBBA QL PQLOB FQ
FK > S>OF>?IB QE>Q TFII AL QEB GL? I>QBO.
4EB @??>@ILQ;L>() CRK@QFLK FP @>IIBA QEB JLJBKQ QEB @LKKB@QFLK ?B@LJBP >@QFSB. /K@B FQfP
>@QFSB, TBfII KBBA QL @>II >KLQEBO CRK@QFLK @LKQFKRLRPIV (B>@E QFJB QEOLRDE >L;Q()), LKB QE>Q
TFII RMA>QB QEB IL@>QFLK LC QEB QO>SBIFKD A>Q>.
F64B?2 10.16
PIC> @??>@ILQ;L>(@FI;N P;F) U
<.@??>@ILQ;L>(P;F*Q?CABN);
W
=F;MM !IHH?=NCIH U
<IIF?;H M?H>CHA = @;FM?;
.3?=NIL M?H>?L;
@FI;N IONJON;
PIC> @??>@ILQ;L>(@FI;N P;F) U
Sending is now true. M?H>CHA = NLO?;
Start the animation at the location of
Neuron A.
M?H>?L = ;.FI=;NCIH.A?N();
Store the output for when it is actually time
to feed it forward.
IONJON = P;F*Q?CABN;
W
PIC> OJ>;N?() U
C@ (M?H>CHA) U
As long as were sending, interpolate our
points.
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R,
0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
476
7BfOB JFPPFKD > HBV BIBJBKQ, ELTBSBO. 7B KBBA QL @EB@H FC QEB PBKABO E>P >OOFSBA >Q IL@>QFLK
?, >KA FC FQ E>P, CBBA CLOT>OA QE>Q LRQMRQ QL QEB KBUQ KBROLK.
,BQfP ILLH >Q QEB !IHH?=NCIH @I>PP >II QLDBQEBO, >P TBII >P LRO KBT >L;Q() CRK@QFLK.
Example 10.4: Animating neural network diagram
PIC> OJ>;N?() U
C@ (M?H>CHA) U
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
How far are we from neuron b? <BE7J : = +0;9JEH.:?IJ(I;D:;H, 8.BE97J?ED);
If were close enough (within one pixel) pass
on the output. Turn off sending.
?< (: < 1) Q
8.<;;:<EHM7H:(EKJFKJ);
I;D:?D= = <7BI;;
W
W
W
PIC> >L;Q() U
<;=EALIOH>(255);
The Network now has a new update()
method that updates all of the Connection
objects.
D;JMEHA.KF:7J;();
H?NQILE.>CMJF;S();
?< (<H7C;CEKDJ % 30 == 0) Q
We are choosing to send in an input every
30 frames.
D;JMEHA.<;;:<EHM7H:(H7D:EC(1));
S
W
=F;MM !IHH?=NCIH U
4EB .>QROB LC #LAB (S005)
477
The Connections data @FI;N Q?CABN;
,?OLIH ;;
,?OLIH <;
Variables to track the animation <IIF?;H M?H>CHA = @;FM?;
.3?=NIL M?H>?L;
@FI;N IONJON = 0;
!IHH?=NCIH(,?OLIH @LIG, ,?OLIH NI, @FI;N Q) U
Q?CABN = Q;
; = @LIG;
< = NI;
W
The Connection is active with data
traveling from a to b.
PIC> @??>@ILQ;L>(@FI;N P;F) U
IONJON = P;F*Q?CABN;
M?H>?L = ;.FI=;NCIH.A?N();
M?H>CHA = NLO?;
W
Update the animation if it is sending. PIC> OJ>;N?() U
C@ (M?H>CHA) U
M?H>?L.R = F?LJ(M?H>?L.R, <.FI=;NCIH.R, 0.1);
M?H>?L.S = F?LJ(M?H>?L.S, <.FI=;NCIH.S, 0.1);
@FI;N > = .3?=NIL.>CMN(M?H>?L, <.FI=;NCIH);
C@ (> < 1) U
<.@??>@ILQ;L>(IONJON);
M?H>CHA = @;FM?;
W
W
W
Draw the connection as a line and traveling
circle.
PIC> >CMJF;S() U
MNLIE?(0);
MNLIE?4?CABN(1+Q?CABN*4);
FCH?(;.FI=;NCIH.R, ;.FI=;NCIH.S, <.FI=;NCIH.R, <.FI=;NCIH.S);
C@ (M?H>CHA) U
@CFF(0);
MNLIE?4?CABN(1);
?FFCJM?(M?H>?L.R, M?H>?L.S, 16, 16);
W
W
W
#E>MQBO 10. .BRO>I .BQTLOHP
478
4EB KBQTLOH FK QEB >?LSB BU>JMIB T>P J>KR>IIV @LKCFDROBA ?V PBQQFKD QEB IL@>QFLK LC
B>@E KBROLK >KA FQP @LKKB@QFLKP TFQE E>OA-@LABA S>IRBP. 2BTOFQB QEFP BU>JMIB QL
DBKBO>QB QEB KBQTLOHfP I>VLRQ SF> >K >IDLOFQEJ. #>K VLR J>HB > @FO@RI>O KBQTLOH
AF>DO>J? ! O>KALJ LKB? !K BU>JMIB LC > JRIQF-I>VBOBA KBQTLOH FP ?BILT.
Exercise 10.5 Exercise 10.5
2BTOFQB QEB BU>JMIB PL QE>Q B>@E KBROLK HBBMP QO>@H LC FQP CLOT>OA >KA ?>@HT>OA
@LKKB@QFLKP. #>K VLR CBBA FKMRQP QEOLRDE QEB KBQTLOH FK >KV AFOB@QFLK?
Exercise 10.6 Exercise 10.6
)KPQB>A LC F?LJ(), RPB JLSFKD ?LAFBP TFQE PQBBOFKD CLO@BP QL SFPR>IFWB QEB CILT LC
FKCLOJ>QFLK FK QEB KBQTLOH.
Exercise 10.7 Exercise 10.7
4EB .>QROB LC #LAB (S005)
479
The Ecosystem Project The Ecosystem Project
Step 10 Exercise:
Try incorporating the concept of a brain into your creatures.
Use reinforcement learning in the creatures decision-making process.
Create a creature that features a visualization of its brain as part of its
design (even if the brain itself is not functional).
Can the ecosystem as a whole emulate the brain? Can elements of the
environment be neurons and the creatures act as inputs and outputs?
The end The end
)C VLRfOB PQFII OB>AFKD, QE>KH VLR! 9LRfSB OB>@EBA QEB BKA LC QEB ?LLH. "RQ CLO >P JR@E
J>QBOF>I >P QEFP ?LLH @LKQ>FKP, TBfSB ?>OBIV P@O>Q@EBA QEB PROC>@B LC QEB TLOIA TB FKE>?FQ
>KA LC QB@EKFNRBP CLO PFJRI>QFKD FQ. )QfP JV FKQBKQFLK CLO QEFP ?LLH QL IFSB >P >K LKDLFKD
MOLGB@Q, >KA ) ELMB QL @LKQFKRB >AAFKD KBT QRQLOF>IP >KA BU>JMIBP QL QEB ?LLHfP TB?PFQB
(EQQM://K>QROBLC@LAB.@LJ) >P TBII >P BUM>KA >KA RMA>QB QEB MOFKQBA J>QBOF>I. 9LRO CBBA?>@H
FP QORIV >MMOB@F>QBA, PL MIB>PB DBQ FK QLR@E SF> BJ>FI >Q (A>KFBIPEFCCJ>K.KBQ) LO ?V
@LKQOF?RQFKD QL QEB 'FQ(R? OBMLPFQLOV (EQQM://DFQER?.@LJ/PEFCCJ>K/4EB-.>QROB-LC-#LAB/), FK
HBBMFKD TFQE QEB LMBK-PLRO@B PMFOFQ LC QEB MOLGB@Q. 3E>OB VLRO TLOH. +BBM FK QLR@E. ,BQfP ?B
QTL TFQE K>QROB.
#E>MQBO 10. .BRO>I .BQTLOHP
480
Further Reading Further Reading
Books Books
i !IBU>KABO, 2. -@.BFII. ,F=B7=D@9G C: AB=A5@ (C7CACH=CB (EQQM://Q.@L/)10FO>K%).
0OFK@BQLK, .*: 0OFK@BQLK 5KFSBOPFQV 0OBPP, 2002.
i "BKQIBV, 0BQBO. !JC@IH=CB5FM 9G=;B 6M CCADIH9FG (EQQM://Q.@L/8)M7?1WT). 3>K
&O>K@FP@L: -LOD>K +>RCJ>KK 0R?IFPEBOP, 1999.
i "LEK>@HBO, (>OQJRQ, "BKBAFHQ 'OLPP, *RIF> ,>R?, >KA #I>RAFRP ,>WWBOLKF. #9B9F5H=J9
9G=;B: 1=GI5@=N9, ,FC;F5A, 5B8 CF95H9 K=H< ,FC79GG=B; (EQQM://Q.@L/8VBHJ>H,). .BT
9LOH: 0OFK@BQLK !O@EFQB@QRO>I 0OBPP, 2012.
i &I>HB, '>OV 7FIIF>J. /<9 CCADIH5H=CB5@ B95IHM C: *5HIF9: CCADIH9F !LD@CF5H=CBG C:
"F57H5@G, C<5CG, CCAD@9L .MGH9AG, 5B8 A85DH5H=CB (EQQM://Q.@L/+A?4L1:8).
#>J?OFADB, -!: -)4 0OBPP, 1998.
i (>IB, .>QE>K #>?LQ. A6GHF57H=CB =B AFH 5B8 *5HIF9 (EQQM://Q.@L/WQ?11W#,). .BT 9LOH:
$LSBO, 1993.
i (FIAB?O>KAQ, 3QBC>K, >KA !KQELKV *. 4OLJ?>. )5H<9A5H=7G 5B8 +DH=A5@ "CFA
(EQQM://Q.@L/)10FO>K%). .BT 9LOH: 3@FBKQFCF@ !JBOF@>K ,F?O>OV, 1985. $FPQOF?RQBA ?V 7.
(. &OBBJ>K.
i +IFKB, -LOOFP. )5H<9A5H=7G 5B8 H<9 ,<MG=75@ 2CF@8 (EQQM://Q.@L/S843:K'U). .BT 9LOH:
#OLTBII, ;1959<.
i +LAF@BH, $>KKV. )5H<9A5H=7G 5B8 ,<MG=7G :CF ,FC;F5AA9FG (EQQM://Q.@L/VD$A(->H).
(FKDE>J, -!: #E>OIBP 2FSBO -BAF>, 2005.
4EB .>QROB LC #LAB (S005)
481
i -@->ELK, 4ELJ>P !., >KA *LEK 4VIBO "LKKBO. +B .=N9 5B8 (=:9 (EQQM://Q.@L/
%E83+T:"). .BT 9LOH: 3@FBKQFCF@ !JBOF@>K ,F?O>OV, 1983. $FPQOF?RQBA ?V 7. (.
&OBBJ>K.
i ->KABI?OLQ, "BKLFQ ". /<9 "F57H5@ #9CA9HFM C: *5HIF9 (EQQM://Q.@L/G(215P1#). 3>K
&O>K@FP@L: 7. (. &OBBJ>K, 1982.
i 0B>O@B, 0BQBO. .HFI7HIF9 =B *5HIF9 %G 5 .HF5H9;M :CF 9G=;B (EQQM://Q.@L/W>'1-/-@).
#>J?OFADB, -!: -)4 0OBPP, 1980.
i 0B>OPLK, ->QQ. #9B9F5H=J9 AFH (EQQM://Q.@L/?8#7CD/#). 'OBBKTF@E, #4: ->KKFKD
0R?IF@>QFLKP, 2011. $FPQOF?RQBA ?V 0B>OPLK %AR@>QFLK.
i 0ORPFKHFBTF@W, 0OWBJVP[>T, >KA !OFPQFA ,FKABKJ>VBO. /<9 A@;CF=H<A=7 B95IHM C:
,@5BHG (EQQM://Q.@L/HL$7&E*1). .BT 9LOH: 3MOFKDBO-6BOI>D, 1990.
i 2B>P, #>PBV, >KA #E>KAIBO -@7FIIF>JP. "CFA+CC89 =B 9G=;B, AFH, 5B8
AF7<=H97HIF9 (EQQM://Q.@L/1G'DTES5). $BPFDK "OFBCP. .BT 9LOH: 0OFK@BQLK
!O@EFQB@QRO>I 0OBPP, 2010.
i 2B>P, #>PBV, >KA "BK &OV. ,FC79GG=B;: A ,FC;F5AA=B; $5B86CC? :CF 1=GI5@
9G=;B9FG 5B8 AFH=GHG (EQQM://Q.@L/AQ/$A/1M). #>J?OFADB, -!: -)4 0OBPP, 2007.
i 4ELJMPLK, $f!O@V 7BKQTLOQE. +B #FCKH< 5B8 "CFA: /<9 CCAD@9H9 -9J=G98
!8=H=CB (EQQM://Q.@L/SK@7>1R7). .BT 9LOH: $LSBO, 1992.
i 6LDBI., 3QBSBK. (=:9 =B )CJ=B; "@I=8G (EQQM://Q.@L/CV4?6Q>1). 0OFK@BQLK, .*: 0OFK@BQLK
5KFSBOPFQV 0OBPP, 1994.
i 7>AB, $>SFA. (=: MB5A=7 "CFA =B *5HIF9 (EQQM://Q.@L/119$IP$(). 7LLABK "LLHP.
.BT 9LOH: 7>IHBO & #L., 2003.
i 7>QBOJ>K, 4>I?LQ (. AB=A5@ *5J=;5H=CB (EQQM://Q.@L/@2LQS8,:). .BT 9LOH: 3@FBKQFCF@
!JBOF@>K ,F?O>OV, 1989. $FPQOF?RQBA ?V 7. (. &OBBJ>K.
i 7EVQB, ,>K@BILQ ,>T. AGD97HG C: "CFA: A .MADCG=IA CB "CFA =B *5HIF9 5B8 AFH
(EQQM://Q.@L/C75H6,1-). -FAI>KA "LLHP, -" 31. "ILLJFKDQLK: )KAF>K> 5KFSBOPFQV
0OBPP, 1966.
&LO LQEBO ?LLHP QE>Q RPB 0OL@BPPFKD, PBB 0OL@BPPFKD "LLHP (EQQM://TTT.MOL@BPPFKD.LOD/
IB>OKFKD/?LLHP).
Papers and Articles Papers and Articles
i '>I>KQBO, 0EFIFM. "4EB 0OL?IBJ TFQE %SLIRQFLK>OV !OQ )Pj" (EQQM://?FQ.IV/37AEKN)
0>MBO MOBPBKQBA >Q %SL#/-.%4f10: 4EB 7QE %ROLMB>K %SBKQ LK QEB !MMIF@>QFLK LC
&ROQEBO 2B>AFKD
482
.>QROB-FKPMFOBA 4B@EKFNRBP CLO 4BIB@LJJRKF@>QFLK .BQTLOHP >KA LQEBO 0>O>IIBI >KA
$FPQOF?RQBA 3VPQBJP, !MOFI 7-9, 2010.
i '>OAKBO, ->OQFK. "->QEBJ>QF@>I '>JBP: 4EB &>KQ>PQF@ #LJ?FK>QFLKP LC *LEK
#LKT>VfP .BT 3LIFQ>FOB '>JB ,FCB." (EQQM://TTT.F?F?IFL.LOD/IFCBM>QQBOKP/
L@QL?BO1970.EQJI) .7=9BH=:=7 AA9F=75B 229 (/@QL?BO 1970): 120-23.
i 2BBSBP, 7FIIF>J 4. "0>OQF@IB 3VPQBJPd! 4B@EKFNRB CLO -LABIFKD > #I>PP LC &RWWV
/?GB@QP." (EQQM://AI.>@J.LOD/@FQ>QFLK.@CJ?FA=357320) AC) /F5BG57H=CBG CB #F5D<=7G
2:2 (!MOFI 1983): 91-108.
i 3FJP, +>OI. "!OQFCF@F>I %SLIRQFLK CLO #LJMRQBO 'O>MEF@P." (EQQM://TTT.H>OIPFJP.@LJ/
M>MBOP/PFDDO>ME91.EQJI) 0>MBO MOBPBKQBA >Q 3)''2!0( '91: 4EB 18QE !KKR>I
#LKCBOBK@B LK #LJMRQBO 'O>MEF@P >KA )KQBO>@QFSB 4B@EKFNRBP, ,>P 6BD>P, .6, *RIV
28-!RDRPQ 2, 1991.
i ---. "%SLISFKD 6FOQR>I #OB>QROBP." (EQQM://TTT.H>OIPFJP.@LJ/M>MBOP/PFDDO>ME94.MAC)
0>MBO MOBPBKQBA >Q 3)''2!0( '94: 4EB 21PQ !KKR>I #LKCBOBK@B LK #LJMRQBO
'O>MEF@P >KA )KQBO>@QFSB 4B@EKFNRBP, /OI>KAL, &,, *RIV 24-29, 1994.
i ---. "0>OQF@IB !KFJ>QFLK >KA 2BKABOFKD 5PFKD $>Q> 0>O>IIBI #LJMRQ>QFLK."
(EQQM://TTT.H>OIPFJP.@LJ/M>MBOP/0>OQF@IBP3FDDO>ME90.MAC) 0>MBO MOBPBKQBA >Q
3)''2!0( '90: 4EB 17QE !KKR>I #LKCBOBK@B LK #LJMRQBO 'O>MEF@P >KA )KQBO>@QFSB
4B@EKFNRBP, $>II>P, 48, !RDRPQ 6-10, 1990.
4EB .>QROB LC #LAB (S005)
483
Index Index
A
! ILDF@>I @>I@RIRP LC QEB FAB>P FJJFKBKQ FK KBOSLRP
>@QFSFQV (-@#RIIL@E/0FQQP)
445
>@@BIBO>QFLK 49 , 50 , 67 , 69 , 104 , 127 , 131
.BTQLK'P PB@LKA I>T 67
>IDLOFQEJP CLO 50
>KDRI>O, ABQBOJFKFKD 127
A>JMFKD 131
CLO@B >@@RJRI>QFLK >KA 69
OLQ>QFLK 104
>@@BIBO>QFLK >IDLOFQEJP 50 , 53 , 57
@LKPQ>KQ 50
FKQBO>@QFSB 57
O>KALJ 53
>@QFLK PBIB@QFLK 262
>@QFS>QFLK CRK@QFLKP LC KBRO>I KBQTLOHP 449
A85DH5H=CB =B *5HIF5@ 5B8 AFH=:=7=5@ .MGH9AG
((LII>KA)
392
>AA() CRK@QFLK (06B@QLO @I>PP) 33 , 34
FJMIBJBKQ>QFLK LC 34
>AA() CRK@QFLK (6B@2 @I>PP) 195
>AA&LO@B() CRK@QFLK (QLUF@IF?P) 256
>AAFQFLK LMBO>QLO 33
>AAFQFSB ?IBKA JLAB 186
>AA,L@>I() CRK@QFLK (6B@2) 195
>AA0>OQF@IB() CRK@QFLK (QLUF@IF?P) 246
A% :CF #5A9 9J9@CD9FG ("LROD/3BBJ>KK) 445
A@;CF=H<A=7 B95IHM C: ,@5BHG, /<9 (0ORPFKHFBTF@W/
,FKABKJ>VBO)
389
>IFDKJBKQ (CIL@HFKD) 309 , 311
FJMIBJBKQFKD 311
>IME>?BQ (,-PVPQBJ @LJMLKBKQ) 383
>JMIFQRAB 117
>KDIB"BQTBBK() CRK@QFLK (06B@QLO @I>PP) 37
>KDIBP 101 , 102 , 104 , 112
JB>PROFKD FK ABDOBBP 101
JB>PROFKD FK O>AF>KP 102
JLQFLK 104
QEBQ> (^) 112
!KDOV "FOAP 190
>KDRI>O >@@BIBO>QFLK 127
>KDRI>O SBIL@FQV, LP@FII>QFLK TFQE 119
>KLJ>IV ABQB@QFLK 447
>KQP, JLABIFKD CLO 299
>MMIV&LO@B() CRK@QFLK ("LU2$) 232
!OFPQLQIB 64
!OO>V,FPQ @I>PP (*>S>) 149 , 150 , 151 , 153 , 366
)QBO>QLO @I>PP >KA 153
CO>@Q>IP >KA 366
DBKBOF@P, RPFKD 150
OBPFW>?FIFQV LC 151
>OO>VP 8 , 15
)KABU
484
,ZSV CIFDEQP, FJMIBJBKQFKD TFQE 15
@RPQLJ AFPQOF?RQFLKP, @OB>QFKD TFQE 8
>OO>VP (2$) 277
>OOFSFKD ?BE>SFLO 270 , 273
PQBBOFKD CLO@B >KA 273
>OQFCF@F>I FKQBIIFDBK@B 444 , 445
M>QQBOK OB@LDKFQFLK 445
AFH=:=7=5@ %BH9@@=;9B79: A )C89FB ADDFC57< (2RPPBII/
.LOSFD)
445
>Q>K() CRK@QFLK (0OL@BPPFKD) 110
>Q>K2() CRK@QFLK (0OL@BPPFKD) 111
!QQO>@QFLK"BE>SFLO @I>PP (QLUF@IF?P) 256
>RQLKLJLRP >DBKQP 260 , 261 , 262 , 263 , 270 ,
274 , 276 , 282 , 286 , 298 , 306 , 308 , 317
>@QFLK PBIB@QFLK 262
>OOFSFKD ?BE>SFLO 270
@LJ?FK>QFLKP 306
@LJMIBU PVPQBJP >KA 298
ABPFOBA SBIL@FQV 274
ALQ MOLAR@Q 282
BCCF@FBK@V 317
CIL@HFKD 308
CILT CFBIA CLIILTFKD 276
HBV @LJMLKBKQP LC 261
IL@LJLQFLK 263
M>QE CLIILTFKD 286
PQBBOFKD 262
>UFLJ (,-PVPQBJ @LJMLKBKQ) 383
B
?>@HMOLM>D>QFLK 468
?BDFK#LKQ>@Q() CRK@QFLK (0"LU2$) 235
?BDFK3E>MB() CRK@QFLK (0OL@BPPFKD) 217
?BII @ROSB 11
JB>K >KA 11
?F>P FKMRQ, MBO@BMQOLK 450
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
?IBKA JLABP 186 , 187
>AAFQFSB 186
IFPQ LC 187
?LAV ("LU2$ BIBJBKQ) 194 , 198 , 199 , 200 , 202 ,
218
"LAV$BC QVMB 198
>QQ>@EFKD CFUQROB BIBJBKQ QL 202
?RFIAFKD 198
?RIIBQ PBQQFKD CLO 200
FKFQF>I PBQQFKDP CLO 200
JRIQFMIB PE>MBP >KA 218
L?GB@Q, @OB>QFKD 200
QVMBP LC 199
?LAV IFPQP, J>FKQ>FKFKD FK 0OL@BPPFKD 203
"LAV$BC QVMB (?LAV BIBJBKQ) 198 , 199 , 209
34!4)# QVMB 209
@LKCFDROFKD 199
?LFAP JLABI 261
?LRK@FKD ?>II PHBQ@E 28 , 35
FJMIBJBKQFKD TFQE SB@QLOP 35
?LRKA>OFBP 209 , 211
@ROSV 211
CFUBA 209
"LROD, $>SFA -. 445
"LU2$ 190 , 192 , 194 , 196 , 209 , 215 , 216 , 241
&FPF@> 192
*"LU2$ >KA 192
0"LU2$ EBIMBO @I>PP 192
06B@QLO SP. 194
0OL@BPPFKD >KA 192
@LJMIBU CLOJP FK 215
@LK@>SB PE>MBP >KA 216
@LLOAFK>QB PVPQBJ SP. 0OL@BPPFKD 196
@LOB BIBJBKQP 194
CFUBA L?GB@QP FK 209
LOABO LC SBOQF@BP 216
LSBOEB>A TFQE 190
QLUF@IF?P SP. 241
RP>DB 192
"O>FQBK?BOD, 6>IBKQFKL 262
?ORQB CLO@B JBQELA 392
4EB .>QROB LC #LAB (S005)
485
C
#>KQLO PBQ 358 , 363 , 386
,-PVPQBJP >KA 386
OB@ROPFLK >KA 363
#>KQLO, 'BLODB 358
#>OQBPF>K @LLOAFK>QBP 112 , 113
MLI>O @LLOAFK>QBP, @LKSBOQFKD COLJ 113
#>QQL, %OFK 190
@BIIRI>O >RQLJ>QLK (>RQLJ>Q>) 324 , 325 , 326 ,
328 , 330 , 334 , 340 , 342 , 351 , 352 , 353
3FBOMF\PHF QOF>KDIB 328
7LICO>J >IDLOFQEJ CLO 325
7LICO>J @I>PPFCF@>QFLK 340
@E>O>@QBOFPQF@P LC 324
@LKQFKRLRP 352
ABCFKBA 324
BIBJBKQ>OV 325
BIBJBKQ>OV, FJMIBJBKQFKD 330
EFPQLOF@>I 353
FJ>DB MOL@BPPFKD 352
JLSFKD @BIIP 353
KBPQFKD 353
KLK-OB@Q>KDRI>O DOFAP >KA 351
MOL?>?FIFPQF@ 352
ORIBPBQP, ABCFKFKD 334
PBIC-OBMIF@>QFKD @BIIP 324
QFJB >KA 326
QTL-AFJBKPFLK>I 342
S>OF>QFLKP LC 351
#E>FK3E>MB @I>PP 211
@LKCFDROFKD 211
@I>PP (0OL@BPPFKD) 2 , 3 , 47
@LKPQOR@QLO 3 , 47
ABCFKBA 2
CRK@QFLK>IFQV 3
@LAB ARMIF@>QFLK, FKEBOFQ>K@B >KA 161
@LBCCF@FBKQ LC COF@QFLK 80
JR (_) 80
@LEBPFLK (CIL@HFKD) 309 , 312
FJMIBJBKQFKD 312
@LIIFPFLKP 190 , 234 , 235 , 241 , 301
"LU2$ >KA 234
>SLFAFKD FK DOLRM ?BE>SFLO 301
?BDFK#LKQ>@Q() CRK@QFLK (0"LU2$) 235
QLUF@IF?P >KA 241
@LJ?FK>QFLKP 306
@LJMIBU PVPQBJP 298 , 299 , 300 , 323 , 342 ,
446
'>JB LC ,FCB >P 342
@BIIRI>O >RQLJ>Q> 323
@LJMBQFQFLK/@LLMBO>QFLK @LJMLKBKQ 300
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
CBBA?>@H @LJMLKBKQ 300
DOLRM ?BE>SFLO 300
HBV MOFK@FMIBP LC 299
KLK-IFKB>OFQV @LJMLKBKQ 299
PRMBOLOD>KFPJP 299
#LJMIBUFQV @I>PP (7LICO>J @I>PPFCF@>QFLK) 341
CCADIH5H=CB5@ B95IHM C: *5HIF9 (&I>HB) 314
@LKKB@QBA PVPQBJP 249 , 253
CLO@B-AFOB@QBA DO>MEP 253
PQOFKDP 249
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
@LKPQO>FK() CRK@QFLK (0OL@BPPFKD) 280
@LKPQOR@QLO 3 , 47 , 75
>ODRJBKQP, >AAFKD QL 75
#LKQ>@Q L?GB@QP (0"LU2$) 236
#LKQ>@Q,FPQBKBO @I>PP (*"LU2$) 234
@LKQFKRLRP (@BIIRI>O >RQLJ>Q>) 352
@LKQO>@Q() CRK@QFLK (0OL@BPPFKD) 149
@LKQOLI (LC MEVPF@>I L?GB@QP) 447
#LKT>V'P '>JB LC ,FCB (+IFPB) 345
#LKT>V, *LEK 342
@LLOAFK>QB PVPQBJP 112 , 196
"LU2$ SP. 0OL@BPPFKD 196
#>OQBPF>K 112
0OL@BPPFKD >KA 112
MLI>O 112
@LLOA0FUBIP4L7LOIA() CRK@QFLK (0"LU2$) 197
@LLOA7LOIA4L0FUBIP() CRK@QFLK (0"LU2$) 198
#LOKBII !BOLK>RQF@>I ,>?LO>QLOV 448
@LP() CRK@QFLK (0OL@BPPFKD) 113
@LPFKB ILLHRM Q>?IBP 318
)KABU
486
#O>VLK 0EVPF@P 190
@OB>QB"LAV() CRK@QFLK (0"LU2$) 200
@OB>QB&FUQROB() CRK@QFLK (0"LU2$) 202
@OLPP() CRK@QFLK (06B@QLO @I>PP) 37
@OLPPLSBO (K>QRO>I PBIB@QFLK >IDLOFQEJP) 400 , 408
FJMIBJBKQFKD 408
D
A>JMFKD 131
A>JMFKD2>QFL PBQQFKD ("LU2$ GLFKQ BIBJBKQ) 223
$>OTFKF>K K>QRO>I PBIB@QFLK 394
ABDOBBP 101 , 103
O>AF>KP, @LKSBOQFKD QL 103
ABIQ> TBFDEQ 454
ABKPFQV 67
ABOFS>QFSBP 238
$BP@>OQBP, 2BKZ 112
ABPFOBA SBIL@FQV 264 , 274
ABPQOLV"LAV() CRK@QFLK (0"LU2$) 208
AFCCBOBKQF>QFLK 238
AFPPFM>QFSB CLO@B 80
AFPQ() CRK@QFLK (06B@QLO @I>PP) 37
AFPQ>K@B GLFKQP 222
$FPQ>K@B*LFKQ$BC ("LU2$ GLFKQ QVMB) 223
AFPQOF?RQFLKP, @RPQLJ 14
AFPQOF?RQFLKP, KLK-RKFCLOJ 7 , 8 , 14 , 15 , 16 , 17
-LKQB #>OIL JBQELA 16
0BOIFK KLFPB 17
@OB>QFKD TFQE >OO>VP 8
@RPQLJ 14
MOL?>?FIFQV >KA 7
NR>IFCVFKD O>KALJ S>IRBP 15
AFPQOF?RQFLKP, KLOJ>I 11
AFS() CRK@QFLK (06B@QLO @I>PP) 41
ALQ MOLAR@Q (06B@QLO) 282 , 283 , 284
ABCFKBA 283
QEBQ> 284
ALQ PVKQ>U 35
ALQ() CRK@QFLK (06B@QLO @I>PP) 37
AO>D CLO@B 83
AVK>JF@ (?LAV QVMB) 199
E
B@LPVPQBJ PFJRI>QFLK DBKBQF@ >IDLOFQEJP 392 , 435 ,
437 , 438 , 439
DBKLQVMB 438
IFCBPM>KP, S>OVFKD 437
MEBKLQVMB 438
OBMOLAR@QFLK 439
PBIB@QFLK 439
BCCF@FBK@V 315 , 316 , 317 , 318 , 319
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
BIBJBKQ>OV @BIIRI>O >RQLJ>Q> 325 , 330 , 332 , 333
, 336
AO>TFKD 336
BADB @>PBP >KA 332
DBKBO>QFLKP, J>FKQ>FKFKD FKQBDOFQV LC 333
FJMIBJBKQFKD 330
BJFQQBO 146
BKA#LKQ>@Q() CRK@QFLK (0"LU2$) 235
BKA3E>MB() CRK@QFLK (0OL@BPPFKD) 217
BNRFIF?OFRJ 64
%R@IFA 27 , 355
%R@IFAB>K DBLJBQOV 355 , 358
CO>@Q>IP >KA 358
%R@IFAB>K SB@QLO 27
%RIBO FKQBDO>QFLK 239 , 240
PVJMIB@QF@ %RIBO ("LU2$) 240
%RIBO, ,BLKE>OA 239
BSLIRQFLK 390 , 391 , 394
$>OTFKF>K K>QRO>I PBIB@QFLK 394
DBKBQF@ >IDLOFQEJP 391
JLABIFKD 390
BSLIRQFLK>OV @LJMRQFKD 392
%SLISBA 6FOQR>I #OB>QROBP (3FJP) 430
BU@IRPFSB LO (8/2) 466
BUFQ @LKAFQFLKP CLO OB@ROPFLK 361
BUM>KA() CRK@QFLK (0OL@BPPFKD) 149
4EB .>QROB LC #LAB (S005)
487
%UMILOFKD %JBODBK@B (2BPKF@H/3FISBOJ>K) 345
BUQBKAP HBVTLOA (0OL@BPPFKD) 164
F
C>@QLOF>I 359
CBBA-CLOT>OA JLABI (KBRO>I KBQTLOHP) 448 , 473
>KFJ>QFKD 473
&FPF@> 192
CFQKBPP CRK@QFLKP (K>QRO>I PBIB@QFLK >IDLOFQEJP) 397 ,
414 , 416 , 417 , 427 , 429 , 436
>SLFA>K@B LC L?PQ>@IBP >KA 427
ABPFDK VLRO LTK 416
B@LPVPQBJ PFJRI>QFLKP >KA 436
BSLISFKD CLO PMB@FCF@ >QQOF?RQBP 429
BUMLKBKQF>I SP. IFKB>O 414
OL?LQF@ BKPI>SBJBKQ LC ERJ>KFQV >KA 417
CFUQROB ("LU2$ BIBJBKQ) 194 , 201 , 202
>QQ>@EFKD QL ?LAV BIBJBKQ 202
@OB>QFKD 201
&I>HB, '>OV 314
CIL@HFKD 308 , 309 , 315 , 316
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
MBOCLOJ>K@B >KA 315
ORIBP LC 309
&IL@HP, (BOAP, >KA 3@ELLIP: ! $FPQOF?RQBA "BE>SFLO>I
-LABI (2BVKLIAP)
308
CILT CFBIA CLIILTFKD 276 , 277
OBPLIRQFLK >KA 277
CIRFA OBPFPQ>K@B, JLABIFKD 83
CLO ILLMP 150
!OO>V,FPQ L?GB@QP >KA 150
BKE>K@BA 150
CLO@B >@@RJRI>QFLK 69
CLO@B-AFOB@QBA DO>MEP 253
CLO@BP 63 , 64 , 68 , 71 , 73 , 77 , 78 , 80 , 83 ,
89 , 127 , 131 , 134 , 173 , 178 , 232 , 260 , 263
(LLHB'P I>T 134
.BTQLK'P I>TP LC JLQFLK 63
>@@RJRI>QFLK LC 68
>MMIV&LO@B() CRK@QFLK 232
>MMIVFKD QL L?GB@QP 71
>MMIVFKD QL PFKDIB L?GB@QP FK > PVPQBJ 178
>RQLKLJLRP >DBKQP >KA 260
@OB>QFKD 73
A>JMFKD 131
ABCFKBA 63
BNRFIF?OFRJ 64
CIRFA OBPFPQ>K@B 83
COF@QFLK, JLABIFKD 80
DO>SFQV, JLABIFKD 77
JLABIP LC, ?RFIAFKD 78
M>OQF@IB PVPQBJP TFQE 173
PMOFKDP 134
PQBBOFKD 263
QBOJFK>I SBIL@FQV 64
QOFDLKLJBQOV >KA 127
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
CLO@BP, JLABIFKD 77
OB>I CLO@BP 77
CLOJRI>B, BS>IR>QFKD FK @LAB 79
"F57H5@ #9CA9HFM C: *5HIF9, /<9 (->KABI?OLQ) 356
CO>@Q>IP 355 , 356 , 357 , 358 , 366 , 374 , 375 ,
382
+L@E @ROSB 366
,-PVPQBJP >KA 382
ABCFKBA 356
CFKB PQOR@QROB LC 358
OB@ROPFLK 358
PBIC-OBMIF@>QFKD ?BE>SFLO LC 357
PQL@E>PQF@ 358
QO>KPCLOJ>QFLK J>QOFU (0OL@BPPFKD) 375
QOBBP >KA 374
COBNRBK@V (LC LP@FII>QFLKP) 119
COBNRBK@V(W PBQQFKD ("LU2$ GLFKQ BIBJBKQ) 223
COF@QFLK 79 , 80 , 81 , 83 , 84 , 131
>MMIVFKD QL >K L?GB@Q 84
@LBCCF@FBKQ LC COF@QFLK 80
A>JMFKD 131
ABQBOJFKFKD AFOB@QFLK/J>DKFQRAB LC 80
CLOJRI> CLO 79
JLABIFKD TFQE CLOJRI>B 80
JR (_) 80
KLOJ>I CLO@B 81
)KABU
488
OEL (a) 83
CRK@QFLK>IFQV 3
CRK@QFLKP 54
PQ>QF@ SP. KLK-PQ>QF@ 54
G
'>IFIBL 77
'>JB LC ,FCB 324 , 342 , 343 , 344
AO>TFKD 344
ORIBP LC 343
'>OAKBO, ->OQFK 342
'>RPP, #>OI &OFBAOF@E 11
'>RPPF>K AFPQOF?RQFLK 11
DBKBQF@ >IDLOFQEJP 391 , 392 , 394 , 395 , 397 ,
409 , 413 , 414 , 420 , 435
$>OTFKF>K K>QRO>I PBIB@QFLK 394
3J>OQ 2L@HBQP (4ELOM) 420
?RFIAFKD 409
ABCFKBA 391
B@LPVPQBJ PFJRI>QFLK 392 , 435
CFQKBPP >IDLOFQEJP, JLAFCVFKD 414
FKQBO>@QFSB PBIB@QFLK 391
JLAFCVFKD 413
JRQ>QFLK O>QB, S>OVFKD 413
MLMRI>QFLK J>UFJRJ, S>OVFKD 413
MLMRI>QFLKP, @OB>QFKD 395
MROMLPB LC 392
PBIB@QFLK, FJMIBJBKQFKD 397
QO>AFQFLK>I 391
DBKLQVMB (K>QRO>I PBIB@QFLK >IDLOFQEJP) 395 , 417 ,
438
B@LPVPQBJ PFJRI>QFLK 438
JLAFCVFKD 417
DBLJBQOF@ SB@QLO 27
DBQ!KDIB() CRK@QFLK (0"LU2$) 207
DBQ"LAV,FPQ() CRK@QFLK (7LOIA @I>PP) 203
DBQ"LAV0FUBI#LLOA() CRK@QFLK (0"LU2$) 207
DBQ'OLRKA"LAV() CRK@QFLK ("LU2$ GLFKQ BIBJBKQ) 229
DO>SFQV 88 , 89 , 94 , 128 , 244
'O>SFQV"BE>SFLO (QLUF@IF?P) 244
FJMIBJBKQFKD JLABI LC 89
JLABIFKD 88
JLABIFKD OB>IFQV SP. >O?FQO>OV S>IRBP 128
JLABIFKD TFQE QOFDLKLJBQOV 128
MI>@FKD IFJFQP LK JLABI LC 94
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
'O>SFQV"BE>SFLO @I>PP (QLUF@IF?P) 244
DOFA (@BIIRI>O >RQLJ>Q>) 324
DOLRM ?BE>SFLO 300 , 301 , 306 , 308
@LIIFPFLKP, >SLFAFKD 301
@LJ?FK>QFLKP 306
CIL@HFKD 308
H
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 37 , 112
EBOBAFQV (K>QRO>I PBIB@QFLK) 394 , 399 , 400 , 401
@OLPPLSBO 400
FJMIBJBKQFKD 399
JRQ>QFLK 401
EFPQLOF@>I (@BIIRI>O >RQLJ>Q>) 353
(LADFK, 2L?BOQ 186
(LII>KA, *LEK 392
(LLHB'P I>T 134 , 135
CLOJRI> CLO BUMOBPPFKD 135
(LLHB, 2L?BOQ 134
I
FJ>DB MOL@BPPFKD (@BIIRI>O >RQLJ>Q>) 352
FJ>DB QBUQROBP 183 , 184 , 186
0)J>DB L?GB@QP (0OL@BPPFKD) 184
0.' CLOJ>Q >KA 184
?IBKA JLABP 186
FKCFKFQB JLKHBV QEBLOBJ 392
FKEBOFQ>K@B 144 , 160 , 163 , 164 , 165 , 166
>AAFKD CRK@QFLK>IFQV QL PRMBO@I>PP L?GB@QP 165
BUQBKAP HBVTLOA (0OL@BPPFKD) 164
FJMIBJBKQFKD 166
LSBOOFAFKD PRMBO@I>PP CRK@QFLKP 165
PR?@I>PP 163
PRMBO() CRK@QFLK (0OL@BPPFKD) 164
4EB .>QROB LC #LAB (S005)
489
PRMBO@I>PPBP 163
PVKQ>U CLO 163
FKQBDO>QFLK 238 , 239 , 240
%RIBO FKQBDO>QFLK 239
2RKDB-+RQQ> JBQELA 240
)KQBO>@QFLK TFQE 'OLRMP LC !RQLKLJLRP #E>O>@QBOP
(2BVKLIAP)
316
FKQBO>@QFSB PBIB@QFLK DBKBQF@ >IDLOFQEJP 391 , 431 ,
433 , 434
QFJB I>D >KA 434
RPBO FKQBO>@QFLK >KA 433
FKQBOC>@BP 234
FQBO>QFKD 152 , 153
)QBO>QLO @I>PP (*>S>) 153
OBJLSFKD BIBJBKQP FK CLO ILLMP 152
)QBO>QLO @I>PP (*>S>) 153
F4RKBP SFPR>IFWBO 186
J
*>S> 192
*"LU2$ 192 , 195 , 234
#LKQ>@Q,FPQBKBO @I>PP 234
CRII AL@RJBKQ>QFLK CLO 195
GLFKQ ("LU2$ BIBJBKQ) 194 , 222 , 225 , 228
AFPQ>K@B 222
JLRPB QVMB 228
OBSLIRQB QVMB 225
K
HFKBJ>QF@ (?LAV QVMB) 199 , 231
-LRPB*LFKQP >KA 231
+IFPB, 3QBSBK 345
+L@E @ROSB 366 , 369
FJMIBJBKQFKD 369
+RQQ>, -. 7. 240
L
,-PVPQBJP 382 , 383 , 386
@LJMLKBKQP LC 383
QO>KPI>QFKD FKQL @LAB 386
,>MI>@B, 0FBOOB-3FJLK 11
,>MI>@F>K AFPQOF?RQFLK 11
IB>OKFKD @LKPQ>KQ 455
(95FB=B; ,FC79GG=B; (3EFCCJ>K) 160
IBOM() CRK@QFLK (06B@QLO @I>PP) 37
IFJFQ() CRK@QFLK (06B@QLO @I>PP) 37 , 51
,FKABKJ>VBO PVPQBJP 382
,FKABKJ>VBO, !OFPQFA 382 , 389
IFKB>OIV PBM>O>?IB MOL?IBJP 466
IL@>QFLKP 31
>P SB@QLOP 31
IL@H() CRK@QFLK (QLUF@IF?P) 247
IL@LJLQFLK 263
,LP !I>JLP .>QFLK>I ,>?LO>QLOV 324
,R@>PCFIJ ,QA. 143
,ZSV CIFDEQ 14 , 15
FJMIBJBKQFKD TFQE >OO>VP 15
FJMIBJBKQFKD TFQE NR>IFCVFKD O>KALJ S>IRBP 15
M
J=M S>OF>?IB (6B@2 @I>PP) 220
J>D() CRK@QFLK (06B@QLO @I>PP) 43 , 318
J>D3N() CRK@QFLK SP. 318
->DKBQLPMEBOB 186
J>DKFQRAB (LC SB@QLOP) 42 , 51
IFJFQFKD 51
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
->KABI?OLQ, "BKLFQ 356
J>M() CRK@QFLK (0OL@BPPFKD) 20 , 117
LP@FII>QFLK >KA 117
->OUBO, 2F@>OA 192
J>PP 67 , 70
JLABIFKD 70
RKFQP LC JB>PROBJBKQ, ABCFKFKD 70
TBFDEQ SP. 67
J>QFKD MLLIP (K>QRO>I PBIB@QFLK) 397 , 405
@OB>QFKD 397
)KABU
490
FJMIBJBKQFKD 405
-@#RIIL@E, 7>OOBK 3. 445
JB>K 11
JBQELAP, PQ>QF@ SP. KLK-PQ>QF@ 54
JFIIFP() CRK@QFLK (0OL@BPPFKD) 117
-LKPQBO @ROSB 367
-LKQB #>OIL JBQELA 16
JLQFLK 45 , 104 , 112
>KDRI>O 104
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 112
JLRPB GLFKQ ("LU2$ GLFKQ QVMB) 228
PBQ4O>KPCLOJ() CRK@QFLK 228
JLRPB GLFKQ ("LU2$ *LFKQ QVMB) 228
JLSFKD @BIIP (@BIIRI>O >RQLJ>Q>) 353
JR (_) 11 , 80
JRIQ() CRK@QFLK (06B@QLO @I>PP) 40
FJMIBJBKQ>QFLK 40
JRQ>QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 401 , 409
FJMIBJBKQFKD 409
O>QB LC 401
N
K>QRO>I CO>@Q>IP 374
K>QRO>I MEBKLJBK> 2 , 7 , 17 , 67 , 70 , 73 , 77 ,
78 , 80 , 83 , 88 , 89 , 127 , 128 , 185 , 260 , 299 ,
300 , 308 , 324 , 355 , 374 , 382 , 383 , 390 , 391
, 394 , 435
$>OTFKF>K K>QRO>I PBIB@QFLK 394
,-PVPQBJP >KA 382
.BTQLK'P PB@LKA I>T, JLABIFKD 67
0BOIFK KLFPB >KA 17
>KQP, JLABIFKD 299
>RQLKLJLRP >DBKQP 260
@BIIRI>O >RQLJ>Q> 324
B@LPVPQBJP, JLABIFKD 435
BSLIRQFLK 390
CIL@HFKD 308
CIRFA OBPFPQ>K@B, JLABIFKD 83
CLO@BP, JLABIFKD 73 , 77
CO>@Q>IP 355
COF@QFLK 80
DBKBQF@ >IDLOFQEJP 391
DO>SFQV 77 , 88 , 89
DOLRM ?BE>SFLO 300
J>PP, JLABIFKD 70
JLABIFKD OB>IFQV SP. >O?FQO>OV S>IRBP 128
JLABIFKD TFQE O>KALJ T>IHP 2
JLABIFKD TFQE QEB O>KALJ() CRK@QFLK 7
MEVPF@P (OB>I TLOIA), JLABIFKD 78
MFSLQP, JLABIFKD 127
MI>KQ DOLTQE, JLABIFKD 383
PJLHB, JLABIFKD TFQE M>OQF@IB PVPQBJP 185
QOBBP >KA 374
K>QRO>I PBIB@QFLK >IDLOFQEJP 394 , 395 , 397 , 398 ,
399
CFQKBPP CRK@QFLKP 397
J>QFKD MLLIP, @OB>QFKD 397
MLMRI>QFLKP, @OB>QFKD 395
MOL?>?FIFQV 398
OBMOLAR@QFLK 399
K>QRO>IIV LOABOBA PBNRBK@B LC KRJ?BOP 17
KBFDE?LOELLA (@BIIRI>O >RQLJ>Q>) 325
KBPQFKD (@BIIRI>O >RQLJ>Q>) 353
KBRO>I KBQTLOHP 444 , 445 , 446 , 447 , 448 , 449 ,
467 , 468 , 473 , 475
>@QFS>QFLK CRK@QFLKP LC 449
>KFJ>QFKD 473
?>@HMOLM>D>QFLK 468
@LKKB@QFLKFPQ @LJMRQ>QFLK>I PVPQBJ 446
AF>DO>JJFKD 468
IB>OKFKD >KA 446
KBQTLOHP LC MBO@BMQOLKP 467
M>QQBOK OB@LDKFQFLK 445
MBO@BMQOLK 448
OB>I SP. PFJRI>QBA 475
OBFKCLO@BJBKQ IB>OKFKD 447
PRMBOSFPBA IB>OKFKD 446
RKPRMBOSFPBA IB>OKFKD 447
RPBP LC 447
*9K '=B8 C: .7=9B79, A (7LICO>J) 325
KBT LMBO>QLO (L?GB@QP) 4
.BTQLK'P CFOPQ I>T 64 , 65
06B@QLO @I>PP >KA 65
.BTQLK'P PB@LKA I>T 67
4EB .>QROB LC #LAB (S005)
491
.BTQLK'P QEFOA I>T 65 , 66
06B@QLO @I>PP >KA 66
.BTQLK, )P>>@ 63
KBUQ'>RPPF>K() CRK@QFLK (2>KALJ @I>PP) 13
ABC>RIQ JB>K/PQ>KA>OA ABSF>QFLK PBQQFKDP LC 13
KLFPB() CRK@QFLK (0OL@BPPFKD) 18
>ODRJBKQP CLO 18
KLFPB$BQ>FI() CRK@QFLK (0OL@BPPFKD) 18
KLK-IFKB>OIV PBM>O>?IB MOL?IBJP 466
KLK-OB@Q>KDRI>O DOFAP (@BIIRI>O >RQLJ>Q>) 351
KLK-RKFCLOJ AFPQOF?RQFLKP 7 , 8 , 14 , 15 , 16 , 17
-LKQB #>OIL JBQELA 16
0BOIFK KLFPB 17
@OB>QFKD TFQE >OO>VP 8
@RPQLJ 14
MOL?>?FIFQV >KA 7
NR>IFCVFKD O>KALJ S>IRBP 15
KLOJ>I AFPQOF?RQFLK 11
KLOJ>I CLO@B 81
KLOJ>I MLFKQP 291 , 295
PBOFBP LC, CLO M>QE CLIILTFKD 295
KLOJ>IFW>QFLK 43 , 398
J>QFKD MLLIP, @OB>QFKD TFQE 398
KLOJ>IFWB() CRK@QFLK (06B@QLO @I>PP) 44
.LOSFD, 0BQBO 445
O
L?GB@Q 2 , 4 , 92 , 349
@BIIP FK @BIIRI>O >RQLJ>Q> >P 349
ABCFKBA 2
FKQBO>@QFLK ?BQTBBK 92
KBT LMBO>QLO 4
L?GB@Q-LOFBKQBA MOLDO>JJFKD 2 , 35 , 72 , 137 ,
144 , 155 , 160 , 168 , 176 , 349 , 419
@BIIRI>O >RQLJ>Q> >KA 349
@I>PP 2
@I>PPBP LC RPBO-ABCFKBA L?GB@QP, @OB>QFKD 155
ALQ PVKQ>U 35
DBKLQVMB/MEBKLQVMB L?GB@QP >KA 419
FKEBOFQ>K@B 144 , 160
FKPQ>K@BP LC PR?@I>PPBP, @OB>QFKD 168
J>FKQ>FKFKD BK@>MPRI>QFLK 176
L?GB@Q 2
MLIVJLOMEFPJ 144 , 160 , 168
OBCBOBK@BP QL SP. @LMFBP LC L?GB@QP 72
OBSFBT LC 2
PQOR@QROBP, @ELPFKD ?BQTBBK 137
LMQFJFW>QFLK 318 , 319
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
LP@FII>QFLK 116 , 117 , 119 , 120 , 122 , 124
>JMIFQRAB 117
>KDRI>O SBIL@FQV >KA 119
COBNRBK@V LC 119
LK QTL >UBP 120
MBOFLA 117
PFJMIB E>OJLKF@ JLQFLK 117
PFJRI>QFKD TFQE PFKB @ROSBP 116
S>OVFKD 124
T>SBP 122
LSBOP>JMIFKD 14
P
M>OQF@IB PVPQBJP 143 , 144 , 145 , 146 , 149 , 155 ,
156 , 157 , 170 , 173 , 178 , 185 , 240 , 246
!OO>V,FPQ, RPFKD 149
6BOIBQ FKQBDO>QFLK >KA 240
>AA0>OQF@IB() CRK@QFLK (QLUF@IF?P) 246
>MMIVFKD CLO@B QL PFKDIB M>OQF@IBP FK 178
@I>PP CLO, @OB>QFKD 155
AB>A M>OQF@IBP, @EB@HFKD CLO 146
BJFQQBO 146
CLO@BP >KA 173
IFCBPM>K LC M>OQF@IBP 146
JRIQFMIB PVPQBJP, LOD>KFWFKD 157
LOFDFK MLFKQ (LC M>OQF@IBP) 156
M>OQF@IBP FK 145
MLIVJLOMEFPJ, RPFKD 170
MROMLPB LC 144
PJLHB, JLABIFKD 185
)KABU
492
M>OQF@IBP 145 , 146 , 147 , 178 , 244 , 245
6BOIBQ0>OQF@IB2$ L?GB@Q (QLUF@IF?P) 245
>MMIVFKD CLO@B QL PFKDIB M>OQF@IBP FK 178
AB>QE, @EB@HFKD CLO 146
IFCBPM>K LC 146
QBPQFKD 147
QLUF@IF?P FJMIBJBKQ>QFLK LC 244
M>QE 286
M>QE CLIILTFKD 286 , 288 , 291 , 292 , 294
@ROOBKQ AFPQ>K@B COLJ M>QE, CFKAFKD 288
JRIQFMIB PBDJBKQP 294
KLOJ>I MLFKQP 291
M>QECFKAFKD SP. 286
Q>ODBQ, ABQBOJFKFKD 292
M>QECFKAFKD 286
M>QQBOK OB@LDKFQFLK 445 , 450
MBO@BMQOLK >KA 450
0"LU2$ EBIMBO @I>PP 192 , 196 , 197 , 207
@LLOAFK>QB PVPQBJP, @LKSBOQFKD ?BQTBBK 197
@OB>QB7LOIA() CRK@QFLK 196
DBQ"LAV0FUBI#LLOA() CRK@QFLK (0"LU2$) 207
MBO@BMQOLK 448 , 450 , 455 , 456 , 460 , 466 ,
467
?F>P FKMRQ 450
BOOLO @>I@RI>QFLKP >KA 450
FJMIBJBKQFKD 448
IB>OKFKD @LKPQ>KQ 455
IFKB>OIV PBM>O>?IB MOL?IBJP >KA 466
KBQTLOHP LC 467
M>QQBOK OB@LDKFQFLK TFQE 450
PQBBOFKD >KA 460
QO>FKFKD 456
MBOCLOJ>K@B 315 , 316 , 317 , 318 , 319
"FD / .LQ>QFLK 315
"FD / .LQ>QFLK .-3NR>OBA 315
?FK-I>QQF@B PM>QF>I PR?AFSFPFLK 316
J>D3N() CRK@QFLK (06B@QLO @I>PP). 318
PFKB/@LPFKB ILLHRM Q>?IBP 318
QBJMLO>OV L?GB@QP >KA 319
MBOFLA 117 , 122
ABCFKBA FK MFUBIP O>QEBO QE>K CO>JBP 122
0BOIFK KLFPB 17 , 18 , 20 , 22 , 279
CILT CFBIA CLIILTFKD >KA 279
J>M() CRK@QFLK 20
K>QRO>I MEBKLJBK>, JLABIFKD TFQE 17
KLFPB() CRK@QFLK (0OL@BPPFKD) 18
QTL-AFJBKPFLK>I 22
MEBKLQVMB (K>QRO>I PBIB@QFLK >IDLOFQEJP) 395 , 417 ,
438
B@LPVPQBJ PFJRI>QFLK 438
MEVPF@P 78 , 189 , 190
@LIIFPFLKP 190
JLABIFKD 78
LMBK-PLRO@B IF?O>OFBP CLO 189
MEVPF@P IF?O>OFBP 189 , 190
"LU2$ 190
MF (`) 103
0) S>OF>?IB (0OL@BPPFKD) 103
0)J>DB L?GB@QP (0OL@BPPFKD) 184
0FQQP, 7>IQBO 445
MFSLQP, JLABIFKD 127
MI>KQ DOLTQE, JLABIFKD 383
0.' DO>MEF@ CFIB CLOJ>Q 184
MLI>O @LLOAFK>QBP 112 , 113
#>OQBPF>K @LLOAFK>QBP, @LKSBOQFKD QL 113
0LIVDLK3E>MB @I>PP 215
>P IFPQ LC SB@QLOP 215
MLIVJLOMEFPJ 144 , 160 , 168 , 170
@OB>QFKD L?GB@Q FKPQ>K@BP TFQE 170
MLM->QOFU() CRK@QFLK (0OL@BPPFKD) 375
MLMRI>QFLKP (DBKBQF@ >IDLOFQEJP) 395 , 402 , 435
@OB>QFKD 395
B@LPVPQBJ PFJRI>QFLKP >KA 435
BIBJBKQP LC 395
FJMIBJBKQFKD 402
MLPQ3LISB() CRK@QFLK (0"LU2$) 235
MOB3LISB() CRK@QFLK (0"LU2$) 235
MOL?>?FIFPQF@ (@BIIRI>O >RQLJ>Q>) 352
MOL?>?FIFQV 7 , 8 , 11 , 352 , 392 , 398
@BIIRI>O >RQLJ>Q> ?>PBA LK 352
FKCFKFQB JLKHBV QEBLOBJ 392
JB>K 11
K>QRO>I PBIB@QFLK >IDLOFQEJP >KA 398
KLK-RKFCLOJ AFPQOF?RQFLKP >KA 7
4EB .>QROB LC #LAB (S005)
493
KLOJ>I AFPQOF?RQFLKP 11
PQ>KA>OA ABSF>QFLK 11
MOL?>?FIFQV LC QEB CFQQBPQ 7
0OL@BPPFKD 2 , 12 , 18 , 30 , 46 , 54 , 102 , 103 ,
110 , 111 , 112 , 117 , 163 , 183 , 184 , 192 , 196 ,
203 , 205 , 241
"LU2$ >KA 192
"LU2$ L?GB@QP, >AAFKD QL MOLGB@QP 205
*"LU2$ 192
//0 LKIFKB QRQLOF>I 46
0)J>DB L?GB@QP 184
2>KALJ @I>PP 12
>KDIBP, JB>PROFKD FK 102
>Q>K() CRK@QFLK 110
>Q>K2() CRK@QFLK 111
?LAV IFPQP, J>FKQ>FKFKD 203
@I>PP FKEBOFQ>K@B, PVKQ>U CLO 163
@LLOAFK>QB PVPQBJP >KA 112
@LLOAFK>QB PVPQBJP SP. "LU2$ 196
FK@LOMLO>QFKD FJ>DBP FKQL MOLGB@QP 183
JB>PROFKD QFJB FK 117
KLFPB() CRK@QFLK 18
KLFPB$BQ>FI() CRK@QFLK 18
O>AF>KP() CRK@QFLK 103
OBSFBT LC L?GB@Q-LOFBKQBA MOLDO>JJFKD TFQE 2
OLQ>QFLK QRQLOF>I 103
PQ>QF@ SP. KLK-PQ>QF@ JBQELAP 54
QLUF@IF?P >KA 241
SB@QLOP >KA 30
0ORPFKHFBTF@W, 0OWBJVP[>T 389
MPBRAL-O>KALJ KRJ?BOP 7 , 17
0BOIFK KLFPB >KA 17
MRPE->QOFU() CRK@QFLK (0OL@BPPFKD) 375
06B@QLO @I>PP (0OL@BPPFKD) 30 , 37 , 38 , 40 , 41 ,
43 , 44 , 51 , 65 , 66 , 112 , 194
"LU2$ SP. 194
.BTQLK'P CFOPQ I>T >KA 65
.BTQLK'P QEFOA I>T >KA 66
AFS() CRK@QFLK 41
CRK@QFLK IFPQ CLO 37
EB>AFKD2$() CRK@QFLK 112
IFJFQ() CRK@QFLK 51
J>D() CRK@QFLK 43
J>QEBJ>QF@>I CRK@QFLKP CLO 37
JRIQ() CRK@QFLK 40
KLOJ>IFWB() CRK@QFLK 44
PR?() CRK@QFLK 38
0VQE>DLO>P 42
0VQE>DLOB>K QEBLOBJ 42
Q
NR>IFCVFKD O>KALJ S>IRBP 15 , 16
-LKQB #>OIL JBQELA 16
R
O>AF>KP 102 , 103
@LKSBOQFKD COLJ ABDOBBP 103
O>AF>KP() CRK@QFLK (0OL@BPPFKD) 103
2>KALJ @I>PP (0OL@BPPFKD) 12 , 13
KBUQ'>RPPF>K() CRK@QFLK 13
2>KALJ @I>PP (7LICO>J @I>PPFCF@>QFLK) 341
O>KALJ KRJ?BO DBKBO>QLOP 3 , 6 , 7 , 14
@RPQLJ AFPQOF?RQFLKP, @OB>QFKD 14
KLK-RKFCLOJ AFPQOF?RQFLKP, @OB>QFKD 7
MPBRAL-O>KALJ KRJ?BOP 7
O>KALJ() CRK@QFLK 3
RKFCLOJ KRJ?BO AFPQOF?RQFLKP >KA 6
O>KALJ T>IHP 1 , 14
'>RPPF>K 14
,ZSV CIFDEQ 14
LSBOP>JMIFKD 14
O>KALJ() CRK@QFLK 3 , 7 , 8
K>QRO>I MEBKLJBK>, JLABIFKD TFQE 7
KLK-RKFCLOJ AFPQOF?RQFLKP, @OB>QFKD TFQE 8
O>KALJ2$() CRK@QFLK (06B@QLO @I>PP) 37
O>KALJ3$() CRK@QFLK (06B@QLO @I>PP) 37
OB>I CLO@BP 77
OB@ROPFLK 358 , 359 , 361 , 366
!OO>V,FPQ L?GB@QP >KA 366
BUFQ @LKAFQFLKP 361
C>@QLOF>I 359
)KABU
494
FJMIBJBKQFKD 359
2BBSBP, 7FIIF>J 4. 143
OBFKCLO@BJBKQ IB>OKFKD (KBRO>I KBQTLOHP) 447
OBFKCLO@BJBKQ IB>OKFKD(KBRO>I KBQTLOHP) 463
OBJLSB() CRK@QFLK (!OO>V,FPQ @I>PP) 151
2BMBQFQFLK @I>PP (7LICO>J @I>PPFCF@>QFLK) 340
OBMOLAR@QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 399 , 407
, 439
B@LPVPQBJ PFJRI>QFLK 439
FJMIBJBKQFKD 407
OBMRIPFLK 302
DOLRM ?BE>SFLO >KA 302
2BPKF@H, -FQ@EBI 262 , 345
OBPLIRQFLK, CILT CFBIA CLIILTFKD >KA 277
OBPQ IBKDQE ("LU2$ GLFKQ BIBJBKQ) 223
OBSLIRQB GLFKQ QVMB ("LU2$) 225 , 226
MOLMBOQFBP, @LKCFDROFKD 226
2BSLIRQB*LFKQ$BC L?GB@Q ("LU2$ GLFKQ BIBJBKQ) 226
2BVKLIAP, #O>FD 261 , 286
M>QE CLIILTFKD >IDLOFQEJ 286
OEL (a) 83
2LPBK?I>QQ, &O>KH 448
OLQ>QB() CRK@QFLK (0"LU2$) 207
OLQ>QB() CRK@QFLK (06B@QLO @I>PP) 37
OLQ>QFLK 104 , 109
MLFKQFKD QLT>OAP JLSBJBKQ 109
OLRIBQQB TEBBI MOL?>?FIFQV JBQELA 398
ORIBP (,-PVPQBJ @LJMLKBKQ) 383
ORIBPBQP CLO @BIIRI>O >RQLJ>Q> 334
2RKDB, #. 240
2RKDB-+RQQ> JBQELA 240
2RPPBII, 3QR>OQ *. 445
S
P@>I>O KLQ>QFLK, SP. SB@QLO KLQ>QFLK 33
P@>I>O MOLGB@QFLK 291
P@>I>O0FUBIP4L7LOIA() CRK@QFLK (0"LU2$) 198
P@>I>O7LOIA4L0FUBIP() CRK@QFLK (0"LU2$) 198
3@EJFAQ, +>OPQBK 241
3BBJ>KK, 'IBKK 445
PBIB@QFLK (K>QRO>I PBIB@QFLK >IDLOFQEJP) 394 , 397 ,
404 , 439
B@LPVPQBJ PFJRI>QFLK 439
FJMIBJBKQFKD 397 , 404
PBIC-OBMIF@>QFKD @BIIP 324
PBIC-PFJFI>OFQV LC CO>@Q>IP 357
PBM>O>QFLK (CIL@HFKD) 309 , 310
FJMIBJBKQFKD 310
PBQ'O>SFQV() CRK@QFLK (7LOIA @I>PP) 196
PBQ4O>KPCLOJ() CRK@QFLK ("LU2$) 228
3E>MB ("LU2$ BIBJBKQ) 200 , 201 , 220
ABCFKFKD 201
COF@QFLK >QQOF?RQB 200
IL@>I MLPFQFLK CLO 220
OBPQFQRQFLK >QQOF?RQB 200
PE>MB ("LU2$ BIBJBKQ) 194
PE>MBP 104 , 112 , 113
AFPMI>VFKD 112
JLSFKD TFQE MLI>O @LLOAFK>QBP 113
OLQ>QFKD 104
PELOQ O>KDB OBI>QFLKPEFMP 299 , 310
@LJMIBU PVPQBJP 299
CIL@HFKD ?BE>SFLO >KA 310
3FBOMF\PHF QOF>KDIB 328
3FBOMF\PHF, 7>@[>T 328
PFDJ> (b) 11
PFDK>I MOL@BPPFKD 447
3FISBOJ>K, "OF>K 345
PFJMIB E>OJLKF@ JLQFLK 117
3FJP, +>OI 430
PFK() CRK@QFLK (0OL@BPPFKD) 113
PFKB ILLHRM Q>?IBP 318
PFWB() CRK@QFLK (!OO>V,FPQ @I>PP) 152
3J>OQ 2L@HBQP (4ELOM) 420
PLCQ PBKPLOP 447
GC<75<HC5 108
PMIF@B() CRK@QFLK (0OL@BPPFKD) 149
PMOFKDP 134 , 135 , 136 , 246 , 247
(LLHB'P I>T 134
6BOIBQ#LKPQO>FKBA3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ-FK$FPQ>K@B3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ3MOFKD @I>PP (QLUF@IF?P) 246
4EB .>QROB LC #LAB (S005)
495
AFOB@QFLK LC CLO@B, ABQBOJFKFKD 136
IL@H() CRK@QFLK (QLUF@IF?P) 247
J>DKFQRAB LC CLO@B, ABQBOJFKFKD 135
OBPQ IBKDQE 136
QLUF@IF?P >KA 246
PQ>KA>OA ABSF>QFLK 11 , 12
@>I@RI>QFKD 12
S>OF>K@B 12
3Q>O 4OBH )): 4EB 7O>QE LC +E>K (1982) 143
PQ>QB (@BIIRI>O >RQLJ>Q>) 324
PQ>QF@ (?LAV QVMB) 199 , 209
PQ>QF@ CRK@QFLKP 55
PQBBOFKD ?BE>SFLOP 274 , 276 , 302 , 460
CILT CFBIA CLIILTFKD 276
DOLRM ?BE>SFLO >KA 302
MBO@BMQOLK CLO 460
T>KABOFKD 274
3QBBOFKD "BE>SFLOP CLO !RQLKLJLRP #E>O>@QBOP
(2BVKLIAP)
262
PQBBOFKD CLO@B 262 , 264 , 266 , 273
>OOFSFKD ?BE>SFLO >KA 273
ABPFOBA SBIL@FQV 264
J>DKFQRAB LC 266
PQBBOFKD MBO@BMQOLK 460 , 463
OBFKCLO@BJBKQ IB>OKFKD(KBRO>I KBQTLOHP) 463
PQBM() CRK@QFLK ("LU2$) 205
PQL@E>PQF@ CO>@Q>IP 358 , 374
QOBBP >P 374
3QOFKD"RCCBO @I>PP 385
PQOFKDP 251 , 385
3QOFKD"RCCBO @I>PP SP. 385
E>KDFKD COLJ CFUBA MLFKQP 251
PR?() CRK@QFLK (06B@QLO @I>PP) 38
PR?@I>PP 163 , 165
>AAFKD CRK@QFLK>IFQV QL PRMBO@I>PP L?GB@QP 165
PR?PBQ() CRK@QFLK (0OL@BPPFKD) 149
PRMBO() CRK@QFLK(0OL@BPPFKD) 164
PRMBO@I>PPBP 163 , 165 , 169 , 170
>AAFKD CRK@QFLK>IFQV TFQEFK PR?@I>PPBP 165
LSBOOFAFKD CRK@QFLKP COLJ 165 , 170
MLIVJLOMEFPJ >KA 169
PRMBOLOD>KFPJP 299
PRMBOSFPBA IB>OKFKD (KBRO>I KBQTLOHP) 446
PVJMIB@QF@ %RIBO ("LU2$) 240
T
Q>KDBKQ 110 , 111 , 112
>Q>K() CRK@QFLK (>O@Q>KDBKQ) 110
>Q>K2() CRK@QFLK 111
EB>AFKD2$() CRK@QFLK (06B@QLO @I>PP) 112
QBOJFK>I SBIL@FQV 64
QEBQ> 284
ALQ MOLAR@Q >KA 284
QEBQ> (^) 112
4ELOM, *BO 420
QFJB 117 , 326
@BIIRI>O >RQLJ>Q> >KA 326
JFIIFP() CRK@QFLK, JB>PROFKD TFQE 117
QFJB PBOFBP MOBAF@QFLK 447
QLUF@IF?P 241 , 242 , 244 , 246 , 249 , 253 , 256
!QQO>@QFLK"BE>SFLO @I>PP 256
"LU2$ SP. 241
6BOIBQ0EVPF@P @I>PP 244
6BOIBQ0EVPF@P2$ @I>PP 244
>QQO>@QFLK/OBMRIPFLK ?BE>SFLOP >KA 256
@LKKB@QBA PVPQBJP 249
ALTKIL>AFKD 242
CLO@B-AFOB@QBA DO>MEP 253
M>OQF@IBP, FJMIBJBKQFKD FK 244
PMOFKDP 246
TLOIA, ?RFIAFKD 244
QO>AFQFLK>I DBKBQF@ >IDLOFQEJP 391
QO>KPCLOJ>QFLK J>QOFU (0OL@BPPFKD) 375
4O>KPCLOJ>QFLKP QRQLOF>I (0OL@BPPFKD) 375
QO>KPI>QB() CRK@QFLK (0"LU2$) 207
QOBBP 374
QOFDLKLJBQOV 108 , 110 , 113 , 127
>Q>K() CRK@QFLK 110
@LP() CRK@QFLK (0OL@BPPFKD) 113
CLO@BP >KA 127
PFK() CRK@QFLK (0OL@BPPFKD) 113
GC<75<HC5 108
)KABU
496
Q>KDBKQ 110
4OLK (1982) 17
4ROQIB DO>MEF@P 386
/IFH@9G, /9FA=H9G, 5B8 /F5::=7 &5AG (2BPKF@H) 262
QTL-AFJBKPFLK>I @BIIRI>O >RQLJ>Q> 342 , 345
FJMIBJBKQFKD 345
U
5I>J, 3Q>KFP[>T 324
RKFCLOJ KRJ?BO AFPQOF?RQFLKP 6
5KFCLOJFQV @I>PP (7LICO>J @I>PPFCF@>QFLK) 340
RKFQ SB@QLOP 43
RKFSBOP>I DO>SFQ>QFLK>I @LKPQ>KQ 89
RKPRMBOSFPBA IB>OKFKD (KBRO>I KBQTLOHP) 447
RMA>QB() CRK@QFLK (QLUF@IF?P) 244
V
S>OF>K@B 12
S>OF>QFLK (K>QRO>I PBIB@QFLK) 394
6B@2 ("LU2$ BIBJBKQ) 194 , 195
>AAFKD SB@QLOP TFQE 195
J>KFQRAB, CFKAFKD 195
JRIQFMIVFKD SB@QLOP TFQE 195
KLOJ>IFWFKD SB@QLOP 195
P@>IFKD SB@QLOP TFQE 195
6B@2$ (QLUF@IF?P QVMB) 243 , 246
6BOIBQ0>OQF@IB2$ @I>PP >KA 246
J>QE CRK@QFLKP CLO 243
6B@3$ (QLUF@IF?P QVMB) 243
SB@QLO KLQ>QFLK, SP. P@>I>O KLQ>QFLK 33
SB@QLOP 27 , 28 , 30 , 31 , 33 , 39 , 40 , 42 , 43 ,
45 , 49 , 109 , 110 , 194 , 278 , 282
0OL@BPPFKD >KA 30
6B@2 ("LU2$ BIBJBKQ) 194
>@@BIBO>QFLK 49
>AAFKD 33
>P OFDEQ QOF>KDIBP 109
>PPL@F>QFSB/AFPQOF?RQFSB ORIBP CLO JRIQFMIF@>QFLK/
AFSFPFLK LC
42
?LRK@FKD ?>II PHBQ@E 28
@LJJRQ>QFSB/>PPL@F>QFSB ORIBP LC >AAFQFLK/
PR?QO>@QFLK TFQE
39
ABCFKBA 27
ALQ MOLAR@Q 282
CILT CFBIAP, @LJMRQFKD CLO 278
IL@>QFLKP >KA 31
J>DKFQRAB 42
JLQFLK, FJMIBJBKQFKD TFQE 33
JRIQFMIVFKD 40
KLOJ>IFW>QFLK 43
KLQ>QFLK 27
P@>IFKD 40
Q>KDBKQ 110
RKFQ SB@QLOP 43
SBIL@FQV >KA 31 , 45
19<=7@9G: !LD9F=A9BHG =B .MBH<9H=7 ,GM7<C@C;M
("O>FQBK?BOD)
262
SBIL@FQV 31 , 45 , 49 , 51 , 274
>@@BIBO>QFLK 49
>P SB@QLO 31
ABPFOBA, CLO >RQLKLJLRP >DBKQP 274
IFJFQFKD 51
6BOIBQ FKQBDO>QFLK 240 , 241
QLUF@IF?P 241
6BOIBQ#LKPQO>FKBA3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ-FK$FPQ>K@B3MOFKD @I>PP (QLUF@IF?P) 246
6BOIBQ0>OQF@IB2$ L?GB@Q (QLUF@IF?P) 245
6BOIBQ0EVPF@P @I>PP (QLUF@IF?P) 242 , 244
@LOB BIBJBKQP LC 242
6BOIBQ0EVPF@P2$ @I>PP (QLUF@IF?P) 244
6BOIBQ3MOFKD @I>PP (QLUF@IF?P) 246
SFP@LRP CLO@B 83
SLK .BRJ>KK, *LEK 324
W
T>KABOFKD ?BE>SFLO (2BVKLIAP) 274
T>SBP 122 , 124
>KDRI>O SBIL@FQV, ABCFKFKD TFQE 122
S>OVFKD 124
4EB .>QROB LC #LAB (S005)
497
TBFDEQ 67 , 446
J>PP SP. 67
KBRO>I KBQTLOHP >KA 446
TEBBI LC CLOQRKB MOL?>?FIFQV JBQELA 398
7LICO>J @I>PPFCF@>QFLK 340 , 341
#LJMIBUFQV @I>PP 341
2>KALJ @I>PP 341
2BMBQFQFLK @I>PP 340
5KFCLOJFQV @I>PP 340
7LICO>J, 3QBMEBK 325 , 340
7LICO>J @I>PPFCF@>QFLK 340
BIBJBKQ>OV @BIIRI>O >RQLJ>Q> >IDLOFQEJ 325
7LOIA @I>PP ("LU2$) 194 , 196 , 203
@OB>QB7LOIA() CRK@QFLK (0"LU2$) 196
DBQ"LAV,FPQ() CRK@QFLK 203
X
8/2 (BU@IRPFSB LO) 466
)KABU
498

You might also like