C# 生成点坐标:基于给定点位置、行、列和间距
使用 C# 根据给出的一个点位置,行,列,间距,生成其他的点,考虑直角坐标系正负。\n\n可以使用嵌套循环来生成其他的点。假设给定的点位置为(x, y),行和列的数量分别为rows和cols,间距为spacing。\n\n首先,确定起始位置的x和y坐标,即左上角的点。由于考虑正负,起始位置的x坐标为x - (cols - 1) * spacing / 2,y坐标为y + (rows - 1) * spacing / 2。\n\n然后,使用嵌套循环遍历所有的行和列,生成其他的点。内层循环控制列,外层循环控制行。在每次循环中,根据当前的行和列计算出相应的点的坐标。\n\n下面是一个示例代码:\n\ncsharp\nint x = 0; // 给定点的x坐标\nint y = 0; // 给定点的y坐标\nint rows = 3; // 行的数量\nint cols = 4; // 列的数量\nint spacing = 10; // 间距\n\nint startX = x - (cols - 1) * spacing / 2; // 起始位置的x坐标\nint startY = y + (rows - 1) * spacing / 2; // 起始位置的y坐标\n\nfor (int row = 0; row < rows; row++)\n{\n for (int col = 0; col < cols; col++)\n {\n int pointX = startX + col * spacing; // 当前点的x坐标\n int pointY = startY - row * spacing; // 当前点的y坐标\n\n // 输出当前点的坐标\n Console.WriteLine("Point {0}: ({1}, {2})", row * cols + col + 1, pointX, pointY);\n }\n}\n\n\n运行上述代码,将会生成以下的点坐标:\n\n\nPoint 1: (-15, 15)\nPoint 2: (-5, 15)\nPoint 3: (5, 15)\nPoint 4: (15, 15)\nPoint 5: (-15, 5)\nPoint 6: (-5, 5)\nPoint 7: (5, 5)\nPoint 8: (15, 5)\nPoint 9: (-15, -5)\nPoint 10: (-5, -5)\nPoint 11: (5, -5)\nPoint 12: (15, -5)\n\n\n
原文地址: https://www.cveoy.top/t/topic/pOdZ 著作权归作者所有。请勿转载和采集!