zoukankan      html  css  js  c++  java
  • 旋转后点的坐标

    二维空间中,给定一个点与一个角度,求其绕另一个点旋转后的坐标。

    公式如下,(x1,y1)为要转的点,(x2,y2)为中心点,

    x=(x1-x2)cosθ-(y1-y2)sinθ+x2

    y=(y1-y2)cosθ+(x1-x2)sinθ+y2

    c++实现

    void rotate(Point src, Point & dst, float angle)
    {
           float theta = angle / 180 * 3.1415926;
           int x1,x2,y1,y2;
           x1 = src.x;  
           x2 = dst.x;
           y1 = src.y; 
           y2 = dst.y;
           dst.x = (x1 - x2) * cos(theta) - (y1 -y2) * sin(theta) + x2;
           dst.y = (y1 - y2) * cos(theta) + (x1 - x2) * sin(theta) + y2;
    
    }
    void Location_Rotation(Point2f* Src_Location, Point2f* Dst_Location, float angle, Mat& input)
    {
    	if (angle != 0)
    	{
    		angle *= CV_PI / 180;
    
    		Point2f center((float)input.cols*0.5, (float)input.rows*0.5);
    
    		float alpha = cos(angle);
    		float beta = sin(angle);
    
    		Mat M(2, 3, CV_32F);
    
    		float* m = (float*)M.data;
    
    		m[0] = alpha;
    		m[1] = beta;
    		m[2] = (1 - alpha)*center.x - beta*center.y;
    		m[3] = -beta;
    		m[4] = alpha;
    		m[5] = beta*center.x + (1 - alpha)*center.y;
    
    		
    		Point2f source = Src_Location[0];
    
    		Dst_Location[0].x = source.x*m[0] + source.y*m[1] + m[2];
    		Dst_Location[0].y = source.x*m[3] + source.y*m[4] + m[5];
    		
    	}
    	else
    	{
    		
    		Dst_Location[0] = Src_Location[0];
    		
    	}
    }
    

      

  • 相关阅读:
    USACO Name That Number
    USACO Milking Cows
    hdu 1540 Tunnel Warfare (线段树维护左右最长连续区间)
    Contest 1
    JNU周练1026
    树形DP
    Python和C扩展实现方法
    Python模拟C++输出流
    SkipList算法实现
    Python 迭代dict 效率
  • 原文地址:https://www.cnblogs.com/klitech/p/7058086.html
Copyright © 2011-2022 走看看