基于x、y坐标的2D单元格位置计算:Point类开发需求
Solution: Point Class for Spreadsheet Position Calculation
Got it, let's tackle this. You need a Point class that calculates the linear position of a cell in a spreadsheet grid based on (x,y) coordinates and the grid size. From your 3x3 example, the pattern is clear—we're counting cells left-to-right, top-to-bottom, so the math is straightforward.
Core Logic
Looking at your 3x3 grid examples:
- (0,0) → 0, (0,1) →1, (0,2) →2 (first row, left to right)
- (1,0) →3, (1,1) →4, (1,2) →5 (second row)
- (2,0) →6, (2,1) →7, (2,2) →8 (third row)
The formula here is:position = x * number_of_columns + y
Where:
x= row index (starts at 0)y= column index (starts at 0)number_of_columns= the width of your spreadsheet (3 in the example)
We also add a quick validation check to make sure the coordinates don't fall outside the grid bounds—this prevents invalid positions from being calculated.
Code Examples
Python Version
class Point: def __init__(self, x, y, grid_cols, grid_rows): self.x = x # Row coordinate (0-indexed) self.y = y # Column coordinate (0-indexed) self.grid_cols = grid_cols # Total columns in the spreadsheet self.grid_rows = grid_rows # Total rows in the spreadsheet def GetPosition(self): # Validate coordinates are within grid bounds if not (0 <= self.x < self.grid_rows and 0 <= self.y < self.grid_cols): raise ValueError("Coordinates are out of the spreadsheet's bounds") # Calculate linear position return self.x * self.grid_cols + self.y
C# Version
public class Point { private readonly int _x; // Row coordinate (0-indexed) private readonly int _y; // Column coordinate (0-indexed) private readonly int _gridCols; // Total columns in the spreadsheet private readonly int _gridRows; // Total rows in the spreadsheet public Point(int x, int y, int gridCols, int gridRows) { _x = x; _y = y; _gridCols = gridCols; _gridRows = gridRows; } public int GetPosition() { // Validate coordinates are within grid bounds if (_x < 0 || _x >= _gridRows || _y < 0 || _y >= _gridCols) { throw new ArgumentOutOfRangeException(nameof(GetPosition), "Coordinates are out of the spreadsheet's bounds"); } // Calculate linear position return _x * _gridCols + _y; } }
Testing the 3x3 Example
Let's verify with your sample coordinates:
Python Test
# Test 3x3 grid cases print(Point(0, 0, 3, 3).GetPosition()) # Output: 0 print(Point(0, 2, 3, 3).GetPosition()) # Output: 2 print(Point(1, 0, 3, 3).GetPosition()) # Output: 3 print(Point(2, 1, 3, 3).GetPosition()) # Output: 7
C# Test
// Test 3x3 grid cases Console.WriteLine(new Point(0, 0, 3, 3).GetPosition()); // Output: 0 Console.WriteLine(new Point(1, 2, 3, 3).GetPosition()); // Output: 5 Console.WriteLine(new Point(2, 0, 3, 3).GetPosition()); // Output: 6
Notes
- If your coordinate system swaps
x(column) andy(row), just adjust the formula toy * gridCols + x—easy to tweak based on your actual needs. - The boundary check is optional but recommended—it helps catch bugs where invalid coordinates are passed in.
内容的提问来源于stack exchange,提问作者arjun
相关产品推荐
相关产品推荐

