views:

33

answers:

1

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?

+1  A: 

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.)

You
I'm pretty sure you're spot on, just to make sure I'm doing the right thing,because english is my second language, could you please elaborate the part where you say "reverse the sign of the second component of the y coordinate" ?
Quickredfox
Well, if `anticlockwise` is true, we would have to replace the y coordinate with `y - radius*sin(angle)`, since it would effectively be mirrored in the x axis. Thus, reverse the sign. (I admit, it was a needlessly complicated wording)
You
Thank you by the way. Solved all my woes.
Quickredfox