3. 可繪出任意角度三角形相關操作的計算程式

數學不好 :/

using System.Linq;

bool[,] grid;

void draw()
{
    for (int y = grid.GetLength(0) - 1; y >= 0; y--)
    {
        for (int x = 0; x < grid.GetLength(1); x++)
        {
            Console.Write(grid[y, x] ? "*" : " ");
            Console.Write(" ");
        }
        Console.WriteLine();
    }
}

void setup(int x1, int x2, int y1, int y2)
{
    int xs = Math.Abs(x2 - x1);
    int ys = Math.Abs(y2 - y1);

    var N = Math.Max(xs, ys);
    double dx = (double)(x2 - x1) / N;
    double dy = (double)(y2 - y1) / N;

    for (int i = 0; i <= N; i++)
    {
        grid[(int)(i * dy + y1), (int)(i * dx + x1)] = true;
    }
}

int getPadding(params int[] x)
{
    var min = x.Min();
    if (min > 0) return 0;
    return -min;
}

int tx1 = 0, tx2 = 0, tx3 = 0, ty1 = 0, ty2 = 0, ty3 = 0;
while (true)
{
    Console.Write(
@"請選擇操作項目 :
      <1>輸入兩點座標(x1,y1), (x2,y2)繪一線 : 
      <2>輸入三個頂點座標(x1,y1), (x2,y2), (x3,y3)繪三角形 : 
      <3>上題三角形水平翻轉 : 
請選擇 : "
);

    switch (Console.ReadLine())
    {
        case "1":
            Console.Write("x1,y1,  x2,y2:");
            int[] data = Console.ReadLine().Replace("  ", " ").Split(' ').Select(int.Parse).ToArray();
            int x1 = data[0];
            int y1 = data[1];
            int x2 = data[2];
            int y2 = data[3];

            x1 += getPadding(x1, x2);
            x2 += getPadding(x1, x2);
            y1 += getPadding(y1, y2);
            y2 += getPadding(y1, y2);

            grid = new bool[Math.Max(y1, y2) + 1, Math.Max(x1, x2) + 1];
            setup(x1, x2, y1, y2);
            draw();
            break;
        case "2":
            Console.Write("x1,y1,  x2,y2,  x3,y3 :");
            data = Console.ReadLine().Replace("  ", " ").Split(' ').Select(int.Parse).ToArray();
            tx1 = data[0];
            ty1 = data[1];
            tx2 = data[2];
            ty2 = data[3];
            tx3 = data[4];
            ty3 = data[5];

            tx1 += getPadding(tx1, tx2, tx3);
            tx2 += getPadding(tx1, tx2, tx3);
            tx3 += getPadding(tx1, tx2, tx3);
            ty1 += getPadding(ty1, ty2, ty3);
            ty2 += getPadding(ty1, ty2, ty3);
            ty3 += getPadding(ty1, ty2, ty3);

            grid = new bool[Math.Max(ty3, Math.Max(ty1, ty2)) + 1, Math.Max(tx3, Math.Max(tx1, tx2)) + 1];
            setup(tx1, tx2, ty1, ty2);
            setup(tx3, tx2, ty3, ty2);
            setup(tx1, tx3, ty1, ty3);
            draw();
            break;
        case "3":
            var oldX = new[] { tx1, tx2, tx3 }.Min();

            tx1 *= -1;
            tx2 *= -1;
            tx3 *= -1;

            tx1 += oldX + getPadding(tx1, tx2, tx3);
            tx2 += oldX + getPadding(tx1, tx2, tx3);
            tx3 += oldX + getPadding(tx1, tx2, tx3);

            grid = new bool[Math.Max(ty3, Math.Max(ty1, ty2)) + 1, Math.Max(tx3, Math.Max(tx1, tx2)) + 1];
            setup(tx1, tx2, ty1, ty2);
            setup(tx3, tx2, ty3, ty2);
            setup(tx1, tx3, ty1, ty3);
            draw();
            break;
    }
    Console.Write("繼續 : 請按1,結束 : 請按0:");
    if (Console.ReadLine() == "0") return;
    else Console.WriteLine();
}

Last updated