-
Notifications
You must be signed in to change notification settings - Fork 7
/
RFGeometry.m
132 lines (107 loc) · 3.49 KB
/
RFGeometry.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#import "RFGeometry.h"
#import <stdio.h>
const CGPoint CGPointNotChange = {RFMathNotChange, RFMathNotChange};
const CGRect CGRectNotChange = {{RFMathNotChange, RFMathNotChange}, {RFMathNotChange, RFMathNotChange}};
const CGSize CGSizeNotChange = {RFMathNotChange, RFMathNotChange};
#pragma mark CGPoint
CGPoint CGPointMid(CGPoint a, CGPoint b) {
return CGPointMake((a.x+b.x)/2, (a.y+b.y)/2);
}
CGFloat CGPointDistance(CGPoint a, CGPoint b) {
return (CGFloat)sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
CGPoint CGPointAtLineRatio(CGPoint start, CGPoint end, CGFloat ratio) {
return CGPointMake(start.x + (end.x-start.x)*ratio, start.y + (end.y-start.y)*ratio);
}
CGPoint CGPointOfRectCenter(CGRect a) {
return CGPointMake(CGRectGetMidX(a), CGRectGetMidY(a));
}
#pragma mark CGSize
CGSize CGSizeFromPoints(CGPoint start, CGPoint end) {
return CGSizeMake(end.x-start.x, end.y-start.y);
}
CGSize CGSizeScaled(CGSize original, double scale) {
return CGSizeMake(original.width*scale, original.height*scale);
}
#pragma mark CGRect
CGRect CGRectMakeWithPoints(CGPoint a, CGPoint b) {
CGFloat x = fmin(a.x, b.x);
CGFloat y = fmin(a.y, b.y);
CGFloat width = fabs(a.x-b.x);
CGFloat height = fabs(a.y-b.y);
return CGRectMake(x, y, width, height);
}
CGRect CGRectMakeWithCenterAndSize(CGPoint centerPoint, CGSize rectSize) {
CGFloat x = centerPoint.x - rectSize.width/2;
CGFloat y = centerPoint.y - rectSize.height/2;
return (CGRect){{x, y}, rectSize};
}
CGRect CGRectResize(CGRect original, CGSize newSize, RFResizeAnchor resizeAnchor) {
CGFloat x = original.origin.x;
CGFloat y = original.origin.y;
CGFloat w = original.size.width;
CGFloat h = original.size.height;
CGFloat wNew = newSize.width;
CGFloat hNew = newSize.height;
switch (resizeAnchor) {
case RFResizeAnchorCenter:
x += (w - wNew)/2;
y += (h - hNew)/2;
break;
case RFResizeAnchorTop:
x += (w - wNew)/2;
break;
case RFResizeAnchorBottom:
x += (w - wNew)/2;
y += (h - hNew);
break;
case RFResizeAnchorLeft:
y += (h - hNew)/2;
break;
case RFResizeAnchorRight:
x += (w - wNew);
y += (h - hNew)/2;
break;
case RFResizeAnchorTopLeft:
break;
case RFResizeAnchorTopRight:
x += (w - wNew);
break;
case RFResizeAnchorBottomLeft:
y += (h - hNew);
break;
case RFResizeAnchorBottomRight:
x += (w - wNew);
y += (h - hNew);
break;
default:
dout_warning(@"CGRectResize >> Unknow RFResizeAnchor.")
return CGRectZero;
}
return CGRectMake(x, y, wNew, hNew);
}
CGRect CGRectChange(CGRect original, RFCGRectChangeFlag flag, CGFloat newValue) {
CGRect rect = original;
switch (flag) {
case RFCGRectChangeX:
rect.origin.x = newValue;
break;
case RFCGRectChangeY:
rect.origin.y = newValue;
break;
case RFCGRectChangeWidth:
rect.size.width = newValue;
break;
case RFCGRectChangeHeight:
rect.size.height = newValue;
break;
}
return rect;
}
#pragma mark CGAngle
CGAngle CGAngleFromPoints(CGPoint start, CGPoint end) {
return atan2(end.y-start.y, end.x-start.x);
}
CGFloat CGAngleDegrees(CGAngle a) {
return a/M_PI * 180.;
}