Chào các bạn! Vì nhiều lý do từ nay Truyen2U chính thức đổi tên là Truyen247.Pro. Mong các bạn tiếp tục ủng hộ truy cập tên miền mới này nhé! Mãi yêu... ♥

namespace _3D

namespace _3D

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

            b = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);

            bg = Graphics.FromImage(b);

            b1Back = Color.LightYellow;

            b1Fore = Color.Red;

            p = new Pen(b1Fore);

            Xo = b.Width / 2;

            Ho = b.Height / 2;

            initwireframe(ref w, 70, 200);

            veWireframe(w, bg);

            khoitaowireframe(ref w1, 200, 70);

            veWireframe(w1, bg);

        }

        int PH = 20;

        struct point3D

        {

            public double x, y, z;

        }

        struct point2D

        {

            public double x, y;

        }

        struct wireframe

        {

            public int ndinh, ncanh;

            public point3D[] dinh;

            public int[,] canh;

        }

        Bitmap b;

        Graphics bg;

        Pen p;

        Color b1Back, b1Fore;

        wireframe w,w1;

        int Xo, Ho;

        double a = 0;

        double da;

        void khoitaowireframe(ref wireframe w, double l, double r)

        {

            w.ndinh = 1 + PH;

            w.ncanh = 2 * PH;

            da = 2 * Math.PI / PH;

            w.dinh = new point3D[w.ndinh];

            w.canh = new int[w.ncanh, 2];

            //dinh

            w.dinh[0].x = 0; w.dinh[0].y = l; w.dinh[0].z = 0;

            for (int i = 1; i <= PH; i++)

            {

                w.dinh[i].y = l/2;

                w.dinh[i].x = r * Math.Cos(a);

                w.dinh[i].z = r * Math.Sin(a);               

                a += da;

                //w.dinh[i] = w.dinh[i];

                //w.dinh[i].y = l;

            }

            //canh            

            //canh xung quanh

            for (int i = 1; i <= PH; i++)

            {

                w.canh[i-1, 0] = 0;

                w.canh[i-1, 1] = i;

            }

            //canh day

            for (int i = 1; i < PH; i++)

            {

                w.canh[i-1 + PH, 0] = i;

                w.canh[i-1 + PH, 1] = i + 1;

            }

            w.canh[2 * PH - 1, 1] = PH ;

            w.canh[2 * PH - 1, 0] = 1;

        }

        point3D xoayDiemOX(point3D p, float a)

        {

            point3D q;

            q.y = p.y * Math.Cos(a) - p.z * Math.Sin(a);

            q.z = p.y * Math.Sin(a) + p.z * Math.Cos(a);

            q.x = p.x;

            return q;

        }

        point3D xoayDiemOY(point3D p, float a)

        {

            point3D q;

            q.z = p.z * Math.Cos(a) - p.x * Math.Sin(a);

            q.x = p.z * Math.Sin(a) + p.x * Math.Cos(a);

            q.y = p.y;

            return q;

        }

        point3D xoayDiemOZ(point3D p, float a)

        {

            point3D q;

            q.x = p.x * Math.Cos(a) - p.y * Math.Sin(a);

            q.y = p.x * Math.Sin(a) + p.y * Math.Cos(a);

            q.z = p.z;

            return q;

        }

        void initwireframe(ref wireframe w, double r, double l)

        {

            w.ncanh = 3 * PH;

            w.ndinh = 2 * PH;

            w.canh = new int[w.ncanh, 2];

            w.dinh = new point3D[w.ndinh];

            //danh sach dinh

            double a = 0, da = Math.PI * 2 / PH;

            for (int i = 0; i < PH; i++)

            {

                w.dinh[i].y = l / 2;

                w.dinh[i].z = r * Math.Cos(a);

                w.dinh[i].x = r * Math.Sin(a);

                a += da;

                w.dinh[i + PH] = w.dinh[i];

                w.dinh[i + PH].y = -l / 2;

            }

            //danh sach canh xung quanh

            for (int i = 0; i < PH; i++)

            {

                w.canh[i, 0] = i; w.canh[i, 1] = i + PH;

            }

            //canh day duoi

            for (int i = 0; i < PH - 1; i++)

            {

                w.canh[i + PH, 0] = i;

                w.canh[i + PH, 1] = i + 1;

            }

            w.canh[2 * PH - 1, 0] = 0; w.canh[2 * PH - 1, 1] = PH - 1;

            //canh day tren

            for (int i = 0; i < PH - 1; i++)

            {

                w.canh[i + 2 * PH, 0] = i + PH;

                w.canh[i + 2 * PH, 1] = i + 1 + PH;

            }

            w.canh[3 * PH - 1, 0] = PH;

            w.canh[3 * PH - 1, 1] = 2 * PH - 1;

        }

        void xoayWireframeOX(ref wireframe w, float a)

        {

            for (int i = 0; i < w.ndinh; i++)

                w.dinh[i] = xoayDiemOX(w.dinh[i], a);

        }

        void xoayWireframeOY(ref wireframe w, float a)

        {

            for (int i = 0; i < w.ndinh; i++)

                w.dinh[i] = xoayDiemOY(w.dinh[i], a);

        }

        void xoayWireframeOZ(ref wireframe w, float a)

        {

            for (int i = 0; i < w.ndinh; i++)

                w.dinh[i] = xoayDiemOZ(w.dinh[i], a);

        }

        void myLine(point2D p1, point2D p2, Graphics b1g)

        {

            p1.y = Ho - p1.y; p1.x += Xo;

            p2.y = Ho - p2.y; p2.x += Xo;

            b1g.DrawLine(p, (float)p1.x, (float)p1.y, (float)p2.x, (float)p2.y);

        }

        point2D chieuSSoxy(point3D p)

        {

            point2D q;

            q.x = p.x;

            q.y = p.y;

            return q;

        }

        void veWireframe(wireframe w, Graphics g)

        {

            point2D q1, q2;

            point3D p1, p2;

            for (int i = 0; i < w.ncanh; i++)

            {

                p1 = w.dinh[w.canh[i, 0]];

                p2 = w.dinh[w.canh[i, 1]];

                q1 = chieuSSoxy(p1); q2 = chieuSSoxy(p2);

                myLine(q1, q2, g);

            }

        }

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            g.DrawImageUnscaled(b, 0, 0);

        }

        const float goc = (float)(5 * Math.PI / 180);

        private void Form1_KeyDown(object sender, KeyEventArgs e)

        {

            switch (e.KeyCode)

            {

                case Keys.X:

                    //quay ox

                    xoayWireframeOX(ref w, goc);

                    xoayWireframeOX(ref w1, goc);

                    bg.Clear(b1Back);

                    veWireframe(w, bg);

                    veWireframe(w1, bg);

                    this.Invalidate();

                    break;

                case Keys.Y:

                    //quay oy

                    xoayWireframeOY(ref w, goc);

                    xoayWireframeOY(ref w1, goc);

                    bg.Clear(b1Back);

                    veWireframe(w, bg);

                    veWireframe(w1, bg);

                    this.Invalidate();

                    break;

                case Keys.Z:

                    //quay ox

                    xoayWireframeOZ(ref w, goc);

                    xoayWireframeOZ(ref w1, goc);

                    bg.Clear(b1Back);

                    veWireframe(w, bg);

                    veWireframe(w1, bg);

                    this.Invalidate();

                    break;

            }

        }

        private void Form2_Load(object sender, EventArgs e)

Bạn đang đọc truyện trên: Truyen247.Pro

Tags: