# 一、怎麼繪製曲線

``````glDrawArrays(GL_POINTS, 0, self.vertexCount);
``````

``````attribute vec4 Position;
uniform float Size;
void main (void) {
gl_Position = Position;
gl_PointSize = Size;
}
``````

``````precision highp float;
uniform float R;
uniform float G;
uniform float B;
uniform float A;
uniform sampler2D Texture;
void main (void) {
vec4 mask = texture2D(Texture, vec2(gl_PointCoord.x, 1.0 - gl_PointCoord.y));
gl_FragColor = A * vec4(R, G, B, 1.0) * mask;
}
``````

``````- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
}
``````

# 三、怎麼使曲線變平滑

1. 不同貝塞爾曲線的長度不一樣，使用同一個 `n` 值，算出來的點的疏密程度肯定不同。
2. 由於貝塞爾曲線隨著 `t` 增長，曲線長度的增長並不是線性的。按照我們上面的算法，最終會得到的結果是 兩頭比較稀疏，中間比較密集

# 四、怎麼生成均勻的點序列

``````+ (NSArray <NSValue *>*)pointsWithFrom:(CGPoint)from
to:(CGPoint)to
control:(CGPoint)control
pointSize:(CGFloat)pointSize;
``````

# 五、繪畫板功能實現

1、顏色混合

``````glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
``````

2、筆觸調整

3、橡皮擦

`GLPaintView` 在初始化的時候，需要傳入一個背景色參數，當用戶切換到橡皮擦功能的時候，內部只是單純地將畫筆的顏色切換成背景色，於是就產生了橡皮擦的效果。

4、撤銷重做

``````@interface MFPaintModel : NSObject
/// 筆刷尺寸
@property (nonatomic, assign) CGFloat brushSize;
/// 筆刷顏色
@property (nonatomic, strong) UIColor *brushColor;
/// 筆刷模式
@property (nonatomic, assign) GLPaintViewBrushMode brushMode;
/// 筆觸紋理圖片文件名
@property (nonatomic, copy) NSString *brushImageName;
/// 點序列
@property (nonatomic, copy) NSArray<NSValue *> *points;
@end
``````

``````- (void)undo {
if ([self.operationStack isEmpty]) {
return;
}
MFPaintModel *model = self.operationStack.topModel;
[self.operationStack popModel];
[self.undoOperationStack pushModel:model];
[self reDraw];
}
- (void)redo {
if ([self.undoOperationStack isEmpty]) {
return;
}
MFPaintModel *model = self.undoOperationStack.topModel;
[self.undoOperationStack popModel];
[self.operationStack pushModel:model];
[self drawModel:model];
}
``````

CID規範