Libraryless. Click here for Pure Java version (8798L/49K).
1 | sclass OnePath {
|
2 | new ByteBuffer steps; |
3 | |
4 | *() {}
|
5 | *(S path) {
|
6 | int n = l(path); |
7 | steps.allocate(n); |
8 | for i to n: |
9 | steps.add(parseDigit(path, i)); |
10 | } |
11 | |
12 | *(OnePath path) {
|
13 | steps = new ByteBuffer(path.steps); |
14 | } |
15 | |
16 | // uses first point as origin, so there will be l(points)-1 steps. |
17 | // Unless you set close to true, then it adds the first point at the end again. |
18 | |
19 | *(Iterable<Pt> points, bool close) {
|
20 | fromPoints(points, close); |
21 | } |
22 | |
23 | void fromPoints(Iterable<Pt> points, bool close) {
|
24 | var it = iterator(points); |
25 | if (empty(it)) ret; |
26 | Pt firstPoint = it.next(), p = firstPoint; |
27 | while (it.hasNext()) {
|
28 | Pt p2 = it.next(); |
29 | steps.add(ptToDigit(ptMinus(p2, p))); |
30 | p = p2; |
31 | } |
32 | |
33 | if (close) |
34 | steps.add(ptToDigit(ptMinus(firstPoint, p))); |
35 | } |
36 | |
37 | int size aka length aka nSteps() { ret steps.size(); }
|
38 | |
39 | toString { ret pathString(); }
|
40 | |
41 | S pathString() {
|
42 | ret singleDigitBytesToString(steps); |
43 | } |
44 | |
45 | // includes first point, so returns length()+1 points in total |
46 | ItIt<Pt> pointIterator(Pt startPt default origin()) {
|
47 | ret new ItIt<Pt> {
|
48 | int i = 0, n = length(); |
49 | Pt p = startPt; |
50 | |
51 | public bool hasNext() { ret i <= n; }
|
52 | |
53 | public Pt next() {
|
54 | var p = this.p; |
55 | if (i < n) this.p = translatePt(this.p, getStepAsPt(i)); |
56 | ++i; |
57 | ret p; |
58 | } |
59 | }; |
60 | } |
61 | |
62 | L<Pt> pointList aka pointsList() { ret ptBuffer(pointIterator()); }
|
63 | |
64 | int getStep(int i) { ret steps.get(i); }
|
65 | |
66 | Pt getStepAsPt(int i) {
|
67 | ret onePathDirections()[steps.get(i)]; |
68 | } |
69 | |
70 | static int ptToDigit(Pt p) {
|
71 | ret p.y < 0 ? p.x < 0 ? 1 : p.x == 0 ? 2 : 3 |
72 | : p.y == 0 ? p.x < 0 ? 8 : p.x == 0 ? 0 : 4 |
73 | : p.x < 0 ? 7 : p.x == 0 ? 6 : 5; |
74 | } |
75 | |
76 | Pt origin() { ret main origin(); }
|
77 | Pt endPoint() { ret last(pointIterator()); }
|
78 | |
79 | Pt drift() {
|
80 | ret ptMinus(endPoint(), origin()); |
81 | } |
82 | |
83 | void addStep(Pt etc p) {
|
84 | int step = onePathLookupDirection(p); |
85 | if (step < 0) fail("Invalid one path step: " + p);
|
86 | addStep(step); |
87 | } |
88 | |
89 | void addStep(int step) {
|
90 | assertBetween(0, 8, step); |
91 | steps.add(step); |
92 | } |
93 | |
94 | void insertStep(int idx, Pt etc p) {
|
95 | int step = onePathLookupDirection(p); |
96 | if (step < 0) fail("Invalid one path step: " + p);
|
97 | insertStep(idx, step); |
98 | } |
99 | |
100 | void insertStep(int idx, int step) {
|
101 | assertBetween(0, 8, step); |
102 | steps.add(idx, step); |
103 | } |
104 | |
105 | Rect bounds() {
|
106 | ret boundsOfPts(pointIterator()); |
107 | } |
108 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1034075 |
| Snippet name: | OnePath - 2d path where each step moves only one pixel (horizontal, vertical or diagonal, or no movement) |
| Eternal ID of this version: | #1034075/37 |
| Text MD5: | 19eb1574de3c0617ea68c1158629dfff |
| Transpilation MD5: | d2b173ee14ba936f53f6d633ea6ca12a |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-05-06 20:11:37 |
| Source code size: | 2597 bytes / 108 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 556 / 963 |
| Version history: | 36 change(s) |
| Referenced in: | [show references] |