LeetCode in Kotlin

1401. Circle and Rectangle Overlapping

Medium

You are given a circle represented as (radius, xCenter, yCenter) and an axis-aligned rectangle represented as (x1, y1, x2, y2), where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the rectangle.

Return true if the circle and rectangle are overlapped otherwise return false. In other words, check if there is any point (xi, yi) that belongs to the circle and the rectangle at the same time.

Example 1:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = 1, y1 = -1, x2 = 3, y2 = 1

Output: true

Explanation: Circle and rectangle share the point (1,0).

Example 2:

Input: radius = 1, xCenter = 1, yCenter = 1, x1 = 1, y1 = -3, x2 = 2, y2 = -1

Output: false

Example 3:

Input: radius = 1, xCenter = 0, yCenter = 0, x1 = -1, y1 = 0, x2 = 0, y2 = 1

Output: true

Constraints:

Solution

class Solution {
    fun checkOverlap(
        radius: Int,
        xCenter: Int,
        yCenter: Int,
        x1: Int,
        y1: Int,
        x2: Int,
        y2: Int,
    ): Boolean {
        // Find the closest point to the circle within the rectangle
        val closestX = clamp(xCenter, x1, x2)
        val closestY = clamp(yCenter, y1, y2)
        // Calculate the distance between the circle's center and this closest point
        val distanceX = xCenter - closestX
        val distanceY = yCenter - closestY
        // If the distance is less than the circle's radius, an intersection occurs
        val distanceSquared = distanceX * distanceX + distanceY * distanceY
        return distanceSquared <= radius * radius
    }

    private fun clamp(`val`: Int, min: Int, max: Int): Int {
        return Math.max(min, Math.min(max, `val`))
    }
}