Skip to content

Commit 9c3b7ff

Browse files
committed
Added trigo and algebra graph options
1 parent 065225f commit 9c3b7ff

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Graphing Calculator</title>
77
<link rel="stylesheet" href="style.css">
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/7.2.0/math.min.js" integrity="sha512-4VdpCxbABk7wjJb/9z/d3EyibeHwUY0FgHbFO4EiIPVtyyV6llhENCvzRk8SH5D86+Jwm6QLagqRRnEJCd3gJg==" crossorigin="anonymous"></script>
89
</head>
910
<body>
1011
<h1>Graph Calculator</h1>
@@ -20,13 +21,13 @@ <h2>Equation: <span id="equation"></span></h3>
2021
<div class="form-group">
2122
<label for="Trigo">Trigonometric Expression:</label>
2223
<input id="trigo" type="text" placeholder="Type the function here">
24+
<small style="font-size: 11px;">Write trigo functions as sin(2x) or cos(3x)</small>
2325
<button id="trigoBtn">View Graph</button>
2426

2527
</div>
2628
</div>
2729
<canvas id="graph"></canvas>
2830
</div>
29-
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/7.2.0/math.min.js" integrity="sha512-4VdpCxbABk7wjJb/9z/d3EyibeHwUY0FgHbFO4EiIPVtyyV6llhENCvzRk8SH5D86+Jwm6QLagqRRnEJCd3gJg==" crossorigin="anonymous"></script>
3031
<script src="script.js"></script>
3132
</body>
3233
</html>

script.js

+80-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ let width = window.innerWidth - 300;
55
let gridSize = 20;
66
const algebra = document.getElementById('algebra');
77
const algebraBtn = document.getElementById('algbBtn');
8+
const trigo = document.getElementById("trigo");
9+
const trigoBtn = document.getElementById('trigoBtn');
810
const span = document.getElementById('equation');
911
canvas.height = height;
1012
canvas.width = width;
@@ -15,7 +17,9 @@ window.addEventListener('resize',()=>{
1517
});
1618
let vlines = 0;
1719
let hlines = 0;
18-
let algb = true;
20+
let algb = true,trigonometry=false;
21+
var value = '';
22+
var pts = [];
1923
function drawGrid()
2024
{
2125
let yPlot = gridSize;
@@ -106,15 +110,90 @@ function init()
106110
{
107111
drawAlgebraGraph();
108112
}
113+
if(trigonometry)
114+
{
115+
drawTrigoGraph();
116+
}
109117
xCoord();
110118
yCoord();
111119
ctx.restore();
112120
}
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+
}
113172
function drawAlgebraGraph()
114173
{
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;
115186
ctx.save();
116187
ctx.translate((Math.floor(hlines/2)*gridSize),Math.floor(vlines/2)*gridSize);
117188
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+
}
118197
ctx.closePath();
119198
}
120199
init();

0 commit comments

Comments
 (0)