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 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来