-
Notifications
You must be signed in to change notification settings - Fork 1
/
Route.cpp
132 lines (119 loc) · 3.29 KB
/
Route.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <iterator>
#include <fstream>
#include "Point.hpp"
#include "Route.hpp"
using namespace std;
ostream& operator<< (ostream& out, Route& route)
{
out << route.name << endl;
out << route.distance << endl;
int num = route.vecInsidePoint.size();
out << num << endl;
for(int i = 0; i<num; i++) {
out << route.vecInsidePoint[i]<<endl;
}
copy(route.edgeList.begin(), route.edgeList.end(), ostream_iterator<int>(out, " "));
return out;
}
istream& operator>> (istream& in, Route& route)
{
in >> route.name;
in >> route.distance;
int num;
in >> num;
vector<Point> vecIns(num);
for(int i=0; i<num; i++) {
in >> vecIns[i];
}
route.vecInsidePoint = vecIns;
int ed;
for(unsigned int i=0; i<route.vecInsidePoint.size()-1; i++) {
in >> ed;
route.edgeList.push_back(ed);
}
return in;
}
Route& Route::operator = (const Route &route)
{
if (&route == this)
return *this;
this->name = route.name;
this->distance = route.distance;
this->vecInsidePoint = route.vecInsidePoint;
this->edgeList = route.edgeList;
return *this;
}
void Route::calcDistance()
{
distance = 0;
for(unsigned int i=0; i<edgeList.size(); i++) {
distance+=edgeList[i];
}
}
void Route::generateVecEdge()
{
vector<int>::iterator it = edgeList.begin();
for(unsigned int i=0; i<vecInsidePoint.size()-1; i++) {
edgeList.push_back(0);
edgeList[i]=(calcLengthOfEdge(vecInsidePoint[i], vecInsidePoint[i+1]));
}
}
int Route::calcLengthOfEdge(Point start, Point finish)
{
pair<float, float> coord;
coord = start.getCoord();
float lotitude1=coord.first;
float longitude1=coord.second;
coord = finish.getCoord();
float lotitude2=coord.first;
float longitude2=coord.second;
lotitude1=(lotitude1*M_PI)/180;//перевод в радианы;
lotitude2=(lotitude2*M_PI)/180;//перевод в радианы;
longitude1=(longitude1*M_PI)/180;//перевод в радианы;
longitude2=(longitude2*M_PI)/180;//перевод в радианы;
float length;//длина самого расстояния
float c1,c2,s1,s2,delta,cdelta,sdelta,x,y;
s1=sin(lotitude1);//синус широты 1;
s2=sin(lotitude2);//синус широты 2;
c1=cos(lotitude1);//косинус широты 1;
c2=cos(lotitude2);//косинус широты 2;
delta=longitude2-longitude1;//разница долгот;
cdelta=cos(delta);//косинус разницы;
sdelta=sin(delta);//синус разинцы;
y=sqrt(pow(c2*sdelta,2)+pow(c1*s2-s1*c2*cdelta,2));//формула;
x=(s1*s2+c1*c2*cdelta);//формула;
length=atan2(y,x);
length *= 6371;
int len = static_cast<int>(ceil(100*length) / 100.0);
return len;
}
void Route::setName(string _name)
{
name = _name;
}
void Route::setInsidePoint(std::vector<Point> _vecInsidePoint)
{
vecInsidePoint = _vecInsidePoint;
}
string Route::getName()
{
return name;
}
vector<Point> Route::getInsidePoint()
{
return vecInsidePoint;
}
vector<int> Route::getEdgeList()
{
return edgeList;
}
int Route::getDistance()
{
return distance;
}