-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathUtil.ts
More file actions
135 lines (128 loc) · 3.65 KB
/
Copy pathPathUtil.ts
File metadata and controls
135 lines (128 loc) · 3.65 KB
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
133
134
135
interface PointF {
x: number;
y: number;
}
export function calculatePath(
center: PointF,
side: number,
size: number,
corner: number,
offset: number
) {
if (corner > 0) {
return calculatePathWithCorner(center, side, size, corner, offset);
} else {
return calculatePathWithoutCorner(center, side, size, offset);
}
}
function calculatePathWithoutCorner(
center: PointF,
side: number,
size: number,
offset: number
): string {
const points: Array<PointF> = regularPolygonCoordinates(center, side, size, offset);
let path: string = '';
for (let i = 0; i < points.length; i++) {
const p: PointF = points[i];
if (i == 0) {
// path.moveTo(point.x, point.y);
path = `${path} M ${p.x}, ${p.y} `;
} else {
// path.lineTo(point.x, point.y);
path = `${path} L ${p.x}, ${p.y} `;
}
}
// path.close();
path = `${path} Z `;
return path;
}
function calculatePathWithCorner(
center: PointF,
side: number,
size: number,
corner: number,
offset: number
): string {
const points: Array<PointF> = regularPolygonCoordinatesWithRoundedCorner(
center,
side,
size,
corner,
offset
);
let path: string = '';
let temPoint: PointF = null;
for (let i = 0; i < points.length; i++) {
const p: PointF = points[i];
if (i == 0) {
// path.moveTo(p.x, p.y);
path = `${path} M ${p.x}, ${p.y} `;
} else {
switch (i % 3) {
case 0:
// path.lineTo(p.x, p.y);
path = `${path} L ${p.x}, ${p.y} `;
break;
case 1:
temPoint = p;
break;
case 2:
// path.quadTo(temPoint.x, temPoint.y, p.x, p.y);
path = `${path} Q ${temPoint.x}, ${temPoint.y}, ${p.x}, ${p.y} `;
break;
default:
break;
}
}
}
// path.close();
path = `${path} Z `;
return path;
}
export function regularPolygonCoordinatesWithRoundedCorner(
center: PointF,
side: number,
radius: number,
corner: number,
offset: number
): Array<PointF> {
const points: Array<PointF> = [];
const CAB = (2 * Math.PI) / side;
const EC = radius * Math.sin(CAB / 2);
const AE = radius * Math.cos(CAB / 2);
const ED = EC - corner;
const EAD = Math.atan(ED / AE);
const DAC = CAB / 2 - EAD;
const AD = Math.sqrt(AE ** 2 + ED ** 2);
for (let i = 0; i < side; i++) {
const angle = i * CAB;
const point = vertexCoordinates(center, radius, angle, offset);
const leftPoint = vertexCoordinates(center, AD, angle - DAC, offset);
const rightPoint = vertexCoordinates(center, AD, angle + DAC, offset);
points.push(leftPoint);
points.push(point);
points.push(rightPoint);
}
return points;
}
export function regularPolygonCoordinates(
center: PointF,
side: number,
radius: number,
offset: number
): Array<PointF> {
const points: Array<PointF> = [];
for (let i = 0; i < side; i++) {
const angle = ((i * (360.0 / side)) / 180) * Math.PI;
const point: PointF = vertexCoordinates(center, radius, angle, offset);
points.push(point);
}
return points;
}
function vertexCoordinates(center: PointF, radius: number, angle: number, offset: number): PointF {
return {
x: center.x + radius * Math.cos(angle + offset),
y: center.y + radius * Math.sin(angle + offset),
};
}