Check if four segments form a rectangle
Last Updated :
23 Jul, 2025
We are given four segments as a pair of coordinates of their end points. We need to tell whether those four line segments make a rectangle or not.
Examples:
Input : segments[] = [(4, 2), (7, 5),
(2, 4), (4, 2),
(2, 4), (5, 7),
(5, 7), (7, 5)]
Output : Yes
Given these segment make a rectangle of length 3X2.
Input : segment[] = [(7, 0), (10, 0),
(7, 0), (7, 3),
(7, 3), (10, 2),
(10, 2), (10, 0)]
Output : Not
These segments do not make a rectangle.
Above examples are shown in below diagram.
This problem is mainly an extension of How to check if given four points form a square
We can solve this problem by using properties of a rectangle. First, we check total unique end points of segments, if count of these points is not equal to 4 then the line segment can’t make a rectangle. Then we check distances between all pair of points, there should be at most 3 different distances, one for diagonal and two for sides and at the end we will check the relation among these three distances, for line segments to make a rectangle these distance should satisfy Pythagorean relation because sides and diagonal of rectangle makes a right angle triangle. If they satisfy mentioned conditions then we will flag polygon made by line segment as rectangle otherwise not.
CPP
// C++ program to check whether it is possible
// to make a rectangle from 4 segments
#include <bits/stdc++.h>
using namespace std;
#define N 4
// structure to represent a segment
struct Segment
{
int ax, ay;
int bx, by;
};
// Utility method to return square of distance
// between two points
int getDis(pair<int, int> a, pair<int, int> b)
{
return (a.first - b.first)*(a.first - b.first) +
(a.second - b.second)*(a.second - b.second);
}
// method returns true if line Segments make
// a rectangle
bool isPossibleRectangle(Segment segments[])
{
set< pair<int, int> > st;
// putting all end points in a set to
// count total unique points
for (int i = 0; i < N; i++)
{
st.insert(make_pair(segments[i].ax, segments[i].ay));
st.insert(make_pair(segments[i].bx, segments[i].by));
}
// If total unique points are not 4, then
// they can't make a rectangle
if (st.size() != 4)
return false;
// dist will store unique 'square of distances'
set<int> dist;
// calculating distance between all pair of
// end points of line segments
for (auto it1=st.begin(); it1!=st.end(); it1++)
for (auto it2=st.begin(); it2!=st.end(); it2++)
if (*it1 != *it2)
dist.insert(getDis(*it1, *it2));
// if total unique distance are more than 3,
// then line segment can't make a rectangle
if (dist.size() > 3)
return false;
// copying distance into array. Note that set maintains
// sorted order.
int distance[3];
int i = 0;
for (auto it = dist.begin(); it != dist.end(); it++)
distance[i++] = *it;
// If line seqments form a square
if (dist.size() == 2)
return (2*distance[0] == distance[1]);
// distance of sides should satisfy pythagorean
// theorem
return (distance[0] + distance[1] == distance[2]);
}
// Driver code to test above methods
int main()
{
Segment segments[] =
{
{4, 2, 7, 5},
{2, 4, 4, 2},
{2, 4, 5, 7},
{5, 7, 7, 5}
};
(isPossibleRectangle(segments))?cout << "Yes\n":cout << "No\n";
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
// java program to check whether it is possible
// to make a rectangle from 4 segments
public class Main {
static int N = 4;
// Utility method to return square of distance
// between two points
public static int getDis(String a, String b)
{
String[] a1 = a.split(",");
String[] b1 = b.split(",");
return (Integer.parseInt(a1[0]) - Integer.parseInt(b1[0]))*(Integer.parseInt(a1[0]) - Integer.parseInt(b1[0])) + (Integer.parseInt(a1[1]) - Integer.parseInt(b1[1]))*(Integer.parseInt(a1[1]) - Integer.parseInt(b1[1]));
}
// method returns true if line Segments make
// a rectangle
public static boolean isPossibleRectangle(int[][] segments)
{
HashSet<String> st = new HashSet<>();
// putting all end points in a set to
// count total unique points
for (int i = 0; i < N; i++)
{
st.add(segments[i][0] + "," + segments[i][1]);
st.add(segments[i][2] + "," + segments[i][3]);
}
// If total unique points are not 4, then
// they can't make a rectangle
if (st.size() != 4)
return false;
// dist will store unique 'square of distances'
HashSet<Integer> dist = new HashSet<>();
// calculating distance between all pair of
// end points of line segments
for(String it1 : st){
for(String it2 : st){
if(it1 != it2){
dist.add(getDis(it1, it2));
}
}
}
// if total unique distance are more than 3,
// then line segment can't make a rectangle
if (dist.size() > 3)
return false;
// copying distance into array. Note that set maintains
// sorted order.
int[] distance = new int[3];
int i = 0;
for(int it: dist){
distance[i] = it;
i = i + 1;
}
// If line seqments form a square
if (dist.size() == 2)
return (2*distance[0] == distance[1]);
// distance of sides should satisfy pythagorean
// theorem
return (distance[0] + distance[1] == distance[2]);
}
public static void main(String[] args) {
int[][] segments =
{
{4, 2, 7, 5},
{2, 4, 4, 2},
{2, 4, 5, 7},
{5, 7, 7, 5}
};
if(isPossibleRectangle(segments) == true){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
// The code is contributed by Nidhi goel.
Python3
# Python program to check whether it is possible
# to make a rectangle from 4 segments
N = 4
# Utility method to return square of distance
# between two points
def getDis(a, b):
return (a[0] - b[0])*(a[0] - b[0]) + (a[1] - b[1])*(a[1] - b[1])
# method returns true if line Segments make
# a rectangle
def isPossibleRectangle(segments):
st = set()
# putting all end points in a set to
# count total unique points
for i in range(N):
st.add((segments[i][0], segments[i][1]))
st.add((segments[i][2], segments[i][3]))
# If total unique points are not 4, then
# they can't make a rectangle
if len(st) != 4:
return False
# dist will store unique 'square of distances'
dist = set()
# calculating distance between all pair of
# end points of line segments
for it1 in st:
for it2 in st:
if it1 != it2:
dist.add(getDis(it1, it2))
# if total unique distance are more than 3,
# then line segment can't make a rectangle
if len(dist) > 3:
return False
# copying distance into array. Note that set maintains
# sorted order.
distance = []
for x in dist:
distance.append(x)
# Sort the distance list, as set in python, does not sort the elements by default.
distance.sort()
# If line seqments form a square
if len(dist) ==2 :
return (2*distance[0] == distance[1])
# distance of sides should satisfy pythagorean
# theorem
return (distance[0] + distance[1] == distance[2])
# Driver code to test above methods
segments = [
[4, 2, 7, 5],
[2, 4, 4, 2],
[2, 4, 5, 7],
[5, 7, 7, 5]
]
if(isPossibleRectangle(segments) == True):
print("Yes")
else:
print("No")
# The code is contributed by Nidhi goel.
C#
// C# program to check whether it is possible
// to make a rectangle from 4 segments
using System;
using System.Collections.Generic;
class GFG {
public static int N = 4;
// Utility method to return square of distance
// between two points
public static int getDis(KeyValuePair<int,int> a, KeyValuePair<int,int> b)
{
return (a.Key - b.Key)*(a.Key - b.Key) +
(a.Value - b.Value)*(a.Value - b.Value);
}
// method returns true if line Segments make
// a rectangle
public static bool isPossibleRectangle(int[,] segments)
{
HashSet<KeyValuePair<int, int>> st = new HashSet<KeyValuePair<int, int>>();
// putting all end points in a set to
// count total unique points
for (int j = 0; j < N; j++)
{
st.Add(new KeyValuePair<int, int>(segments[j,0], segments[j,1]));
st.Add(new KeyValuePair<int, int>(segments[j,2], segments[j,3]));
}
// If total unique points are not 4, then
// they can't make a rectangle
if (st.Count != 4)
return false;
// dist will store unique 'square of distances'
HashSet<int> dist = new HashSet<int>();
// calculating distance between all pair of
// end points of line segments
foreach(var it1 in st){
foreach(var it2 in st){
if(it1.Key != it2.Key && it1.Value != it2.Value){
dist.Add(getDis(it1, it2));
}
}
}
// if total unique distance are more than 3,
// then line segment can't make a rectangle
if (dist.Count > 3)
return false;
// copying distance into array. Note that set maintains
// sorted order.
int[] distance = new int[3];
int i = 0;
foreach(var it in dist){
distance[i] = it;
i = i + 1;
}
// If line seqments form a square
if (dist.Count == 2)
return (2*distance[0] == distance[1]);
// distance of sides should satisfy pythagorean
// theorem
return (distance[0] + distance[1] == distance[2]);
}
// Driver code
public static void Main()
{
int[,] segments = {
{4, 2, 7, 5},
{2, 4, 4, 2},
{2, 4, 5, 7},
{5, 7, 7, 5}
};
if(isPossibleRectangle(segments) == true){
Console.WriteLine("Yes");
}
else{
Console.WriteLine("No");
}
}
}
// The code is contributed by Nidhi goel.
JavaScript
// JavaScript program to check whether it is possible
// to make a rectangle from 4 segments
const N = 4;
// Utility method to return square of distance
// between two points
function getDis(a, b)
{
return (parseInt(a[0]) - parseInt(b[0]))*(parseInt(a[0]) - parseInt(b[0])) + (parseInt(a[1]) - parseInt(b[1]))*(parseInt(a[1]) - parseInt(b[1]));
}
// method returns true if line Segments make
// a rectangle
function isPossibleRectangle(segments)
{
let st = new Set();
// putting all end points in a set to
// count total unique points
for (let i = 0; i < N; i++)
{
let tmp1 = [segments[i][0], segments[i][1]];
let tmp2 = [segments[i][2], segments[i][3]];
st.add(tmp1.join(''));
st.add(tmp2.join(''));
}
// If total unique points are not 4, then
// they can't make a rectangle
if (st.size != 4)
{
return false;
}
// dist will store unique 'square of distances'
let dist = new Set();
// calculating distance between all pair of
// end points of line segments
for(let it1 of st)
{
for(let it2 of st)
{
if(it1 !== it2)
{
dist.add(getDis(it1.split(''), it2.split('')));
}
}
}
// if total unique distance are more than 3,
// then line segment can't make a rectangle
if (dist.size > 3)
{
return false;
}
// copying distance into array. Note that set maintains
// sorted order.
let distance = new Array();
for (let x of dist)
{
distance.push(x);
}
// If line seqments form a square
if (dist.size === 2)
{
return (2*distance[0] == distance[1]);
}
// distance of sides should satisfy pythagorean
// theorem
return (distance[0] + distance[1] == distance[2]);
}
// Driver code to test above methods
{
let segments = [
[4, 2, 7, 5],
[2, 4, 4, 2],
[2, 4, 5, 7],
[5, 7, 7, 5] ]
if(isPossibleRectangle(segments)){
console.log("Yes");
}
else{
console.log("No");
}
}
// The code is contributed by Nidhi Goel
Output:
Yes
Time Complexity: O(n2logn)
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem