Convert this C++ code to Nim :
- User
Convert this C++ code to Nim :
- Assistant
Unfortunately, you did not provide the C++ code that you would like to convert to Nim. Please provide the C++ code that you want to convert, and I will be happy to help you with the Nim conversion.
- User
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;} }
- Assistant
Here is the equivalent code converted to Nim:
type Rect = object x, y: int w, h: int wasPacked: bool proc SortByHeight(a, b: var Rect): int = # Implement your sorting logic here if a.h < b.h: result = -1 elif a.h > b.h: result = 1 else: result = 0 proc PackRectsNaiveRows(var rects: seq[Rect]) = # Sort by a heuristic rects.sort(SortByHeight) var xPos = 0 yPos = 0 largestHThisRow = 0 # Loop over all the rectangles for rect in 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 = trueNote that I made a few changes to adapt the code to Nim syntax and idioms.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.