将相同线性方程的所有行分组。

huangapple go评论62阅读模式
英文:

Group All Lines of Same Linear Equation

问题

以下是要翻译的内容:

let input = [
  [[1, 4], [40, 4]],
  [[1, 5], [40, 5]],
  [[4, 7], [4, 24]],
  [[1, 9], [4, 1]],
  [[1, 2], [6, 4]],
  [[80, 4], [90, 4]],
  [[4, 1], [4, 40]],
  [[4, 35], [4, 29]],
  [[4, 28], [4, 35]],
  [[5, 3.6], [9, 5.2]],
]; // 输入
Output = [
  [[[1, 4], [40, 4]], [[80, 4], [90, 4]]],
  [[[1, 5], [40, 5]]],
  [[[4, 7], [4, 24]], [[4, 1], [4, 40]]],
  [[[4, 35], [4, 29]], [[4, 28], [4, 35]]],
  [[[1, 9], [4, 1]]],
  [[[1, 2], [6, 4]], [[5, 3.6], [9, 5.2]]],
]; // 输出

如果给定一系列起点和终点坐标的输入,例如,[[1,4],[40,4]] 表示有2个点连接 [1,4] 和 [40,4] 形成一条直线。我的目标是将所有共享相同方程 y=mx+c 的线分组到一个嵌套数组中,如上所示。例如,

[[1,4],[40,4]]  [[80,4],[90,4]] 共享相同的线性方程 y=4

[[4,7],[4,24]],[[4,1],[4,40]] 共享相同的线性方程 x=4

[[1,2],[6,4]]  [[5,3.6],[9,5.2]] 共享相同的线性方程 y=0.4x+1.6

[[1,9],[4,1]] 单独并且它具有线性方程 -2.67x+11.67

这是我的工作代码演示

我知道如何编写代码来找到 y=mx+c 中的 m 和 c,但问题是,当例如,[[4,7],[4,24]] 和 [[4,1],[4,40]] 时,梯度 m 变成无穷大,无法解决。 有人能否指导我如何获得正确的输出?

英文:
let input = [
  [[1, 4], [40, 4]],
  [[1, 5], [40, 5]],
  [[4, 7], [4, 24]],
  [[1, 9], [4, 1]],
  [[1, 2], [6, 4]],
  [[80, 4], [90, 4]],
  [[4, 1], [4, 40]],
  [[4, 35], [4, 29]],
  [[4, 28], [4, 35]],
  [[5, 3.6], [9, 5.2]],
]; // Input
Output = [
  [[[1, 4], [40, 4]], [[80, 4], [90, 4]]],
  [[[1, 5], [40, 5]]],
  [[[4, 7], [4, 24]], [[4, 1], [4, 40]]],
  [[[4, 35], [4, 29]], [[4, 28], [4, 35]]],
  [[[1, 9], [4, 1]]],
  [[[1, 2], [6, 4]], [[5, 3.6], [9, 5.2]]],
];

If given an input of series of each start and end coordinates of a line, for example, [[1,4],[40,4]] means that it has 2 points connecting [1,4] and [40,4] to form a straight line. My objective now is to group all those lines which share the same equation y=mx+c, together into a nested array as shown above. For example,

[[1,4],[40,4]] and [[80,4],[90,4]] share the same linear equation y=4

[[4,7],[4,24]],[[4,1],[4,40]]      share the same linear equation x=4

 [[1,2],[6,4]] and [[5,3.6],[9,5.2]]  share the same linear equation y=0.4x+1.6

[[1,9],[4,1]]   is alone and it has the linear equation of -2.67x+11.67

Here is my working codepen demo

I know how to code out to find those m and c in y=mx+c, but the problem is when for example,[[4,7],[4,24]] and [[4,1],[4,40]] , the m gradient becomes infinity which unsolvable.

Can anyone please guide me on this on how to get the correct output?

答案1

得分: 2

以下是翻译好的部分:

您可以为每组点计算斜率方程并将其分配给每个数组项然后分组

要处理四舍五入问题您需要将其四舍五入

请注意,代码部分不会被翻译。

英文:

You can calculate the slope equation for each set of points and assign it to each array item, then group:

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const input=[[[1,4],[40,4]],[[1,5],[40,5]],[[4,7],[4,24]],[[1,9],[4,1]],[[1,2],[6,4]],[[80,4],[90,4]],[[4,1],[4,40]],[[4,35],[4,29]],[[4,28],[4,35]],[[5,3.6],[9,5.2]]];

const inputsWithSlope = input.map((points) =&gt; {
  const [[x, y], [x1, y1]] = points;
  const slope = (y1 - y) / (x1 - x)
  const b = y1 - slope * x1
  return {
    points,
    line: x1 == x ? `x = ${x}` : `y = ${slope}x + ${b}`
  }
})

const res = inputsWithSlope.reduce((acc, curr) =&gt; {
  const accProp = acc[curr.line]
  acc[curr.line] = !accProp ? [curr.points] : [...accProp, curr.points]
  return acc
}, {})
const result = Object.values(res)
result.forEach(e =&gt; console.log(JSON.stringify(e)))

<!-- end snippet -->

To deal with the rounding issue, you'll have to round it:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input=[[[1,4],[40,4]],[[1,5],[40,5]],[[4,7],[4,24]],[[1,9],[4,1]],[[1,2],[6,4]],[[80,4],[90,4]],[[4,1],[4,40]],[[4,35],[4,29]],[[4,28],[4,35]],[[5,3.6],[9,5.2]]];

const inputsWithSlope = input.map((points) =&gt; {
  const [[x, y], [x1, y1]] = points;
  const slope = (y1 - y) / (x1 - x)
  const b = y1 - slope * x1
  return {
    points,
    line: x1 == x ? `x = ${x}` : `y = ${slope.toFixed(2)}x + ${b.toFixed(2)}`
  }
})

const res = inputsWithSlope.reduce((acc, curr) =&gt; {
  const accProp = acc[curr.line]
  acc[curr.line] = !accProp ? [curr.points] : [...accProp, curr.points]
  return acc
}, {})
const result = Object.values(res)
result.forEach(e =&gt; console.log(JSON.stringify(e)))

<!-- end snippet -->

答案2

得分: 0

以下是您要的代码部分的中文翻译:

这是我的方法策略是首先为每对点获取一条线的描述然后将这些描述分组成一个嵌套映射首先按斜率分组然后按截距分组取决于线是否垂直),然后提取这些分组到一个单一的数组中

// 定义输入数组
let input = [ [[1,4],[40,4]] , [[1,5],[40,5]] , [[4,7],[4,24]] ,[[1,9],[4,1]], [[1,2],[6,4]], [[80,4],[90,4]] , [[4,1],[4,40]] , [[4,35],[4,29]] , [[4,28],[4,35]] ,[[5,3.6],[9,5.2]] ] ;

// 定义一个函数来获取由一对点形成的线的斜率和截距
function describeLine([[x1, y1], [x2, y2]]) {
  if (x1 == x2) { // 垂直线
    return {m: "vertical", x: x1}
  }
  const p1 = x1 > x2 ? { x: x1, y: y1 } : { x: x2, y: y2 }
  const p2 = x1 < x2 ? { x: x1, y: y1 } : { x: x2, y: y2 }
  
  const m = (p1.y - p2.y) / (p1.x - p2.x)
  const y = y1 - m * x1
  return { m, y }
}

// 这段代码遍历输入数组并将所有点对积累到一个字典结构中,以斜率和截距为键
// 对于斜率为2且y截距为3的线,它将进入映射的 { 2 : { 3: [[ [点1], [点2]] ], [ 具有相同属性的其他线] ...
const maps = input.reduce((acc, line) => {
    const desc = describeLine(line)
    const m = acc[desc.m] || { }
    if (desc.x) { // 垂直线
      x = m[desc.x] || []
      return { ...acc, [desc.m]: { ...m, [desc.x]: [ ...x, line ]}}
    } else {
      y = m[desc.y] || []
      return { ...acc, [desc.m]: { ...m, [desc.y]: [ ...y, line ]}}
    }
}, {})

// 一旦我们积累了这个结构,我们只需将各个数组收集到一个大数组中
const sameLines = Object.values(maps).flatMap(Object.values)
console.log(JSON.stringify(sameLines, null, 2))

希望这有助于您理解代码的工作原理。

英文:

Here's my take at it. The strategy is to first get a description of the line for each pair of points, and then group those together into a map of maps, first keyed by slopes, and then by intercepts (either x or y, depending on if the line is vertical or not), and then extract the groups into a single array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let input=[ [[1,4],[40,4]] , [[1,5],[40,5]] , [[4,7],[4,24]] ,[[1,9],[4,1]], [[1,2],[6,4]], [[80,4],[90,4]] , [[4,1],[4,40]] , [[4,35],[4,29]] , [[4,28],[4,35]] ,[[5,3.6],[9,5.2]] ] ; 
// a function to get the slope and intercept of the line formed by a pair of points
function describeLine([[x1, y1], [x2, y2]]) {
if (x1 == x2) { // vertical line
return {m: &quot;vertical&quot;, x: x1}
}
const p1 = x1 &gt; x2 ? { x: x1, y: y1 } : { x: x2, y: y2 }
const p2 = x1 &lt; x2 ? { x: x1, y: y1 } : { x: x2, y: y2 }
const m = (p1.y - p2.y) / (p1.x - p2.x)
const y = y1 - m * x1
return { m, y }
}
// this runs through the input array and accumulates all the pairs of points into a dictionary structure keyed by slope and then intercept
// so for a line with slope 2 and y-intercept 3, it would go into the map at { 2 : { 3: [[ [point 1], [point2]] ], [ line 2 with same props] ...
const maps = input.reduce((acc, line) =&gt; {
const desc = describeLine(line)
const m = acc[desc.m] || { }
if (desc.x) { // vertical line
x = m[desc.x] || []
return { ...acc, [desc.m]: { ...m, [desc.x]: [ ...x, line ]}}
} else {
y = m[desc.y] || []
return { ...acc, [desc.m]: { ...m, [desc.y]: [ ...y, line ]}}
}
}, {})
// once we&#39;ve accumulated that structure, we can just collect the individual arrays into one big array
const sameLines = Object.values(maps).flatMap(Object.values)
console.log(JSON.stringify(sameLines, null, 2))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月8日 09:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定