Skip to content

Instantly share code, notes, and snippets.

@UnVoiding
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save UnVoiding/a02600bdd3c5693c0c51 to your computer and use it in GitHub Desktop.

Select an option

Save UnVoiding/a02600bdd3c5693c0c51 to your computer and use it in GitHub Desktop.
Canvas arc. Tangent from point to circle.
function TangentToCircle(fromx, fromy, cx, cy, r) {
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
var fi = 0;
context.beginPath();
if (fromx != cx && fromy != cy) {
var length = Math.sqrt(Math.pow(fromx - cx, 2) + Math.pow(fromy - cy, 2));
var a = Math.atan(r/length);
var g = Math.acos((cx - fromx)/length);
fi = Math.PI/2 - g + a;
context.moveTo(fromx, fromy);
}
context.arc(cx, cy, r, fi, 2*Math.PI + fi);
context.stroke();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment