贝塞尔曲线
- 贝塞尔曲线公式
贝塞尔 -
y=y0·(1-t)³+3·y1·t·(1-t)²+3·y2·t²·(1-t)+y3·t³
x=x0·(1-t)³+3·x1·t·(1-t)²+3·x2·t²·(1-t)+x3·t³```
- 贝塞尔的四个控制点
贝塞尔的 四个 控制点
(0 , 0 ) (0 , 0.57) (0.44 , 1 ) (1 ,1);```
- 实现
.h
#import <Foundation/Foundation.h>
typedef struct
{
float x;
float y;
} WSPoint;
@interface WSBezier : NSObject
@property (nonatomic,assign) WSPoint wsStart; // 开始点
@property (nonatomic,assign) WSPoint wsFirst; // 第一个点
@property (nonatomic,assign) WSPoint wsSecond; // 第二个点
@property (nonatomic,assign) WSPoint wsEnd; // 终止点
- (WSPoint )pointWithdt:(float )dt; // 曲线赋值
@end```
- .m
import "WSBezier.h"
@implementation WSBezier
// 初始化
-
(instancetype)init{
if (self = [super init]) {
[self _initPoints];
}
return self;
}
// 初始化四个点
-
(void)_initPoints{
WSPoint start;
WSPoint first;
WSPoint second;
WSPoint end;
start.x = 0;
start.y = 0;
first.x = 0;
first.y = 0.57;
second.x = 0.44;
second.y = 1;
end.x = 1;
end.y = 1;// 赋值
self.wsStart = start;
self.wsFirst = first;
self.wsSecond = second;
self.wsEnd = end;
}
-
(WSPoint)pointWithdt:(float )dt{
WSPoint result;
float t = 1 - dt;
float tSqure = t * t;
float tCube = t * tSqure;
float dtSqure = dt * dt;
float dtCube = dtSqure * dt;result.x = self.wsStart.x * tCube + 3 * self.wsFirst.x * dt * tSqure + 3 * self.wsSecond.x * dtSqure * t + self.wsEnd.x * dtCube;
result.y = self.wsStart.y * tCube + 3 * self.wsFirst.y * dt * tSqure + 3 * self.wsSecond.y * dtSqure * t + self.wsEnd.y * dtCube;
return result;
}
@end```