Rect Algorithm

The Rect Algorithm, also known as the rectangle algorithm, is a computer vision-based technique used for detecting objects within an image or video stream. It is a powerful and versatile method that can be employed for various applications, such as object recognition, motion detection, and even optical character recognition (OCR). The fundamental concept behind the Rect Algorithm is the systematic division of an image into a series of smaller rectangular regions, followed by the analysis of each region's features to identify the presence of objects of interest. This approach allows the algorithm to efficiently search through the image space and accurately detect the desired objects while minimizing the computational complexity and resources required. One of the key advantages of the Rect Algorithm is its ability to handle scale and rotation invariant object detection. By analyzing the features of each rectangular region and comparing them to a predefined template or set of features, the algorithm can identify objects that may be in different orientations or sizes within the image. Furthermore, the Rect Algorithm can be easily adapted to work with various feature extraction methods, such as edge detection, intensity-based features, or more advanced deep learning-based approaches. This makes the Rect Algorithm a highly flexible and effective technique for a wide range of computer vision tasks, from simple object detection to complex scene understanding and analysis.
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.algorithmexamples.geometry

data class Rect(val x1: Int, val y1: Int, val x2: Int, val y2: Int) {
    val width = x2 - x1
    val height = y2 - y1
    val origin = Point(x1, y1)
    val center = Point(origin.x + width / 2, origin.y + height / 2)
    val TL = origin
    val BR = Point(origin.x + width, origin.y + height)

    constructor(TL: Point, BR: Point) : this(TL, (BR.x - TL.x), (BR.y - TL.y))

    constructor(origin: Point, width: Int, height: Int) : this(origin.x, origin.y, origin.x + width, origin.y + height)

    fun isInside(point: Point): Boolean =
            point.x >= origin.x && point.y >= origin.y &&
                    point.x <= origin.x + width && point.y <= origin.y + height

    fun cover(point: Point): Rect =
            Rect(Point(minOf(x1, point.x), minOf(y1, point.y)), Point(maxOf(x2, point.x), maxOf(y2, point.y)))

    fun intersects(other: Rect) =
            !(other.x1 > this.x2 || other.x2 < this.x1 || other.y1 > this.y2 || other.y2 < this.y1)
}

LANGUAGE:

DARK MODE: