Plotting a semi circle
create a plot describing a semi-circle.
HTML
<!-- Graph layout with semi circle plotted -->
<body>
<canvas id="myCanvas" width="578" height="500"></canvas>
</body>
CSS
body {
margin: 0px;
padding: 0px;
}
JavaScript
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var width = 500;
var height = 500;
drawGrid(width,height,'#ccc');
drawAxis(width,height,'#666');
// whole circle in grey
drawSemicircle(width/2, height/2, 100, 0, 360, 10, '#ccc');
function drawSemicircle(cx, cy, r, startAngle, endAngle, increment, style)
{
var x=0;
var y=0;
for (a=startAngle;a<=endAngle;a+=increment) {
x = r * Math.cos(deg2Rad(a)) + cx;
y = r * Math.sin(deg2Rad(a)) + cy;
plotPoint(x, y, 4, style);
}
}
function plotPoint(x, y, size, style) {
var radius = size/2;
context.save();
context.beginPath();
context.translate(x, y);
context.arc(0,0,radius,0,2 * Math.PI, true);
context.fillStyle=style;
context.fill();
context.restore();
}
function drawAxis(w,h,style)
{
// x axis
var x = w/2;
var y = h/2;
context.fillStyle=style;
for (var i=-x;i<x;i+=(w*.05)){
context.fillText(i, x+i, y+12);
}
// y axis
for (var i=-y;i<y;i+=(h*.05)){
context.fillText(i, x-(context.measureText(i).width+4), y+i);
}
}
function drawGrid(w,h,style)
{
context.beginPath();
var x=0;
var y=0;
for (y=0;y<h;y+=(h*.05)){
context.moveTo(x, y);
context.lineTo(w, y);
}
y=0;
for (var x=0;x<w;x+=(w*.05)){
context.moveTo(x, y);
context.lineTo(x, h);
}
context.strokeStyle=style;
context.stroke();
}
function deg2Rad(deg){
return deg*Math.PI/180;
}