diff --git a/math/optimal_way/helpers_functions.cpp b/math/optimal_way/helpers_functions.cpp index 03b22cb..1fe0749 100644 --- a/math/optimal_way/helpers_functions.cpp +++ b/math/optimal_way/helpers_functions.cpp @@ -262,14 +262,8 @@ bool AreThereIntersections(const PolygonObstacle& poly_obst, const Point& pnt1, std::vector vertexes = poly_obst.GetVertexes(); for (std::size_t i = 0; i < vertexes.size() - 1; ++i) { LinearFunction v_line(vertexes[i], vertexes[i + 1]); - if (((line.a_coef * vertexes[i].x + line.b_coef * vertexes[i].y + - line.c_coef) * - (line.a_coef * vertexes[i + 1].x + - line.b_coef * vertexes[i + 1].y + line.c_coef) < - 0) && - ((v_line.a_coef * pnt1.x + v_line.b_coef * pnt1.y + v_line.c_coef) * - (v_line.a_coef * pnt2.x + v_line.b_coef * pnt2.y + v_line.c_coef) < - 0)) + if ((line.Substitute(vertexes[i]) * line.Substitute(vertexes[i + 1]) < 0) && + (v_line.Substitute(pnt1) * v_line.Substitute(pnt2) < 0)) return true; } return false; diff --git a/math/optimal_way/obstacles.h b/math/optimal_way/obstacles.h index 725d680..2aaabf2 100644 --- a/math/optimal_way/obstacles.h +++ b/math/optimal_way/obstacles.h @@ -25,6 +25,10 @@ struct LinearFunction { double a_coef, b_coef, c_coef; + double Substitute(const lib::Point& p) { + return a_coef * p.x + b_coef * p.y + c_coef; + } + bool operator==(const LinearFunction& other) { return (std::abs(a_coef - other.a_coef) < precision && std::abs(b_coef - other.b_coef) < precision && diff --git a/math/optimal_way/path_graph.cpp b/math/optimal_way/path_graph.cpp index 4df089f..3d29464 100644 --- a/math/optimal_way/path_graph.cpp +++ b/math/optimal_way/path_graph.cpp @@ -9,21 +9,19 @@ void DijkstrasAlgorithm::Calculate_Min_Path() { std::shared_ptr min_len_key; for (auto& elem : graphs_vertex_) if ((elem.second == min_length_) && - (!(*path_nodes_[elem.first]).is_visited)) + (!path_nodes_[elem.first]->is_visited)) min_len_key = path_nodes_[elem.first]; - for (std::size_t i = 0; i < (*min_len_key).edges.size(); ++i) - if ((graphs_vertex_.find((*(*min_len_key).edges[i]).number) == + for (std::size_t i = 0; i < min_len_key->edges.size(); ++i) + if ((graphs_vertex_.find(min_len_key->edges[i]->number) == graphs_vertex_.end()) || - (graphs_vertex_[(*(*min_len_key).edges[i]).number] > - graphs_vertex_[(*min_len_key).number] + - (*min_len_key).edges_lens[i])) - graphs_vertex_[(*(*min_len_key).edges[i]).number] = - graphs_vertex_[(*min_len_key).number] + - (*min_len_key).edges_lens[i]; + (graphs_vertex_[min_len_key->edges[i]->number] > + graphs_vertex_[min_len_key->number] + min_len_key->edges_lens[i])) + graphs_vertex_[min_len_key->edges[i]->number] = + graphs_vertex_[min_len_key->number] + min_len_key->edges_lens[i]; else continue; - (*min_len_key).is_visited = true; + min_len_key->is_visited = true; min_length_ = inf; for (auto& elem : graphs_vertex_) @@ -36,11 +34,11 @@ void DijkstrasAlgorithm::Calculate_Min_Path() { std::size_t end = second_point_; min_path_.push_back(end); while (end != first_point_) { - for (std::size_t i = 0; i < (*path_nodes_[end]).edges.size(); ++i) - if (graphs_vertex_[(*path_nodes_[end]).number] == - graphs_vertex_[(*(*path_nodes_[end]).edges[i]).number] + - (*path_nodes_[end]).edges_lens[i]) { - end = (*(*path_nodes_[end]).edges[i]).number; + for (std::size_t i = 0; i < path_nodes_[end]->edges.size(); ++i) + if (graphs_vertex_[path_nodes_[end]->number] == + graphs_vertex_[path_nodes_[end]->edges[i]->number] + + path_nodes_[end]->edges_lens[i]) { + end = path_nodes_[end]->edges[i]->number; min_path_.push_back(end); break; }