我想按距离过滤许多坐标,但是我无法与另一个计算二维表的每个元素。我已经完成了将格式从文本更改为二维数组,其中每个元素都是一个具有X和Y的坐标。 不能解释全部,但我有小提琴项目,清楚地显示了它。
我想要得到的结果是一个来自textfield1的坐标列表,对于每个距textfield2的坐标,它们的距离都不会大于5
请签出:https://jsfiddle.net/Ashayas/9c7mjts4/
console.log("==== 1 ====");
//Get inputs
var coords1 = document.getElementById("coord1").value.split(" ");
var coords2 = document.getElementById("coord2").value.split(" ");
console.log(coords1);
console.log(coords2);
console.log("==== 2 ====");
//Make them X and Y
var coords1_intoXY = [];
for (var i = 0; i < coords1.length; i++) {
coords1_intoXY[i] = coords1[i].split("|");
}
var coords2_intoXY = [];
for (var i = 0; i < coords2.length; i++) {
coords2_intoXY[i] = coords2[i].split("|");
}
console.log(coords1_intoXY);
console.log(coords2_intoXY);
//Now combine all x|y and put into new array, after that sort and get result
//I dont know is that good thinking, maybe there is another way to achieve result
/*
var beforesort = [];
for (var i = 0; i < originalContent.length; i++) {
paste7[i] = originalContent[i].join(" ").split(splitCharacter).filter(x => !unwantedContent.includes(x));
}
function compare(coords1,coords2){
const coords3 = [];
let result;
coords1.forEach((e1,i)=>coords2.forEach(e2=>{
if(e1.length > 1 && e2.length){
result = compare(e1,e2);
}else if(e1 !== e2 ){
result = false
}else{
result = true
}
})
)
return result
}
*/
x1 = 10
y1 = 10
x2 = 1
y2 = 1
example = Math.hypot(x2 - x1, y2 - y1)
console.log("Example = " + example);
function distance(x1, y1, x2, y2) {
a = Math.abs(Number(x1 - x2));
b = Math.abs(Number(y1 - y2));
c = Math.sqrt((a * a) + (b * b));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Program sort coordinates X|Y from inputs. Result is a list of coordinates from input1 which their distances between second input coords arent highter than the value from input 3 (for example "5")
<br>
<br> Two inputs for coordinates X|Y
<br>
<textarea id="coord1">3|1 5|5 10|10</textarea>
<textarea id="coord2">9|1 1|1</textarea>
<br> Sort by max distance
<br>
<input value="5" id="coord3">
<br>
<br>
<textarea id="result">Result</textarea>
<br>
<button id="show">Show</button>
<br>
<br> EXAMPLE:
<br>
<br> We have "3|1 5|5 10|10" inside coord1
<br> We have "9|1 1|1" inside coord2
<br>
<br> Program calculate distance between coords
<br> 3|1 and 9|1 = 6
<br> 3|1 and 1|1 = 2
<br> 5|5 and 9|1 = 5.65 ~ 6
<br> 5|5 and 1|1 = 5.65 ~ 6
<br> 10|10 and 9|1 = 9.05 ~ 9
<br> 10|10 and 1|1 = 12.72 ~ 13
<br>
<br> And the result should looks like this:
<br> 3|1
<br> Other coords are higter than 5 distance in any case so we skip them
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
我在学校项目中需要它,并且想为将来学习,因此代码之间的注释会很棒!
答案 0 :(得分:0)
我不确定您是否想要列出textfield1的坐标列表,对于所有来自textfield2的坐标,它们的距离都大于5,或者只是来自textfield 2的任何坐标。无论哪种方式,代码都非常相似。
您要过滤掉不满足某些条件的值,因此我们从方法filter
coords1_intoXY.filter()
那么标准是什么?我假设到文本字段2的所有数字的距离必须小于5,所以我们将需要every
coords1_intoXY.filter(coords1 => coords2_intoXY.every())
请注意,如果您要使用文本字段2中任何坐标范围内的坐标,只需将every
替换为some
现在,正如奥利弗(Oliver)所说的,我们计算距离。
var coords1_intoXY = [["3", "1"], ["5", "5"], ["10", "10"]];
var coords2_intoXY = [["9", "1"], ["1", "1"]];
let answer = coords1_intoXY.filter(coords1 => coords2_intoXY.some(coords2 => {
let x = Number(coords1[0]) - Number(coords2[0])
let y = Number(coords1[1]) - Number(coords2[1])
return Math.hypot(x, y) < 5
}))
console.log(...answer.map(coords => coords.join('|')));
最后一部分,根据需要显示
return answer.map(coords => coords.join('|'));