S
S
Sergey Topolov2017-04-16 18:27:31
satellite navigation
Sergey Topolov, 2017-04-16 18:27:31

What are these coordinates?

I counted the first function f1();.
Everything is simple here.

let p1 = [-100, 1]; 
  let p2 = [200, 32];

p1= x1 y1
p2= x2 x2
And what are these coordinates of points from 6 numbers? If there are 3 more, then it's understandable x, y, z.
What is it
let p1 = [-100, 1, 5, 4, 3, 15];
  let p2 = [2, 65, 45, 14, 443, 115];
?
// 1) Рассчитать расстояние между точками и вывести в консоль
function f1() {
  let p1 = [-100, 1];
  let p2 = [200, 32];
  let r = getLength(p1, p2);
  console.log("----------------------------------------");
  console.log("-> f1() - Расстояние м/у 2мя точками = " + r);
}

// 2) Рассчитать расстояние между точками и вывести в консоль
function f2() {
  let p1 = [-100, 1, 5, 4, 3, 15];
  let p2 = [2, 65, 45, 14, 443, 115];
  let r = getLength(p1, p2);
}
// =========================================================
function getLength(p1, p2){
  let r = 0;
  let dX = p2[0]-p1[0];
  let dY = p2[1]-p1[1];
  let dis = Math.pow(Math.pow(dX,2) + Math.pow(dY,2),0.5).toFixed(0);
  return dis;
}
// =========================================================
f1();

-------------------------------------------------- ---------------------------------
QUESTION CLOSED = SOLUTION
------------- -------------------------------------------------- --------------------
function f2() {
  let p1 = [-100, 1, 5, 4, 3, 15];
  let p2 = [2, 65, 45, 14, 443, 115];
  let r = getLength(p1, p2);
  console.log("-> f2() - Расстояние м/у 2-мя точками N-мерного массива = " + r);
}
function getLength(p1, p2){
  let r = 0; let i = 0; let ai = 0; let dis = 0; let len = p1.length;
  for (; i < len; i++)
  {
    ai += Math.pow((p2[i] - p1[i]), 2);
  }
  dis = Math.pow(ai, 0.5).toFixed(0);
  return dis;
}
// -------------------------------
f2();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Lerg, 2017-04-16
@topus009

Just six-dimensional space.

X
x67, 2017-04-17
@x67

There can be anything, from angles to speeds or regular garbage. Think of it more abstractly, just like an object with 6 parameters, where the first two parameters are the coordinates of this object along the X and Y axes. Here the question is more for you - where do you get p-objects (which are most likely just points, judging by your comments) and why are you getting them? There you will find the answers you need.
Ii.. I'm not a js expert, but is it normal that r is an external variable for a function, but you assign something to it inside the function? In most languages, this construct either won't work or is called a crutch that will shoot you in the leg after n development cycles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question