I want to draw a curved path (an arc or a cubic bezier curve) from a point to a circle. The following parameters are known: pX (x-position of the point), pY (y-position of the point), cX (x-position of the circle's center), cY (y-position of the circle's center) and cR (radius of the circle).
This figure illustrates the problem.
The circle is not opaque and when the path is drawn from the point to the circle's center, it is visible through the circle (see #1 in the linked figure). It shouldn't be happening as there exists some background content that has to be visible through the circle. So, the solution I imagine is to draw the path up to the circle's border.
Drawing a straight line until the circle's border is easy (see #2 in the linked figure):
var theta = Math.atan2(cY - pY, cX - pX);
var startX = pX;
var startY = pY;
var endX = cX - cR * Math.cos(theta);
var endY = cY - cR * Math.sin(theta);
line.attr('x1', startX).attr('y1', startY).attr('x2', endX).attr('y2', endY);
If I apply the same principle to a curved path, it will be drawn incorrectly (see #3 in the linked figure). It has to keep the shape as if it was pointing to the circle's center, but end without overlapping the circle (see #4 in the linked figure).
Additional observations:
- the curve can be as thick as the circle
- there can exist several hundred of circle-arc pairs simultaneously in the visualization, therefore the performance has also be taken into consideration
Any suggestions on how to solve this? Thanks in advance!
exclusion(line, circle)
. – Mike 'Pomax' Kamermans