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

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];
}
```

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];
}
```

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
},
radius: 5.0
},
{
center: {
x: 20.0,
y: 20.0
},
radius: 15.0
},
{
center: {
x: 20.0,
y: 10.0
},
radius: 5.0
},
{
center: {
x: 20.0,
y: 25.0
},
radius: 7.5
},
];
const q7_result1 = AreCirclesIntersecting(circles[0], circles[1]);
console.log(q7_result1); // Expected output: true
const q7_result2 = AreCirclesIntersecting(circles[0], circles[2]);
console.log(q7_result2); // Expected output: true
const q7_result3 = AreCirclesIntersecting(circles[1], circles[3]);
console.log(q7_result3); // Expected output: false
const q7_result4 = AreCirclesIntersecting(circles[2], circles[3]);
console.log(q7_result4); // Expected output: false
```

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.`

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;
}
```

### Other "returns-javascript" queries related to "A JavaScript function that returns the x,y points of intersection between two circles?"

- Uncaught TypeError: onPageChange is not a function for DataGrid in Material UI
- Binding vs arrow-function (for react onClick event)
- Code inside arrow function of setInterval not executing
- How can I send data from a child to a parent functional component in ReactJS?
- How to correctly write ReactJS function component?
- Can't call functions in render
- React Native Redux state not getting updated by reducer function through slice?
- Having a problem with making this function typescript conform
- Uncaught TypeError: Cannot read property 'call' of undefined javascript .filter()
- Find method not returning '0th' matched object react, or react-native or javascript
- Map two arrays and compare in Javascript(React)
- How to call multiple functions on the same onClick event ReactJS
- ReactJS onChange function not firing on entering input
- How do I mockImplementation of Jest function mock used for react-i18next useTranslation hook?
- How can I mock only a function inside an component when tests are running in React?
- How do I test this function?
- NextJS - re-using a function from index in a component?
- Expected an assignment or function call Redux action creator
- Item doesn't get deleted after launching .splice function
- Taking disparate datasets and merging together in javascript problem
- Change color for the last three digit in input value in javascript [duplicate]
- Can't use array functions like .includes in Object literals
- How do you pass state variables between two separate functional components in React Native?
- How to add a CSS class to an element with React function components
- UseSelector returns me undefined

#### Related Questions

- Why duck typing is allowed for classes in TypeScript
- Get keys of json-object in JavaScript [duplicate]
- Keeping only certain properties in a JavaScript object
- Serializing object that contains cyclic object value
- How to get the pure text without HTML element using JavaScript?
- Backbone.js model.get() returning 'undefined' even though I can see the attributes in console.log
- Javascript Iframe innerHTML
- How to get all CSS classes of an element?
- When does document.ready() get invoked?
- What are advantages of using google.load('jQuery', ...) vs direct inclusion of hosted script URL?
- Web SQL Database + Javascript loop
- Angular ng-repeat with condition
- Create shortcut to console.log() in Chrome
- How to access array in circular manner in JavaScript
- JQuery to get Hidden Field Value in Table Row
- Why Geolocation HTML5 getCurrentPosition() is not working on Google Map?
- Easiest way to sort DOM nodes?
- Can I instantiate a superclass and have a particular subclass be instantiated based on the parameters supplied
- Are any JavaScript engines tail call (TCO) optimized?
- Chrome refuses to execute an AJAX script due to wrong MIME type