gpt4 book ai didi

ios - ccDrawline 直到触摸位置的一半 cocos2d

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:11 24 4
gpt4 key购买 nike

我的问题是我想画一条恰好是触摸位置一半的线即,我在 cctouchesmoved 上画了一条线,它从第一个位置到我的触摸位置画了一条线,但我的问题是我只需要显示该线直到触摸位置的一半这是我的代码

-(void)draw{
glEnable(GL_LINE_SMOOTH);
glLineWidth(3.0f); // set line width
glColor4f(0.8, 1.0, 0.76, 1.0); // set line color.
ccDrawLine(point1,Point2);
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

point1 = location;
Point2=CGPointMake(size.width/2, size.height/2);
}

任何帮助将不胜感激。

最佳答案

给出两个点,a 和 b,其中 a 是“原点”:

ccpMidPoint(a,b);

或者,更一般地说,您可以通过从 b 中减去 a,乘以所需的系数,然后重新添加 a 来获得沿直线的任意距离:

float percentageOfDistanceAlongLine = 0.5f;
CGPoint pointAlongLine = ccpMult( ccpSub(b, a), percentageOfDistanceAlongLine);
pointAlongLine = ccpAdd(a, pointAlongLine);

所以,在你的例子中,point2 = a, point1 = b

关于ios - ccDrawline 直到触摸位置的一半 cocos2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com