简体   繁体   English

如何找到包含所有点或与y = x轴对称的点的最小矩形?

[英]How can I find the least rect, which contains all points or the point symmetrical to the y=x axis?

I have a set of points (x,y). 我有一组点(x,y)。 How can I find the smallest rectangle (edges are perpendicular to the axis of the coordinate system) that contains these points or points symmetrical to the y=x axis? 如何找到包含这些点或与y = x轴对称的点的最小矩形(边与坐标系的轴垂直)? Not all the points have to be mirror images at the same time, so it is possible to have some normal and some symmetrical points to the original ones. 并非所有的点都必须同时是镜像的,因此可以对原始点具有一些法线和对称点。

You could solve this using the brute-force O(n*2^n) approach: define your points as some class/structure which, in addition to the (x,y) coordinates of your point, also holds a boolean mirrored state. 可以使用蛮力O(n*2^n)方法解决此问题:将点定义为某些类/结构,除了点的(x,y)坐标外,还保留布尔mirrored状态。 For n number of points, loop over all the 2^n possible set of states of this set of points. 对于n个点,循环遍历该点集的所有2^n个状态集。 For each set of states, calculate the circumference ( 2x+2y ) of the bounding rectangle. 对于每个状态集,计算边界矩形的周长( 2x+2y )。

Below follows this solution implemented in Swift. 下面是在Swift中实现的此解决方案。 If you choose to use this brute-force approach, hopefully you can translate it into your language of choice. 如果选择使用这种蛮力方法,希望可以将其翻译成您选择的语言。


Point container and help functions: 点容器和帮助功能:

struct Point {
    private var xOrig : Double
    private var yOrig : Double
    private var mirrored = false
    var x : Double { return mirrored ? yOrig : xOrig }
    var y : Double { return mirrored ? xOrig : yOrig }

    init(_ x: Double, _ y: Double) {
        xOrig = x
        yOrig = y
    }

    mutating func setState(state: Bool) {
        mirrored = state
    }
}

func boundingBoxCircumference(points: [Point]) -> Double {
    let xMax = points.maxElement { $0.x < $1.x }?.x ?? 0.0
    let xMin = points.minElement { $0.x < $1.x }?.x ?? 0.0
    let yMax = points.maxElement { $0.y < $1.y }?.y ?? 0.0
    let yMin = points.minElement { $0.y < $1.y }?.y ?? 0.0

    return 2*((xMax - xMin) + (yMax - yMin))
}

func setMirroredStates(inout points: [Point], states: [Bool]) {
    for i in 0..<points.count {
        points[i].setState(states[i])
    }
}

Main function and an example 主要功能和实例

func findSmallestBox(var points: [Point]) -> Double {
    var smallestBox = Double.infinity
    for i in 0..<Int(pow(2,Double(points.count))) {
        smallestBox = min(smallestBox, boundingBoxCircumference(points))

        var binString = String(i, radix: 2)
        if let a = Optional(points.count - binString.characters.count) where a > 0 {
            binString = "".stringByPaddingToLength(a, withString: "0", startingAtIndex: 0) + binString
        }
        let pointStates = Array(binString.characters).map { String($0) == "1" }

        setMirroredStates(&points, states: pointStates)
    }
    return smallestBox
}

var myPoints : [Point] = [Point(1,2), Point(-4,3), Point(7,-5), Point(5,4), Point(-5, -1)]
print(findSmallestBox(myPoints)) // 34

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从一堆点中找到包含三角形的最小点P(x,y)? - How do i find the smallest point P(x,y) containing triangle from a bunch of points? 如何在二进制图像中找到线段的x,y的中间点? - How can I find the middle points of x, y of a line segment in a binary image? 在X1 y1 X2 y2形式的10,000个点的文件中,如何检测至少四个正方形? 爪哇 - In a file of 10,000 points of the form X1 y1 X2 y2 ,,, How to detect at least 4 which form a square ? java 保存点 (x_i, y_i) 的数据结构和查找 x_i &gt; a &amp; y_i &gt; b 的所有点的过程 - Data structure that holds points (x_i, y_i) and a procedure to find all points that x_i > a & y_i > b 如何直接找到一个点旁边的所有点? - How to find all points directly next to a point? 给定许多圆和一个点,如何找到哪个圆包含该点 - Given many circles and a point, how to find which circle contains that point 如何从多个点找到最远的 x, y 坐标? - How to find the farthest x, y coordinates from many points? 给定几个点和圆,我怎么知道哪个点在哪个圆上? - Given a few points and circles, how can I tell which point lies in which circles? 如何从可以平移或旋转的四个点找到一个点的坐标? 所有这些点形成一个僵硬的身体 - How to find co-ordinates of a point from four points that can translate or rotate? All these points form a rigid body 给定顶点,基本中点和基本宽度,如何找到等腰三角形的所有点? - How do I find all points of an isosceles triangle given the vertex point, base midpoint, and base width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM