示例#1
0
        public static void Main()
        {
            Console.WriteLine("v1=(1,1,-2.4)");
             Vector3 v1 = new Vector3(1,1,-2.4);
             Console.WriteLine("v2=(4,5,6)");
             Vector3 v2 = new Vector3(4,5,6);
             Console.WriteLine("v3=(-1,5,2)");
             Vector3 v3 = new Vector3(-1,3,-2);
             Console.Write("v1*v2=");
             Vector3.CrossProduct(v1,v2).Vypis();
             Console.Write("v1.v2=");
             Console.WriteLine(Vector3.DotProduct(v1,v2));
             Console.Write("v1+v2=");
             (v1+v2).Vypis();
             Console.Write("v1-v2=");
             (v1+v2).Vypis();
             Console.Write("|v1|=");
             Console.WriteLine(v1.Abs());

             Console.WriteLine("Normala plochy v1 v2 v3");
             Vector3.GetNormVect(v1,v2,v3).Vypis();

             Console.WriteLine("v1 perspektiva");
              Projection p = new Projection((double)Math.PI/4,1,2.4,66);
              p.Project(v1).Vypis();
              Console.WriteLine("Rozmery projekcni plochy:{0},{1}",p.Width,p.Height);
              Vector3 v4 = p.Project(v1);
              Console.WriteLine("z original = -2.4, z projected = {0}",p.ProjectZ(-2.4));
              Console.WriteLine("projekceXY v1");
              p.ProjectXY(v1).Vypis();

              Viewport port = new Viewport(p.Width,p.Height,200,200);
              Console.WriteLine("souradnice v1 na obrazovce 200X200={0},{1}",
                    port.GetIntX(v4.X),port.GetIntY(v4.Y));
              Console.WriteLine("v2d=(1,2),sirka obrazovky = 2,rozliseni x = 200");
              Vector2 v2d = new Vector2(1,2);
              int x = v2d.GetIntX(2,200);
              Console.WriteLine("x souradnice bodu na obrazovce={0}",x);
              Console.WriteLine("v2d=(-1,2),sirka obrazovky = 2,rozliseni x = 200");
              v2d = new Vector2(-1,2);
              x = v2d.GetIntX(2,200);
              Console.WriteLine("x souradnice bodu na obrazovce={0}",x);
              Console.WriteLine("v2d=(0,2),sirka obrazovky = 2,rozliseni x = 200");
              v2d = new Vector2(0,2);
              x = v2d.GetIntX(2,200);
              Console.WriteLine("x souradnice bodu na obrazovce={0}",x);

              Console.WriteLine("Trojuhelnik abc ma vrcholy v1,v2,v3");
              Triangle3D abc = new Triangle3D(v1,v2,v3);
              Console.WriteLine("normalovy vektor k rovine trojuhelniku je");
              abc.GetNormVect().Vypis();

              Matrix44 matrix1 = new Matrix44(new double[,]{{1,2,3,4},{5,6,7,8},
                        {9,10,11,12},{13,14,15,16}});
              Matrix44 matrix2 = new Matrix44(new double[,]{{3,2,1,0},{0,1,2,3},
                          {6,8,7,4},{2,3,1,9}});
            Console.WriteLine("m1=");
            matrix1.Vypis();
            Console.WriteLine("m2=");
            matrix2.Vypis();
            Console.WriteLine("m1*m2=");
            Matrix44.Multiply(matrix1,matrix2).Vypis();

            Console.WriteLine("Zkouska trideni");
            v1.Vypis();
            v2.Vypis();
            v3.Vypis();

            Vector3.SortX(ref v1,ref v2,ref v3);

            Console.WriteLine("Setrideno podle x:");
            v1.Vypis();
            v2.Vypis();
            v3.Vypis();

            Console.WriteLine("Line2D getx gety");
            Line2D line2d = new Line2D(v1,v2);
            Console.WriteLine(line2d.GetX(0));
            Console.WriteLine(line2d.GetY(0));

            Line3D line3d = new Line3D(v1,v3);
            v4 = new Vector3(line3d.GetX(v2.Y),v2.Y,line3d.GetZFromY(v2.Y));
            v1.Vypis();
            v2.Vypis();
            v3.Vypis();
            v4.Vypis();

            Console.WriteLine("interpolace:");
            for(int i = 0;i<10;i++)
              line3d.Interpolate((double)i/(double)10).Vypis();
        }
示例#2
0
        //nakresli vyplneny trojuhelnik s vrcholy v1,v2,v3
        public void DrawFilledTriangle3d(Vector3 v1,Vector3 v2,Vector3 v3)
        {
            //transformace view maticí
            v1 = v1.Transform(viewMatrix);
            v2 = v2.Transform(viewMatrix);
            v3 = v3.Transform(viewMatrix);

            //stinovani
            Vector3 normal = Vector3.GetNormVect(v1,v2,v3);
            normal = normal.Normalised();
            Int32 shadecolor = getShadeColor(color,normal);

            //projekce
            v1 = projection.Project(v1);
            v2 = projection.Project(v2);
            v3 = projection.Project(v3);

            Vector3.SortY(ref v1,ref v2,ref v3);

            Line3D line = new Line3D(v1,v3);
            //spocteni pomocneho bodu v4
            Vector3 v4 = new Vector3(line.GetX(v2.Y),v2.Y,line.GetZFromY(v2.Y));

            Vector3.SortX(ref v2,ref v4);

            //prepocteni realnych souradnic na celociselne
            Vector2i v1i = new Vector2i(v1,viewport);
            Vector2i v2i = new Vector2i(v2,viewport);
            Vector2i v3i = new Vector2i(v3,viewport);
            //urceni okrajovych primek
            Line3D LeftLine = new Line3D(v1,v2);
            Line3D RightLine = new Line3D(v1,v4);

            //kresleni horni casti trojuhelnika
            for(int i = v1i.Y;i<v2i.Y;i++)
            {
              //urceni okraju radku
                Vector3 LeftVector = LeftLine.Interpolate((double)(v1i.Y - i)
              /(double)(v1i.Y-v2i.Y));
              Vector3 RightVector = RightLine.Interpolate((double)(v1i.Y - i)
              /(double)(v1i.Y-v2i.Y));
              //prevedeni okraju na cela cisla
              int leftx = viewport.GetIntX(LeftVector.X);
              int rightx = viewport.GetIntX(RightVector.X);
              //primka slouzici k vypoctu hloubky pomoci interpolace
              Line3D Radek = new Line3D(LeftVector,RightVector);
              double z = 0;
              for(int j = leftx;j<rightx;j++)
              {
                z = Radek.Interpolate((double)(leftx - j)/(double)(leftx - rightx)).Z;
                DrawPixel2d(j,i,z,shadecolor);
              }
            }

            //urceni okrajovych primek
              LeftLine = new Line3D(v2,v3);
              RightLine = new Line3D(v4,v3);
            //kresleni spodni casti trojuhelnika
              for(int i = v2i.Y;i<v3i.Y;i++)
              {
                Vector3 LeftVector = LeftLine.Interpolate((double)(v2i.Y - i)
                /(double)(v2i.Y-v3i.Y));
                Vector3 RightVector = RightLine.Interpolate((double)(v2i.Y - i)
                /(double)(v2i.Y-v3i.Y));
                //urceni okraju radku
                int leftx = viewport.GetIntX(LeftVector.X);
                int rightx = viewport.GetIntX(RightVector.X);

                Line3D Radek = new Line3D(LeftVector,RightVector);
                double z = 0;
                for(int j = leftx;j<rightx;j++)
                {
                  z = Radek.Interpolate((double)(leftx - j)/(double)(leftx - rightx)).Z;
                  DrawPixel2d(j,i,z,shadecolor);
                }
                }
        }