Repository navigation
Expand file tree
/
Copy pathdrafting.ts
More file actions
337 lines (289 loc) · 12.3 KB
/
Copy pathdrafting.ts
File metadata and controls
337 lines (289 loc) · 12.3 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Drafting helpers for the examples. tsdraft draws lines, polylines, circles, arcs and text, and
// these functions compose them into patterns and symbols. In CAD they arrive as the plain lines
// and text they are made of.
import type { Point, TextAlign, TextVerticalAlign } from "../../src/index.ts";
/**
* The builder methods the helpers draw with, as they call them, so a drawing with a standard or
* without one can be given.
*/
export interface Pen {
line(from: Point, to: Point): void;
polyline(points: readonly Point[], closed?: boolean): void;
circle(center: Point, radius: number): void;
arc(center: Point, radius: number, startAngle: number, endAngle: number): void;
text(
at: Point,
value: string,
options: {
readonly height: number;
readonly style?: string;
readonly align?: TextAlign;
readonly valign?: TextVerticalAlign;
readonly rotation?: number;
},
): void;
}
/** A rectangle as its lower-left corner, width and height. */
export type Box = readonly [x: number, y: number, width: number, height: number];
/** Writes inches the way architects do: `27` is `2'-3"`, `36` is `3'-0"`, `1.5` is `1 1/2"`. */
export function feetInches(inches: number): string {
const sixteenths = Math.round(inches * 16);
const feet = Math.floor(sixteenths / 192);
const rest = sixteenths - feet * 192;
const whole = Math.floor(rest / 16);
const fraction = reduce(rest % 16, 16);
let inchText = String(whole);
if (fraction) inchText = whole ? `${whole} ${fraction}` : fraction;
return feet > 0 ? `${feet}'-${inchText}"` : `${inchText}"`;
}
function reduce(numerator: number, denominator: number): string {
if (numerator === 0) return "";
let top = numerator;
let bottom = denominator;
while (top % 2 === 0 && bottom % 2 === 0) {
top /= 2;
bottom /= 2;
}
return `${top}/${bottom}`;
}
/**
* A slim closed arrowhead with its tip at `tip`, pointing away from `from`. It draws no text, so
* a drawing whose texts name roles of a standard can be given.
*/
export function arrowhead(d: Pick<Pen, "polyline">, tip: Point, from: Point, length: number): void {
const dx = tip[0] - from[0];
const dy = tip[1] - from[1];
const size = Math.hypot(dx, dy);
const [ux, uy] = [dx / size, dy / size];
const half = length / 6;
const base: Point = [tip[0] - ux * length, tip[1] - uy * length];
d.polyline(
[tip, [base[0] - uy * half, base[1] + ux * half], [base[0] + uy * half, base[1] - ux * half]],
true,
);
}
/** A horizontal or vertical line from `from` to `to`, leaving out the parts inside `holes`. */
export function lineOutside(d: Pen, from: Point, to: Point, holes: readonly Box[]): void {
const along = from[1] === to[1] ? 0 : 1;
const across = 1 - along;
const c = from[across] ?? 0;
const point = (a: number): Point => (along === 0 ? [a, c] : [c, a]);
const cuts = holes
.flatMap((h) =>
c > (h[across] ?? 0) && c < (h[across] ?? 0) + (h[across + 2] ?? 0)
? [[h[along] ?? 0, (h[along] ?? 0) + (h[along + 2] ?? 0)] as const]
: [],
)
.sort((p, q) => p[0] - q[0]);
let start = Math.min(from[along], to[along]);
const end = Math.max(from[along], to[along]);
for (const [cutStart, cutEnd] of cuts) {
if (cutStart > start) d.line(point(start), point(Math.min(cutStart, end)));
start = Math.max(start, cutEnd);
if (start >= end) return;
}
d.line(point(start), point(end));
}
/** The inside lines of a square grid over `area`, such as tile joints, leaving out `holes`. */
export function gridLines(d: Pen, area: Box, size: number, holes: readonly Box[]): void {
const [x0, y0, w, h] = area;
for (let x = x0 + size; x < x0 + w - 0.01; x += size) {
lineOutside(d, [x, y0], [x, y0 + h], holes);
}
for (let y = y0 + size; y < y0 + h - 0.01; y += size) {
lineOutside(d, [x0, y], [x0 + w, y], holes);
}
}
/** Rounds a point to 0.01 so patterns of many short lines stay small in the files. */
function round(p: Point): Point {
return [Math.round(p[0] * 100) / 100, Math.round(p[1] * 100) / 100];
}
/** A linear congruential generator: the same seed gives the same numbers on every run. */
function seededRandom(seed: number): () => number {
let state = seed;
return () => {
state = (state * 1664525 + 1013904223) % 4294967296;
return state / 4294967296;
};
}
function inside(p: Point, box: Box, margin: number): boolean {
const [x, y, w, h] = box;
return p[0] > x - margin && p[0] < x + w + margin && p[1] > y - margin && p[1] < y + h + margin;
}
export interface StippleOptions {
/** The size of the grid cell that gets one mark. Smaller is denser. */
readonly spacing: number;
/** The length of the longest stroke. Dots are 0.15 of it. */
readonly stroke: number;
/** Chooses the marks. Each seed gives its own pattern, the same on every run. */
readonly seed: number;
/** The clear space kept around each hole and inside the edge of the area. */
readonly margin?: number;
}
/**
* A stucco stipple: a grid of cells over `area`, with one short stroke or dot at a random spot
* in each cell, so the marks look scattered but cover the area evenly. A mark that would touch a
* hole, or come within `margin` of one, is left out.
*/
export function stipple(d: Pen, area: Box, holes: readonly Box[], options: StippleOptions): void {
const { spacing, stroke, seed, margin = spacing / 2 } = options;
const random = seededRandom(seed);
const [x0, y0, w, h] = area;
const inner: Box = [x0 + margin, y0 + margin, w - margin * 2, h - margin * 2];
for (let y = y0; y < y0 + h; y += spacing) {
for (let x = x0; x < x0 + w; x += spacing) {
const center: Point = [x + random() * spacing, y + random() * spacing];
const length = random() < 0.6 ? stroke * 0.15 : stroke * (0.4 + 0.6 * random());
const angle = random() * Math.PI;
const half: Point = [(Math.cos(angle) * length) / 2, (Math.sin(angle) * length) / 2];
const a = round([center[0] - half[0], center[1] - half[1]]);
const b = round([center[0] + half[0], center[1] + half[1]]);
const clear = (p: Point) =>
inside(p, inner, 0) && !holes.some((hole) => inside(p, hole, margin));
if (clear(a) && clear(b)) d.line(a, b);
}
}
}
/**
* Diagonal hatch lines `spacing` apart, clipped to `area`. Slope 1 rises to the right and -1
* falls. The lines line up across neighboring areas hatched with the same spacing.
*/
export function hatch(d: Pen, area: Box, spacing: number, slope: 1 | -1 = 1): void {
const [x0, y0, w, h] = area;
const [x1, y1] = [x0 + w, y0 + h];
// Each line is y = slope * x + c, and c steps so that the lines are `spacing` apart.
const step = spacing * Math.SQRT2;
const [cMin, cMax] = slope === 1 ? [y0 - x1, y1 - x0] : [y0 + x0, y1 + x1];
for (let c = Math.ceil(cMin / step) * step; c < cMax; c += step) {
const from = slope === 1 ? Math.max(x0, y0 - c) : Math.max(x0, c - y1);
const to = slope === 1 ? Math.min(x1, y1 - c) : Math.min(x1, c - y0);
if (to - from > 0.05) d.line(round([from, slope * from + c]), round([to, slope * to + c]));
}
}
/**
* Earth below grade, as the soil pattern CAD programs hatch with: a weave of short line triples
* set at 45 degrees, each triple turned across its neighbors. Triples that would leave the area or
* touch a hole are left out.
*/
export function earth(d: Pen, area: Box, holes: readonly Box[], size: number): void {
const [x0, y0, w, h] = area;
// Cells are laid out along the diagonals u = (x + y) / sqrt 2 and v = (y - x) / sqrt 2.
const toXY = (u: number, v: number): Point => [(u - v) / Math.SQRT2, (u + v) / Math.SQRT2];
const uMin = (x0 + y0) / Math.SQRT2;
const uMax = (x0 + w + y0 + h) / Math.SQRT2;
const vMin = (y0 - x0 - w) / Math.SQRT2;
const vMax = (y0 + h - x0) / Math.SQRT2;
const clear = (p: Point) =>
inside(p, area, 0) && !holes.some((hole) => inside(p, hole, size * 0.2));
for (let i = Math.floor(uMin / size); i * size < uMax; i++) {
for (let j = Math.floor(vMin / size); j * size < vMax; j++) {
const u = (i + 0.5) * size;
const v = (j + 0.5) * size;
const alongU = (i + j) % 2 === 0;
for (const k of [-0.25, 0, 0.25]) {
const [du, dv] = alongU ? [0.3 * size, 0] : [0, 0.3 * size];
const [ou, ov] = alongU ? [0, k * size] : [k * size, 0];
const from = round(toXY(u + ou - du, v + ov - dv));
const to = round(toXY(u + ou + du, v + ov + dv));
if (clear(from) && clear(to)) d.line(from, to);
}
}
}
}
/**
* Batt insulation: a looping line across `area`, the symbol drafters draw insulation with. Each
* loop is as tall as the area and half as wide.
*/
export function insulation(d: Pen, area: Box): void {
const [x0, y0, w, h] = area;
const radius = h / 2;
const across = h / 4; // half the width of a loop
const advance = h * 0.35; // how far the line moves along for each loop
const loops = Math.max(1, Math.floor((w - across * 2) / advance));
const start = x0 + (w - loops * advance) / 2;
const points: Point[] = [];
for (let i = 0; i <= loops * 24; i++) {
const t = (i / 24) * 2 * Math.PI;
const x = start + (advance * t) / (2 * Math.PI) - across * Math.sin(t);
points.push(round([x, y0 + radius - radius * Math.cos(t)]));
}
d.polyline(points);
}
/** A dashed line from `from` to `to`, starting and ending with a dash. */
export function dashedLine(d: Pen, from: Point, to: Point, dash: number, gap: number): void {
const length = Math.hypot(to[0] - from[0], to[1] - from[1]);
const count = Math.max(1, Math.round((length + gap) / (dash + gap)));
const space = count > 1 ? (length - count * dash) / (count - 1) : 0;
const along = (distance: number): Point => {
const t = Math.min(distance / length, 1);
return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
};
for (let i = 0; i < count; i++) {
const start = i * (dash + space);
d.line(round(along(start)), round(along(count > 1 ? start + dash : length)));
}
}
/** A break line from `from` to `to`, with a zigzag `size` high at its middle. */
export function breakLine(d: Pen, from: Point, to: Point, size: number): void {
const length = Math.hypot(to[0] - from[0], to[1] - from[1]);
const [ux, uy] = [(to[0] - from[0]) / length, (to[1] - from[1]) / length];
const [mx, my] = [(from[0] + to[0]) / 2, (from[1] + to[1]) / 2];
const at = (a: number, n: number): Point => [mx + ux * a - uy * n, my + uy * a + ux * n];
d.polyline([
from,
at(-size * 0.5, 0),
at(-size * 0.2, size),
at(size * 0.2, -size),
at(size * 0.5, 0),
to,
]);
}
export interface ViewTitleOptions {
readonly number: string;
readonly title: string;
readonly scale: string;
/** The length of the underline, from the left of the bubble. */
readonly width: number;
/** The height of the title. The bubble and the scale are sized from it. */
readonly height: number;
}
/**
* A view title: the view number in a bubble, the title above a long underline, and the scale
* below it. `at` is the left edge of the bubble, level with its center and the underline.
*/
export function viewTitle(d: Pen, at: Point, options: ViewTitleOptions): void {
const { number, title, scale, width, height } = options;
const radius = height * 0.9;
const [x, y] = at;
d.circle([x + radius, y], radius);
d.text([x + radius, y], number, { height, align: "center", valign: "middle" });
d.line([x + radius * 2, y], [x + width, y]);
const textX = x + radius * 2 + height * 0.5;
d.text([textX, y + height * 0.35], title, { height });
d.text([textX, y - height * 0.9], scale, { height: height / 2 });
}
/**
* A section-cut mark: a bubble with the number of the view the cut is drawn in, an arrow beside it
* pointing the way the section looks, and a cut line from the bubble to `cut`.
*/
export function sectionMark(
d: Pen,
center: Point,
cut: Point,
number: string,
pointing: 1 | -1,
height: number,
): void {
const radius = height * 0.9;
const [x, y] = center;
d.circle(center, radius);
d.text(center, number, { height, align: "center", valign: "middle" });
d.line([x, y - radius], cut);
// The arrow is two lines from its tip that touch the bubble.
const reach = 2.2;
const tip: Point = [x + pointing * radius * reach, y];
const across = (pointing * radius) / reach;
const up = radius * Math.sqrt(1 - 1 / reach ** 2);
d.line(tip, [x + across, y + up]);
d.line(tip, [x + across, y - up]);
}