Getting Center and Radius of Irregural Object

Posted by Moaz ELdeen on Game Development See other posts from Game Development or by Moaz ELdeen
Published on 2012-08-31T21:55:03Z Indexed on 2012/09/01 3:50 UTC
Read the original article Hit count: 216

Filed under:
|

I have drawn an asteroid object manually , and would like to get its center/radius by a specific equation. I think I can get them by calculated and hard-coded values.

The code to draw the asteroid:

void Asteroid::Draw()
{
    float ratio = app::getWindowWidth()/app::getWindowHeight();
    gl::pushMatrices();
    gl::translate(m_Pos*ratio);

    gl::scale(3.5*ratio,3.5*ratio,3.5*ratio);
    gl::color(ci::Color(1,1,1));

    gl::drawLine(Vec2f(-15,0),Vec2f(-10,-5));
    gl::drawLine(Vec2f(-10,-5),Vec2f(-5,-5));
    gl::drawLine(Vec2f(-5,-5),Vec2f(-5,-8));
    gl::drawLine(Vec2f(-5,-8),Vec2f(5,-8));
    gl::drawLine(Vec2f(5,-8),Vec2f(5,-5));
    gl::drawLine(Vec2f(5,-5),Vec2f(10,-5));
    gl::drawLine(Vec2f(10,-5),Vec2f(15,0));
    gl::drawLine(Vec2f(15,0),Vec2f(10,5));
    gl::drawLine(Vec2f(10,5),Vec2f(-10,5));
    gl::drawLine(Vec2f(-10,5),Vec2f(-10,5));
    gl::drawLine(Vec2f(-15,0),Vec2f(-10,5));

    gl::popMatrices();

}

According to the answer I have written that code to calculate the radius, is it correct or not ?

cinder::Vec2f Asteroid::getCenter()
{
    return ci::Vec2f(m_Pos.x, m_Pos.y);
}

double Asteroid::getRadius()
{
    ci::Vec2f  _vec = (getCenter()- Vec2f(15,5));
    return _vec.length()*0.3f; 
}

enter image description here

© Game Development or respective owner

Related posts about c++

Related posts about math