Question: Represent polygons using ordered arrays of points. Each point is itself an array of doubles, where the doubles represent the coordinates of the point. A

Represent polygons using ordered arrays of points. Each point is itself an array of
doubles, where the doubles represent the coordinates of the point. A point may have varying
numbers of coordinates, depending on how many dimensions the polygon is in. For example, a
2D polygon has points with 2 coordinates (x, y), a 3D polygon has points with 3 coordinates (x, y,
z) and an n-dimensional polygon has points with n coordinates (x0, x1, x2,..., xn).
Below is a triangle, which we can consider 2-dimensional polygon consisting of 3 points.
A graph of a line with points Description automatically generated
For this 2D polygon, we have 3 points, {A, B, C}={{2,1},{7,1},{7,4}}.
The bounding box for this polygon can be represented as {{2,1},{7,4}}.
The total length of the lines connecting the points, which we will refer to as the length of the
polygon, is ~13.8 units.
Make the following changes to the file Asso1.java and Polygon.java which are given below:
a.(1 Mark) Complete the constructor and setter for the Polygon class. These methods should
both accept an array of points and save it to the corresponding field on the Polygon class.
b.(3 Marks) Write the code for the calculateDistance() method. This method will accept two
points as input, with each point being represented by a 1-dimensional array of doubles. You
can calculate the distance between two points using Euclidean Distance.
c.(3 Marks) Complete the calculateLength() method. This method will determine the total
length of the polygon if we were to connect each point to the next point in order. Note that
our polygon is considered to be closed. This means that the last point should connect back
to the first point.
d.(3 Marks) Write the calculateBoundingBox() method. This method should return an array of
points that can be used to represent the maximum extents of the polygon. A bounding box
for a shape can be determined using only two points. The first point will represent the
minimum coordinates for the polygon in each dimension, while the second point represents
the maximums.
e.(3 Marks) Write the code for the append() method. The append method of a Polygon should
accept another Polygon as input. The method will then combine the Polygons together by
adding all of the points from the input polygon to the array of the second polygon.
f.(3 Marks) Complete the checkDimensions() method. This method will accept an array of
points as input. The method will then check that there is at least one point in the array, and
that all of the points in the array exist in the same number of dimensions. (Eg: one array of
points should not have points that are 2D as well as points that are 3D) Return true if the
array passes the check.
g.(2 Marks) Finally, we are going to add some validation to our class. If a validation ever fails,
an IllegalArgumentException should be thrown. Here are our validation rules:
a. Anywhere that the points field on the Polygon class may be changed, we must check
that all of the points in the array have the same number of dimensions.
b. If we are to append two polygons together, we must ensure the two polygons are in
the same number of dimensions.
c. To calculate the distance between two points, the points must have the same
number of dimensions.
Asso1.java :
import java.util.Arrays;
/**
* This is a driver class for Assignment 01
* It will instantiate and run some operations on Polygon objects
*/
public class Ass01
{
public static void main(String[] args){
// A 2 dimensional square
System.out.println("Square:");
double[][] sqPoints ={{0.0,1.0},{1.0,1.0},{1.0,0.0},{0.0,0.0}};
Polygon square = new Polygon(sqPoints);
System.out.println("\tPoints: "+ square);
System.out.println("\tLength: "+ square.calculateLength());
System.out.println("\tBounding Box: "+ Arrays.deepToString(square.calculateBoundingBox()));
// A 2 dimensional triangle
System.out.println("Triangle:");
double[][] trPoints ={{-1.0,0.0},{-2.0,0.0},{-1.0,1.0}};
Polygon triangle = new Polygon(trPoints);
System.out.println("\tPoints: "+ triangle);
System.out.println("\tLength: "+ triangle.calculateLength());
System.out.println("\tBounding Box: "+ Arrays.deepToString(triangle.calculateBoundingBox()));
// Combine the square and triangle into 12D poly
System.out.println("Square append Triangle:");
square.append(triangle);
System.out.println("\tPoints: "+ square);
System.out.println("\tLength: "+ square.calculateLength());
System.out.println("\tBounding Box: "+ Arrays.deepToString(square.calculateBoundingBox()));

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!