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

49
LINES

< > BotCompany Repo | #1035816 // GSI [General Scanline Image, multiple horizontal lines per row]

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

Libraryless. Click here for Pure Java version (9921L/55K).

persistable sclass GSI extends AbstractHasHi15Color is G2Drawable, IPixelSet {
  int y1; // starting y coordinate
  int[] rowStarts; // where in xData each row starts
  int[] xData; // all x coordinates in a long list (x1, x2, x1, x2, ...)
  
  *(int *y1, int[] *rowStarts, int[] *xData) {}
  
  public int getHeight() { ret rowStarts.length; }
  int y2() { ret y1+rowStarts.length; }
  
  // bounds are calculated & cached transiently
  public simplyCached Rect bounds() {
    int x1 = Int.MAX_VALUE, x2 = 0;
    for (int i = 0; i < xData.length; i += 2) {
      x1 = min(x1, xData[i]);
      x2 = max(x2, xData[i+1]);
    }
    ret rectFromPoints(x1, y1, x2, y2());
  }
  
  public bool contains(Pt p) { ret contains(p.x, p.y); }
  public bool contains(int x, int y) {
    if (y < y1 || y >= y2()) false;
    int i1 = rowStart(y), i2 = rowEnd(y);
    for (int i = i1; i < i2; i += 2)
      if (x < xData[i+1])
        // found the right entry to check
        ret x >= xData[i];
    false;
  }
  
  int rowStart(int y) { ret rowStarts[y-y1]; }
  int rowEnd(int y) {
    ret y == y2()-1 ? rowStarts.length : rowStarts[y-y1+1];
  }
  
  public void drawOn(Graphics2D g) {
    g.setColor(color());
    int h = height();
    for y to h: {
      int i1 = rowStart(y-y1), i2 = rowEnd(y-y1);
      for (int i = i1; i < i2; i += 2) {
        int x1 = xData[i], x2 = xData[i+1];
        if (x1 < x2)
          g.drawLine(x1, y1+y, x2-1, y1+y);
      }
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): mowyntqkapby, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1035816
Snippet name: GSI [General Scanline Image, multiple horizontal lines per row]
Eternal ID of this version: #1035816/13
Text MD5: 3a89beba9a579207ef407f269ff7788d
Transpilation MD5: a63a4301d8e399cddc1ae0f8c0b6bfcc
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-12 16:50:42
Source code size: 1504 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 94 / 176
Version history: 12 change(s)
Referenced in: [show references]