|
|||
|
|||
|
|||
|
Enter R and θ, θ should be in degrees.
R θ
Enter R and θ, θ should be in degrees. R <input type="text" id="demo1rForm"> θ <input type="text" id="demo1thetaForm">
function Reset() { document.getElementById("demo1rForm").value = 0; document.getElementById("demo1thetaForm").value = 0; Clear("circles"); PolarAxis("circles"); UpdateTable(0, 0); return; }
function PlotPolarPoint() { let r = document.getElementById("demo1rForm").value; let theta = document.getElementById("demo1thetaForm").value; PlotPoint("circles", r, theta); UpdateTable(r,theta); return; }
<p> <button type="button" onclick="PlotPolarPoint()"> Plot</button> <button type="button" onclick="Reset()"> Clear </button>
function PlotPolarPoint() { let r = document.getElementById("demo1rForm").value; let theta = document.getElementById("demo1thetaForm").value; PlotPoint("circles", r, theta); UpdateTable(r,theta); return; }
function UpdateTable(r, theta) { let x,y; x = Math.round(r * Math.cos(theta*Math.PI/180.0)); y = Math.round(r * Math.sin(theta*Math.PI/180.0)); document.getElementById("xvalue").innerText = x; document.getElementById("yvalue").innerText = y; document.getElementById("rvalue").innerText = r; document.getElementById("tvalue").innerText = theta; return; }
function Clear(canvasName) { let canvas = document.getElementById(canvasName); let ctx = canvas.getContext('2d'); ctx.clearRect(0,0,canvas.width, canvas.height); return; }
function PlotPoint(canvasName, r, theta){ let canvas = document.getElementById(canvasName); let ctx = canvas.getContext('2d'); let cx = canvas.width/2; let cy = canvas.height/2; let t2 = theta * Math.PI/180.0; let rstep = Math.min(canvas.width, canvas.height)/20; let r2 = r * rstep; let x = cx + r2*Math.cos(t2); // but the y axis of the canvas is upside down, so -r sin(theta) let y = cy - r2*Math.sin(t2) ctx.fillStyle = "red"; ctx.beginPath(); ctx.arc(x,y,3,0, 2*Math.PI); ctx.fill(); return; }
let x = cx + r2*Math.cos(t2); // but the y axis of the canvas is upside down, so -r sin(theta) let y = cy - r2*Math.sin(t2)
function PolarAxis(canvasName){ let canvas = document.getElementById(canvasName); let ctx = canvas.getContext('2d'); let size = Math.min(canvas.width, canvas.height)/2; let rstep = size/10; let cx = canvas.width/2; let cy = canvas.height/2; let theta; let x,y; ctx.strokeStyle = "black"; ctx.lineWidth = 1; // draw the circles ctx.beginPath(); for(let i=0; i <= 10; i++) { ctx.arc(cx, cy, i*rstep, 0, 2*Math.PI); } // draw the lines for (let i=0; i<8;i++) { ctx.moveTo(cx,cy); theta = Math.PI/4 * i; x = cx + size*Math.cos(theta); y = cy + size*Math.sin(theta) ctx.lineTo(x,y); } ctx.stroke(); return; }