我想按距离过滤许多坐标,但我不能用另一个计算二维表的每个元素 . 我已经完成了从文本到二维数组的格式更改,其中每个元素都是具有X和Y的坐标 . 无法解释所有内容,但我已经清楚地显示了项目 .
结果我想得到的是来自textfield1的coords列表,对于textfield2中的每个coord,它们的距离不高于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>
我需要它用于学校项目,并希望将来学习,所以代码之间的评论会很棒!
1 回答
我不确定你是否需要textfield1中的coords列表,对于textfield2中的所有coords,它们的距离不高于5,或者只是来自textfield2的任何coords . 无论哪种方式,代码都非常相似 .
您想过滤掉不符合某些条件的值,因此我们从方法
filter
开始coords1_intoXY.filter()
那么标准是什么?我假设文本字段2上的所有数字的距离必须小于5,所以我们需要
every
coords1_intoXY.filter(coords1 => coords2_intoXY.every())
请注意,如果您想要在textfield 2中的任何coords范围内的coords,只需将
every
替换为some
现在,正如奥利弗所说,我们计算距离 .
最后一部分,根据需要显示