最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to find coords from angle in a circle - Stack Overflow

programmeradmin4浏览0评论

I'm trying to find the coords of the red point in the image, I have the mouse coords, the initial point and the radio but I don't know how to find the coords of the red point.

I am using JavaScript and canvas.

I'm trying to find the coords of the red point in the image, I have the mouse coords, the initial point and the radio but I don't know how to find the coords of the red point.

I am using JavaScript and canvas.

Share Improve this question edited Apr 17, 2014 at 1:44 user1693593 asked Apr 13, 2014 at 7:42 J261J261 6722 gold badges6 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6
  • First find the angle between the mouse point and center of circle
  • Then calculate the needed point using that angle and radius of circle

To find the angle:

var diffX = mouseX - centerX;
var diffY = mouseY - centerY;
var angle = Math.atan2(diffY, diffX);

To find the new point use that angle with radius:

var x = cx + radius * Math.cos(angle);
var y = cy + radius * Math.sin(angle);

Live demo

first find the angle between the mouse and the point.

dx = mouseCoordX - coordX;
dy = mouseCoordY - coordY;
angle = Math.atan2(dy, dx);

second, find the coords of the red point

coordToFindX = coordX+ Math.cos(angle ) * radio
coordToFindY = coordY + Math.sin(angle ) * radio
发布评论

评论列表(0)

  1. 暂无评论