• 计算一/二元一次方程的类(用于动画控制)


    计算一/二元一次方程的类(用于动画控制)

    源码:

    //
    //  YXMath.h
    //
    //  http://home.cnblogs.com/u/YouXianMing/
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface YXMath : NSObject
    
    /*---- 计算一元一次方程 ----
    
        y = kX + b
     
     ------------------------*/
    @property (nonatomic, assign, readonly) CGFloat  k;
    @property (nonatomic, assign, readonly) CGFloat  b;
    - (instancetype)initWithLinearFunctionPointA:(CGPoint)pointA
                                          pointB:(CGPoint)pointB;
    
    
    /*---- 计算一元二次方程普通式 ----
    
     y = aX^2 + bX + c
     
     ----------------------------*/
    @property (nonatomic, assign, readonly) CGFloat  A;
    @property (nonatomic, assign, readonly) CGFloat  B;
    @property (nonatomic, assign, readonly) CGFloat  C;
    - (instancetype)initWithQuadraticFunctionPointA:(CGPoint)pointA
                                             pointB:(CGPoint)pointB
                                             pointC:(CGPoint)pointC;
    
    /*---- 计算一元二次方程顶点式 ----
     
     y = a(X - h)^2 + k
     注意:顶点为(h, k)
     
     ----------------------------*/
    @property (nonatomic, assign, readonly) CGFloat  a;
    - (instancetype)initWithQuadraticFunctionPointApex:(CGPoint)apex
                                                 point:(CGPoint)point;
    
    
    @end
    //
    //  YXMath.m
    //
    //  http://home.cnblogs.com/u/YouXianMing/
    //
    //  Copyright (c) 2014年 Y.X. All rights reserved.
    //
    
    #import "YXMath.h"
    
    @implementation YXMath
    
    - (instancetype)initWithQuadraticFunctionPointA:(CGPoint)pointA
                                             pointB:(CGPoint)pointB
                                             pointC:(CGPoint)pointC
    {
        self = [super init];
        if (self)
        {
            CGFloat x1 = pointA.x; CGFloat y1 = pointA.y;
            CGFloat x2 = pointB.x; CGFloat y2 = pointB.y;
            CGFloat x3 = pointC.x; CGFloat y3 = pointC.y;
            
            _A = calculateA(x1, y1, x2, y2, x3, y3);
            _B = calculateB(x1, y1, x2, y2, x3, y3);
            _C = calculateC(x1, y1, x2, y2, x3, y3);
        }
        return self;
    }
    
    - (instancetype)initWithLinearFunctionPointA:(CGPoint)pointA
                                            pointB:(CGPoint)pointB
    {
        self = [super init];
        if (self)
        {
            CGFloat x1 = pointA.x; CGFloat y1 = pointA.y;
            CGFloat x2 = pointB.x; CGFloat y2 = pointB.y;
            
            _k = calculateSlope(x1, y1, x2, y2);
            _b = calculateConstant(x1, y1, x2, y2);
        }
        return self;
    }
    
    - (instancetype)initWithQuadraticFunctionPointApex:(CGPoint)apex
                                                 point:(CGPoint)point
    {
        self = [super init];
        if (self)
        {
            CGFloat h  = apex.x; CGFloat   k = apex.y;
            CGFloat x  = point.x; CGFloat  y = point.y;
            
            _a = (y - k)/((x - h)*(x - h));
        }
        return self;
    }
    
    #pragma mark - 计算常数a b c
    CGFloat calculateA(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat x3, CGFloat y3)
    {
        return ((y2 - y1)/(x2 - x1) - (y3 - y2)/(x3 - x2))/(x1 - x3);
    }
    
    CGFloat calculateB(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat x3, CGFloat y3)
    {
        return (y2 - y1)/(x2 - x1) - (((y2 - y1)/(x2 - x1) - (y3 - y2)/(x3 - x2))/(x1 - x3));
    }
    
    CGFloat calculateC(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat x3, CGFloat y3)
    {
        CGFloat a = calculateA(x1, y1, x2, y2, x3, y3);
        CGFloat b = calculateB(x1, y1, x2, y2, x3, y3);
        return y1 - a*x1*x1 - b*x1;
    }
    
    #pragma mark - 计算斜率 k
    CGFloat calculateSlope(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
    {
        return (y2 - y1) / (x2 - x1);
    }
    
    #pragma mark - 计算常数 b
    CGFloat calculateConstant(CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2)
    {
        return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
    }
    
    
    @end

  • 相关阅读:
    C语身教程第七章:结构与勾结(3)
    C言语教程第八章:枚举,位运算(4)
    C说话教程第七章:机关与连合(1)
    C语身教程第七章:结构与连系(8)
    C语身教程第七章:布局与勾结(2)
    C言语教程第十章:文件(1)
    C语身教程第八章:列举,位运算(3)
    C言语教程第七章:机关与团结(6)
    C语身教程第六章:指针(7)
    C言语教程第七章:构造与联合(4)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4095964.html
Copyright © 2020-2023  润新知