Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

108
LINES

< > BotCompany Repo | #1034075 // OnePath - 2d path where each step moves only one pixel (horizontal, vertical or diagonal, or no movement)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (8798L/49K).

sclass OnePath {
  new ByteBuffer steps;
  
  *() {}
  *(S path) {
    int n = l(path);
    steps.allocate(n);
    for i to n:
      steps.add(parseDigit(path, i));
  }
  
  *(OnePath path) {
    steps = new ByteBuffer(path.steps);
  }

  // uses first point as origin, so there will be l(points)-1 steps.
  // Unless you set close to true, then it adds the first point at the end again.
  
  *(Iterable<Pt> points, bool close) {
    fromPoints(points, close);
  }
  
  void fromPoints(Iterable<Pt> points, bool close) {
    var it = iterator(points);
    if (empty(it)) ret;
    Pt firstPoint = it.next(), p = firstPoint;
    while (it.hasNext()) {
      Pt p2 = it.next();
      steps.add(ptToDigit(ptMinus(p2, p)));
      p = p2;
    }
    
    if (close)
      steps.add(ptToDigit(ptMinus(firstPoint, p)));
  }
  
  int size aka length aka nSteps() { ret steps.size(); }
  
  toString { ret pathString(); }
  
  S pathString() {
    ret singleDigitBytesToString(steps);
  }
  
  // includes first point, so returns length()+1 points in total
  ItIt<Pt> pointIterator(Pt startPt default origin()) {
    ret new ItIt<Pt> {
      int i = 0, n = length();
      Pt p = startPt;
      
      public bool hasNext() { ret i <= n; }
      
      public Pt next() {
        var p = this.p;
        if (i < n) this.p = translatePt(this.p, getStepAsPt(i));
        ++i;
        ret p;
      }
    };
  }
  
  L<Pt> pointList aka pointsList() { ret ptBuffer(pointIterator()); }
  
  int getStep(int i) { ret steps.get(i); }
  
  Pt getStepAsPt(int i) {
    ret onePathDirections()[steps.get(i)];
  }
  
  static int ptToDigit(Pt p) {
    ret p.y < 0 ? p.x < 0 ? 1 : p.x == 0 ? 2 : 3
      : p.y == 0 ? p.x < 0 ? 8 : p.x == 0 ? 0 : 4
      : p.x < 0 ? 7 : p.x == 0 ? 6 : 5;
  }
  
  Pt origin() { ret main origin(); }
  Pt endPoint() { ret last(pointIterator()); }
  
  Pt drift() {
    ret ptMinus(endPoint(), origin());
  }
  
  void addStep(Pt etc p) {
    int step = onePathLookupDirection(p);
    if (step < 0) fail("Invalid one path step: " + p);
    addStep(step);
  }
  
  void addStep(int step) {
    assertBetween(0, 8, step);
    steps.add(step);
  }
  
  void insertStep(int idx, Pt etc p) {
    int step = onePathLookupDirection(p);
    if (step < 0) fail("Invalid one path step: " + p);
    insertStep(idx, step);
  }
  
  void insertStep(int idx, int step) {
    assertBetween(0, 8, step);
    steps.add(idx, step);
  }
  
  Rect bounds() {
    ret boundsOfPts(pointIterator());
  }
}

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: 289 / 637
Version history: 36 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#1034415 - OnePathWithOrigin - OnePath plus starting position