【问题标题】:How do I find point coordinates knowing only distance to another point?如何找到仅知道到另一个点的距离的点坐标?
【发布时间】:2022-11-02 16:10:54
【问题描述】:

首先,我们有两个坐标为 x, y, z 的点:start(0, 0, 0) 和随机生成的 r(x, y, z),其中 x, y, z 是从 0 到 100 的随机整数。

有一个函数(已经由我编写)计算起点和 r 之间的距离。

我需要编写另一个仅将距离作为参数的函数。我们需要使用起始点内的不同坐标调用第一个函数(改变点之间的距离),直到找到 r 的坐标(即距离应该等于 0)。

在这种情况下,最好的算法是什么?

我的第一部分代码包括计算距离的函数:

const r = [];
for (let i = 0; i < 3; i++) {
    r.push(Math.floor(Math.random() * 100) + 1); // Generating random r point
}

const s = [0, 0, 0];
let distance;

function calculateDistance(random, myPoint) {
    distance = Math.abs(
                    Math.floor(
                        Math.sqrt(
                            Math.pow((random[0] - myPoint[0]), 2) +
                            Math.pow((random[1] - myPoint[1]), 2) + 
                            Math.pow((random[2] - myPoint[2]), 2)
                        )
                    )
                    );
    return distance;
}

接下来我做了以下事情:


function searchPoint(distance) {
    let min = 173,   // biggest possible distance if r(100, 100, 100)
        tempArr = [],
        closePoints = [],
        prev;

    for (let i = 0; i < s.length; i++) {
        while (s[i] < 100) {  // For x, y, z of start point I go through every possible value
            prev = min; // Keep track on previos min value                              
            if (min > distance) {
                min = distance;
            }
    
            if (prev < distance) { // If the distance increases it means we're drifting away from needed coordinates
                break;
            }

            tempArr.push({distance, point: s[i]});  // Here I save pairs distance-coordinate
            s[i]++;
            distance = calculateDistance(r, s);
        }
        closePoints.push(tempArr.filter(obj => obj.distance === min)); // For each of 3 coordinates I leave only minimal values, but the thing is there are several of them
        tempArr = [];
        min = 173;
    }

    let mappedPoints = closePoints.map(arr => {
        let mid = Math.floor((arr.length - 1) / 2); // So as a solution I try to pick up middle element from an array of points close to the needed one (which works most of the time BUT*)
        return arr[mid];
    });
    console.log(mappedPoints);
    mappedPoints.forEach((obj, i) => s[i] = obj.point); 

    console.log(s);
    console.log(calculateDistance(r, s));

    if (calculateDistance(r, s) === 0) {            // We check up on the distance and it's only logical that if it's equal to 0 - we found the point
        console.log('Point was found!!!');
    }
  • 但是!在边界值(比如 <14 和 >86)中,closePoints 数组中所需的点不会在中间(例如,2/5/94/98) 此外,在 [3, 45, 93] 之类的情况下,这样的点可能不止一个。 所以这是我卡住的部分......

【问题讨论】:

  • 仅给定与原点的距离,如何确定点?那是一个圆的图形,而不是一个点。
  • 你尝试过哪些算法?您应该在帖子中包含所有相关功能,最好是作为 sn-p。
  • 一个球的图形
  • 我认为这里可能有一个有趣的问题,但问题设置尚不清楚。你能添加一个例子或插图吗?
  • 伙计们,我更新了问题,请看一下

标签: javascript algorithm geometry coordinates


【解决方案1】:

我很好奇计算所有匹配点(在整数的 3d 空间中)的性能(朴素算法)。

function distance_squared(x, y, z) {
  return x * x + y * y + z * z;
}

var r_squared = 9230
for (var x = 0; x <= 100; x++) {
  for (var y = 0; y <= 100; y++) {
    for (var z = 0; z <= 100; z++) {
      if (distance_squared(x, y, z) == r_squared) {
        console.log(x, y, z)
      }
    }
  }
}

// console.log(distance_squared(79,42,35))

现在结合最近的question about isometric projection 3D 坐标,给出:

function Coords_3D_To_2D(x, y, z) {
  return {
    w: ((x - y) * (Math.sqrt(3) / 2)),
    h: (((-x - y) / 2) - z)
  }
}

function distance_squared(x, y, z) {
  return x * x + y * y + z * z;
}


function Projectile(x, y, speed, direction, duration) {
  Object.assign(this, {
    x,
    y,
    speed,
    direction,
    duration
  });
  this.draw = ctx => {
    ctx.arc(this.x, this.y, 3.75, 0, 2 * Math.PI);
    ctx.fillStyle = 'white';
    ctx.fill();
  }
  this.update = ctx => {
    ctx.beginPath();
    this.x += Math.cos(this.direction) * this.speed;
    this.y += Math.sin(this.direction) * this.speed;
    this.draw(ctx);
    this.duration--;
  }
  this.isDone = () => this.duration <= 0;
}



const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var x0 = canvas.width / 2
var y0 = canvas.height / 2;
var projectileArray = []


;
(function update() {
  ctx.fillStyle = "black";
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  for (var i = 0; i < projectileArray.length; i++) {
    let bullet = projectileArray[i];
    bullet.update(ctx)
  }
  projectileArray = projectileArray.filter(bullet => !bullet.isDone());



  requestAnimationFrame(update);
})();

document.addEventListener("keydown", event => {
  if (event.code in {
      Space: false,
    }) {
    let bullet = new Projectile(x0, y0, 3, 2, 1500)
    projectileArray.push(bullet);
  }
  // console.log("boom")
});



var r_squared = 9230
for (var x = 0; x <= 100; x++) {
  for (var y = 0; y <= 100; y++) {
    for (var z = 0; z <= 100; z++) {
      if (distance_squared(x, y, z) == r_squared) {
        // console.log(x, y, z)
        var cord = Coords_3D_To_2D(x, y, z)
        let bullet = new Projectile(x0 + cord.w, y0 + cord.h, 1, -Math.PI * 1.5, 500)
        projectileArray.push(bullet);

      }
    }
  }
}
body {
  margin: 0;
  padding: 0;
  overflow: hidden;
}

【讨论】:

    【解决方案2】:

    给定距离r,我们应该学习的是找到x^2+y^2+z^2=r^2的所有非负整数解,然后随机选择一些解。

    我们可以分别从1到r枚举xy,并检查r^2-x^2-y^2是否是一个完全平方数。

    复杂度为O(r^2)

    【讨论】:

    • 问题是我无法检查 searchPoint 函数中的 r 坐标,只能检查距离
    • 对不起,我之前没有仔细阅读声明。而且我没有卡点?你能再描述一下吗?
    • 我认为closePoints 中每个元素的 arr.length() 必须为 1?因为考虑函数 dis_to_r(x,y,z)=(x-x_0)^2+(y-y_0)^2+(z-z_0^2) 对于固定的 y 和 z,我们将只有一个 x 来最小化 dis_to_r (x,y,z)
    • 还有一些其他的想法~一个假设是你可以像这个问题的二维版本一样查询距离为(0,0,distance),(0,distance,0),(distance,0,0),你可以查询(0,distance), (distance,0) 找到随机点的位置。正确的?
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案3】:

    我想我知道这个任务的解决方案。在我看来,我知道这个测试任务是为哪家公司做的,我自己现在正在做。我想问你几个关于技术面试和实习的问题。你能在电报上给我发短信吗? @Dimadaga

    【讨论】:

    • 在此处发布该联系人可能意味着您无法再使用它。认为它被烧毁了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多