# A JavaScript function that returns the x,y points of intersection between two circles?

Active3 hr before
Viewed126 times

returnsjavascriptfunction
90%

Meta Stack Overflow ,Translated the C function on the site to JavaScript:, Stack Overflow help chat ,Stack Overflow en español

Translated the C function on the site to JavaScript:

```function intersection(x0, y0, r0, x1, y1, r1) {
var a, dx, dy, d, h, rx, ry;
var x2, y2;

/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
dx = x1 - x0;
dy = y1 - y0;

/* Determine the straight-line distance between the centers. */
d = Math.sqrt((dy * dy) + (dx * dx));

/* Check for solvability. */
if (d > (r0 + r1)) {
/* no solution. circles do not intersect. */
return false;
}
if (d < Math.abs(r0 - r1)) {
/* no solution. one circle is contained in the other */
return false;
}

/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/

/* Determine the distance from point 0 to point 2. */
a = ((r0 * r0) - (r1 * r1) + (d * d)) / (2.0 * d);

/* Determine the coordinates of point 2. */
x2 = x0 + (dx * a / d);
y2 = y0 + (dy * a / d);

/* Determine the distance from point 2 to either of the
* intersection points.
*/
h = Math.sqrt((r0 * r0) - (a * a));

/* Now determine the offsets of the intersection points from
* point 2.
*/
rx = -dy * (h / d);
ry = dx * (h / d);

/* Determine the absolute intersection points. */
var xi = x2 + rx;
var xi_prime = x2 - rx;
var yi = y2 + ry;
var yi_prime = y2 - ry;

return [xi, xi_prime, yi, yi_prime];
}```
88%

I got the (x,y) center location of two circles and their radius but I need to find their intersection points (marked with red) using JavaScript.,I think the best explanation as far as the math is concerned is found here (Intersection of two circles), but I don't really understand the math so I'm not able to implement it.,Translated the C function on the site to JavaScript:,And also P2 = P0 + a ( P1 - P0 ) / d , aren't the P's here something like (10, 50)? But doing (10,50)+13 in JavaScript gives you 63, so it just ignores the first number, so what's suppose to happen? Should the outcome be (23,63) here or? And also the P1-P0 part or (40,30)-(10,60), how do you express that in JavaScript?

Translated the C function on the site to JavaScript:

```function intersection(x0, y0, r0, x1, y1, r1) {
var a, dx, dy, d, h, rx, ry;
var x2, y2;

/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
dx = x1 - x0;
dy = y1 - y0;

/* Determine the straight-line distance between the centers. */
d = Math.sqrt((dy * dy) + (dx * dx));

/* Check for solvability. */
if (d > (r0 + r1)) {
/* no solution. circles do not intersect. */
return false;
}
if (d < Math.abs(r0 - r1)) {
/* no solution. one circle is contained in the other */
return false;
}

/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/

/* Determine the distance from point 0 to point 2. */
a = ((r0 * r0) - (r1 * r1) + (d * d)) / (2.0 * d);

/* Determine the coordinates of point 2. */
x2 = x0 + (dx * a / d);
y2 = y0 + (dy * a / d);

/* Determine the distance from point 2 to either of the
* intersection points.
*/
h = Math.sqrt((r0 * r0) - (a * a));

/* Now determine the offsets of the intersection points from
* point 2.
*/
rx = -dy * (h / d);
ry = dx * (h / d);

/* Determine the absolute intersection points. */
var xi = x2 + rx;
var xi_prime = x2 - rx;
var yi = y2 + ry;
var yi_prime = y2 - ry;

return [xi, xi_prime, yi, yi_prime];
}```
72%

I’m trying to do a function to detect intersections between two circles. If yes it scores true, otherwise it scores false, but I think I got lost so it does not display what I want. If anyone can help me please . Thank you Surely I have incorrectly coded in javascript if there is a person who knows the answer I am all ears,Javascript natively offers a hypothenus function, useful here to calculate the distance between 2 points on a 2 D system, Draw Lines between Multiple markers on Google Map , Set some styles if an element is followed by an element with same attribute

```function AreCirclesIntersecting(c0, c1) {

x0 = c0['center']['x'];
y0 = c0['center']['y'];
r0 = c0['center']['r'];
x1 = c1['center']['x'];
y1 = c1['center']['y'];
r1 = c1['center']['r'];

var a, dx, dy, d, h, rx, ry;
var x2, y2;

/* dx and dy are the vertical and horizontal distances between
* the circle centers.
*/
dx = x1 - x0;
dy = y1 - y0;

/* Determine the straight-line distance between the centers. */
d = Math.sqrt((dy * dy) + (dx * dx));

/* Check for solvability. */
if (d > (r0 + r1)) {
/* no solution. circles do not intersect. */
return false;
}
if (d < Math.abs(r0 - r1)) {
/* no solution. one circle is contained in the other */
return false;
}

/* 'point 2' is the point where the line through the circle
* intersection points crosses the line between the circle
* centers.
*/

/* Determine the distance from point 0 to point 2. */
a = ((r0 * r0) - (r1 * r1) + (d * d)) / (2.0 * d);

/* Determine the coordinates of point 2. */
x2 = x0 + (dx * a / d);
y2 = y0 + (dy * a / d);

/* Determine the distance from point 2 to either of the
* intersection points.
*/
h = Math.sqrt((r0 * r0) - (a * a));

/* Now determine the offsets of the intersection points from
* point 2.
*/
rx = -dy * (h / d);
ry = dx * (h / d);

/* Determine the absolute intersection points. */
var xi = x2 + rx;
var xi_prime = x2 - rx;
var yi = y2 + ry;
var yi_prime = y2 - ry;

return [xi, xi_prime, yi, yi_prime];

}

const circles = [{
center: {
x: 10.0,
y: 10.0
},
},
{
center: {
x: 20.0,
y: 20.0
},
},
{
center: {
x: 20.0,
y: 10.0
},
},
{
center: {
x: 20.0,
y: 25.0
},
},
];

const q7_result1 = AreCirclesIntersecting(circles, circles);
console.log(q7_result1); // Expected output: true

const q7_result2 = AreCirclesIntersecting(circles, circles);
console.log(q7_result2); // Expected output: true

const q7_result3 = AreCirclesIntersecting(circles, circles);
console.log(q7_result3); // Expected output: false

const q7_result4 = AreCirclesIntersecting(circles, circles);
console.log(q7_result4); // Expected output: false```
65%

There are two circle A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. Task is to check both circles A and B touch each other or not.Examples :  ,Check if two given circles touch or intersect each other,Check if a circle lies inside another circle or not,Check if a line touches or intersects a circle

There are two circle A and B with their centres C1(x1, y1) and C2(x2, y2) and radius R1 and R2. Task is to check both circles A and B touch each other or not.
Examples :

```Input: C1 = (3, 4)
C2 = (14, 18)
R1 = 5, R2 = 8
Output: Circles do not touch each other.

Input: C1 = (2, 3)
C2 = (15, 28)
R1 = 12, R2 = 10
Output: Circles intersect with each other.

Input: C1 = (-10, 8)
C2 = (14, -24)
R1 = 30, R2 = 10
Input: -10 8
14 - 24
30 10
Output: Circle touch each other.```

```Distance between centres C1 and C2 is calculated as
C1C2 = sqrt((x1 - x2) 2 + (y1 - y2) 2).
There are three condition arises.
1. If C1C2 == R1 + R2
Circle A and B are touch to each other.
2. If C1C2 > R1 + R2
Circle A and B are not touch to each other.
3. If C1C2 < R1 + R2
Circle intersects each other.```

`Circle touch to each other.`
75%

Start by calculating the distance between the circle centres as before,\$h\$: half the distance between the intersection points,We can get two expressions for these unknowns using Pythagoras's theorem (again):,\$d\$: the distance between the centres

```function circlesIntersect(c1, c2) {
const dx = c1.x - c2.x;
const dy = c1.y - c2.y;
const d = Math.sqrt(dx * dx + dy * dy);
return d <= c1.r + c2.r;
}```