The code consists of the following files:
-
Rectangle.h
andRectangle.cpp
: These files define theRectangle
class, which represents a zone in the environment. It provides functions to set and get the zone's properties and check if a point is inside the zone or within a certain range of the zone. -
Robot.h
andRobot.cpp
: These files define theRobot
class, which represents the industrial robot. It contains functions to set and get the robot's path and add points to the path. -
Point.h
: This file defines thePoint
structure, which represents a 2D point withx
andy
coordinates. -
main.cpp
: This file contains the main entry point of the program. It includes the necessary header files, defines theinitializeZonesAndPath
function, and implements the main algorithm of the program in themain()
function.
To compile and run the program, follow these steps:
-
Make sure you have a C++ compiler (such as GCC or Clang) installed on your system.
-
Download or clone the code files to your local machine.
-
Open a terminal or command prompt and navigate to the directory containing the code files.
-
Compile the code using the following command:
g++ main.cpp Robot.cpp Rectangle.cpp -o program
This command compiles the source files and creates an executable file named program
.
-
Run the program by executing the generated executable:
./program
The program will read the input file (input.txt
by default), process the robot's path and the zones, and log the occurrences where the robot interacts with the zones.
Note: If you want to use a different input file, update the filename variable in the main()
function of main.cpp
before compiling.
The provided C++ code reads input from a text file, processes the data, and performs operations based on the input. Here is a step-by-step explanation of how the algorithm works:
-
File Initialization:
- Include necessary header files and define the
initializeZonesAndPath
function. - Open the specified file using an
std::ifstream
object namedinputFile
. If the file fails to open, an exception is thrown. - Declare a string variable named
line
to store each line read from the file.
- Include necessary header files and define the
-
Lambda Function:
- Define a lambda function named
readNextNonEmptyLine
to handle the task of reading the next non-empty line from the file. - Use a
while
loop withstd::getline
to read lines until a non-empty line is encountered. - The lambda function returns
true
if a non-empty line is found andfalse
if there are no more lines to read.
- Define a lambda function named
-
Path Extraction:
- Call the
readNextNonEmptyLine
function to check for a non-empty line. - If a non-empty line is found:
- Process the line to extract pairs of
x
andy
coordinates using anstd::istringstream
object namediss
. - Add the extracted coordinates to the
Robot
object's path using theaddPointToPath
method.
- Process the line to extract pairs of
- If no non-empty line is found for the path information, an exception is thrown.
- Call the
-
Zone Extraction:
- Call the
readNextNonEmptyLine
function to check for a non-empty line. - If a non-empty line is found:
- Process the line to extract the zone name,
x
andy
coordinates, width, and height using theiss
object. - Create
Rectangle
objects with the extracted information and add them to thezones
vector.
- Process the line to extract the zone name,
- If there is an issue with the input format (e.g., incorrect syntax or missing values), an exception is thrown.
- Call the
-
File Closing:
- Once all the lines have been processed, close the input file.
-
Main Function:
- Create a
Robot
object namedrobot
, declare a filename variable, and initialize an empty vector ofRectangle
objects namedzones
. - Call the
initializeZonesAndPath
function, passing the filename,zones
vector, androbot
object as arguments. - This function reads the input file and populates the
robot
object with path information and thezones
vector with zone information.
- Create a
-
Flag Initialization:
- Create a vector of characters named
flags
with the same size as thezones
vector. - This vector will be used to track the status of each zone.
- Create a vector of characters named
-
Zone Tracking:
How it works: If the point is not within a 1-meter range, keep the flag as false. Once it enters the 1-meter range, check the flag. If the flag is false, it means the point was not previously in the range, so print "entering". If the flag is true, it means the point was already inside the zone, so print "leaving". If the point is inside the zone, print "inside" and set/keep the flag true in order to track its state when the point is outside the zone but within the 1-meter range.
- Initialize an integer variable named
count
to zero. It will keep track of the index of the current zone while iterating over thezones
vector. - Enter a nested loop:
- The outer loop iterates over each point in the
robot
object's path. - The inner loop iterates over each
Rectangle
object in thezones
vector. - For each point and zone pair:
- Check if the point is within the range of the zone by calling the
checkIfPointWithinRange
method of theRectangle
object. - If the point is within the zone's range:
- Check if the point is inside the zone by calling the
checkIfPointInZone
method. - If the point is inside the zone:
- Print a message indicating that the point is inside the current zone's name.
- Set the corresponding flag in the
flags
vector to true if it is not already set.
- If the point is outside the zone but within its range:
- Check the corresponding flag in the
flags
vector. - If the flag is set, print a message indicating that the point is exiting the current zone.
- If the flag is not set, print a message indicating that the point is about to enter the current zone.
- Check the corresponding flag in the
- Check if the point is inside the zone by calling the
- If the point is outside the zone's range, reset the corresponding flag in the
flags
vector to false. - Increment the
count
variable to move to the next zone.
- Check if the point is within the range of the zone by calling the
- After processing all points for a given zone, reset the
count
variable to zero and print a newline character.
- The outer loop iterates over each point in the
- Initialize an integer variable named
-
Exception Handling:
- Use a
try-catch
block to catch any exceptions thrown during the execution of the program. - If an exception occurs, print an error message indicating the exception's details.
- Use a
This algorithm utilizes a lambda function, readNextNonEmptyLine
, to handle the repetitive task of reading the next non-empty line from the input file. In SecondVersion branch, I added the use of smart pointers.
-
Lambda Functions
-
Range-based for loop
-
Auto type inference
I used the auto keyword to automatically deduce the type of the variable in
zones
vector:std::unique_ptr<Rectangle>
. -
Initializer Lists
Initialize objects using braced initializer lists ({}) for uniform and concise initialization.
-
Using std::optional
Instead of using flags to represent the state of zones, I used
std::optional
to provide a more expressive and type-safe representation. It allows me to express the possibility of having a value (true
orfalse
) or having no value (std::nullopt
). -
Smart Pointers
In this modified code, the zones vector is now a vector of
std::unique_ptr<Rectangle>
. TheinitializeZonesAndPath
function usesstd::make_unique
to dynamically allocate Rectangle objects and stores them as unique pointers in the zones vector. The deallocation of memory for the Rectangle objects is automatically handled by thestd::unique_ptr
when the vector goes out of scope.In the code, I made the following changes:
-
Changed the
zones
vector tostd::vector<std::unique_ptr<Rectangle>> zones
to store Rectangle objects asstd::unique_ptr
. It ensures that the dynamically allocated objects are properly deallocated when they are no longer needed. By usingstd::unique_ptr
, I avoid the need to manually call delete on the objects. -
Modified the
initializeZonesAndPath
function to usestd::make_unique
: Instead of directly creating Rectangle objects and adding them to the zones vector, I usedstd::make_unique
to store them asstd::unique_ptr<Rectangle>
. -
Changed the iteration over zones to use
const auto& z
: When iterating over the zones vector, I usedconst auto& z
instead of copying thestd::unique_ptr<Rectangle>
objects. This avoids unnecessary copying of the smart pointers and improves performance. -
In the previous code,
z.checkIfPointWithinRange(p)
was used to call the member functioncheckIfPointWithinRange
on the Rectangle object z. However, in the modified code that uses smart pointers, we store Rectangle objects asstd::unique_ptr<Rectangle>
in the zones vector. To access the member functions of the Rectangle objects through the smart pointer, we use the arrow operator(->)
. The arrow operator is used to dereference the smart pointer and access the member functions. It is equivalent to (*z).checkIfPointWithinRange(p), where the * operator is used to dereference the smart pointer and access the underlying object.
-