Related Articles

Related Articles

Generate all integral points lying inside a rectangle
  • Last Updated : 22 Oct, 2020

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

(0, 0) (0, 1) (1, 0) (1, 1) (2, 0) (2, 1)




 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :