Ruby中的矩形交集

时间:2015-07-31 22:24:24

标签: arrays ruby intersection intersect

我正在尝试理解这个程序,但我遇到了一些困难。我不理解y_minx_maxy_max[0][0]的部分。

我知道程序通过左下角和右上角坐标的两个矩形,但数组索引[1][1]# Write a function, `rec_intersection(rect1, rect2)` and returns the # intersection of the two. # # Rectangles are represented as a pair of coordinate-pairs: the # bottom-left and top-right coordinates (given in `[x, y]` notation). # # Hint: You can calculate the left-most x coordinate of the # intersection by taking the maximum of the left-most x coordinate of # each rectangle. Likewise, you can calculate the top-most y # coordinate of the intersection by taking the minimum of the top most # y coordinate of each rectangle. # # Difficulty: 4/5 def rec_intersection(rect1, rect2) x_min = [rect1[0][0], rect2[0][0]].max x_max = [rect1[1][0], rect2[1][0]].min y_min = [rect1[0][1], rect2[0][1]].max y_max = [rect1[1][1], rect2[1][1]].min return nil if ((x_max < x_min) || (y_max < y_min)) return [[x_min, y_min], [x_max, y_max]] end puts rec_intersection( [[0, 0], [2, 1]], [[1, 0], [3, 1]] ) == [[1, 0], [2, 1]] puts rec_intersection( [[1, 1], [2, 2]], [[0, 0], [5, 5]] ) == [[1, 1], [2, 2]] puts rec_intersection( [[1, 1], [2, 2]], [[4, 4], [5, 5]] ) == nil puts rec_intersection( [[1, 1], [5, 4]], [[2, 2], [3, 5]] ) == [[2, 2], [3, 4]] 等来自何处?

我对发生的事感到困惑,所以解释会有所帮助。

class CustomReportVC: UIViewController, UIAdaptivePresentationControllerDelegate, UIPopoverPresentationControllerDelegate {

  func showPicker(pickerValues:[String], field:UITextField) -> AnyPickerVC{
    //Set up modal
    let storyboard = UIStoryboard(name: "Popovers", bundle: nil)
    let modal = storyboard.instantiateViewControllerWithIdentifier("AnyPickerModal") as! AnyPickerVC
    modal.modalPresentationStyle = UIModalPresentationStyle.Popover
    let pc = modal.popoverPresentationController
    pc?.permittedArrowDirections = .Down
    pc?.sourceView = field
    pc?.sourceRect = field.bounds
    modal.preferredContentSize = CGSizeMake(300,180)
    pc?.delegate = self

    //Pass in data
    modal.data = pickerValues

    //Set the value from within the picker controller
    modal.passDataToParent = { (value) in
      field.text = value
    }

    return modal
  }

  //Required for the popover
  func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
  }

}

3 个答案:

答案 0 :(得分:8)

变量x_minx_maxy_miny_max用于存储相交区域的坐标。它们是使用传入的矩形在双值数组上使用maxmin获得的。调用[1 ,2].max将返回2,并且调用[1,2].min将返回1

这些变量代表交叉矩形的原因可能更容易通过图像理解(非常详细和专业的图表传入): rectangle intersect for dummies

如您所见,黄色(交叉)矩形的最小值不能小于红色矩形的最小值。最大值可以不小于蓝色矩形的最大值。

答案 1 :(得分:3)

  

我特别得到的是x_min,y_min,x_max,y_max的部分。我得到程序通过左下角和右上角坐标点的2个矩形。但阵列索引来自哪里? [0] [0],[1] [1]等?

上面评论的这一部分代码对于理解这一点非常重要:

# Rectangles are represented as a pair of coordinate-pairs: the
# bottom-left and top-right coordinates (given in `[x, y]` notation).

因此,如果rect是一个矩形,则rect[0]表示左下角,rect[1]表示右上角。此外,rect[0][0]表示左下角的x坐标,rect[0][1]是该角的y坐标,依此类推。

评论的这一部分也很重要:

# Hint: You can calculate the left-most x coordinate of the
# intersection by taking the maximum of the left-most x coordinate of
# each rectangle. [...]

如果rect是矩形,则该矩形的最左侧x坐标是左下角的x坐标。如上所述,rect[0][0]表示左下角的x坐标。所以,在这一行:

x_min = [rect1[0][0], rect2[0][0]].max

rect1[0][0]rect2[0][0]是矩形的两个最左边的x坐标,这行代码表示两个矩形的交叉点最左边的x坐标是相等的无论其中哪一个更大。

答案 2 :(得分:0)

基本上当它正在寻找x_min的最大值或x_max的最小值时,它首先询问“x值的哪个数组”,然后它会询问“哪个值”。

代码 x_min = [rect1[0][0], rect2[0][0]].max

专门寻找[rect1 [第一个数组(0)] [第一个值(0)]