How do i get the x/y coordinates of the first and last points of the drawn arc relative to the top left corner of the canvas?

StackOverflow https://stackoverflow.com/questions/3671611

Question

I have a square canvas with a width of 100 and a height of 100. Within that square I draw an arc like so:

var canvas = document.getElementById('myCanvas');
var ctx    = canvas.getContext('2d');
ctx.clearRect(0,0,100,100) // clears "myCanvas" which is 100pixels by 100 pixels
ctx.beginPath();
ctx.arc( 50, 50, 30, 0, Math.PI*2/6 , false )
ctx.stroke();

The question is: How do i get the x/y coordinates of the first and last points of the drawn line relative to the top left corner of the canvas?

Was it helpful?

Solution

The starting point is trivially (x + radius, y). The ending point is, by simple trigonometrics, (x + radius*cos(angle), y + radius*sin(angle)). Note that the starting point in this case is a special case of the more general ending point, with angle equal to zero. These values also need to be rounded to the nearest integer, for obvious reasons.

(Note that this applies only when the anticlockwise argument is false, and assuming all coordinates are measured from the top left. If anticlockwise is true, reverse the sign of the second component of the y coordinate. If coordinates are measured from another corner, apply simple arithmetics to correct for this. Also note that this is completely backwards for any real mathematician.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top