📄 ---speakers.nlogo
字号:
breeds [ lefts rights sums ]turtles-own [ yvel-old yvel-new ypos-old ypos-new time ]globals [ time-ticks listening-point ]to setup ca set-default-shape turtles "circle" ;; Create the turtles that represent the waves ;; We need three lines across the screen, so it sets ;; their xcor based on their incrementing turtle id cct-lefts screen-size-x [ set xcor ( who - screen-edge-x ) set color yellow ] cct-rights screen-size-x [ set xcor ( who - screen-edge-x ) set color cyan ] cct-sums screen-size-x [ set xcor ( who - screen-edge-x ) set color red ] ;; Initialize all variables to zero. All of the turtles are stationary. set listening-point 0 set time-ticks 0 ask turtles [ set yvel-old 0 set ypos-old 0 set time 0 ] ;; The ends of the waves are special. One side drives the wave, while ;; the other side anchors the waves- (prevents wrapping) ;; First define the driving turtles, which are colored green. ;; Next define the anchor turtles, which are colored blue ask lefts [ if ( xcor = screen-edge-x ) [ set color blue ] if ( xcor = (- screen-edge-x) ) [ set color green ] ] ask rights [ if ( xcor = (- screen-edge-x) ) [ set color blue ] if ( xcor = screen-edge-x ) [ set color green ] ] ;; draw the speakers, gray centered line, and listening point ask patches [ if pycor = 0 [ set pcolor gray ] draw-left-speaker draw-right-speaker if ( pxcor = listening-point and pycor > 0 and pycor < 4 ) [ set pcolor white ] ] setup-plotendto go ;; Increase the model time set time-ticks time-ticks + 1 ;; Move all the wave turtles ask turtles [ if ( color = green ) [ drive-force ] if ( color = yellow or color = cyan ) [ driven-force ] if ( color = blue ) [ update ] if ( color = red ) [ interfere ] ] ;; Update the plot if ( plot? ) [ get-a-point plot-the-point ] ;; Reset the velocities ask turtles [ set yvel-old yvel-new set ypos-old ypos-new ] ;; Check if a hide/show switch changed show-or-hideendto drive-force ;; procedure for green turtles set time time + 1 ifelse (breed = lefts) [ set ypos-new amplitude-left * ( sin (frequency-left * 0.1 * time )) ] [ set ypos-new amplitude-right * ( sin (frequency-right * 0.1 * time )) ] set ypos-old ypos-new set ycor ypos-newendto driven-force ;; procedure for yellow and cyan turtles set yvel-new yvel-old + ( ypos-old-of turtle ( who - 1 ) ) - ypos-old + ( ypos-old-of turtle ( who + 1 ) ) - ypos-old set yvel-new ( ( 1000 - friction ) / 1000 ) * yvel-new set ypos-new ypos-old + yvel-new set ycor ypos-newendto update ;; procedure for blue turtles ifelse ( breed = lefts ) [ set ypos-new ypos-old-of turtle ( who - 1 ) ] [ set ypos-new ypos-old-of turtle ( who + 1 ) ] set ycor ypos-newendto interfere ;; procdure for red turtles set ypos-new ( ( ypos-new-of turtle ( who - screen-size-x ) ) + ( ypos-new-of turtle ( who - ( 2 * screen-size-x ) ) ) ) set ycor ypos-new ifelse (( abs ypos-new ) <= screen-edge-y) and show-sum? [ showturtle ] [ hideturtle ]endto setup-plot set-current-plot "Speaker amplitude" clear-plot set-current-plot-pen "left" set-plot-pen-color yellow set-current-plot-pen "right" set-plot-pen-color cyan set-current-plot-pen "sum" set-plot-pen-color red set-plot-y-range ( ( - amplitude-left ) - amplitude-right ) ( amplitude-left + amplitude-right ) set-plot-x-range 0 250 auto-plot-onendto plot-the-point if ((time-ticks mod 200) = 0) [ ifelse (not show-only-recent-plot?) [ set-plot-x-range 0 (time-ticks + 200) ] [ set-plot-x-range (time-ticks - 210) (time-ticks + 210)] ] set-current-plot-pen "left" if show-left? and any? lefts with [ round xcor = listening-point ] [ plotxy time-ticks ypos-new-of random-one-of lefts with [ round xcor = listening-point ] ] set-current-plot-pen "right" if show-right? and any? rights with [ round xcor = listening-point ] [ plotxy time-ticks ypos-new-of random-one-of rights with [ round xcor = listening-point ] ] set-current-plot-pen "sum" if show-sum? and any? sums with [ round xcor = listening-point ] [ plotxy time-ticks ypos-new-of random-one-of sums with [ round xcor = listening-point ] ]endto get-a-point ;; Changes the listening-point if the mouse is down if mouse-down? [ ask patches with [ pxcor = listening-point and pycor > 0 and pycor < 4 ] [ set pcolor black ] set listening-point round mouse-xcor ask patches with [ pxcor = listening-point and pycor > 0 and pycor < 4 ] [ set pcolor white ] ]endto draw-right-speaker ;; patch procedure if ( pxcor = screen-edge-x ) and ( pycor > ( -0.1 * screen-edge-x ) ) and ( pycor < ( 0.1 * screen-edge-x ) ) [ set pcolor orange ] if ( pxcor = round ( 0.9 * screen-edge-x ) ) and ( pycor > ( -0.2 * screen-edge-x ) ) and ( pycor < ( 0.2 * screen-edge-x ) ) [ set pcolor orange ]endto draw-left-speaker ;; patch procedure if ( pxcor = ( - screen-edge-x ) ) and ( pycor > ( -0.1 * screen-edge-x ) ) and ( pycor < ( 0.1 * screen-edge-x ) ) [ set pcolor orange ] if ( pxcor = round ( - ( 0.9 * screen-edge-x ) ) ) and ( pycor > ( -0.2 * screen-edge-x ) ) and ( pycor < ( 0.2 * screen-edge-x ) ) [ set pcolor orange ]endto show-or-hide ;; The sums are hidden in the interference procedure because it has the possibility ;; of wrapping when the sum amplitudes are too large. ifelse show-left? [ if any? lefts with [ hidden? ] [ ask lefts [ st ] ] ] [ if any? lefts with [ not hidden? ] [ ask lefts [ ht ] ] ] ifelse show-right? [ if any? rights with [ hidden? ] [ ask rights [ st ] ] ] [ if any? rights with [ not hidden? ] [ ask rights [ ht ] ] ]end; *** NetLogo Model Copyright Notice ***;; This model was created as part of the project: CONNECTED MATHEMATICS:; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL; MODELS (OBPML). The project gratefully acknowledges the support of the; National Science Foundation (Applications of Advanced Technologies; Program) -- grant numbers RED #9552950 and REC #9632612.;; Copyright 1998 by Uri Wilensky. All rights reserved.;; Permission to use, modify or redistribute this model is hereby granted,; provided that both of the following requirements are followed:; a) this copyright notice is included.; b) this model will not be redistributed for profit without permission; from Uri Wilensky.; Contact Uri Wilensky for appropriate licenses for redistribution for; profit.;; This model was converted to NetLogo as part of the project:; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN; CLASSROOMS. The project gratefully acknowledges the support of the; National Science Foundation (REPP program) -- grant number REC #9814682.; Converted from StarLogoT to NetLogo, 2002. Updated 2002.;; To refer to this model in academic publications, please use:; Wilensky, U. (1998). NetLogo Speakers model.; http://ccl.northwestern.edu/netlogo/models/Speakers.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; In other publications, please use:; Copyright 1998 by Uri Wilensky. All rights reserved. See; http://ccl.northwestern.edu/netlogo/models/Speakers; for terms of use.;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW3231081744560504.01101110001CC-WINDOW5556826651Command Center0BUTTON851015943gogoT1TOBSERVERTNILBUTTON11108543setupsetupNIL1TOBSERVERTNILSLIDER1599147132amplitude-leftamplitude-left050.012.011NILSLIDER1761130844frictionfriction0100.00.011NILSLIDER156614799frequency-leftfrequency-left1100.071.011NILMONITOR16144113193listening-pointlistening-point31SWITCH212474316507plot?plot?01-1000PLOT9199311459Speaker amplitudeTimeAmplitude0.0250.0-61.050.0falsetruePENS"left" 1.0 0 -1184463 true"right" 1.0 0 -11221820 true"sum" 1.0 0 -2674135 trueSWITCH11474209507show-only-recent-plot?show-only-recent-plot?01-1000SWITCH10509122542show-left?show-left?01-1000SWITCH245509362542show-right?show-right?1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -