как устранить пересечение точек между двумя сферами

0

Пожалуйста, сэр, можете ли вы рассказать мне, как я могу исправить этот код, идея состоит в том, чтобы исключить точки пересечения в моих сферах, потому что это делает мою фигуру непонятной. Вот часть моего кода, вот как я сохранил координаты

int nombre=0;

for (int i = 0; i < size; i++)
{
    Sphere *s = &sphereTree.nodes.index(sphNum);//this is a sphere

    Point rt(s->c.x, s->c.y, s->c.z);//this is the center
    Vpoint.push_back(rt);//I saved centers here
    Vrayon.push_back(s->r);//I saved  radiu of spheres
    std::vector<Point> pp;
    pp=triangulateSphere(rt, s->r);//the points of sphere (rt,s->r)

    for (int indice=0;indice<pp.size();indice++)
    { 
        Point p1=pp[indice];
        tableau2[i].push_back(p1);//I saved points of sphere i in //tableau2[i];
    }

    nombre++;
}

Затем, чтобы получить только те точки, которые не включены в другие сферы, мне понравилось это

glBegin(GL_TRIANGLE_STRIP);
for(int indsphere=0; indsphere<=nombre;indsphere++)// the spheres indexes
{
    for (int indautresphere = 0;indautresphere<=nombre;indautresphere++)//other spheres created
    {
        if(indsphere!=indautresphere)
        {
            for (int nbrpointsi=0;nbrpointsi<tableau2[indsphere].size();nbrpointsi++)
            {
                float v1=(tableau2[indsphere][nbrpointsi].x)-(Vpoint[indautresphere].x);
                float v2=(tableau2[indsphere][nbrpointsi].y)-(Vpoint[indautresphere].y);
                float v3=(tableau2[indsphere][nbrpointsi].z)-(Vpoint[indautresphere].z);
                float val=sqrt(v1*v1+v2*v2+v3*v3);//calculate distance between points

                if(val >= (Vrayon[indautresphere]))
                    glVertex3fv(&((tableau2[indsphere][nbrpointsi]).x));
            }
        }
    }
}

glEnd();

у этого нет ошибок компиляции, но он показывает все точки, даже имеющие пересечение с другими сферами. Это не устраняет какой-либо момент

Теги:
visual-c++

1 ответ

0

Вы в настоящее время добавляете вершину, если точка находится за пределами любой другой сферы...

Следующее может помочь:

// You should already have a function
// to compute (Square-)distance between 2 Points
const float square_distance(const Point& p, const Point& p2)
{
    const float diffx = p.x - p2.x;
    const float diffy = p.y - p2.y;
    const float diffz = p.z - p2.z;
    return diffx * diffx + diffy * diffy + diffz * diffz;
}

bool isInsideSphere(const Point& p, const Point& center, float radius)
{
    // we compare square instead of squareRoot for efficiency.
    return square_distance(p, center) <= radius * radius;
}

А потом:

for (int i = 0; i <= nombre; ++i) {                // the spheres indexes
    glBegin(GL_TRIANGLE_STRIP);
    for (int j = 0; j < tableau2[i].size(); ++j) { // each point
        bool is_present_in_other_sphere = false;
        for (int k = 0; k <= nombre; ++k) {        //other spheres created
            if (i != k) { continue; }

            if (isInsideSphere(tableau2[i][j], Vpoint[k], Vrayon[k])) {
                is_present_in_other_sphere = true;
                break;
            }
        }
        if (is_present_in_other_sphere == false) {
            glVertex3fv(&tableau2[i][j].x);
        }
    }
    glEnd();
}
  • 0
    Сэр, большое спасибо за помощь, но она дает точно такие же результаты и сохраняет точки сферы s1, которые существуют внутри других сфер.
  • 0
    @ user3320319: Хорошо, только что увидел другую ошибку, которую вы сделали: вы рисуете сферу не сферой, а целым, поэтому между каждой сферой есть треугольник ... Попробуйте отредактированный ответ ( glBegin и glEnd находятся в цикле) ,
Показать ещё 14 комментариев

Ещё вопросы

Сообщество Overcoder
Наверх
Меню