
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Random Point in Non-overlapping Rectangles in C++
Suppose we have a list of non-overlapping axis-aligned rectangles rects, we have to write a function pick which randomly and uniformly picks an integer number, point in the space covered by the rectangles. So we have to keep in mind some points −
- An integer point is a point that has integer coordinates.
- A point on the perimeter of a rectangle is included in the space covered by the rectangles.
- The ith rectangle = rects[i] denotes [x1,y1,x2,y2], where [x1, y1] are the integer coordinates of the bottom-left corner, and [x2, y2] are the integer coordinates of the top-right corner.
- The length and the width of each rectangle does not exceed 2000.
- 1 <= rects.length <= 100
- pick return a point as an array of integer coordinates [p_x, p_y]
If the input is like [1,1,5,5], and we call pick() three times, then the output will be [4,1], [4,1], [3,3]
To solve this, we will follow these steps −
- Make two arrays area and rect
- In the initializer do the following −
- rect := rects, sum := 0
- for i in range 0 to size of rects – 1
- (x1, y1) := (rects[i, 0], rects[i, 1])
- (x2, y2) := (rects[i, 2], rects[i, 3])
- temp := |x2 – x1 + 1| * |y2 – y1 + 1|
- sum := sum + temp, and insert sum into area
- In the pick method, do the following −
- randArea := random number mod sum + 1
- for i in range 0 to size of area – 1
- if randArea <= area[i], then come out from the loop
- dist_x := random number mod |rect[i,0] – rect[i,2] + 1|
- dist_y := random number mod |rect[i,1] – rect[i,3] + 1|
- return a pair (dist_x + rect[i, 0], dist_y + rect[i, 1])
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector <int> area; vector < vector <int> > rect; int sum; Solution(vector<vector<int> >& rects) { rect = rects; sum = 0; for(int i =0 ; i < rects.size(); i++){ int x1 = rects[i][0]; int y1 = rects[i][1]; int x2 = rects[i][2]; int y2 = rects[i][3]; int temp = (abs(x2 - x1) + 1) * (abs(y2 - y1) + 1); sum += temp; area.push_back(sum); } } vector<int> pick() { int randArea = rand() % sum + 1; int i; for(i = 0; i < area.size(); i++){ if(randArea <= area[i]) break; } int dist_x = rand() % (abs(rect[i][0] - rect[i][2] ) + 1); int dist_y = rand() % (abs(rect[i][1] - rect[i][3] ) + 1); return {dist_x + rect[i][0], dist_y + rect[i][1]}; } }; main(){ vector<vector<int> > v = {{1, 1, 5, 5}}; Solution ob(v); print_vector(ob.pick()); print_vector(ob.pick()); print_vector(ob.pick()); }
Input
["Solution", "pick", "pick", "pick"] [[[[1, 1, 5, 5]]], [], [], []]
Output
[2, 3, ] [4, 1, ] [3, 5, ]
- Related Articles
- Non-overlapping Intervals in C++
- Minimum Size of Two Non-Overlapping Intervals in C++
- Maximum Sum of Two Non-Overlapping Subarrays in C++
- Maximum Sum of 3 Non-Overlapping Subarrays in C++
- Maximum sum two non-overlapping subarrays of given size in C++
- Max sum of M non-overlapping subarrays of size K in C++
- Generate Random Point in a Circle in C++
- Count pairs of non-overlapping palindromic sub-strings of the given string in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Program to find maximum number of non-overlapping substrings in Python
- Maximum sum of lengths of non-overlapping subarrays with k as the max element in C++
- Circle and Rectangle Overlapping in C++
- Program to find maximum sum of two non-overlapping sublists in Python
- Program to find number of sets of k-non-overlapping line segments in Python
- Count of non-overlapping sub-strings “101” and “010” in the given binary string

Advertisements