修剪CGRect中的直线(2 CGPoint)

问题描述:

给定CGRect并用2创建的线CGPoint是否有找到直线与直线相交的坐标的方法?修剪CGRect中的直线(2 CGPoint)

enter image description here

从前面的图像:我想要做到的,是替代点的矩形之外与相交的矩形边框的红点。

简而言之,我正在寻找一种方法来修剪矩形内的一条线。

这是一个数学问题,但我想知道如何使用基础解决这个问题,如果可能的话。

以下最新评论:似乎Core Graphics在这个过程中不能真正有用。我可以在Swift中转换的任何其他提示或公式?

+0

你尝试过什么?你做了什么研究?必须有无数的这样做的例子。找到一些,做一些尝试翻译成你最喜欢的语言。 [编辑]你的问题到目前为止你所尝试过的。 – rmaddy

+0

其实我是在寻求帮助,因为我找不到任何类似的东西(和我的问题中详细说明的一样),我正在寻求任何利用基础和核心图形的解决方案。 – MatterGoal

+1

您可能过度地限制自己,“利用基础和核心图形”。没有具体的功能可以做到这一点。你需要做数学。你当然可以返回'CGPoint',这样你就可以“利用核心图形”。但是解决这个问题的方法就是求解一些线性方程组。如果你已经知道如何做到这一点,就完成了。核心图形在这里没有提供任何东西,但有些类型可以使用(并且Foundation没有真正提供任何对这个问题有用的东西)。您还需要定义如果线与矩形的多个边相交会发生什么情况。 –

像这样的东西(随便测试),基于How do you detect where two line segments intersect?

import CoreGraphics 

let rect = CGRect(x: 10, y: 10, width: 100, height: 100) 

let point1 = CGPoint(x: 200, y: 200) 
let point2 = CGPoint(x: 20, y: 20) 

struct LineSegment { 
    var point1: CGPoint 
    var point2: CGPoint 

    func intersection(with line: LineSegment) -> CGPoint? { 
     // We'll use Gavin's interpretation of LeMothe: 
     // https://stackoverflow.com/a/1968345/97337 

     let p0_x = self.point1.x 
     let p0_y = self.point1.y 
     let p1_x = self.point2.x 
     let p1_y = self.point2.y 

     let p2_x = line.point1.x 
     let p2_y = line.point1.y 
     let p3_x = line.point2.x 
     let p3_y = line.point2.y 

     let s1_x = p1_x - p0_x 
     let s1_y = p1_y - p0_y 
     let s2_x = p3_x - p2_x 
     let s2_y = p3_y - p2_y 

     let denom = (-s2_x * s1_y + s1_x * s2_y) 

     // Make sure the lines aren't parallel 
     guard denom != 0 else { return nil } // parallel 

     let s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y))/denom 
     let t = (s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x))/denom 

     // We've parameterized these lines as "origin + scale*vector" 
     // (s is the "scale" along one line, t is the "scale" along the other. 
     // At scale=0, we're at the origin at scale=1, we're at the terminus. 
     // Make sure we crossed between those. For more on what I mean by 
     // "parameterized" and why we go from 0 to 1, look up Bezier curves. 
     // We're just making a 1-dimentional Bezier here. 
     guard (0...1).contains(s) && (0...1).contains(t) else { return nil } 

     // Collision detected 
     return CGPoint(x: p0_x + (t * s1_x), y: p0_y + (t * s1_y)) 
    } 
} 

extension CGRect {  
    var edges: [LineSegment] { 
     return [ 
      LineSegment(point1: CGPoint(x: minX, y: minY), point2: CGPoint(x: minX, y: maxY)), 
      LineSegment(point1: CGPoint(x: minX, y: minY), point2: CGPoint(x: maxX, y: minY)), 
      LineSegment(point1: CGPoint(x: minX, y: maxY), point2: CGPoint(x: maxX, y: maxY)), 
      LineSegment(point1: CGPoint(x: maxX, y: minY), point2: CGPoint(x: maxX, y: maxY)), 
     ] 
    } 

    func intersection(with line: LineSegment) -> CGPoint? { 

    // Let's be super-simple here and require that one point be in the box and one point be outside, 
    // then we can ignore lots of corner cases 
     guard contains(line.point1) && !contains(line.point2) || 
      contains(line.point2) && !contains(line.point1) else { return nil } 

     // There are four edges. We might intersect with any of them (we know 
     // we intersect with exactly one, based on the previous guard. 
     // We could do a little math and figure out which one it has to be, 
     // but the `if` would be really tedious, so let's just check them all. 
     for edge in edges { 
      if let p = edge.intersection(with: line) { 
       return p 
      } 
     } 

     return nil 
    } 
} 

rect.intersection(with: LineSegment(point1: point1, point2: point2)) 
+0

它似乎与一个单一的交叉口,我试图弄清楚如何使它的工作也与直线的两条边相交的线(我将通过一个完整的检查明天)。 – MatterGoal

+0

主要问题将是定义你想要的答案。注意'CGRect.intersection'顶部的警戒声明和注释。目前它故意遗漏了这种情况。如果你想得到一个半任意交点,只需删除guard语句。 –

+0

谢谢,这段代码很完美。 而不是返回的交点,有几个编辑我要返回一个线被修剪在矩形内: - 我保持守卫,当两个点都包含在矩形中时返回原始线。 - 然后我选择所有与行 相交的边缘 - 根据我定义哪些点与替代计算出的交叉点的边缘。 我会更新我的问题添加此代码。 – MatterGoal