duan010167787 2018-06-06 20:22
浏览 76
已采纳

给定2d空间中的一组线,如何将它们截断为界?

Background:

Heya! I'm trying to generate a circuit board which has a subset of San Francisco printed on it. Most of the pieces of this are done, and I'm generating images that look like this:

Rendered San Francisco

The problem is that I am rendering lines which extend outside my hardcoded cutoff boundary (I am rendering lines which one side is in and one side is out of bounds).

Question:

Given a set of lines like this:

# x1,y1,  x2,y2
10,10,40,40
80,80,120,120

How can I modify the co-ordinates of each line such that it 'cuts off' at a specific bound?

In the case above, the second line (which in original form) extends to (120,120), should only extend to (100,100) assuming bounds of 100,100.

Thoughts

Based on what I remember from high-school math, I should plug something into the formula y=mx+b yeah? Even then, how would I deal with an infinite gradient or the like?

Thanks for any and all help :D Puesdocode/python/Go preferred, but explanations just as graciously recieved.

<3 Tom

  • 写回答

2条回答 默认 最新

  • douluan5444 2018-06-07 08:20
    关注

    Your best friend is the Cohen–Sutherland line clipping algorithm.

    https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗