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