Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given two arrays denoting x- and y-coordinates of a set of points in a p

ID: 3880355 • Letter: Y

Question

You are given two arrays denoting x- and y-coordinates of a set of points in a plane. For plotting the point set, we need to know the x- and y-coordinates of the smallest rectangle containing the points. How can you obtain these values? The code so far...

public class SmallestRectangle {

/**

Calculates and prints the x and y coordinates of the smallest rectangle covering points in datapoints

*/

public void coordinates_of_rectangle(double[][] datapoints) {

double[] UpperRight = new double[2]; // UpperRight[0] = x coordinate, UpperRight[1] = y coordinate

double[] UpperLeft = new double[2]; // UpperLeft[0] = x coordinate, UpperLeft[1] = y coordinate

double[] LowerRight = new double[2]; // LowerRight[0] = x coordinate, LowerRight[1] = y coordinate

double[] LowerLeft = new double[2]; // LowerLeft[0] = x coordinate, LowerLeft[1] = y coordinate

// Complete this method

// task: fill out the four arrays UpperRight, UpperLeft, LowerRight, LowerLeft

// your work starts here

// your work ends here

System.out.println("UpperRight: (" + UpperRight[0] + "," + UpperRight[1]+")");

System.out.println("UpperLeft: (" + UpperLeft[0] + "," + UpperLeft[1]+")");

System.out.println("LowerRight: (" + LowerRight[0] + "," + LowerRight[1]+")");

System.out.println("LowerLeft: (" + LowerLeft[0] + "," + LowerLeft[1]+")");

System.out.println(" Expeted Output");

System.out.println("UpperRight: (3,7)");

System.out.println("UpperLeft: (-5,7)");

System.out.println("LowerRight: (3,1)");

System.out.println("LowerLeft: (-5,1)");

}

public static void main(String[] args) {

// you do not need to alter this method

// Points[i] = i-th point

// Points[i][0] = x coordinate of i-th point

// Points[i][1] = y coordinate of i-th point

double[][] Points = {{0,1},{-1,3},{3,5},{2,7},{-4,1},{-5,6}};

SmallestRectangle smallrec = new SmallestRectangle();

smallrec.coordinates_of_rectangle(Points);

}

}

Explanation / Answer

Here is the Implementation.

public class SmallestRectangle {

/**
Calculates and prints the x and y coordinates of the smallest rectangle covering points in data points
**/

public void coordinates_of_rectangle(double[][] datapoints) {

double[] UpperRight = new double[2]; // UpperRight[0] = x coordinate, UpperRight[1] = y coordinate
double[] UpperLeft = new double[2]; // UpperLeft[0] = x coordinate, UpperLeft[1] = y coordinate
double[] LowerRight = new double[2]; // LowerRight[0] = x coordinate, LowerRight[1] = y coordinate
double[] LowerLeft = new double[2]; // LowerLeft[0] = x coordinate, LowerLeft[1] = y coordinate

// Complete this method
/**
* Solution Start
*/

/*
min_x and min_y will store the minimum x coordinate and minimum y coordinate of rectangle. (Lower Left Corner)
min_x and min_y will store the maximum x coordinate and maximum y coordinate of rectangle. (Upper Right Corner)
*/
double min_x, max_x, min_y, max_y;
int len = datapoints.length; // It will store the no of points present in the array.
/*
If No point is present then there will not be any rectangle.
*/
if (len == 0){
System.out.println("No points present");
return;
}

/*
Assign the first point as minimum and maximum points
*/
max_x = min_x = datapoints[0][0];
min_y = max_y = datapoints[0][1];

for(int i = 1; i < len; i++){
/*
If the x co-ordinate of ith point is lower than current min_x, then update min_x with the ith x-coordinate.
Else if the x co-ordinate of ith point is grater than current max_x, then update max_x with ith current x-coordinate.
*/
if(datapoints[i][0] < min_x){
min_x = datapoints[i][0];
} else if(datapoints[i][0] > max_x){
max_x = datapoints[i][0];
}

/*
If the y co-ordinate of ith point is lower than current min_y, then update min_x with the ith y-coordinate.
Else if the y co-ordinate of ith point is grater than current max_y, then update max_x with ith current y-coordinate.
*/
if(datapoints[i][1] < min_y){
min_y = datapoints[i][1];
} else if(datapoints[i][1] > max_y){
max_y = datapoints[i][1];
}

}

/*
Points will be
(max_x,min_y) (max_x,max_y)
(min_x,min_y) (max_x,min_y)
Assign the value to the points.
*/

LowerLeft[0] = UpperLeft[0] = min_x;
LowerRight[0] = UpperRight[0] = max_x;
LowerLeft[1] = LowerRight[1] = min_y;
UpperLeft[1] = UpperRight[1] = max_y;

/*
* Solution end
*/

// task: fill out the four arrays UpperRight, UpperLeft, LowerRight, LowerLeft

// your work starts here

// your work ends here

System.out.println("UpperRight: (" + UpperRight[0] + "," + UpperRight[1]+")");

System.out.println("UpperLeft: (" + UpperLeft[0] + "," + UpperLeft[1]+")");

System.out.println("LowerRight: (" + LowerRight[0] + "," + LowerRight[1]+")");

System.out.println("LowerLeft: (" + LowerLeft[0] + "," + LowerLeft[1]+")");

System.out.println(" Expeted Output");

System.out.println("UpperRight: (3,7)");

System.out.println("UpperLeft: (-5,7)");

System.out.println("LowerRight: (3,1)");

System.out.println("LowerLeft: (-5,1)");

}

public static void main(String[] args) {

// you do not need to alter this method

// Points[i] = i-th point

// Points[i][0] = x coordinate of i-th point

// Points[i][1] = y coordinate of i-th point

double[][] Points = {{0,1},{-1,3},{3,5},{2,7},{-4,1},{-5,6}};


SmallestRectangle smallrec = new SmallestRectangle();

smallrec.coordinates_of_rectangle(Points);

}

}

Here is the steps to find the points.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote