-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.cpp
More file actions
102 lines (86 loc) · 2.53 KB
/
first.cpp
File metadata and controls
102 lines (86 loc) · 2.53 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
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <math.h>
/** stucture containing float values of latitude and longitude of a point **/
struct Position
{
float latitude;
float longitude;
};
class Gps
{
public:
/** Customised constructor **/
Gps(char filename[20]);
/** Get Functions**/
/** Get distance between two points **/
void GetDistance();
/** Get angle of line between two points **/
void GetAngleOfLine();
private:
/** Array of object of structure Position **/
Position position[2];
/** Accessor function to get Position Coordinates from a file **/
void readPositionCoordinates(char filename[20]);
/** Arithematic Functions **/
/** Function to calculate distance between 2 points using euclidean's formula (temporarily) **/
float calculateDistance();
/** Function to calculate angle of line between 2 points using atan2 (temporarily) **/
float calculateAngleOfLine();
};
Gps::Gps(char filename[20])
{
this->readPositionCoordinates(filename);
}
void Gps::GetDistance()
{
float distance = calculateDistance();
std::cout<<"Distance between 2 points = "<<distance<<std::endl;
}
void Gps::GetAngleOfLine()
{
float angle = calculateAngleOfLine();
std::cout<<"Angle of line between 2 points (in degrees) = "<<angle<<std::endl;
}
void Gps::readPositionCoordinates(char filename[20])
{
std::ifstream file;
file.open(filename);
std::string line;
int points=0;
while(std::getline(file,line))
{
std::vector<float> lineData;
std::stringstream lineStream(line);
float value;
while(lineStream >> value)
{
lineData.push_back(value);
}
position[points].latitude=lineData[0];
position[points].longitude=lineData[1];
std::cout<<position[points].latitude<<" "<<position[points].longitude<<std::endl;
points++;
}
file.close();
}
float Gps::calculateDistance()
{
return sqrt(pow(position[0].latitude - position[1].latitude,2) + pow(position[0].longitude - position[1].longitude,2));
}
float Gps::calculateAngleOfLine()
{
return (atan2(position[1].longitude - position[0].longitude,position[1].latitude - position[0].latitude))*(180/M_PI);
}
int main()
{
/** g is an object of class Gps **/
Gps g("abc.txt");
/** calling GET functions of class Gps using its object **/
g.GetDistance();
g.GetAngleOfLine();
return 0;
}