Вопрос

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.

enter image description here

I am using JavaScript and canvas.

Это было полезно?

Решение

  • 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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top