-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSBezierPath+SMDrawing.m
110 lines (87 loc) · 3.29 KB
/
NSBezierPath+SMDrawing.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// NSBezierPath+SMDrawing.m
// SplineSubdivision
//
// Created by Stephan Michels on 21.07.12.
//
//
#import "NSBezierPath+SMDrawing.h"
@implementation NSBezierPath (SMDrawing)
- (void)drawTangents {
CGFloat dashPattern[2];
dashPattern[0] = 5.0f;
dashPattern[1] = 2.0f;
// draw tangents to the control points
NSPoint points[3];
NSPoint previousPoint;
NSUInteger numberOfElements = [self elementCount];
for (NSUInteger elementIndex = 0; elementIndex < numberOfElements; elementIndex++) {
NSBezierPathElement element = [self elementAtIndex:(NSInteger)elementIndex associatedPoints:points];
switch (element) {
case NSMoveToBezierPathElement: {
previousPoint = points[0];
} break;
case NSLineToBezierPathElement: {
previousPoint = points[0];
} break;
case NSCurveToBezierPathElement: {
[[NSColor redColor] set];
NSBezierPath *path2 = [NSBezierPath bezierPath];
[path2 setLineDash:dashPattern count: 2 phase: 0.0];
[path2 moveToPoint:previousPoint];
[path2 lineToPoint:points[0]];
[path2 moveToPoint:points[1]];
[path2 lineToPoint:points[2]];
[path2 stroke];
previousPoint = points[2];
} break;
case NSClosePathBezierPathElement: {
} break;
default:
break;
}
}
}
- (void)drawHandles {
// draw handle for all points
NSPoint points[3];
NSPoint previousPoint;
NSUInteger numberOfElements = [self elementCount];
for (NSUInteger elementIndex = 0; elementIndex < numberOfElements; elementIndex++) {
NSBezierPathElement element = [self elementAtIndex:(NSInteger)elementIndex associatedPoints:points];
switch (element) {
case NSMoveToBezierPathElement: {
[[NSColor whiteColor] set];
[self drawHandleAtPoint:points[0]];
previousPoint = points[0];
} break;
case NSLineToBezierPathElement: {
[[NSColor whiteColor] set];
[self drawHandleAtPoint:points[0]];
previousPoint = points[0];
} break;
case NSCurveToBezierPathElement: {
[[NSColor redColor] set];
[self drawHandleAtPoint:points[0]];
[[NSColor redColor] set];
[self drawHandleAtPoint:points[1]];
[[NSColor whiteColor] set];
[self drawHandleAtPoint:points[2]];
previousPoint = points[2];
} break;
case NSClosePathBezierPathElement: {
} break;
default:
break;
}
}
}
- (void)drawHandleAtPoint:(NSPoint)point {
point.x = roundf(point.x + 0.5f) - 0.5f;
point.y = roundf(point.y + 0.5f) - 0.5f;
NSRect rect = NSMakeRect(point.x - 3.5f, point.y - 3.5f, 7.0f, 7.0f);
NSRectFill(rect);
[[NSColor blackColor] set];
NSFrameRect(rect);
}
@end