一条线 被分割成3段的,线上的所有坐标计算,C#代码
时间: 2024-03-11 09:47:49 浏览: 72
以下是将线分割成三段后计算每段上的坐标的C#代码示例:
```csharp
// 定义线段起点和终点的坐标
int startX = 0;
int startY = 0;
int endX = 10;
int endY = 10;
// 将线段分割成三段
int segment1X = startX + (endX - startX) / 3;
int segment1Y = startY + (endY - startY) / 3;
int segment2X = startX + 2 * (endX - startX) / 3;
int segment2Y = startY + 2 * (endY - startY) / 3;
// 计算每段上的坐标
for (int i = startX; i <= segment1X; i++)
{
int y = startY + (i - startX) * (segment1Y - startY) / (segment1X - startX);
Console.WriteLine("({0},{1})", i, y);
}
for (int i = segment1X + 1; i <= segment2X; i++)
{
int y = segment1Y + (i - segment1X) * (segment2Y - segment1Y) / (segment2X - segment1X);
Console.WriteLine("({0},{1})", i, y);
}
for (int i = segment2X + 1; i <= endX; i++)
{
int y = segment2Y + (i - segment2X) * (endY - segment2Y) / (endX - segment2X);
Console.WriteLine("({0},{1})", i, y);
}
```
以上代码将线段起点和终点的坐标设为(0,0)和(10,10),将线段分割成三段后,分别计算每段上的坐标并输出。你可以根据实际情况修改代码中的坐标值。
阅读全文