Display different values ​​at different angles in a circle using html5 canvas

Using HTML5 and Javascript canvas I need to display different values ​​(represented by dots) at different angles inside the circle.

Examples of data:
val 34% @ 0 °,
val 54% @ 12 °,
val 23% @ 70 °,
and so on ...

If I have a canvas 300 x 300px and the center of the circle is at x: 150px and y: 150px with a radius of 150px, how would I figure out where to set my point for a value of 54% at 12 degrees

My math looks awful xD

I would be grateful for any help and please ask questions if I do not make myself clear enough.

Thank you for listening and in advance for your deep understanding: D

EDIT (explain in more detail):

, , : Illustration: values ​​at different angles / degrees

, .
( , , )

!

+5
2

(, ) :

// θ : angle in [0, 2π[
function polarToCartesian(r, θ) {
    return {x:r*Math.cos(θ), y: r*Math.sin(θ)};
}

, 12 °, :

var p = polarToCartesian(150, 12*2*Math.PI/360);
p.x += 150; p.y += 150;

EDIT: polarToCartesian radians , Canvas API. , :

 function degreesToRadians(a) {
     return Math.PI*a/180;
 }
+6

()

var can = document.getElementById('mycanvas');
var ctx = can.getContext('2d');

var drawAngledLine = function(x, y, length, angle) {
    var radians = angle / 180 * Math.PI;
    var endX = x + length * Math.cos(radians);
    var endY = y - length * Math.sin(radians);

    ctx.beginPath();
    ctx.moveTo(x, y)
    ctx.lineTo(endX, endY);
    ctx.closePath();
    ctx.stroke();
}

var drawCircle = function(x, y, r) {
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI*2, true);
    ctx.closePath();
    ctx.fill();
}

var drawDot = function(x, y, length, angle, value) {
    var radians = angle / 180 * Math.PI;
    var endX = x + length*value/100 * Math.cos(radians);
    var endY = y - length*value/100 * Math.sin(radians);
    drawCircle(endX, endY, 2);
}

var drawText = function(x, y, length, angle, value) {
    var radians = angle / 180 * Math.PI;
    var endX = x + length*value/100 * Math.cos(radians);
    var endY = y - length*value/100 * Math.sin(radians);
    console.debug(endX+","+endY);
    ctx.fillText(value+"%", endX+15, endY+5);
    ctx.stroke();
}

var visualizeData = function(x, y, length, angle, value) {

    ctx.strokeStyle = "#999";
    ctx.lineWidth = "1";
    drawAngledLine(x, y, length, angle);

    ctx.fillStyle = "#0a0";
    drawDot(x, y, length, angle, value);

    ctx.fillStyle = "#666";
    ctx.font = "bold 10px Arial";
    ctx.textAlign = "center";
    drawText(x, y, length, angle, value);
}

ctx.fillStyle = "#FFF0B3";
drawCircle(150, 150, 150);

visualizeData(150, 150, 150, 0, 34);
visualizeData(150, 150, 150, 12, 54);
visualizeData(150, 150, 150, 70, 23)

visualizeData(150, 150, 150, 120, 50);
visualizeData(150, 150, 150, -120, 80);
visualizeData(150, 150, 150, -45, 60);
+1

All Articles