Instantly share code, notes, and snippets.

# dharmatech/pse problem 4.15 - general.cpp Created Dec 8, 2012

What would you like to do?
 #include "stdafx.h" #include "..\..\include\symbolicc++.h" Symbolic Pi = "Pi"; Symbolic g = "g"; Symbolic Radians(Symbolic n) { return n * Pi / 180; } Symbolic Degrees(Symbolic n) { return 180 * n / Pi; } class Point { public: Symbolic x; Symbolic y; Point() { x = "NOTSET" ; y = "NOTSET"; } Point(Symbolic x_val, Symbolic y_val) { x = x_val; y = y_val; } void Print() { cout << "Point(" << x << ", " << y << ")"; } static Point FromAngle(Symbolic angle, Symbolic mag) { return Point(cos(angle) * mag, sin(angle) * mag); } Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator*(Symbolic sym) { return Point(x * sym, y * sym); } Point operator/(Symbolic sym) { return Point(x / sym, y / sym); } Symbolic Norm() { return sqrt(x*x + y*y); } double ToAngle() { return atan2(y, x); } }; void Unset(Symbolic &sym) { sym = "NOTSET"; } bool IsSet(Symbolic sym) { if (sym == "NOTSET") return false; else return true; } bool HasVal(Symbolic sym) { if (sym == "NOTSET") return false; else return true; } class Obj { public: Point position, velocity, acceleration; Symbolic speed; Symbolic time; void Print() { // cout << "Obj:" << endl; cout << "time: " << time << endl; cout << "position.x: " << position.x << endl; cout << "position.y: " << position.y << endl; cout << "velocity.x: " << velocity.x << endl; cout << "velocity.y: " << velocity.y << endl; cout << "acceleration.x: " << acceleration.x << endl; cout << "acceleration.y: " << acceleration.y << endl; } Obj AtTime(Symbolic t) { Obj obj; obj.time = t; auto dt = t - time; obj.acceleration = acceleration; obj.velocity = velocity + acceleration * dt; obj.position = position + velocity * dt + acceleration * dt * dt / 2; return obj; } }; Symbolic CalcTime(Obj& a, Obj& b, int flag=0) { if (HasVal(b.velocity.x) && HasVal(a.velocity.x) && HasVal(a.acceleration.x) && a.acceleration.x != 0.0 && a.acceleration.x != 0) return (b.velocity.x - a.velocity.x) / a.acceleration.x; if (HasVal(b.velocity.y) && HasVal(a.velocity.y) && HasVal(a.acceleration.y) && a.acceleration.y != 0.0 && a.acceleration.y != 0) return (b.velocity.y - a.velocity.y) / a.acceleration.y; if (HasVal(a.position.x) && HasVal(b.position.x) && HasVal(a.velocity.x) && a.velocity.x != 0 && a.velocity.x != 0.0) return (b.position.x - a.position.x) / a.velocity.x; if (HasVal(b.position.x) && HasVal(a.position.x) && HasVal(a.velocity.x) && HasVal(a.acceleration.x) && a.acceleration.x != 0 && a.acceleration.x != 0.0) { if (flag == 0) return (-a.velocity.x + sqrt(pow(a.velocity.x, Symbolic(2)) - 2 * a.acceleration.x * (a.position.x - b.position.x))) / a.acceleration.x; else return (-a.velocity.x - sqrt(pow(a.velocity.x, Symbolic(2)) - 2 * a.acceleration.x * (a.position.x - b.position.x))) / a.acceleration.x; } if (HasVal(a.position.y) && HasVal(b.position.y) && HasVal(a.velocity.y) && HasVal(a.acceleration.y) && a.acceleration.y != 0 && a.acceleration.y != 0.0) { if (flag == 0) return (-a.velocity.y + sqrt(pow(a.velocity.y, Symbolic(2)) - 2 * a.acceleration.y * (a.position.y - b.position.y))) / a.acceleration.y; else return (-a.velocity.y - sqrt(pow(a.velocity.y, Symbolic(2)) - 2 * a.acceleration.y * (a.position.y - b.position.y))) / a.acceleration.y; } throw "exception"; } Symbolic CalcInitialVelocityX(Obj& a, Obj& b) { if (HasVal(a.position.x) && HasVal(a.position.y) && HasVal(a.time) && HasVal(b.time) && HasVal(a.acceleration.x)) { auto dt = b.time - a.time; return (b.position.x - a.position.x - a.acceleration.x * dt^2 / 2) / dt; } throw "exception"; } auto _g = Point(0, -g); double SymmetricalAbout(double a, double b) { return a + a - b; } // sin(a th) = b Symbolic SineCalcTheta(Symbolic a, Symbolic b, int solution = 0, int n = 0) { if (solution == 0) return (- asin(b) + 2 * Pi * n + Pi) / a; if (solution == 1) return (asin(b) + 2 * Pi * n) / a; } Symbolic CalcInitialAngle(Obj a, Obj b, int sol = 0, int n = 0) { if (HasVal(a.position.x) && HasVal(b.position.x) && HasVal(a.acceleration.y) && HasVal(a.speed)) if (sol == 0) return (- asin(- a.acceleration.y * (b.position.x - a.position.x) / (a.speed^2)) + 2 * Pi * n + Pi) / 2; if (sol == 1) return (asin(- a.acceleration.y * (b.position.x - a.position.x) / (a.speed^2)) + 2 * Pi * n) / 2; throw "exception"; } int _tmain(int argc, _TCHAR* argv[]) { // A projectile is fired in such a way that its horizontal // range is equal to three times its maximum height. // // What is the angle of projection? // // Give your answer to three significant figures. { Obj objA; auto vxA = Symbolic("vxA"); auto vyA = Symbolic("vyA"); objA.time = 0; objA.position = Point(0, 0); objA.velocity = Point(vxA, vyA); objA.acceleration = _g; Obj objB; objB.velocity.y = 0; objB.acceleration = _g; auto timeAB = CalcTime(objA, objB); auto timeB = objA.time + timeAB; objB = objA.AtTime(timeB); auto timeC = timeB*2; auto objC = objA.AtTime(timeC); cout << "maximum height is " << objB.position.y << endl; cout << "horizontal range is " << objC.position.x << endl; // H = 1/2 vyA^2 / g // R = 2 vxA vyA / g // // "its horizontal range is equal to three times its maximum height" // // R = 3 H // // 2 vxA vyA / g = 3 1/2 vyA^2 / g // // 4 vxA = 3 vyA // // vxA = vA cos(th) // vyA = vA sin(th) // // 4 vA cos(th) = 3 vA sin(th) // // 4 cos(th) = 3 sin(th) // // 4/3 = tan(th) } system("pause"); return 0; }