Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

如下图所示,已知一条黑色折线,求向两侧平移后(平移距离为x,假设为1),红线的坐标。求算法。

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

题中给的信息不足,因为偏移除了距离要素,还需要角度,这才能确定最终偏移后的数据。有了角度后,可以用三角函数推,很简单,初中的知识

function offsetLine(line, d, angle){
  //let line = [[1, 1], [4, 2], [5, 5], [7, 6]]
  //let d = 1 
  //let angle = 30

  //斜边长度d已知,角度angle已知
  //对边长度就是y的偏移量 就是 d * sin(angle) ==> d * Math.sin(angle * Math.PI / 180)
  //邻边长度就是x的偏移量 就是 d * cos(angle) ==> d * Math.cos(angle * Math.PI / 180)
  let ox = d * Math.cos(angle * Math.PI / 180)
  let oy = d * Math.sin(angle * Math.PI / 180)
  return line.map(coords => [coords[0] + ox, coords[1] + oy])
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...