在圆圈周围放置一个图像

时间:2011-03-13 15:58:32

标签: c++ graphics openframeworks

我正在使用Openframeworks为Reactivision开发,我需要在圆圈周围放置一个ofImage(可以放在屏幕的任何位置),并且还可以按特定角度旋转它。我提供了一个图片来帮助解决这个问题。

http://i.stack.imgur.com/WCyPu.png

如你所见,x和y轴从0到1,1,我在屏幕中间有一个圆圈(中心位于0.5,0.5),半径为0.42(所以它没有填满屏幕。)

最终,用户在屏幕上放置一个图像(在这种情况下,红色笑脸),位置为0.2,0.2。它的宽度和高度都是0.08,旋转中心位于左上角(像往常一样)。

我需要做的就是拿出那个笑脸,把它放在圆圈周围,在最近的位置(它可以跟随半径,如果它变得更容易)它将能够旋转它,所以它将是面对圆圈的中心。

我还需要找到笑脸“鼻子”,距离图像轴0.04 0.04,移动它并旋转它以保持笑脸鼻子而不是空白点。红色笑脸最好的应该是绿色笑脸。

我的三角学有点生疏,我最好的方法不起作用。我在构造函数中定义它的最终位置,并在draw()步骤中旋转sprite。

Smiley::Smiley(int _id, float x, float y)
{
    smileyImg.loadImage("smiley.png");

    float tangent = (0.5-x)/(0.5-y);
    //angle is a private float variable; 
    angle = atanf(tangent);
    //those 0.06 just lets me move the smiley a little over the circle itself
    x = 0.5+cos(angle)*(RADIUS+0.06);
    y = 0.5+sin(angle)*(RADIUS+0.06);
    float xNose = 0.5+cos(angle)*(RADIUS+0.06);
    float yNose = 0.5+sin(angle)*(RADIUS+0.06);
    angle = ofRadToDeg(angle);
    //pos and nose are Vector3 classes which hold three floats. Nothing too much important here
    pos.set(x, y, 0.0);
    nose.set(xNose, yNose, 0.0);
}

void Smiley::draw()
{
    ofPushMatrix();
    ofTranslate(pos.x,pos.y);
    ofRotateZ(angle);
    ofTranslate(-pos.x,-pos.y);
    ofSetColor(255,255,255);
    ofEnableAlphaBlending();
    smileyImg.draw(pos.x,pos.y,0.08,0.08);
    ofDisableAlphaBlending();
    ofPopMatrix();
}

有人可以帮帮我吗?非常感谢提前。

编辑:在调整@datenwolf解决方案后,我得到的位置是完美的,但是当我将旋转和平移(或只是翻译)放入变换矩阵时,它会使笑脸消失。我很确定我对矩阵做错了:

Smiley::Smiley(int _id, float x, float y)
{
    smileyImg.loadImage("smiley.png");

    float distance = sqrt( pow((x - 0.46),2) + pow((y - 0.42),2));
    Vector3 director((x - 0.46)/ distance, (y - 0.42)/ distance, 0.0);
    pos.x = 0.46 + RADIUS * director.x;
    pos.y = 0.42 + RADIUS * director.y;

    float distanceNose = sqrt( pow((x - 0.46),2) + pow((y - 0.42),2));
    Vector3 noseDirector((x - 0.46)/ distanceNose, (y - 0.42)/ distanceNose, 0.0);
    nose.x = 0.46 + RADIUS * noseDirector.x;
    nose.y = 0.42 + RADIUS * noseDirector.y;

    /* matrix is a float matrix[16]. I think the indexing is ok, but even going from
    0 4 8 12... to 0 1 2 3..., it still doesn't work*/
    matrix[0] = -director.y; matrix[4] = director.x; matrix[8] = 0.0; matrix[12] = pos.x;
    matrix[1] = director.x; matrix[5] = director.y; matrix[9] = 0.0; matrix[13] = pos.y;
    matrix[2] = 0.0; matrix[6] = 0.0; matrix[10] = 1.0; matrix[14] = 0.0;
    matrix[3] = 0.0; matrix[7] = 0.0; matrix[11] = 0.0; matrix[15] = 1.0;
}

void Smiley::draw()
{
    ofPushMatrix();
    glLoadMatrixf(matrix);
    ofSetColor(255,255,255);
    ofEnableAlphaBlending();
    // Now that I have a transformation matrix that also transposes, I don't need to define the coordinates here, so I put 0.0,0.0
    noseImg.draw(0.0,0.0,0.08,0.08);
    ofDisableAlphaBlending();
    ofPopMatrix();
}

此外,我需要根据每个笑脸旋转鼻子位置。您认为哪种方式最好?非常感谢你

1 个答案:

答案 0 :(得分:1)

像往常一样,你不需要三角学。让C成为圆的中心,r半径和P用户点击的点。然后投射到圆圈的点击的位置U是

U = C + r * (P - C)/|P - C|

,其中

| A | = sqrt(A_x² + A_y² + …);

所以如果你有两个维度

U.x = C.x + r * (P.x - C.x)/sqrt( (P.x - C.x)² + (P.y - C.y)² )
U.y = C.y + r * (P.y - C.y)/sqrt( (P.x - C.x)² + (P.y - C.y)² )

旋转可以用旋转矩阵R来描述,旋转矩阵R包括从圆C到P的方向和垂直于它的T。所以大胖编辑:规范化方向:

D = P - C = (P.x - C.x , P.y - C.y)/sqrt( (P.x - C.x)² + (P.y - C.y)² )
T = Z × D 

另请注意,您可以根据U

撰写D
U = C + r*D

用它来构造一个旋转矩阵

    | T.x D.x |
R = |         |
    | T.y D.y |

R是一个旋转矩阵,它遵循T.y == D.x并评估T.x

T.x = -P.y + C.y = -D.y

所以

    | -D.y D.x |
R = |          |
    |  D.x D.y |

大脂肪编辑:我在矩阵的最后一列使用了错误的组件。 是U. {x,y} 不是D. {x.y}。我很抱歉!

我们可以把它放到一个完整的转换矩阵M:

    | -D.y D.x   0 U.x |   | -D.y D.x   0 (C + r*D.x) |
M = |  D.x D.y   0 U.y | = |  D.x D.y   0 (C + r*D.y) |
    |    0   0   1   0 |   |    0   0   1      0      |
    |    0   0   0   1 |   |    0   0   0      1      |

您可以使用glLoadMatrix加载此矩阵以放置精灵。