Files
freeCodeCamp/curriculum/challenges/italian/10-coding-interview-prep/rosetta-code/circles-of-given-radius-through-two-points.md
2022-02-03 11:16:32 -08:00

4.5 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5951815dd895584b06884620 Cerchi di raggio dato attraverso due punti 5 302231 circles-of-given-radius-through-two-points

--description--

Dati due punti su un piano, e un raggio, in genere due cerchi di raggio dato possono essere disegnati attraverso i punti.

Eccezioni:

  • Un raggio di zero deve essere trattato come non descrivente mai cerchi (eccetto nel caso in cui i punti sono coincidenti).
  • Se i punti sono coincidenti allora si può disegnare un numero infinito di cerchi con i punti sulla loro circonferenza; a meno che il raggio non sia pure uguale a zero, il che collassa i cerchi a un punto.
  • Se i punti formano un diametro allora restituisci un singolo cerchio.
  • Se i punti sono troppo distanti allora nessun cerchio può essere disegnato.

--instructions--

Implementa una funzione che accetta due punti e un raggio e restituisce i due cerchi attraverso quei punti. Per ogni cerchio risultante, provvedi le coordinate del centro di ogni cerchio arrotondate a quattro cifre decimali. Restituisci ogni coordinata come un array, e le coordinate come un array di array.

Per casi limite, restituisci le cose seguenti:

  • Se i punti sono sul diametro, restituisci un punto. Se il raggio è pure zero allora restituisci "Radius Zero".
  • Se i punti sono coincidenti, restituisci "Coincident point. Infinite solutions".
  • Se i punti sono più distanti tra loro del diametro, restituisci "No intersection. Points further apart than circle diameter".

Esempio di input:

      p1                p2           r
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

--hints--

getCircles dovrebbe essere una funzione.

assert(typeof getCircles === 'function');

getCircles([0.1234, 0.9876], [0.8765, 0.2345], 2.0) dovrebbe restituire [[1.8631, 1.9742], [-0.8632, -0.7521]].

assert.deepEqual(getCircles(...testCases[0]), answers[0]);

getCircles([0.0000, 2.0000], [0.0000, 0.0000], 1.0) dovrebbe restituire [0, 1]

assert.deepEqual(getCircles(...testCases[1]), answers[1]);

getCircles([0.1234, 0.9876], [0.1234, 0.9876], 2.0) dovrebbe restituire Coincident point. Infinite solutions

assert.deepEqual(getCircles(...testCases[2]), answers[2]);

getCircles([0.1234, 0.9876], [0.8765, 0.2345], 0.5) dovrebbe restituire No intersection. Points further apart than circle diameter

assert.deepEqual(getCircles(...testCases[3]), answers[3]);

getCircles([0.1234, 0.9876], [0.1234, 0.9876], 0.0) dovrebbe restituire Radius Zero

assert.deepEqual(getCircles(...testCases[4]), answers[4]);

--seed--

--after-user-code--

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'
];

--seed-contents--

function getCircles(...args) {

  return true;
}

--solutions--

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)));

function getCircles(...args) {
  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;
}