Generate all integral points lying inside a rectangle
Last Updated :
30 Jan, 2023
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++
// 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)
Similar Reads
Count Integral points inside a Triangle Given three non-collinear integral points in XY plane, find the number of integral points inside the triangle formed by the three points. (A point in XY plane is said to be integral/lattice point if both its co-ordinates are integral). Example: Input: p = (0, 0), q = (0, 5) and r = (5,0) Output: 6Ex
8 min read
Check if a point lies inside a rectangle | Set-2 Given coordinates of bottom-left and top-right corners of a rectangle. Check if a point (x, y) lies inside this rectangle or not. Examples: Input: bottom-left: (0, 0), top-right: (10, 8), point: (1, 5) Output: Yes Input: bottom-left: (-1, 4), top-right:(2, 3), point:(0, 4) Output: No This problem is
6 min read
Check whether a given point lies inside a rectangle or not Given four points of a rectangle, and one more point P. Write a function to check whether P lies within the given rectangle or not.Examples:Â Â Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)] P = (0, 0)Output : yesIllustration :Input : R = [(10, 10), (10, -10), (-10, -10), (-10, 10)], P = (20
14 min read
Coordinates of rectangle with given points lie inside Given two arrays X[] and Y[] with n-elements, where (Xi, Yi) represent a point on coordinate system, find the smallest rectangle such that all points from given input lie inside that rectangle and sides of rectangle must be parallel to Coordinate axis. Print all four coordinates of an obtained recta
6 min read
Number of points lying inside a rectangle as well as a triangle Given two 2D arrays rectangle[][] and triangle[][], representing the coordinates of vertices of a rectangle and a triangle respectively, and another array points[][] consisting of N coordinates, the task is to count the number of points that lies inside both the rectangle and the triangle. Examples:
15 min read