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
D polygon has points with coordinates x y a D polygon has points with coordinates x y
z and an ndimensional polygon has points with n coordinates x x x xn
Below is a triangle, which we can consider dimensional polygon consisting of points.
A graph of a line with points Description automatically generated
For this D polygon, we have points, A B C
The bounding box for this polygon can be represented as
The total length of the lines connecting the points, which we will refer to as the length of the
polygon, is ~ units.
Make the following changes to the file Assojava and Polygon.java which are given below:
a 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 Marks Write the code for the calculateDistance method. This method will accept two
points as input, with each point being represented by a dimensional array of doubles. You
can calculate the distance between two points using Euclidean Distance.
c 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 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 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 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 D as well as points that are D Return true if the
array passes the check.
g 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.
Assojava :
import java.util.Arrays;
This is a driver class for Assignment
It will instantiate and run some operations on Polygon objects
public class Ass
public static void mainString args
A dimensional square
System.out.printlnSquare:;
double sqPoints ;
Polygon square new PolygonsqPoints;
System.out.printlntPoints: square;
System.out.printlntLength: square.calculateLength;
System.out.printlntBounding Box: Arrays.deepToStringsquarecalculateBoundingBox;
A dimensional triangle
System.out.printlnTriangle:;
double trPoints ;
Polygon triangle new PolygontrPoints;
System.out.printlntPoints: triangle;
System.out.printlntLength: triangle.calculateLength;
System.out.printlntBounding Box: Arrays.deepToStringtrianglecalculateBoundingBox;
Combine the square and triangle into D poly
System.out.printlnSquare append Triangle:";
square.appendtriangle;
System.out.printlntPoints: square;
System.out.printlntLength: square.calculateLength;
System.out.printlntBounding Box: Arrays.deepToStringsquarecalculateBoundingBox;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
