-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeometry.cpp
More file actions
79 lines (70 loc) · 1.83 KB
/
geometry.cpp
File metadata and controls
79 lines (70 loc) · 1.83 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
#include <iostream>
#include <cmath>
using namespace std;
const float PI = 3.14159;
float distance(float x1, float y1, float x2, float y2)
{
float dist = sqrt(pow((x2-x1),2) + pow((y2-y1),2));
return dist;
}
void centerOfMass(float points[][2], int size, float& cx, float& cy)
{
float xsum = 0;
float ysum = 0;
for (int i = 0; i < size; i++)
{
xsum += points[i][0];
ysum += points[i][1];
}
cx = xsum/size;
cy = ysum/size;
}
//farthest from the center of Mass
void farFromCenter(float points[][2], int size, float& cx, float& cy)
{
float maxDist = 0;
float tempDist = 0;
int index = 0;
for (int i = 0; i < size; i++)
{
tempDist = distance(cx,cy,points[i][0],points[i][1]);
if (tempDist > maxDist)
{
maxDist = tempDist;
index = i;
}
}
cout << "Point (" << points[index][0] << "," << points[index][1] << ") is the farthest from the center of mass with distance " << maxDist << endl;
}
float toRadians(float degrees)
{
return degrees*(PI/180);
}
void rotate(float x, float y, float theta)
{
float rx = 0.0;
float ry = 0.0;
theta = toRadians(theta);
rx = x*cos(theta)-y*sin(theta);
ry = y*cos(theta)+x*sin(theta);
cout << "(" << x << "," << y << ") rotated 45 degrees is: " << "(" << rx << "," << ry << ")" << endl;
}
int main()
{
const int rows = 4;
const int cols = 2;
float coords[rows][cols] = {0,0, 5,-1, 10,0, 5,10};
for(int i = 0; i < rows; i++)
{
cout << "(" << coords[i][0] << "," << coords[i][1] << ") has magnitude: " << distance(0,0,coords[i][0],coords[i][1]) << endl;
}
float cx = 0.0;
float cy = 0.0;
centerOfMass(coords,rows,cx,cy);
cout << "Center of Mass: (" << cx << "," << cy << ") has magnitude " << distance(0,0,cx,cy) << endl;
farFromCenter(coords,rows,cx,cy);
for(int i = 0; i < rows; i++)
{
rotate(coords[i][0],coords[i][1],45);
}
}