Во время сортировки первый элемент устанавливается в 0 после оси 0,0

Я пытаюсь отсортировать три точки треугольника, чтобы успешно добавить его в физический движок. Все треугольники, положение которых меньше 0.0 на оси x, отображаются правильно. Однако после этой точки первый элемент (v[0]) всегда установлен на {0.0, 0.0} а остальные кажутся нормальными. Вот моя основная функция, которая выполняет сортировку и добавление.

void Core::addPoly(float x1, float y1,
float x2, float y2,
float x3, float y3) {

std::vector<PVector> v(3);
v.push_back(PVector(x1, y1));
v.push_back(PVector(x2, y2));
v.push_back(PVector(x3, y3));

PVector center((x1+x2+x3)/3, (y1+y2+y3)/3);

std::sort(v.begin(), v.end(), [center](PVector b, PVector a) {
if (a.x >= 0 && b.x < 0)
return true;
if (a.x == 0 && b.x == 0)
return a.y > b.y;

// compute the cross product of vectors (center -> a) x (center -> b)
float det = (a.x-center.x) * (b.y-center.y) - (b.x - center.x) * (a.y - center.y);
if (det < 0)
return true;
if (det > 0)
return false;

// points a and b are on the same line from the center
// check which point is closer to the center
float d1 = (a.x-center.x) * (a.x-center.x) + (a.y-center.y) * (a.y-center.y);
float d2 = (b.x-center.x) * (b.x-center.x) + (b.y-center.y) * (b.y-center.y);
return d1 > d2;
});

emap->polys.push_back(Polygon(v[0], v[1], v[2]));
}

Я использую функцию сортировки при условии Вот. Первоначально первый элемент всех треугольников указывал на центр (однако я не верю, что это правильное поведение) — я переключил a и b в лямбда-объявлении, и теперь он отображается только после 0.0 на оси x.

Если мы передали 3 треугольника (представьте, что | = 0,0 и ___ = 0,0 на плоскости)

^     |
^ |   ^
______|_____________

0-я вершина третьего треугольника фактически сделает это так:

^     |
^ |  ___/|
______|_/______________

(Это должен быть треугольник)

Однако 0.0 никогда не передавалось в addPoly.

-1

Решение

Эта строка:

std::vector<PVector> v(3);

Заставляет ваш вектор инициализироваться тремя <0.0,0.0> ценности. Меняя его на

std::vector<PVector> v;

должен исправить вашу проблему.

Код кажется работать так:

struct PVector
{
float x;
float y;
PVector(float x, float y) : x(x),y(y){}
PVector() : x(0.0f), y(0.0f) {}
};static void addPoly(float x1, float y1,
float x2, float y2,
float x3, float y3) {

std::vector<PVector> v;
v.push_back(PVector(x1, y1));
v.push_back(PVector(x2, y2));
v.push_back(PVector(x3, y3));

PVector center((x1+x2+x3)/3, (y1+y2+y3)/3);

std::sort(v.begin(), v.end(), [center](PVector b, PVector a) -> bool {
if (a.x >= 0 && b.x < 0)
return true;
if (a.x == 0 && b.x == 0)
return a.y > b.y;

// compute the cross product of vectors (center -> a) x (center -> b)
float det = (a.x-center.x) * (b.y-center.y) - (b.x - center.x) * (a.y - center.y);
if (det < 0)
return true;
if (det > 0)
return false;

// points a and b are on the same line from the center
// check which point is closer to the center
float d1 = (a.x-center.x) * (a.x-center.x) + (a.y-center.y) * (a.y-center.y);
float d2 = (b.x-center.x) * (b.x-center.x) + (b.y-center.y) * (b.y-center.y);
return d1 > d2;
});

//emap->polys.push_back(Polygon(v[0], v[1], v[2]));
}
1

Другие решения

Других решений пока нет …