Open In App

Generate all integral points lying inside a rectangle

Last Updated : 30 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

  1. Create a set of pairs to store all the coordinates(X, Y) that lie within the rectangle.
  2. 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.
  3. Print all possible L × W coordinates (X, Y) that lie within the rectangle.

Below is the implementation of the above approach: 

C++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to generate coordinates
// lying within the rectangle
void generatePoints(int L, int W)
{
    // Store all possible coordinates
    // that lie within the rectangle
    set<pair<int, int> > hash;

    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);

    // Use current time as seed
    // for random generator
    srand(time(0));

    // Generate all possible
    // coordinates
    while (total--) {
        // Generate all possible
        // X-coordinates
        int X = rand() % L;

        // Generate all possible
        // Y-coordinates
        int Y = rand() % W;

        // If coordinates(X, Y) has
        // not been generated already
        while (hash.count({ X, Y })) {
            X = rand() % L;
            Y = rand() % W;
        }

        // Insert the coordinates(X, Y)
        hash.insert({ X, Y });
    }

    // Print the coordinates
    for (auto points : hash) {
        cout << "(" << points.first << ", "
             << points.second << ") ";
    }
}

// Driver Code
int main()
{
    // Rectangle dimensions
    int L = 3, W = 2;

    generatePoints(L, W);
}
Java
import java.util.HashSet;
import java.util.Random;

class GFG
{
  
  // Function to generate coordinates
  // lying within the rectangle
  public static void generatePoints(int L, int W) 
  {
    
    // Store all possible coordinates
    // that lie within the rectangle
    HashSet<String> hash = new HashSet<>();

    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);

    // Generate all possible
    // coordinates
    while (total-- > 0) {
      // Generate all possible
      // X-coordinates
      int X = new Random().nextInt(L);
      // Generate all possible
      // Y-coordinates
      int Y = new Random().nextInt(W);

      // If coordinates(X, Y) has
      // not been generated already
      while (hash.contains(X + "," + Y)) {
        X = new Random().nextInt(L);
        Y = new Random().nextInt(W);
      }

      // Insert the coordinates(X, Y)
      hash.add(X + "," + Y);
    }

    // Print the coordinates
    for (String element : hash) {
      System.out.print("(" + element + ") ");
    }
  }

  // Driver Code
  public static void main(String[] args)
  {
    
    // Rectangle dimensions
    int L = 3, W = 2;

    generatePoints(L, W);
  }
}


// This code is contributed by phasing17.
Python3
# Python3 program to implement
# the above approach
import time
import random

random.seed(time.time())

# Function to generate coordinates
# lying within the rectangle
def generatePoints(L, W):
    
    # Store all possible coordinates
    # that lie within the rectangle
    hash = {}

    # Stores the number of possible
    # coordinates that lie within
    # the rectangle
    total = (L * W)

    # Generate all possible
    # coordinates
    for i in range(total):
        
        # Generate all possible
        # X-coordinates
        X = random.randint(0, L) % L

        # Generate all possible
        # Y-coordinates
        Y = random.randint(0, W) % W

        # If coordinates(X, Y) has
        # not been generated already
        while ((X, Y) in hash):
            X = random.randint(0, L) % L
            Y = random.randint(0, W) % W

        # Insert the coordinates(X, Y)
        hash[(X, Y)] = 1

    # Print the coordinates
    for points in sorted(hash):
        print("(", points[0], 
             ", ", points[1],
             ") ", end = "")

# Driver code 
if __name__ == '__main__':
    
    # Rectangle dimensions
    L, W = 3, 2

    generatePoints(L, W)

# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
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);    
        }
    }
}
     
// Function to generate coordinates
// lying within the rectangle
static void generatePoints(int L, int W)
{
    
    // Store all possible coordinates
    // that lie within the rectangle
    SortedSet<KeyValuePair<int,
                           int>> hash = new SortedSet<KeyValuePair<int,
                                                                   int>>(new store());
 
    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    int total = (L * W);
 
    // For random generator
    Random rand = new Random();
 
    // Generate all possible
    // coordinates
    while ((total--) != 0) 
    {
        
        // Generate all possible
        // X-coordinates
        int X = rand.Next() % L;
 
        // Generate all possible
        // Y-coordinates
        int Y = rand.Next() % W;
 
        // If coordinates(X, Y) has
        // not been generated already
        while (hash.Contains(
            new KeyValuePair<int, int>(X, Y)))
        {
            X = rand.Next() % L;
            Y = rand.Next() % W;
        }
 
        // Insert the coordinates(X, Y)
        hash.Add(new KeyValuePair<int, int>(X, Y));
    }
 
    // Print the coordinates
    foreach(KeyValuePair<int, int> x in hash)
    {
        Console.Write("(" + x.Key + ", " +
                          x.Value + ") ");
    }
}

// Driver Code
public static void Main(string[] args)
{
    
    // Rectangle dimensions
    int L = 3, W = 2;
 
    generatePoints(L, W);
}
}

// This code is contributed by rutvik_56
JavaScript
// JavaScript program to implement
// the above approach

// Function to generate coordinates
// lying within the rectangle
function generatePoints(L, W)
{
    // Store all possible coordinates
    // that lie within the rectangle
    let hash = new Set();

    // Stores the number of possible
    // coordinates that lie within
    // the rectangle
    let total = (L * W);

    // Generate all possible
    // coordinates
    while (total--) {
        // Generate all possible
        // X-coordinates
        let X = Math.floor((Math.random()*(L+1))) % L;

        // Generate all possible
        // Y-coordinates
        let Y = Math.floor((Math.random()*(W+1))) % W;

        // If coordinates(X, Y) has
        // not been generated already
        while (hash.has([X, Y].join())) {
            X = Math.floor((Math.random()*(L+1))) % L;
            Y = Math.floor((Math.random()*(W+1))) % W;
        }

        // Insert the coordinates(X, Y)
        // console.log(X, Y);
        hash.add([X, Y].join());
    }

    // Print the coordinates
    hash.forEach(element=>{
        document.write("(", element, ") ");
    })

}

// Driver Code

// Rectangle dimensions
let L = 3, W = 2;

generatePoints(L, W);

// The code is contributed by Gautam goel (gautamgoel962)

Output: 
(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)

 

Time Complexity: O(L * W) 
Auxiliary Space: O(L * W)
 


Next Article
Article Tags :
Practice Tags :

Similar Reads