44var isFirstLoop = true ;
55
66function makeMainLoop ( ) {
7- let args = Array . from ( arguments ) ;
8- return function ( ) {
9- mainLoop ( ...args ) ;
10- } ;
7+ let args = Array . from ( arguments ) ;
8+ return function ( ) {
9+ mainLoop ( ...args ) ;
10+ } ;
1111}
1212
1313function mainLoop ( particlesLeft , particlesRight , translation ) {
14- let leftTemps = [ ] ;
15- let rightTemps = [ ] ;
16- backgroundLoop ( ) ;
17- c = ctx ;
14+ let leftTemps = [ ] ;
15+ let rightTemps = [ ] ;
16+ backgroundLoop ( ) ;
17+ c = ctx ;
1818
19- particlesLeft . map ( function ( particle , i ) {
20- leftTemps . push ( particle . vel . r ) ;
21- particle . x += particle . vel . x / particle . mass ;
22- particle . y += particle . vel . y / particle . mass ;
23- c . fillStyle = particleColor ;
24- reflectParticle ( particle , 0 , i , particlesLeft , particlesRight ) ;
25- particleCollisions ( i , particlesLeft ) ;
26- particle . fill ( c , particle . radius ) ;
19+ particlesLeft . map ( function ( particle , i ) {
20+ leftTemps . push ( particle . vel . r ) ;
21+ particle . x += particle . vel . x / particle . mass ;
22+ particle . y += particle . vel . y / particle . mass ;
23+ c . fillStyle = particleColor ;
24+ reflectParticle ( particle , 0 , i , particlesLeft , particlesRight ) ;
25+ particleCollisions ( i , particlesLeft ) ;
26+ particle . fill ( c , particle . radius ) ;
2727
28- c . strokeStyle = 'black' ;
29- c . lineWidth = 0.2 ;
30- } ) ;
31-
32- roundedTempLeft = Math . round ( average ( leftTemps ) * particleMass * roundingAccuaracy ) / roundingAccuaracy ;
33- $ ( '#tempLeft' ) . html ( roundedTempLeft ) ;
28+ c . strokeStyle = 'black' ;
29+ c . lineWidth = 0.2 ;
30+ } ) ;
31+
32+ roundedTempLeft = Math . round ( average ( leftTemps ) * particleMass * roundingAccuaracy ) / roundingAccuaracy ;
33+ $ ( '#tempLeft' ) . html ( roundedTempLeft ) ;
3434
35- particlesRight . map ( function ( particle , i ) {
36- rightTemps . push ( particle . vel . r ) ;
37- particle . x += particle . vel . x / particle . mass ;
38- particle . y += particle . vel . y / particle . mass ;
39- c . fillStyle = particleColor ;
40- reflectParticle ( particle , translation , i , particlesLeft , particlesRight ) ;
41- particleCollisions ( i , particlesRight ) ;
42- particle . fill ( c , particle . radius ) ;
35+ particlesRight . map ( function ( particle , i ) {
36+ rightTemps . push ( particle . vel . r ) ;
37+ particle . x += particle . vel . x / particle . mass ;
38+ particle . y += particle . vel . y / particle . mass ;
39+ c . fillStyle = particleColor ;
40+ reflectParticle ( particle , translation , i , particlesLeft , particlesRight ) ;
41+ particleCollisions ( i , particlesRight ) ;
42+ particle . fill ( c , particle . radius ) ;
4343
44- c . strokeStyle = 'black' ;
45- c . lineWidth = 0.2 ;
46- } ) ;
47-
48- roundedTempRight = Math . round ( average ( rightTemps ) * particleMass * roundingAccuaracy ) / roundingAccuaracy ;
49- $ ( '#tempRight' ) . html ( roundedTempRight ) ;
50-
51- if ( ! allowCrossOver ) {
52- c . beginPath ( )
53- c . fillStyle = barrierColor ;
54- c . rect ( c . canvas . width / canvasRatio , 0 , ( ( 2 - canvasRatio ) * c . canvas . width ) / canvasRatio , c . canvas . height )
55- c . fill ( )
56- c . closePath ( )
57- }
58-
59- if ( isStopwatchRunning ) {
60- stopwatchTime += 1 ;
61- $ ( '#time' ) . html ( Math . round ( stopwatchTime / fps ) ) ;
62- if ( Math . abs ( roundedTempLeft - roundedTempRight ) <= 0.01 ) {
63- $ ( '[type=checkbox] ' ) . click ( ) ;
64- alert ( 'You have reached a thermal temperature of ' + roundedTempLeft + " in " + Math . round ( stopwatchTime / fps ) + " seconds" ) ;
65- stopwatchTime = 0 ;
66- $ ( '#time' ) . html ( 0 ) ;
67- }
68- }
44+ c . strokeStyle = 'black' ;
45+ c . lineWidth = 0.2 ;
46+ } ) ;
47+
48+ roundedTempRight = Math . round ( average ( rightTemps ) * particleMass * roundingAccuaracy ) / roundingAccuaracy ;
49+ $ ( '#tempRight' ) . html ( roundedTempRight ) ;
50+
51+ if ( ! allowCrossOver ) {
52+ c . beginPath ( )
53+ c . fillStyle = barrierColor ;
54+ c . rect ( c . canvas . width / canvasRatio , 0 , ( ( 2 - canvasRatio ) * c . canvas . width ) / canvasRatio , c . canvas . height )
55+ c . fill ( )
56+ c . closePath ( )
57+ }
58+
59+ if ( isStopwatchRunning ) {
60+ stopwatchTime += 1 ;
61+ $ ( '#time' ) . html ( Math . round ( stopwatchTime / fps ) ) ;
62+ if ( Math . abs ( roundedTempLeft - roundedTempRight ) <= 0.01 ) {
63+ $ ( '#barrierButton ' ) . click ( ) ;
64+ alert ( 'You have reached a thermal temperature of ' + roundedTempLeft + " in " + Math . round ( stopwatchTime / fps ) + " seconds" ) ;
65+ stopwatchTime = 0 ;
66+ $ ( '#time' ) . html ( 0 ) ;
67+ }
68+ }
6969}
7070
7171function average ( array ) {
72- var sum = array . reduce ( function ( acc , val ) {
73- return acc + val ;
74- } ) ;
75- var avg = sum / array . length ;
76- return avg ;
72+ var sum = array . reduce ( function ( acc , val ) {
73+ return acc + val ;
74+ } ) ;
75+ var avg = sum / array . length ;
76+ return avg ;
7777}
7878
7979function particleCollisions ( checkIndex , particles ) {
80- particle = particles [ checkIndex ] ;
81- particles . map ( function ( particleToCheck , i ) {
82- if ( i < checkIndex ) {
83- var distance = particleToCheck . distance ( particle ) ;
84- if ( distance . r <= particleToCheck . radius + particle . radius ) {
85- let wallAngle = distance . deg + 90 ;
86- particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
87- let newSpeed = ( particleToCheck . vel . r + particle . vel . r ) / 2
88- particleToCheck . vel . deg = 2 * wallAngle + particleToCheck . vel . deg ;
89- particleToCheck . vel . r = newSpeed ;
90- particle . vel . r = newSpeed ;
91- }
92- }
93- } ) ;
80+ particle = particles [ checkIndex ] ;
81+ particles . map ( function ( particleToCheck , i ) {
82+ if ( i < checkIndex ) {
83+ var distance = particleToCheck . distance ( particle ) ;
84+ if ( distance . r <= particleToCheck . radius + particle . radius ) {
85+ let wallAngle = distance . deg + 90 ;
86+ particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
87+ let newSpeed = ( particleToCheck . vel . r + particle . vel . r ) / 2
88+ particleToCheck . vel . deg = 2 * wallAngle + particleToCheck . vel . deg ;
89+ particleToCheck . vel . r = newSpeed ;
90+ particle . vel . r = newSpeed ;
91+ }
92+ }
93+ } ) ;
9494}
9595
9696function backgroundLoop ( ) {
97- c = ctx ;
98- c . beginPath ( )
99- c . fillStyle = backgroundColor ;
100- c . rect ( 0 , 0 , c . canvas . width , c . canvas . height )
101- c . fill ( )
102- c . closePath ( )
97+ c = ctx ;
98+ c . beginPath ( )
99+ c . fillStyle = backgroundColor ;
100+ c . rect ( 0 , 0 , c . canvas . width , c . canvas . height )
101+ c . fill ( )
102+ c . closePath ( )
103103}
104104
105105function reflectParticle ( particle , translation , i , particlesLeft , particlesRight ) {
106- //right side
107- if ( particle . x + particle . radius >= 100 + translation ) {
108- if ( translation == 0 && allowCrossOver ) {
109- particle . x += 1 ;
110- particlesRight . push ( particle ) ;
111- particlesLeft . splice ( i , 1 ) ;
112- } else {
113- while ( particle . x + particle . radius >= 100 + translation ) {
114- particle . x -= 1 ;
115- }
116- let wallAngle = 90 ;
117- particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
118- }
119- }
106+ //right side
107+ if ( particle . x + particle . radius >= 100 + translation ) {
108+ if ( translation == 0 && allowCrossOver ) {
109+ particle . x += 1 ;
110+ particlesRight . push ( particle ) ;
111+ particlesLeft . splice ( i , 1 ) ;
112+ } else {
113+ while ( particle . x + particle . radius >= 100 + translation ) {
114+ particle . x -= 1 ;
115+ }
116+ let wallAngle = 90 ;
117+ particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
118+ }
119+ }
120120
121- //left side
122- if ( particle . x - particle . radius <= 0 + translation ) {
123- if ( translation != 0 && allowCrossOver ) {
124- particle . x -= 1 ;
125- particlesLeft . push ( particle ) ;
126- particlesRight . splice ( i , 1 ) ;
127- } else {
128- while ( particle . x + particle . radius <= 0 + translation ) {
129- particle . x += 1 ;
130- }
131- let wallAngle = - 90 ;
132- particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
133- }
134- }
121+ //left side
122+ if ( particle . x - particle . radius <= 0 + translation ) {
123+ if ( translation != 0 && allowCrossOver ) {
124+ particle . x -= 1 ;
125+ particlesLeft . push ( particle ) ;
126+ particlesRight . splice ( i , 1 ) ;
127+ } else {
128+ while ( particle . x + particle . radius <= 0 + translation ) {
129+ particle . x += 1 ;
130+ }
131+ let wallAngle = - 90 ;
132+ particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
133+ }
134+ }
135135
136- //top
137- if ( particle . y + particle . radius >= 100 ) {
138- while ( particle . y + particle . radius >= 100 ) {
139- particle . y -= 1 ;
140- }
141- let wallAngle = 0 ;
142- particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
143- }
136+ //top
137+ if ( particle . y + particle . radius >= 100 ) {
138+ while ( particle . y + particle . radius >= 100 ) {
139+ particle . y -= 1 ;
140+ }
141+ let wallAngle = 0 ;
142+ particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
143+ }
144144
145- //bottomn
146- if ( particle . y - particle . radius <= 0 ) {
147- while ( particle . y + particle . radius <= 0 ) {
148- particle . y += 1 ;
149- }
150- let wallAngle = 180 ;
151- particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
152- }
145+ //bottomn
146+ if ( particle . y - particle . radius <= 0 ) {
147+ while ( particle . y + particle . radius <= 0 ) {
148+ particle . y += 1 ;
149+ }
150+ let wallAngle = 180 ;
151+ particle . vel . deg = 2 * wallAngle - particle . vel . deg ;
152+ }
153153}
0 commit comments