## Transcribed Text

Observe the following rules:
1. DO NOT use System.exit().
2. DO NOT add the project or package statements.
3. DO NOT change the class name.
4. DO NOT change the headers of ANY of the given methods.
5. DO NOT add any new class fields.
6. ONLY display the result as specified by the example for each problem.
7. DO NOT print other messages, follow the examples for each problem.
8. USE StdIn, StdOut, and StdDraw libraries.
1. Polygon transform. Write a library of static methods that performs
various geometric transforms on polygons. Mathematically, a polygon is defined by its
sequence of vertices (x0, y 0), (x 1, y 1), (x 2, y 2), …. In Java, we will represent a polygon by
storing the x– and y-coordinates of the vertices in two parallel arrays x[] and y[].
Three useful geometric transforms are scale, translate and rotate.
o Scale the coordinates of each vertex (x i, y i) by a factor α.
o x‘
i = α xi
o y‘
i = α yi
o Translate each vertex (x i
, y i) by a given offset (dx, dy).
o x‘
i = xi + dx
o y‘
i = yi + dy
o Rotate each vertex (x i, y i) by θ degrees counterclockwise, around the origin.
o x‘
i = xi cos θ – yi sin θ
o y‘
i = yi cos θ + xi sin θ
Write a two-dimensional transformation library by implementing the following API:
public class PolygonTransform {
// Returns a new array object that is an exact copy of the given array.
// The given array is not mutated.
public static double[] copy(double[] array)
// Scales the polygon by the factor alpha.
public static void scale(double[] x, double[] y, double alpha)
// Translates the polygon by (dx, dy).
public static void translate(double[] x, double[] y, double dx, double
dy)
// Rotates the polygon theta degrees counterclockwise, about the
origin.
public static void rotate(double[] x, double[] y, double theta)
// Tests each of the API methods by directly calling them.
public static void main(String[] args)
}
2. Sierpinski (30 points). The Sierpinski triangle is an example of a fractal pattern like the
H-tree pattern from Section 2.3 of the textbook.
The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has
appeared in Italian art since the 13th century. Though the Sierpinski triangle looks complex,
it can be generated with a short recursive function. Your main task is to write a recursive
function sierpinski() that plots a Sierpinski triangle of order n to standard drawing. Think
recursively: sierpinski() should draw one filled equilateral triangle (pointed downwards) and
then call itself recursively three times (with an appropriate stopping condition). It should
draw 1 filled triangle for n = 1; 4 filled triangles for n = 2; and 13 filled triangles for n = 3;
and so forth.
Restrictions: You may not change either the scale or size of the drawing window.
API specification. When writing your program, exercise modular design by organizing it into
four functions, as specified in the following API:
public class Sierpinski {
// Height of an equilateral triangle whose sides are of the specified
length.
public static double height(double length)
// Draws a filled equilateral triangle whose bottom vertex is (x, y)
// of the specified side length.
public static void filledTriangle(double x, double y, double length)
// Draws a Sierpinski triangle of order n, such that the largest filled
// triangle has bottom vertex (x, y) and sides of the specified length.
public static void sierpinski(int n, double x, double y, double length)
// Takes an integer command-line argument n;
// draws the outline of an equilateral triangle (pointed upwards) of
length 1;
// whose bottom-left vertex is (0, 0) and bottom-right vertex is (1,
0); and
// draws a Sierpinski triangle of order n that fits snugly inside the
outline.
public static void main(String[] args)
}
Textbook H problem mentioned:

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

import java.awt.Color;

/**

*

* @author

*/

public class PolygonTransform {

public static double[] copy(double[] array) {

// The given array is not mutated.

if (array == null) {

return null;

}

double[] newArray = new double[array.length];

for (int i = 0; i < newArray.length; i++) {

newArray[i] = array[i];

}

return newArray;

}

public static void scale(double[] x, double[] y, double alpha) {

// Scales the polygon by the factor alpha.

// Scale the coordinates of each vertex (x_i, y_i) by a factor alpha

// y_i = a * y_i

for (int i = 0; i < y.length; i++) {

y[i] = alpha * y[i];

}

// x_i = a * x_i

for (int i = 0; i < x.length; i++) {

x[i] = alpha * x[i];

}

}

public static void translate(double[] x, double[] y, double dx, double dy) {

// Translates the polygon by (dx, dy).

// y_i = y_i + dy

for (int i = 0; i < y.length; i++) {

y[i] = y[i] + dy;

}

// x_i = x_i + dx

for (int i = 0; i < x.length; i++) {

x[i] = x[i] + dx;

}

}

public static void rotate(double[] x, double[] y, double theta) {

// Rotates the polygon theta degrees counterclockwise, about the origin.

double x_i, y_i;

for (int i = 0; i < y.length && i < x.length; i++) {

// x_i = x_i * cos θ – y_i * sin θ

x_i = x[i] * Math.cos(theta) - y[i] * Math.sin(theta);

// y_i = y_i * cos θ + x_i * sin θ

y_i = y[i] * Math.cos(theta) + x[i] * Math.sin(theta);

x[i] = x_i;

y[i] = y_i;

}

}...