Libraryless. Click here for Pure Java version (5805L/32K).
1 | // has a rectangular window (visionW*visionH) |
2 | // walking range per frame is a circle with radius "range" |
3 | asclass G22Walker<Img extends WidthAndHeight> { |
4 | // configuration (set) |
5 | int range = 1; // how far we can walk in each frame |
6 | double roundingPolicy = .9; // for making the range circle (between 0 and 1) |
7 | int visionW = 3, visionH = 3; // window rectangle size (both should be odd) |
8 | |
9 | // current position (set/get) |
10 | Pt position; // current position (center), not null |
11 | |
12 | // status variables (read) |
13 | bool stuck; // can neither stay nor move anywhere for some reason |
14 | int guessWork; // how many "optimal" new positions we chose from |
15 | |
16 | void move(Img image) { |
17 | stuck = false; |
18 | guessWork = 0; |
19 | |
20 | // How far can I move until I hit the image's borders |
21 | Rect movement1 = rectFromPoints(visionW/2, visionH/2, |
22 | image.getWidth()-(visionW+1)/2, (visionH+1)/2); |
23 | |
24 | // How far can I walk in one frame |
25 | Rect movement2 = rectAroundPt(position, range*2+1); |
26 | |
27 | Rect movement = intersectRects(movement1, movement2); |
28 | if (empty(movement)) ret with set stuck; |
29 | |
30 | printVars(+movement, +movement1, +movement2); |
31 | |
32 | if (hasRememberedImage()) |
33 | ret with rememberImage(image); |
34 | |
35 | Rect r = window_uncropped(); |
36 | |
37 | new MultiBest<Pt> bestPositions; |
38 | |
39 | int mx1 = movement.x1(), mx2 = movement.x2(); |
40 | int my1 = movement.y1(), my2 = movement.y2(); |
41 | for (int y = my1; y < my2; y++) |
42 | for (int x = mx1; x < mx2; x++) { |
43 | var newPos = pt(x, y); |
44 | bestPositions.put(newPos, scorePosition(newPos, image)); |
45 | } |
46 | var best = bestPositions!; |
47 | guessWork = l(best); |
48 | position = random(best); |
49 | |
50 | // It's actually a question how often we grab a fresh image. |
51 | // For now, every time. |
52 | rememberImage(image); |
53 | } |
54 | |
55 | // only works when we already have a rememberedImage |
56 | abstract double scorePosition(Pt pos, Img image); |
57 | |
58 | abstract void rememberImage(Img image); |
59 | abstract bool hasRememberedImage(); |
60 | |
61 | int visionX1() { ret position.x-visionW; } |
62 | int visionY1() { ret position.y-visionH; } |
63 | |
64 | Rect window_uncropped(Pt center default position) { |
65 | ret rectAroundPt(center, visionW, visionH); |
66 | } |
67 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034119 |
Snippet name: | G22Walker - for video input. follows a rectangular remembered image around the animation |
Eternal ID of this version: | #1034119/21 |
Text MD5: | 266dafe6352855a632bf4e12a578a49a |
Transpilation MD5: | 4a61e51aec79a1540a029249593e658d |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-14 15:33:17 |
Source code size: | 2259 bytes / 67 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 203 / 392 |
Version history: | 20 change(s) |
Referenced in: | [show references] |