Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

float ratio = (float)app::getWindowWidth()/app::getWindowHeight();
gl::pushMatrices();

gl::translate(m_Pos*ratio);

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

    gl::drawLine(Vec2f(20,0),Vec2f(80,0));
    gl::drawLine(Vec2f(80,0),Vec2f(100,20));
    gl::drawLine(Vec2f(100,20),Vec2f(100,50));
    gl::drawLine(Vec2f(100,50),Vec2f(60,100));
    gl::drawLine(Vec2f(60,100),Vec2f(40,100));
    gl::drawLine(Vec2f(40,100),Vec2f(50,70));
    gl::drawLine(Vec2f(50,70),Vec2f(25,90));
    gl::drawLine(Vec2f(25,90),Vec2f(0,70));
    gl::drawLine(Vec2f(0,70),Vec2f(20,40));
    gl::drawLine(Vec2f(20,40),Vec2f(0,20));
    gl::drawLine(Vec2f(0,20),Vec2f(20,0));

gl::popMatrices();

gl::pushMatrices();

float radius = getRadius();

Vec2f center = Vec2f(m_Pos.x+50, m_Pos.y+50);

gl::drawStrokedCircle(center*ratio, 10);
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; 
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

To get the center of the extents, take the max and min of both the x and y values, add them together and divide by two.

In your trivial example:

XMin = -15
XMax = 15
YMin = -5
YMax = 5

-15 + 15 = 0, 0/2 = 0
-5 + 5 = = 0, 0/2 = 0

So obviously the center is at (0,0)

The radius of the bounding circle is the largest distance from the center to a point that makes up the shape.

share|improve this answer
    
what about the radius of the bounding circle ? –  Ahmed Saleh Aug 31 '12 at 22:24
    
See my edit, you can check each point or better, check the points that contain one of the maximum x/y values. –  Byte56 Aug 31 '12 at 22:28
    
In case of the object is moving, the center will be m_Pos+0 correct ? –  Ahmed Saleh Aug 31 '12 at 22:30
    
I have edited my answer, it seems getradius is wrong, because it returns a different radius at each frame.. why ? –  Ahmed Saleh Aug 31 '12 at 22:44
1  
Yes, I suggest you read through this blog series. The equation seems strange. You're calculating two points, then finding the distance between those two points. The first point is your center and the second point is the farthest extent from your center. blog.wolfire.com/2009/07/… –  Byte56 Aug 31 '12 at 23:11
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.