Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Can you convert this code to Nim : struct Rect { int x, y; int w, h; bool wasPacked =…

  1. User

    Can you convert this code to Nim : struct Rect { int x, y; int w, h; bool wasPacked = false; };

    void PackRectsNaiveRows(std::vector& rects) { // Sort by a heuristic std::sort(rects.begin(), rects.end(), SortByHeight());

    int xPos = 0; int yPos = 0; int largestHThisRow = 0;

    // Loop over all the rectangles for (Rect& rect : rects) { // If this rectangle will go past the width of the image // Then loop around to next row, using the largest height from the previous row if ((xPos + rect.w) > 700) { yPos += largestHThisRow; xPos = 0; largestHThisRow = 0; }

    // If we go off the bottom edge of the image, then we've failed
    if ((yPos + rect.h) > 700)
      break;
    
    // This is the position of the rectangle
    rect.x = xPos;
    rect.y = yPos;
    
    // Move along to the next spot in the row
    xPos += rect.w;
    
    // Just saving the largest height in the new row
    if (rect.h > largestHThisRow)
      largestHThisRow = rect.h;
      
    // Success!
    rect.wasPacked = true;
    

    } }

  2. Assistant

    import sort

    type Rect = object x, y: int w, h: int wasPacked: bool = false

    proc SortByHeight(a, b: Rect): int = result = cmp(a.h, b.h)

    proc PackRectsNaiveRows(rects: var seq[Rect]) = sort(seq, SortByHeight)

    var xPos, yPos, largestHThisRow: int

    for rect in rects: if (xPos + rect.w) > 700: yPos += largestHThisRow xPos = 0 largestHThisRow = 0

    if (yPos + rect.h) > 700:
      break
    
    rect.x = xPos
    rect.y = yPos
    
    xPos += rect.w
    
    if rect.h > largestHThisRow:
      largestHThisRow = rect.h
      
    rect.wasPacked = true