dongzongxun8491 2014-01-23 04:32
浏览 16
已采纳

C ++到PHP的翻译

I'm trying to understand the answer (copy/pasted below) that is laid out here: https://stackoverflow.com/a/3838294/1541165

The problem is that it's in C++ and I want to apply the described solution in PHP.

Can someone help with just a bit of the translation? Like what would A.x - B.x look like in PHP?

first step; move the origin point.

x' = A.x - B.x y' = A.y - B.y

second step, perform rotation

x'' = x' * cos(C) - y' * sin(C) = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C)

y'' = y' * cos(C) + x' * sin(C) = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C)

third and final step, move back the coordinate frame

x''' = x'' + B.x = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.x

y''' = y'' + B.y = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

And presto! we have our rotation formula. I'll give it to you without all those calculations:

Rotating a point A around point B by angle C

A.x' = (A.x-B.x) * cos(C) - (A.y-B.y) * sin(C) + B.x

A.y' = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

  • 写回答

1条回答 默认 最新

  • duanhuantong8278 2014-01-23 05:05
    关注

    A and B are just C++ structures containing two floats, to achieve this in PHP, you'd make a simple "Point" class:

    class Point {
        public $X;
        public $Y;
        public function __construct($x = 0, $y = 0) {
            $this->X = $x;
            $this->Y = $y;
        }
    }
    

    Once you have this class, you can create points A and B like so:

    $A = new Point(0, 1);
    $B = new Point(1, 0);
    

    With these two points, and a rotation angle $C in radians:

    $C = 3.14;
    
    // The long way
    $x1 = $A->X - $B->X;
    $y1 = $A->Y - $B->Y;
    
    $sinC = sin($C);
    $cosC = cos($C);
    
    $x2 = $x1 * $cosC - $y1 * $sinC;
    $y2 = $y1 * $cosC + $x1 * $sinC;
    
    $resultX = $x2 + $B->X;
    $resultY = $y2 + $B->Y;
    
    // The quick way
    $sinC = sin($C);
    $cosC = cos($C);
    
    $diff = new Point($A->X - $B->X, $A->Y - $B->Y);
    $result = new Point($diff->X * $cosC - $diff->Y * $sinC + $B->X, 
                        $diff->Y * $cosC + $diff->X * $sinC + $B->Y);
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档