2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								id: 5951815dd895584b06884620
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								title: Circles of given radius through two points
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								challengeType: 5
							 
						 
					
						
							
								
									
										
										
										
											2019-08-05 09:17:33 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								forumTopicId: 302231
							 
						 
					
						
							
								
									
										
										
										
											2021-01-13 03:31:00 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								dashedName: circles-of-given-radius-through-two-points
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --description--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Given two points on a plane and a radius, usually two circles of given radius can be drawn through the points.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Exceptions:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > A radius of zero should be treated as never describing circles (except in the case where the points are coincident).< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If the points are coincident then an infinite number of circles with the point on their circumference can be drawn, unless the radius is equal to zero as well which then collapses the circles to a point.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If the points form a diameter then return a single circle.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If the points are too far apart then no circles can be drawn.< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
									
										
										
										
											2019-03-01 17:10:50 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --instructions--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Implement a function that takes two points and a radius and returns the two circles through those points. For each resulting circle, provide the coordinates for the center of each circle rounded to four decimal digits. Return each coordinate as an array, and coordinates as an array of arrays.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**For edge cases, return the following:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-25 13:36:09 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								< ul >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If points are on the diameter, return one point. If the radius is also zero however, return < code > "Radius Zero"< / code > .< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If points are coincident, return < code > "Coincident point. Infinite solutions"< / code > .< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  < li > If points are farther apart than the diameter, return < code > "No intersection. Points further apart than circle diameter"< / code > .< / li > 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / ul >  
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Sample inputs:**
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< pre >       p1                p2           r 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								0.1234, 0.9876    0.8765, 0.2345    2.0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0.0000, 2.0000    0.0000, 0.0000    1.0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0.1234, 0.9876    0.1234, 0.9876    2.0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0.1234, 0.9876    0.8765, 0.2345    0.5
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								0.1234, 0.9876    0.1234, 0.9876    0.0
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								< / pre >  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --hints--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`getCircles`  should be a function. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert(typeof getCircles === 'function');
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0)`  should return `[[1.8631, 1.9742], [-0.8632, -0.7521]]` . 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(getCircles(...testCases[0]), answers[0]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0)`  should return `[0, 1]`  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								assert.deepEqual(getCircles(...testCases[1]), answers[1]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2020-09-15 09:57:40 -07:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0)`  should return `Coincident point. Infinite solutions`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(getCircles(...testCases[2]), answers[2]);
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getCircles([0.1234, 0.9876], [0.8765, 0.2345], 0.5)`  should return `No intersection. Points further apart than circle diameter`  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(getCircles(...testCases[3]), answers[3]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`getCircles([0.1234, 0.9876], [0.1234, 0.9876], 0.0)`  should return `Radius Zero`  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								assert.deepEqual(getCircles(...testCases[4]), answers[4]);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# --seed--
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## --after-user-code--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2018-10-20 21:02:47 +03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								const testCases = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[0.1234, 0.9876], [0.8765, 0.2345], 2.0],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[0.0000, 2.0000], [0.0000, 0.0000], 1.0],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[0.1234, 0.9876], [0.1234, 0.9876], 2.0],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[0.1234, 0.9876], [0.8765, 0.2345], 0.5],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[0.1234, 0.9876], [0.1234, 0.9876], 0.0]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const answers = [
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [[1.8631, 1.9742], [-0.8632, -0.7521]],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  [0, 1],
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Coincident point. Infinite solutions',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'No intersection. Points further apart than circle diameter',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  'Radius Zero'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								];
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## --seed-contents--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								function getCircles(...args) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return true;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-27 19:02:05 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# --solutions--
  
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```js
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const hDist = (p1, p2) => Math.hypot(...p1.map((e, i) => e - p2[i])) / 2;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const pAng = (p1, p2) => Math.atan(p1.map((e, i) => e - p2[i]).reduce((p, c) => c / p, 1));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const solveF = (p, r) => t => [parseFloat((r * Math.cos(t) + p[0]).toFixed(4)), parseFloat((r *  Math.sin(t) + p[1]).toFixed(4))];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const diamPoints = (p1, p2) => p1.map((e, i) => parseFloat((e + (p2[i] - e) / 2).toFixed(4)));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-26 17:07:07 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								function getCircles(...args) {
							 
						 
					
						
							
								
									
										
										
										
											2018-09-30 23:01:58 +01:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								  const [p1, p2, s] = args;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const solve = solveF(p1, s);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const halfDist = hDist(p1, p2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  let msg = [];
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  switch (Math.sign(s - halfDist)) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 0:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      msg = s ? diamPoints(p1, p2) :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        'Radius Zero';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case 1:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      if (!halfDist) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        msg = 'Coincident point. Infinite solutions';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const theta = pAng(p1, p2);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const theta2 = Math.acos(halfDist / s);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        [1, -1].map(e => solve(theta + e * theta2)).forEach(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          e => msg.push(e));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    case -1:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      msg = 'No intersection. Points further apart than circle diameter';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    default:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      msg = 'Reached the default';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return msg;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```