Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/4173921 to your computer and use it in GitHub Desktop.
Save dharmatech/4173921 to your computer and use it in GitHub Desktop.
#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); }
};
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 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;
}
throw "exception";
}
auto _g = Point(0, -g);
int _tmain(int argc, _TCHAR* argv[])
{
// In a popular lecture demonstration, a projectile is fired at a
// target in such a way that the projectile leaves the gun at the
// same time the target is dropped from rest.
// Show that if the gun is initially aimed at the stationary
// target, the projectile hits the target.
Obj obj1A; // object 1 at A
obj1A.position = Point(0,0);
auto th = Symbolic("th");
auto v1A = Symbolic("v1A"); // The velocity of object 1 at A
obj1A.velocity = Point::FromAngle(th, v1A);
obj1A.acceleration = _g;
obj1A.time = 0;
Obj obj1C; // object 1 at C
obj1C.position = Point();
auto w = Symbolic("w");
obj1C.position.x = w;
obj1C.velocity = Point();
obj1C.velocity.x = obj1A.velocity.x;
obj1C.acceleration = _g;
auto timeAC = CalcTime(obj1A, obj1C);
cout << "time from A to C:" << endl;
cout << timeAC << endl << endl;
cout << "Height of object 1 at C:" << endl;
cout << obj1A.AtTime(timeAC).position.y << endl << endl;
Obj obj2B; // Object 2 at B
auto h = Symbolic("h");
obj2B.position = Point(w, h);
obj2B.velocity = Point(0, 0);
obj2B.acceleration = _g;
auto obj2D = obj2B.AtTime(timeAC);
cout << "Height of object 2 at D:" << endl;
cout << obj2D.position.y << endl << endl;
// The answer is in terms of 'h'.
// Let's write it in terms of 'w'.
// tan(th) = h / w
//
// h = w tan(th)
cout << "Height of object 2 at D (in terms of h):" << endl;
cout << obj2D.position.y[h == w * tan(th)] << endl << endl;
if (obj1A.AtTime(timeAC).position.y == obj2D.position.y[h == w * tan(th)])
cout << "object 1 and object 2 are at the same height" << endl;
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment