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条)

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染