Answered step by step
Verified Expert Solution
Question
1 Approved Answer
An axis - aligned rectangle can be represented by the position ( x , y of its upper left corner and its dimensions ( width
An axisaligned rectangle can be represented by the position of its upper left corner and its dimensions width and height Figure illustrates an axisaligned rectangle in a Windows coordinate system where the axis pointing to the right East and the yaxis pointing down South
rigure : axisdigned reclange
According to the location of a point from an axisaligned rectangle, the distance can be calculated as follows:
a Point inside the rectangle: If the point lies within the boundaries of the rectangle, the distance is simply
b Point outside the rectangle: This requires considering the four edges of the rectangle and calculating the minimum distance between the point and each edge. The final distance is the minimum of these four distances.
Assume you are provided with a text file datatxt containing a number of text lines. The first line contains a comma separated list of decimals representing respectively the position and the dimension of a given rectangle. The subsequent lines contain records for some points. Each point has two values and Figure shows a sample of data.txt file.
Figure : Sample of data.txt file
To Do:
Write a Java program that given an input file containing a given axisaligned rectangle and some points, calculates the distances between the given rectangle and each of those points. The program should display a table which includes information about the given rectangle and the distances of the points that are outside the rectangle. For each point outside the rectangle indicate the edge North East, West, South of the rectangle that has been used to calculate the distance. You program should display the number of points inside the rectangle. See the sample runs.
Additional requirements:
You can assume that there are no errors in the input file.
If the minimum distance between a point and two or more edges of a rectangle is equal, then select according to your choice one of these edges and consider it as the edge used to calculate the distance between the point and the rectangle.
A rectangle and a point objects are constructed only once. Then just update the location for each point using PointD API methods.
Use the PointD LineD and RectangleD API javaawt methods whenever is applicable.
Hints:
Browse the API Documentation to get familiar with the public interface of the following classes:
PointD
LineD
RectangleD
String.
Notice that in the java.awt.geom package, the primary class that models a point is PointD The PointD Double class represents the point's coordinates with double precision. This apply to both segment LineDLineD Double and rectangle RectangleD RectangleDDouble
No need to know how to calculate the distance between a point and an arbitrary segment There is a method in the class LineD that calculates it Look at it in the API documentation.
Sample runs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started