win32让RECT旋转 [英] win32 make RECT spin

查看:79
本文介绍了win32让RECT旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为RECT结构定义的对象获得一个简单的旋转效果,我在数学上遇到了一些麻烦。



这是我目前所拥有的



  int  moveAroundHimself(RECT& r,RECT limit, double 步骤, int  id){
< span class =code-keyword> static double info [ 100 ] [ 10 ] = {};
if (!info [id] [ 0 ]){
info [ id] [ 0 ] = r.left;
info [id] [ 1 ] = r.right;
info [id] [ 2 ] = r.top;
info [id] [ 3 ] = r.bottom;
info [id] [ 4 ] = 0 ;
info [id] [ 5 ] =(r.right + r.left)/ 2; // xCenter
info [id] [ 6 ] =(r.bottom + r.top)/ 2; // yCenter

}
// xRot = xCenter + cos(角度)*(x - xCenter) - sin(角度)*(y - yCenter)
// yRot = yCenter + sin(Angle)*(x - xCenter)+ cos(Angle)*( y - yCenter)
r.left = info [id] [ 5 ] + cos(info [id] [ 4 ])*(info [id] [ 0 ] - info [id] [ 5 ]) - sin(info [id] [ 4 ])*(info [id] [ 2 ] - info [id] [ 6 ]);
r.right = info [id] [ 5 ] + cos(info [id] [ 4 ])*(info [id] [ 1 ] - info [id] [ 5 ]) - sin(info [id] [ 4 ])*(info [id] [ 3 ] - info [id] [ 6 ]);
r.top = info [id] [ 6 ] + sin(info [id] [ 4 ])*(info [id] [ 0 ] - info [id] [ 5 ]) + cos(info [id] [ 4 ])*(info [id] [ 2 ] - info [id] [ 6 ]);
r.bottom = info [id] [ 6 ] + sin(info [id] [ 4 ])*(info [id] [ 1 ] - info [id] [ 5 ]) + cos(info [id] [ 4 ])*(info [id] [ 3 ] - info [id] [ 6 ]);


info [id] [ 4 ] + = step;

return 0 ;

}



你现在可以忽略限制矩形。



任意关于什么是错的想法?​​

解决方案

这是一个计算与矩形'中心相关的旋转的版本。

< pre lang =c ++> RECT RotateRect( const Rect& r, double angle)
{
double sina = sin(angle);
double cosa = cos(angle);

double xc =(r.left + r.right)* 0 5 ;
double yc =(r.top + r.bottom)* 0 5 ;

double w2 = r.right - xc; // 宽度的一半
double h2 = r.bottom - yc; // 高度的一半

double dx = cosa * w2 - sina * h2;
double dy = sina * w2 + cosa * h2;

RECT q;
q.left = xc - dx;
q.top = yc - dy;
q.right = xc + dx;
q.bottom = yc + dy;
return q;
}



我删除了那个实际属于调用代码的静态数组。该函数返回旋转的矩形。我没有花时间调试那段代码,把它作为练习给你。



注意角落的移动是对称的。因此,我们只需要计算一次新的角矢量,然后从中心逐步减去它。


I''m trying to get a simple spin effect for an object defined by RECT structure and I''m having a little bit of trouble with the math of it.

Here is what i have so far

int moveAroundHimself(RECT &r, RECT limit, double step, int id) {
	static double info[100][10] = {};
	if(!info[id][0]) {
		info[id][0] = r.left;
		info[id][1] = r.right;
		info[id][2] = r.top;
		info[id][3] = r.bottom;
		info[id][4] = 0;
		info[id][5] = (r.right + r.left)/2; // xCenter
		info[id][6] = (r.bottom + r.top)/2; // yCenter

	}
	// xRot = xCenter + cos(Angle) * (x - xCenter) - sin(Angle) * (y - yCenter)
	// yRot = yCenter + sin(Angle) * (x - xCenter) + cos(Angle) * (y - yCenter)
	r.left = info[id][5] + cos(info[id][4]) * (info[id][0] - info[id][5]) - sin(info[id][4]) * (info[id][2] - info[id][6]);
	r.right = info[id][5] + cos(info[id][4]) * (info[id][1] - info[id][5]) - sin(info[id][4]) * (info[id][3] - info[id][6]);
	r.top = info[id][6] + sin(info[id][4]) * (info[id][0] - info[id][5]) + cos(info[id][4]) * (info[id][2] - info[id][6]);
	r.bottom = info[id][6] + sin(info[id][4]) * (info[id][1] - info[id][5]) + cos(info[id][4]) * (info[id][3] - info[id][6]);


	info[id][4] += step;

	return 0;

}


You can ignore the limit rect for now.

Any idea on what is wrong ?

解决方案

Here is a version that calculates the rotation in relation to the rectangle''s center.

RECT RotateRect (const Rect& r, double angle)
{
    double sina = sin (angle);
    double cosa = cos (angle);

    double xc = (r.left + r.right) * 0.5;
    double yc = (r.top + r.bottom) * 0.5;

    double w2 = r.right - xc;   // half the width
    double h2 = r.bottom - yc;  // half the height

    double dx = cosa * w2 - sina * h2;
    double dy = sina * w2 + cosa * h2;

    RECT q;
    q.left   = xc - dx;
    q.top    = yc - dy;
    q.right  = xc + dx;
    q.bottom = yc + dy;
    return q;
}


I have removed that static array, which actually belongs into the calling code. The function returns the rotated rectangle. I didn''t take the time to debug that code, leaving that as an exercise for you.

Note that movement of the corners is symmetrical. Hence we need to calculate the new corner vector only once, and then add repsectively subtract it from the center.


这篇关于win32让RECT旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆