0

我正在开发一个从画布中获取这些数据的应用程序:

windowPosition (CGFloat) previousWindowPosition (CGFloat) timeStamp (NSTimeInterval)

目前我有一个获取两点之间距离的公式

-(float)calculateDistanceFrom:(CGPoint)point1 to:(CGPoint)point2
{
    CGFloat xDist = (point2.x - point1.x);
    CGFloat yDist = (point2.y - point1.y);
    return sqrt((xDist * xDist) + (yDist * yDist));
}

我需要得到的是获得行程速度的公式,有没有办法获得它???

提前致谢


编辑:好的,这是我为这种需要开发的公式,如果需要,请帮助我分析和改进它:

所需变量:

float           speedStroke;      //stroke speed
float           strokeDistance;      //stroke distance
NSDate          *startTimeStroke;  //records begin of stroke
CGPoint         prevPointStroke;  //saves previous point of stroke 

每次画布捕获手写笔事件(如绘图)时,都会执行下一个代码块,这是逻辑开始的地方:

//start running the line of time
if (startTimeStroke == nil)  startTimeStroke = [NSDate date];

//this applies when the first point of the line is drawn
        if (prevPointStroke.x == 0 && prevPointStroke.y == 0)
            speedStroke = [self calculateSpeedfromPointA:[touch locationInView:self]
                                                toPointB:[touch locationInView:self]
                                               startTime:startTimeStroke];

        // if the line has already begun
        if (prevPointStroke.x != 0 && prevPointStroke.y != 0)
            speedStroke = [self calculateSpeedfromPointA:prevPointStroke
                                                toPointB:[touch locationInView:self]
                                               startTime:startTimeStroke];
        //then saves the current point for next calculation
        prevPointStroke = [touch locationInView:self];

而这另一个块是获得速度的公式

-(float)calculateSpeedfromPointA:(CGPoint)puntoA
                        toPointB:(CGPoint)puntoB
                       startTime:(NSDate *)startTime{

    //1. calculates distance
    CGFloat Xdist = (puntoB.x - puntoA.x);
    CGFloat Ydist = (puntoB.y - puntoA.y);

    strokeDistance += sqrt((Xdist * Xdist) + (Ydist * Ydist));;



    //2. calculates time
    float strokeTime = [[NSDate date] timeIntervalSinceDate:startTimeStroke];

    //3. calculates and returns speed
    return (strokeDistance / strokeTime);
}

如果我没有错,结果将以像素/秒的形式获得,对吗?

提前感谢您的支持

4

0 回答 0