-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPlanet.cc
More file actions
91 lines (71 loc) · 2.57 KB
/
Planet.cc
File metadata and controls
91 lines (71 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <ostream>
#include <sstream>
#include <math.h>
#include <iostream>
#include "Planet.h"
Planet::Planet(int mass, int radius, Point origin, Vector heading, int color)
: mass(mass), radius(radius), origin(origin), heading(heading), color(color)
{
}
std::string Planet::toString()
{
std::stringstream x;
x << "Mass: " << mass << "\n"
<< "Radius: " << radius << "\n"
<< "Position: " << origin.toString() << "\n"
<< "Heading: " << heading.toString() << "\n"
<< "Color: " << color << "\n";
return x.str();
}
// Calculates the gravitational pull from this planet to a given planet P
double Planet::calculateGravity(Planet &p)
{
int mass1 = mass;
int mass2 = p.mass;
double distanceBetween = origin.distance(p.origin);
return ((mass1 * mass2) / pow(distanceBetween, 2));
}
// Calculates the horizontal and vertical distance to another given planet P.
void Planet::distance(Planet &p, double &horizontalDistance, double &verticalDistance)
{
horizontalDistance = p.origin.x - origin.x;
verticalDistance = p.origin.y - origin.y;
//printf("H: %f V: %f\n", horizontalDistance, verticalDistance);
}
// Given a planet, calculates a gravitational vector towards that planet.
Vector Planet::findVector(Planet &p)
{
double forceBetween = calculateGravity(p);
double horizontalDistance = 0;
double verticalDistance = 0;
distance(p, horizontalDistance, verticalDistance);
//printf("HDIS: %f\n", horizontalDistance);
double totalDistance = abs(horizontalDistance) + abs(verticalDistance);
double xComponent = (forceBetween / totalDistance) * horizontalDistance;
double yComponent = (forceBetween / totalDistance) * verticalDistance;
//printf("H: %f \t V: %f \t X: %f\t Y: %f HEAD: ", horizontalDistance, verticalDistance, xComponent, yComponent);
//std::cout << heading.toString() << std::endl;
Vector toReturn(xComponent, yComponent);
return toReturn;
}
// Given a deque of planets, sums them into a final heading vector which will be its orbital path
void Planet::sumVector(std::deque<Planet> dq)
{
Vector final(heading.x, heading.y);
Vector temp(0, 0);
for (int i = 0; i < dq.size(); i++)
{
if (origin.x != dq.at(i).origin.x && origin.y != dq.at(i).origin.y)
{
if (origin.x != 1 || origin.y != 1)
{
temp = findVector(dq.at(i));
final.x = final.x + temp.x;
final.y = final.y + temp.y;
}
}
}
heading = final;
origin.x += heading.x;
origin.y += heading.y;
}