実習 三角形の構造体

        /// <summary>
        /// 2次元の点をあらわす構造体
        /// </summary>
        struct Point
        {
            public double x; // x 座標
            public double y; // y 座標

            public override string ToString()
            {
                return "(" + x + ", " + y + ")";
            }
        }

        /// <summary>
        /// 三角形をあらわす構造体
        /// </summary>
        struct Triangle{
            public Point a;
            public Point b;
            public Point c;

            /// <summary>
            /// 三角形の面積を求める。
            /// </summary>
            /// <returns>面積</returns>
            public double GetArea()
            {
                double abx, aby, acx, acy;
                abx = b.x - a.x;
                aby = b.y - a.y;
                acx = c.x - a.x;
                acy = c.y - a.y;
                return Math.Abs(abx * acy - aby * acx) / 2;
            }
        }

        static void Main(string[] args)
        {
            Triangle t;
            t.a.x = 5;
            t.a.y = 3;
            t.b.x = 4;
            t.b.y = 6;
            t.c.x = 7;
            t.c.y = 9;

            Console.WriteLine("三角形の面積は{0}", t.GetArea()); // 6
        }