ОписаниеПроверить ближайшие прямоугольниками
Пусть координаты 4 стороны прямоугольника, обозначенного (x1, y1), (x2, y2), (х3, у3) и (x4, y4). Как это Изображение-
И у меня есть набор координат 100000 прямоугольников, сохраненных в текстовом файле. Например здесь значения координат 16 прямоугольников, порожденных моей code-
#Rect x1 y1 x2 y2 x3 y3 x4 y4 area
1 0.0000 0.0000 0.8147 0.0000 0.8147 0.1355 0.0000 0.1355 0.1104
2 0.8147 0.0000 1.0000 0.0000 1.0000 0.1355 0.8147 0.1355 0.0251
3 0.8147 0.1355 0.9058 0.1355 0.9058 0.8350 0.8147 0.8350 0.0637
4 0.0000 0.1355 0.1270 0.1355 0.1270 0.9689 0.0000 0.9689 0.1058
5 0.9058 0.1355 0.9134 0.1355 0.9134 0.2210 0.9058 0.2210 0.0006
6 0.9058 0.8350 1.0000 0.8350 1.0000 1.0000 0.9058 1.0000 0.0155
7 0.8147 0.8350 0.9058 0.8350 0.9058 1.0000 0.8147 1.0000 0.0150
8 0.1270 0.1355 0.6324 0.1355 0.6324 0.3082 0.1270 0.3082 0.0873
9 0.1270 0.9689 0.8147 0.9689 0.8147 1.0000 0.1270 1.0000 0.0214
10 0.0000 0.9689 0.1270 0.9689 0.1270 1.0000 0.0000 1.0000 0.0040
11 0.9134 0.1355 1.0000 0.1355 1.0000 0.2210 0.9134 0.2210 0.0074
12 0.9134 0.2210 1.0000 0.2210 1.0000 0.8350 0.9134 0.8350 0.0532
13 0.9058 0.2210 0.9134 0.2210 0.9134 0.8350 0.9058 0.8350 0.0047
14 0.6324 0.1355 0.8147 0.1355 0.8147 0.3082 0.6324 0.3082 0.0315
15 0.6324 0.3082 0.8147 0.3082 0.8147 0.9689 0.6324 0.9689 0.1205
16 0.1270 0.3082 0.6324 0.3082 0.6324 0.9689 0.1270 0.9689 0.3339
Этих координаты разбивает единичный квадрат на подразделы прямоугольников, как этот изображе-
Примеров Ближайших прямоугольников
На рисунке изображены ближайшие прямоугольники для прямоугольника № 3 - 9,15,14,1,2,5,13,6 и 7.
Для прямоугольника # 9 они - 10,4,16, 15,3 и 7.
Моя проблема
Теперь я хотел бы, чтобы вычислить число ближайших прямоугольников для каждого из прямоугольников, используя C/C++. Как мне это сделать?
Edit: На основе ответов
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct Rectangle {
double x1, y1;
double x2, y2;
double x3, y3;
double x4, y4;
};
vector<double> get_touching_rectangles(Rectangle base, vector<Rectangle> rectangles) {
for (auto it = rectangles.begin(); it != rectangles.end(); it++) {
Rectangle other = *it;
if (base == other) {
continue; // This is our rectangle... skip it
}
// Top or bottom
if ((other.x2 >= base.x1 && other.x1 <= base.x2) && (other.y1 == base.y3 || other.y3 == base.y1)) {
ret.push_back(other);
continue;
}
// Left or right
if ((other.y3 >= base.y2 && other.y2 <= base.y3) && (other.x1 == base.x3 || other.x3 == base.x1)) {
ret.push_back(other);
continue;
}
}
return ret;
}
int main(int argc, char const *argv[])
{
vector<Rectangle> rectangles;
//parse_txt_file(file, &rectangles); // Or whateer I need to do to parse that .txt file
ifstream inputFile;
inputFile.open("RectCoordinates.txt");
//std::vector<Rectangle> touching =
get_touching_rectangles(rectangles.at(2) /* Rectangle #3 */, rectangles);
inputFile.close();
return 0;
}
Ok я пишу код выше на основе ответов. Но он показывает следующее сообщение об ошибке -
g++ -std=c++11 st5.cpp -o ssst5.cpp: In function ‘std::vector<double> get_touching_rectangles(Rectangle, std::vector<Rectangle>)’:
st5.cpp:23:21: error: no match for ‘operator==’ in ‘base == other’
st5.cpp:23:21: note: candidates are:
In file included from /usr/include/c++/4.7/iosfwd:42:0,
from /usr/include/c++/4.7/ios:39,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from st5.cpp:1:
/usr/include/c++/4.7/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.7/bits/postypes.h:218:5: note: template argument deduction/substitution failed:
st5.cpp:28:13: error: ‘ret’ was not declared in this scope
st5.cpp:33:13: error: ‘ret’ was not declared in this scope
st5.cpp:37:12: error: ‘ret’ was not declared in this scope
Что я делаю неправильно?
Быстрое замечание: вам не нужно иметь x2, y2 и x4, y4 ... вы можете автоматически заполнить их с помощью x1, y1 и x3, y3 или, то есть, если вы имеете дело только с прямоугольники, как вы говорите. – MiJyn
@MiJyn: Вы правы. x1, y1 и x3, y3 заполняет значения для x2, y2 & x4, y4. – aries0152
Разве это не похоже на [это] (http://stackoverflow.com/questions/17328004/count-the-number-of-adjacent-boxes) и [это] (http://stackoverflow.com/questions/17274934/count-the-number-of-nearby-rectangles) ваш пост? –