public static void main(String[] argv) { DrawTool.display(); DrawTool.setXYRange(-2, 2, -2, 2); DrawTool.drawMiddleAxes(true); double[][] A = { {5, -2}, {0, 1} }; // Now start with some initial vector u double[] u = {1, 0}; // Draw u DrawTool.drawVector(u); // In black. // We'll draw the vectors obtained in sequence: DrawTool.setArrowColor("blue"); int n = 10; for (int i = 0; i < n; i++) { // Apply A to u. u = MatrixTool.matrixVectorMult(A, u); // We'll normalize the length so that we stay // within the bounds of the drawing area. This is fine // because we just want the direction. u = normalize(u); DrawTool.drawVector(u); } }
static double[] normalize(double[] x) { double normX = MatrixTool.norm(x); double[] y = new double[x.length]; for (int i = 0; i < x.length; i++) { y[i] = (1.0 / normX) * x[i]; } return y; }
public static void main(String[] argv) { // Random seed for generation. UniformRandom.set_seed(System.currentTimeMillis()); // Start with a random unit length vector: a random angle. double theta = UniformRandom.uniform(0, 2.0 * Math.PI); double[] x = new double[2]; x[0] = Math.cos(theta); x[1] = Math.sin(theta); // Find perpendicular unit length vector by adding 90-deg double[] y = new double[2]; y[0] = Math.cos(theta + Math.PI / 2); y[1] = Math.sin(theta + Math.PI / 2); // Test. double xDoty = MatrixTool.dotProduct(x, y); System.out.println("x dot y = " + xDoty); // Make the matrix with x and y as columns. double[][] A = new double[2][2]; A[0][0] = x[0]; A[1][0] = x[1]; A[0][1] = y[0]; A[1][1] = y[1]; double delTheta = 0.1; double[] u = new double[2]; Function F = new Function("alpha vs theta"); for (theta = 0; theta <= 2 * Math.PI; theta += delTheta) { // Generate next vector along unit circle. u[0] = Math.cos(theta); u[1] = Math.sin(theta); // Apply A to u. double[] z = MatrixTool.matrixVectorMult(A, u); double alpha = Math.atan2(z[1], z[0]); // Get z's angle. alpha = fixAngle(alpha); // Add to data set. F.add(theta, alpha); } // Plot data set. F.show(); }
void drawingCommands() { // Rotation about z: double theta = Math.PI / 3.0; double a11 = Math.cos(theta); double a12 = -Math.sin(theta); double a21 = Math.sin(theta); double a22 = Math.cos(theta); double[][] A = { {a11, a12, 0, 0}, {a21, a22, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }; /* // INSERT YOUR CODE for rotation about x by 30 degrees double[][] B = { }; // INSERT YOUR CODE for translation by (2,3,4) double[][] C = { }; double[][] temp = MatrixTool.matrixMult (B,A); double[][] transform = MatrixTool.matrixMult (C,temp); */ // Replace this transform with the above after implementing // matrices B and C double[][] transform = A; double x0 = 0, y0 = 0; // Center of original 2D ellipse. double a = 7; // Major axis double b = 4; // Minor axis double delT = 0.25; // t-increment for drawing d3.setDrawColor(Color.RED); for (double t = 0; t <= 2 * Math.PI + delT; t += delT) { double[] x = new double[4]; x[0] = a * Math.cos(t); x[1] = b * Math.sin(t); x[2] = 0; x[3] = 1; double[] z = MatrixTool.matrixVectorMult(transform, x); d3.drawPoint(z[0], z[1], z[2]); } }