bade

Màu nền
Font chữ
Font size
Chiều cao dòng

struct Point3D

    {

        public float X, Y, Z;

        public Point3D (float x, float y, float z)

        {

            X=x;

            Y=y;

            Z=z;

        }

    }

    struct wireframe

    {

       public Point3D[] vert;

       public int[,] edge;

    }

    public partial class Form1 : Form

    {

        const int a = 100;

        Graphics g;

        wireframe w;

        int H, W;

     double al;

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            g = panel1.CreateGraphics();

            H = panel1.Height;

            W = panel1.Width;

            al = (float)30 * Math.PI / 180;

           w.vert= new Point3D [8];

            w.vert[0]=new Point3D(0,0,0);

            w.vert[1]=new Point3D(0,a,0);

            w.vert[2]=new Point3D(0,a,a);

            w.vert[3]=new Point3D(0,0,a);

            w.vert[4]=new Point3D(a,0,0);

            w.vert[5]=new Point3D(a,a,0);

            w.vert[6]=new Point3D(a,a,a);

            w.vert[7]=new Point3D(a,0,a);

            w.edge= new int [12,2];

            w.edge[0,0]=0;

             w.edge[0,1]=1;

             w.edge[1,0]=1;

             w.edge[1,1]=2;

             w.edge[2,0]=2;

             w.edge[2,1]=3;

             w.edge[3,0]=3;

             w.edge[3,1]=0;

             w.edge[4,0]=4;

             w.edge[4,1]=5;

             w.edge[5,0]=5;

             w.edge[5,1]=6;

             w.edge[6,0]=6;

             w.edge[6,1]=7;

             w.edge[7,0]=7;

             w.edge[7,1]=4;

             w.edge[8,0]=7;

             w.edge[8,1]=3;

             w.edge[9,0]=4;

             w.edge[9,1]=0;

             w.edge[10,0]=5;

             w.edge[10,1]=1;

             w.edge[11,0]=6;

             w.edge[11,1]=2;

        }

        PointF ChieuSSOXY(Point3D p)

        {

            PointF Q = new PointF();

            Q.X = p.X;

            Q.Y = p.Y;

            return Q;

        }

        void VeW(wireframe w, Graphics g, int W, int H, Color c)

        {

            Point3D p1, p2;

            PointF Q1, Q2;

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

            {

                p1 = w.vert[w.edge[i,0]];

                p2 = w.vert[w.edge[i,1]];

                Q1 = ChieuSSOXY(p1);

                Q2 = ChieuSSOXY(p2);

                Myline(g, W,H, Q1, Q2, c);

            }

        }

        void Myline(Graphics g, int W, int H, PointF p1, PointF p2, Color c)

        {

            p1.Y = H - p1.Y;

            p1.X += W;

            p2.Y = H - p2.Y;

            p2.X += W;

            g.DrawLine(new Pen(Color.Red), p1, p2);

        }

        private void panel1_Paint(object sender, PaintEventArgs e)

        {

            VeW(w, g,panel1.Width / 2, panel1.Height / 2, Color.Red);

        }

        Point3D XoayOx(Point3D p, float al)

        {

             Point3D q = new Point3D();

             q.X = p.X;

             q.Y =(float) (p.Y * Math.Cos(al) - p.Z*Math.Sin(al));

             q.Z = (float)(p.Y * Math.Sin(al) + p.Z * Math.Cos(al));

             return q;

                    }

        void XoayWOx(ref wireframe w, float al)

        {

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

            {

                w.vert[i] = XoayOx(w.vert[i], al);

            }

        }

        Point3D XoayOy(Point3D p, float al)

        {

            Point3D q = new Point3D();

            q.Y = p.Y;

            q.X = (float)(p.Z * Math.Sin(al) + p.X * Math.Cos(al));

            q.Z = (float)(p.Z * Math.Cos(al) - p.X * Math.Sin(al));

            return q;

        }

        void XoayWOy(ref wireframe w, float al)

        {

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

            {

                w.vert[i] = XoayOy(w.vert[i], al);

            }

        }

        Point3D XoayOz(Point3D p, float al)

        {

            Point3D q = new Point3D();

            q.Z = p.Z;

            q.Y = (float)(p.X * Math.Sin(al) + p.Y * Math.Cos(al));

            q.X = (float)(p.X * Math.Cos(al) - p.Y * Math.Sin(al));

            return q;

        }

        void XoayWOz(ref wireframe w, float al)

        {

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

            {

                w.vert[i] = XoayOz(w.vert[i], al);

            }

        }

        Point3D BDOx(Point3D p, float hxy, float  hxz)

        {

            Point3D q = new Point3D();

            q.X = p.X + hxy * p.Y + hxz * p.Z;

            q.Y = p.Y;

                q.Z=p.Z;

            return q;

        }

        void BDWOx(ref wireframe w,float hxy, float hxz)

        {

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

            {

                w.vert[i] = BDOx(w.vert[i], hxy, hxz);

            }

        }

        private void button1_Click(object sender, EventArgs e)

        {

            XoayWOx(ref w,(float) al);

            g.Clear(panel1.BackColor);

                     VeW(w, g, panel1.Width / 2, panel1.Height / 2, Color.Red);

        }

        private void button2_Click(object sender, EventArgs e)

        {

            XoayWOy(ref w, (float)al);

            g.Clear(panel1.BackColor);

            VeW(w, g, panel1.Width / 2, panel1.Height / 2, Color.Red);

        }

        private void button3_Click(object sender, EventArgs e)

        {

            XoayWOz(ref w, (float)al);

            g.Clear(panel1.BackColor);

            VeW(w, g, panel1.Width / 2, panel1.Height / 2, Color.Red);

        }

        private void button4_Click(object sender, EventArgs e)

        {

            BDWOx(ref w, 2,3);

            g.Clear(panel1.BackColor);

            VeW(w, g, panel1.Width / 2, panel1.Height / 2, Color.Red);

        }}

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