Пример #1
0
void cut(points &p, point b, point a, points &res) {
	res.clear();
	int n = p.size();
	for (int i = 0; i < n; i ++) {
		point c = p[i];
		point d = p[next(i)];
		if (dcmp(det(b - a, c - a)) >= 0) res.push_back(c);
		if (dcmp(det(b - a, c - d)) != 0) {
			point cp = interpoint(line(a, b), line(c, d));
			if (onseg(cp, c, d)) res.push_back(cp);
		}
	}
}
Пример #2
0
int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		std::cout << usage << std::endl;
		return -1;
	}

	std::string line;
	std::ifstream input(argv[1], std::ifstream::in);

	std::getline(input, line);
	std::istringstream seiss(line);
	double sx, sy, ex, ey;
	seiss >> sx >> sy >> ex >> ey;
	point start(sx, sy), end(ex, ey);

	std::getline(input, line);
	std::istringstream niss(line);
	int count = 0;
	niss >> count;
	for (int i = 0; i < count; i++)
	{
		std::getline(input, line);
		std::istringstream iss(line);
		double x, y;
		iss >> x >> y;
		polygon_points.push_back(point(x, y));
	}

	int vertices_count = polygon_points.size();
	int triangles_count = polygon_points.size() - 2;

	double (*vertices)[2] = new double[vertices_count + 1][2];
	int (*triangles)[3] = new int[triangles_count][3];

	for (int i = 1; i <= polygon_points.size(); i++)
	{
		vertices[i][0] = polygon_points[i - 1].x;
		vertices[i][1] = polygon_points[i - 1].y;
	}
	int ncontours = 1;
	int cntr[1] = {polygon_points.size()};
	triangulate_polygon(ncontours, cntr, vertices, triangles);

	for (int i = 0; i < triangles_count; i++)
	{
		triangles[i][0]--;
		triangles[i][1]--;
		triangles[i][2]--;
	}
	std::ofstream output(argv[2], std::ofstream::out);
	int start_tri = locate_point(triangles, triangles_count, start);
	int end_tri = locate_point(triangles, triangles_count, end);
	bool distance = false;
	if (strcmp(argv[3], "path") == 0)
		distance = false;
	else if (strcmp(argv[3], "distance") == 0)
		distance = true;
	if (start_tri == end_tri)
	{
		if (distance)
			output << (int)ceil(dist(start, end)) << std::endl;
		else
		{
			output << "s" << std::endl;
			output << "e" << std::endl;
		}
	}
	else
	{
		std::vector<int> triangles_list = dfs(triangles, triangles_count, start_tri, end_tri);
		std::vector<diagnal> d_list = diagnals_list(triangles, triangles_list);
		path shortest_path = simple_stupid_funnel(d_list, start, end);

		if (distance)
		{
			double total_dist = 0;
			for (int i = 0; i < shortest_path.size(); i++)
			{
				point last;
				point current = polygon_points[shortest_path[i]];
				if (i == 0)
					last = start;
				else
					last = polygon_points[shortest_path[i - 1]];
				total_dist += dist(last, current);
			}
			if (shortest_path.size() > 0)
				total_dist += dist(polygon_points[shortest_path[shortest_path.size() - 1]], end);
			else
				total_dist += dist(start, end);
			output << (int)ceil(total_dist) << std::endl;
		}
		else
		{
			output << "s" << std::endl;
			for (int i = 0; i < shortest_path.size(); i++)
				output << shortest_path[i] << std::endl;
			output << "e" << std::endl;
		}
	}

	delete[] vertices;
	delete[] triangles;
	return 0;
}