Write a function perpendicular() which determines whether two 2D vectors are perpendicular to each other.
In order to make this determination, we will compute two perpendicular vectors from a given vector (x, y). The first perpendicular vector, (px1, py1), is defined as px1 = y and py1 = x. The second perpendicular vector, (px2, py2), is defined as px2 = px1 and py2 = py1. For example if (x, y) = (1, 2) then (px1, py1) = (2, 1) and (px2, py2) = (2, 1). Thus, (px1, py1) and (px2, py2) are perpendicular vectors to (x, y). You must implement the following algorithm to determine if two vectors, (x1, y1) and (x2, y2), are perpendicular: (a) Normalize (x1, y1) and (x2, y2); Let’s call the new vectors v1 = (vx1, vy1) and v2 = (vx2, vy2), respectively (use better variable names). Call the normalize() function to compute these. Note that v1 and v2 are unit vectors. (b) Compute two perpendicular vectors to v1 (see instructions above). Let’s call the two new vectors p1 and p2. (c) Check whether v2 is the same as either p1 or p2. If so, then output ”Vectors are PERPENDICULAR.” to the screen. Otherwise, output ”Vectors are NOT PERPENDICULAR.” See next step for more details. (d) Let p1 = (px1, py1), and p2 = (px2, py2). i. To determine if v2 is the same vector as p1, check that both vx2 = px1 and vy2 = py1. Use EPSILON as described in step 4 above to determine each equality. ii. To determine if v2 is the same vector as p2, check that both vx2 = px2 and vy2 = py2. Use EPSILON as described in step 4 above to determine each equality. Define the function so that it does not return a value and has four parameters that are all passed by value: • a number x1 of type double representing the x coordinate of the first 2D vector. • a number y1 of type double representing the y coordinate of the first 2D vector. • a number x2 of type double representing the x coordinate of the second 2D vector; • a number y2 of type double representing the y coordinate of the second 2D vector.
#include
#include #include using namespace std; const double EPSILON(1e-12); // function prototypes // ENTER FUNCTION PROTOTYPE FOR read_vector HERE. void read_vector (const string & prompt, double & x, double & y); // ENTER FUNCTION PROTOTYPE FOR vector_length HERE. double vector_length (double x, double y); // ENTER FUNCTION PROTOTYPE FOR write_vector HERE. void write_vector (const string & prompt, double & x, double & y); // ENTER FUNCTION PROTOTYPE FOR vector_add HERE. void vector_add (double x1, double y1, double x2, double y2, double & x3, double & y3); // ENTER FUNCTION PROTOTYPE FOR vector_subtract HERE. void vector_subtract (double x1, double y1, double x2, double y2, double & x3, double & y3); // ENTER FUNCTION PROTOTYPE FOR scalar_mult HERE. void scalar_mult (double x1, double y1, double s, double & x2, double & y2); // ENTER FUNCTION PROTOTYPE FOR normalize HERE. void normalize (double & x, double & y); // ENTER FUNCTION PROTOTYPE FOR perpendicular HERE. void perpendicular (double & x1, double & y1, double & x2, double & y2); // *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION. int main() { double u1, v1; // coordinates of first vector double u2, v2; // coordinates of second vector double u3, v3; double scalar; read_vector(\"Enter first vector (2 floats): \", u1, v1); read_vector(\"Enter second vector (2 floats): \", u2, v2); cout << \"Enter scalar multiplier: \"; cin >> scalar; cout << endl; write_vector(\"First vector: \", u1, v1); write_vector(\"Second vector: \", u2, v2); cout << endl; vector_add(u1, v1, u2, v2, u3, v3); write_vector(\"Vector add: \", u3, v3); vector_subtract(u1, v1, u2, v2, u3, v3); write_vector(\"Vector subtract: \", u3, v3); scalar_mult(u1, v1, scalar, u3, v3); write_vector(\"Scalar multiplier: \", u3, v3); cout << endl; write_vector(\"First vector: \", u1, v1); write_vector(\"Second vector: \", u2, v2); perpendicular(u1, v1, u2, v2); return(0); } // DEFINE FUNCTION read_vector HERE. void read_vector (const string & prompt, double & x, double & y) { cout << prompt; cin >> x >> y; } // DEFINE FUNCTION vector_length HERE. double vector_length (double x, double y) { double v_length = sqrt(pow(x, 2) + pow(y, 2)); return (v_length); } // DEFINE FUNCTION write_vector HERE. void write_vector (const string & prompt, double & x, double & y) { cout << \"(\" << x << \", \" << y << \") has length \" << vector_length(x,y) << endl; } // DEFINE FUNCTION vector_add HERE. void vector_add (double x1, double y1, double x2, double y2, double & x3, double & y3) { x3 = x1 + x2; y3 = y1 + y2; cout << \"Vector add: \"; } // DEFINE FUNCTION vector_subtract HERE. void vector_subtract (double x1, double y1, double x2, double y2, double & x3, double & y3) { x3 = x1 - x2; y3 = y1 - y2; cout << \"Vector subtract: \"; } // DEFINE FUNCTION scalar_mult HERE. void scalar_mult (double x1, double y1, double s, double & x2, double & y2) { x2 = s * x1; y2 = s * y1; cout << \"Vector multiplier: \"; } // DEFINE FUNCTION normalize HERE. void normalize (double & x, double & y) { double v = vector_length(x, y); if (v < EPSILON) { x = x / v; y = y / v; } } // DEFINE FUNCTION perpendicular HERE. void perpendicular (double & x1, double & y1, double & x2, double & y2) { normalize(x1, y1); normalize (x2, y2); double p1x = -y1; double p1y = x1; double p2x = -p1x; double p2y = -p1y; if (abs(x2 - p1x) < EPSILON && abs(y2 - p1y) < EPSILON || (abs(x2 - p2x) < EPSILON && abs(y2 - p2y) < EPSILON)) { cout << \"Vectors are PERPENDICULAR.\" << endl; } else { cout << \"Vectors are NOT PERPENDICULAR.\" << endl; } }