Generate all integral points lying inside a rectangle
Given a rectangle of length L, and width, W, the task is to generate all integral coordinates (X, Y) that lie within a rectangle pf dimensions L * W having one of its vertexes in the origin (0, 0).
Examples:
Input: L = 3, W = 2
Output: (0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)
Explanation: Total number of integral coordinates existing within the rectangle are L × W = 6. Therefore, the output is (0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1).
Input: L = 5, W = 3
Output: (0, 0) (0, 1) (0, 2) (1, 0) (1, 1) (1, 2) (2, 0) (2, 1) (2, 2) (3, 0) (3, 1) (3, 2) (4, 0) (4, 1) (4, 2)
Approach: The problem can be solved by generating all integral numbers from the range 0 to L for X-coordinates and from 0 to W for Y-coordinates using the rand() function. Follow the steps below to solve the problem:
- Create a set of pairs to store all the coordinates(X, Y) that lie within the rectangle.
- Use the equation rand() % L to generate all the integers lying between 0 to L and rand() % W to generate all the integers lying between 0 to W.
- Print all possible L × W coordinates (X, Y) that lie within the rectangle.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void generatePoints( int L, int W)
{
set<pair< int , int > > hash;
int total = (L * W);
srand ( time (0));
while (total--) {
int X = rand () % L;
int Y = rand () % W;
while (hash.count({ X, Y })) {
X = rand () % L;
Y = rand () % W;
}
hash.insert({ X, Y });
}
for ( auto points : hash) {
cout << "(" << points.first << ", "
<< points.second << ") " ;
}
}
int main()
{
int L = 3, W = 2;
generatePoints(L, W);
}
|
Java
import java.util.HashSet;
import java.util.Random;
class GFG
{
public static void generatePoints( int L, int W)
{
HashSet<String> hash = new HashSet<>();
int total = (L * W);
while (total-- > 0 ) {
int X = new Random().nextInt(L);
int Y = new Random().nextInt(W);
while (hash.contains(X + "," + Y)) {
X = new Random().nextInt(L);
Y = new Random().nextInt(W);
}
hash.add(X + "," + Y);
}
for (String element : hash) {
System.out.print( "(" + element + ") " );
}
}
public static void main(String[] args)
{
int L = 3 , W = 2 ;
generatePoints(L, W);
}
}
|
Python3
import time
import random
random.seed(time.time())
def generatePoints(L, W):
hash = {}
total = (L * W)
for i in range (total):
X = random.randint( 0 , L) % L
Y = random.randint( 0 , W) % W
while ((X, Y) in hash ):
X = random.randint( 0 , L) % L
Y = random.randint( 0 , W) % W
hash [(X, Y)] = 1
for points in sorted ( hash ):
print ( "(" , points[ 0 ],
", " , points[ 1 ],
") " , end = "")
if __name__ = = '__main__' :
L, W = 3 , 2
generatePoints(L, W)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
public class store : IComparer<KeyValuePair< int , int >>
{
public int Compare(KeyValuePair< int , int > x,
KeyValuePair< int , int > y)
{
if (x.Key != y.Key)
{
return x.Key.CompareTo(y.Key);
}
else
{
return x.Value.CompareTo(y.Value);
}
}
}
static void generatePoints( int L, int W)
{
SortedSet<KeyValuePair< int ,
int >> hash = new SortedSet<KeyValuePair< int ,
int >>( new store());
int total = (L * W);
Random rand = new Random();
while ((total--) != 0)
{
int X = rand.Next() % L;
int Y = rand.Next() % W;
while (hash.Contains(
new KeyValuePair< int , int >(X, Y)))
{
X = rand.Next() % L;
Y = rand.Next() % W;
}
hash.Add( new KeyValuePair< int , int >(X, Y));
}
foreach (KeyValuePair< int , int > x in hash)
{
Console.Write( "(" + x.Key + ", " +
x.Value + ") " );
}
}
public static void Main( string [] args)
{
int L = 3, W = 2;
generatePoints(L, W);
}
}
|
Javascript
function generatePoints(L, W)
{
let hash = new Set();
let total = (L * W);
while (total--) {
let X = Math.floor((Math.random()*(L+1))) % L;
let Y = Math.floor((Math.random()*(W+1))) % W;
while (hash.has([X, Y].join())) {
X = Math.floor((Math.random()*(L+1))) % L;
Y = Math.floor((Math.random()*(W+1))) % W;
}
hash.add([X, Y].join());
}
hash.forEach(element=>{
document.write( "(" , element, ") " );
})
}
let L = 3, W = 2;
generatePoints(L, W);
|
Output:
(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)
Time Complexity: O(L * W)
Auxiliary Space: O(L * W)
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...