Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP WITH CODE ASAP: For this task, we are going to have nested triangles. The robot wants to drill a hole on the vertices

NEED HELP WITH CODE ASAP:

For this task, we are going to have nested triangles. The robot wants to drill a hole on the vertices of triangles and draw a line for the horizontal side of the triangles.

This method starts with three points, which are the vertices of the largest (the most outer) triangle.

A nested triangle is a triangle, whose vertices fall on the midpoints of the sides of the outer triangle.

The method gets an integer called order, that specifies how deeply the recursive method should be called. If order = 1, only one horizontal line and three points are drawn, and three points are returned as the coordinates that should be drilled by the robot. Please see the pictures below where nested triangles with different orders are created by the robot.

To make it more visible, the points that should be drilled and returned from the method are drawn in black. You dont need to change the color as we only check the return value of the method.

Since this method is recursive, it is possible that a drilled point (i.e. a triangle vertex) is computed more than once. The ArrayList that holds the points should not contain any duplicate points.

Also, for the ease of testing, we ask you to add point.toString()to the ArrayList. This method can be found in Point class.

Sample Call 1:

Point p1 = new Point(0.1, 0.1);

Point p2 = new Point(0.9, 0.1);

Point p3 = new Point(0.5, 0.9);

ArrayList points = new ArrayList();

points = PE1.drillPoints(p1, p2, p3, 1, blankCanvas, points);

Corresponding Output:

[0.1, 0.1][0.9, 0.1][0.5, 0.9]

CODE:

public class PE1 {

public static void main(String[] args) {

/* A canvas should be created for drawing.

* All the shapes should be drawn on the canvas.

* The bottom left coordinate of the canvas is (0,0)

* The top right coordinates of the canvas is (1, 1)

* The input parameter to Draw() constructor is the

* title of the canvas.

*/

Draw blankCanvas = new Draw("Lab4");

/* To draw a point, point function is called with proper parameters:

* point(x_coordinate_of_point, y_coordinate_of_point)

*/

blankCanvas.point(0.7, 0.7);

/* To draw a circle, circle function is called with proper parameters:

* circle(x_coordinate_of_center, y_coordinate_of_center, radius)

*/

blankCanvas.circle(0.5, 0.5, 0.5);

/* To draw a square, square function is called with proper parameters:

* square(x_coordinate_of_center, y_coordinate_center, sides_half_length)

*/

blankCanvas.square(0.5, 0.5, 0.4);

/*

* To change the color of the pen, setPenColor is used with three numbers that are in [0, 255] range.

* All the colors can be made as a combination of red, green and blue.

* The parameters show the richness of red, green and blue of a color.

* For example setPenColor(new Color(0, 0, 0) sets the color of the pen

* to black and setPenColor(new Color(255, 255, 255) sets the color to

* white.

*/

blankCanvas.setPenColor(new Color(150, 150, 150));

/* To draw a line, line function is called

* with proper parameters:

* line(x_coordinate_of_center, y_coordinate_center, sides_half_length)

*/

blankCanvas.line(0.0, 0.5, 1, 0.5);

}

/**

* This method specifies which coordinates should be drilled. It also draw the

* horizontal line of each triangle. No duplicate point should be added to the output.

* @param p1 is one of the vertex of the triangle

* @param p2 is the second vertex of the triangle

* @param p3 is the third vertex of the triangle

* @param order is the number of times a nested triangle should be drawn.

* order >= 0 , however if it is zero, nothing should be drawn

* @param page is the canvas on which this method draws.

* @param array is the list of the points that should be drilled. To add to this list point.toString() must be added.

* @return an array that contains all the points that should be drilled. this method should not have any duplicate points in it.

*/

public static ArrayList drillPoints(Point p1, Point p2, Point p3, int order, Draw page, ArrayList array) {

// your code goes here. Task 3

return new ArrayList();

}

}

/**

* This class creates a point.

*

*/

class Point {

double x;

double y;

/**

* This is the constructor that builds a point

* @param x is the x-coordinate of the point

* @param y is the y-coordinate of the point

*/

public Point(double x, double y) {

// your code goes here. Task 0

this.x = x; //initialize x

this.y = y; //initialize y

}

/**

* This method returns the mid point of a line,

* whose two ends are given.

* @param p1 is one end of the line

* @param p2 is the other end of the line

* @return the mid point of the line. Both the

* coordinates are cut to two decimal points.

* e.g. 0.37654 is cut to 0.37

*/

public static Point midpoint(Point p1, Point p2) {

// your code goes here. Task 0

double scale = Math.pow(10, 2);

double newX = Math.round((p1.x + p2.x) / 2 * scale) / scale; // round to 2 decimal places

double newY = Math.round((p1.y + p2.y) / 2 * scale) / scale; // round to 2 decimal places

return new Point(newX, newY);

}

@Override

/**

* This method returns the coordinate of this object as a string.

*/

public String toString() {

return "["+this.x + ", "+ this.y +"]";

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Should proportionality become a new ground judicial review?

Answered: 1 week ago

Question

14-18 Compare the two major types of planning and control tools.

Answered: 1 week ago