
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Line Reflection in C++
Suppose we have n points on a 2D plane, we have to check whether there is any line parallel to y-axis that reflect the given points symmetrically, in other words, check whether there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.
So, if the input is like points = [[1,1],[-1,1]]
then the output will be true
To solve this, we will follow these steps −
Define one set ok
n := size of points
minVal := inf
maxVal := -inf
for initialize i := 0, when i < n, update (increase i by 1), do −
minVal := minimum of minVal and points[i, 0]
maxVal := maximum of maxVal and points[i, 0]
insert points[i] into ok
mid := maxVal + minVal
for initialize i := 0, when i < n, update (increase i by 1), do −
x := points[i, 0]
y := points[i, 1]
x := mid - x
if { x, y } is not in ok, then −
return false
return true
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: bool isReflected(vector<vector<int<>& points) { set<vector<int< > ok; int n = points.size(); int minVal = INT_MAX; int maxVal = INT_MIN; for (int i = 0; i < n; i++) { minVal = min(minVal, points[i][0]); maxVal = max(maxVal, points[i][0]); ok.insert(points[i]); } int mid = maxVal + minVal; for (int i = 0; i < n; i++) { int x = points[i][0]; int y = points[i][1]; x = mid - x; if (!ok.count({ x, y })) return false; } return true; } }; main(){ Solution ob; vector<vector<int<> v = {{1,1},{-1,1}}; cout << (ob.isReflected(v)); }
Input
{{1,1},{-1,1}}
Output
1
- Related Articles
- Reflection in C#
- Reflection of Light and Laws of Reflection
- Mirror Reflection in C++
- Sound Reflection
- Why do we draw a normal line between the angle of incidence and angle of reflection?
- Reflection Array Class in Java
- Differentiate between regular and diffused reflection. Does diffused reflection mean the failure of the laws of reflection?
- What is Reflection?
- Reflection of Waves
- Specular Diffuse Reflection
- Reflection of Light
- Reflection Lateral Inversion
- The Reflection Attack
- Total Internal Reflection
- How to use Reflection in Golang?
