如何镜像CGPoint

How to mirror a CGPoint

本文关键字:CGPoint 镜像 何镜像      更新时间:2023-10-16

我不想将触摸点围绕屏幕中心旋转180度(镜像它)。我尝试了ccpRotateByAngle(screenCenter, touchPoint, 180),但它给了我错误的数据。

三角学:

// get your center in some way, here I'm using fixed coordinates 'cuz I'm lazy
CGPoint screenCenter = ccp(240, 160);
// this creates the vector between center and touch location
CGPoint touchPointToCenter = ccpSub(screenCenter, touchPoint);
// just add the vector to screen center and you mirrored it around center
CCPoint mirror = ccpAdd(screenCenter, touchPointToCenter);

例如,假设你的触摸点是200,200:

touchPointToCenter: {240, 160} - {200, 200} = {40, -40}
mirror: {240, 160} + {40, -40} = {280, 120}

镜像点为280,120 .

注:我使用的函数来自cocos2d-iphone。我假设它们也存在于cocos2d-x中。它们的名字可能不同,我不确定。您也可以像我在示例中那样"手动"运行计算。