@@ -5,6 +5,8 @@ let width = window.innerWidth - 300;
5
5
let gridSize = 20 ;
6
6
const algebra = document . getElementById ( 'algebra' ) ;
7
7
const algebraBtn = document . getElementById ( 'algbBtn' ) ;
8
+ const trigo = document . getElementById ( "trigo" ) ;
9
+ const trigoBtn = document . getElementById ( 'trigoBtn' ) ;
8
10
const span = document . getElementById ( 'equation' ) ;
9
11
canvas . height = height ;
10
12
canvas . width = width ;
@@ -15,7 +17,9 @@ window.addEventListener('resize',()=>{
15
17
} ) ;
16
18
let vlines = 0 ;
17
19
let hlines = 0 ;
18
- let algb = true ;
20
+ let algb = true , trigonometry = false ;
21
+ var value = '' ;
22
+ var pts = [ ] ;
19
23
function drawGrid ( )
20
24
{
21
25
let yPlot = gridSize ;
@@ -106,15 +110,90 @@ function init()
106
110
{
107
111
drawAlgebraGraph ( ) ;
108
112
}
113
+ if ( trigonometry )
114
+ {
115
+ drawTrigoGraph ( ) ;
116
+ }
109
117
xCoord ( ) ;
110
118
yCoord ( ) ;
111
119
ctx . restore ( ) ;
112
120
}
121
+ algebraBtn . addEventListener ( "click" , ( ) => {
122
+ algb = true ;
123
+ trigonometry = false ;
124
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
125
+ ctx . beginPath ( ) ;
126
+ ctx . closePath ( ) ;
127
+ init ( ) ;
128
+ } )
129
+ trigoBtn . addEventListener ( "click" , ( ) => {
130
+ algb = false ;
131
+ trigonometry = true ;
132
+ ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
133
+ ctx . beginPath ( ) ;
134
+ ctx . closePath ( ) ;
135
+ init ( ) ;
136
+ } )
137
+ algebra . addEventListener ( 'input' , ( e ) => {
138
+ value = '' ;
139
+ value = e . target . value ;
140
+ } ) ;
141
+ trigo . addEventListener ( "input" , ( e ) => {
142
+ value = '' ;
143
+ value = e . target . value ;
144
+ } ) ;
145
+ function drawTrigoGraph ( )
146
+ {
147
+ var i = 0 ;
148
+ const parser = math . parser ( ) ;
149
+ if ( value . length > 0 )
150
+ {
151
+ parser . evaluate ( `f(x) = ${ value } ` ) ;
152
+ for ( var x = - 40 ; x < 40 ; x += Math . PI / 180 )
153
+ {
154
+ let ypt = parser . evaluate ( `f(${ x } )` ) ;
155
+ pts [ i ] = { x :x , y :ypt } ;
156
+ i ++ ;
157
+ }
158
+ }
159
+ span . innerText = value ;
160
+ ctx . save ( ) ;
161
+ ctx . translate ( ( Math . floor ( hlines / 2 ) * gridSize ) , Math . floor ( vlines / 2 ) * gridSize ) ;
162
+ ctx . beginPath ( ) ;
163
+ for ( var i = 0 ; i < pts . length ; i ++ )
164
+ {
165
+ ctx . lineTo ( pts [ i ] . x * gridSize , - pts [ i ] . y * gridSize ) ;
166
+ ctx . strokeStyle = 'red' ;
167
+ ctx . lineJoin = 'round' ;
168
+ ctx . stroke ( ) ;
169
+ }
170
+ ctx . closePath ( ) ;
171
+ }
113
172
function drawAlgebraGraph ( )
114
173
{
174
+ var i = 0 ;
175
+ pts = [ ] ;
176
+ if ( value . length > 0 )
177
+ {
178
+ for ( var x = - 100 ; x < 100 ; x += 0.3 )
179
+ {
180
+ let ypt = eval ( value ) ;
181
+ pts [ i ] = { x :x , y :ypt } ;
182
+ i ++ ;
183
+ }
184
+ }
185
+ span . innerText = value ;
115
186
ctx . save ( ) ;
116
187
ctx . translate ( ( Math . floor ( hlines / 2 ) * gridSize ) , Math . floor ( vlines / 2 ) * gridSize ) ;
117
188
ctx . beginPath ( ) ;
189
+ for ( var i = 0 ; i < pts . length ; i ++ )
190
+ {
191
+ ctx . lineTo ( pts [ i ] . x * gridSize , - pts [ i ] . y * gridSize ) ;
192
+ ctx . lineWidth = '2' ;
193
+ ctx . strokeStyle = 'red' ;
194
+ ctx . lineJoin = 'round' ;
195
+ ctx . stroke ( ) ;
196
+ }
118
197
ctx . closePath ( ) ;
119
198
}
120
199
init ( ) ;
0 commit comments