C# 3D到2D投射

  public class iGraphic
    {

        public class i3D
        {
            public class iVector
            {
                public double x;
                public double y;
                public double z;
                public iVector(double X,double Y,double Z)
                {
                    x = X;
                    y = Y;
                    z = Z;
                }
            }
            public class iCamera
            {
                public double x;
                public double y;
                public double z;

                public iVector V;

                public iCamera(double X,double Y,double Z,iVector v)
                {
                    x = X;
                    y = Y;
                    z = Z;
                    V = v;
                }

            }
            public class iPoint3D :IComparable
            {
                public double x;
                public double y;
                public double z;
                public Color color;

                public iPoint3D(double X,double Y,double Z, Color Color)
                {
                    x = X;
                    y = Y;
                    z = Z;
                    color = Color;
                }

                public int CompareTo(iPoint3D other)
                {
                    return x.CompareTo(other.x);
                }

            }
            public class ScreenData
            {
                public double X_2dWorld;
                public double Y_2dWorld;
                public Color color;
                public ScreenData(double x, double y, Color c)
                {
                    X_2dWorld = x;
                    Y_2dWorld = y;

                    color = c;
                }

                public ScreenData()
                {
                }
            }

            //public static ScreenData Projection2(iPoint3D point, int constant)
            //{
            //   return new ScreenData(point.x / point.z * constant, point.y/point.z* constant,Color.Black);
            //}

            public static ScreenData Projection(iCamera C,iPoint3D point)
            {
                double d = Math.Sqrt(C.V.x * C.V.x + C.V.y * C.V.y + C.V.z * C.V.z);
                double cosX = C.V.x / d;
                double cosY = C.V.y / d;
                double cosZ = C.V.z / d;

                double sinX = Math.Sqrt(1 - cosX * cosX);
                double sinY = Math.Sqrt(1 - cosY * cosY);
                double sinZ = Math.Sqrt(1 - cosZ * cosZ);

                double Dx=cosY*(sinZ*(point.y-C.y)+cosZ*(point.y-C.x))-sinY*(point.z-C.z);
                double Dy=sinX*(cosY*(point.z-C.z)+sinY*(sinZ*(point.y-C.y)+cosZ*(point.x-C.x)))+cosX*(cosZ*(point.y-C.y)-sinZ*(point.x-C.x));
                double Dz=cosX*(cosY*(point.z-C.z)+sinY*(sinZ*(point.y-C.y)+cosZ*(point.x-C.x)))-sinX*(cosZ*(point.y-C.y)-sinZ*(point.x-C.x));

                double Ex = 0.5;
                double Ey = 0.5;
                double fov = Math.PI / 4;
                double Ez = 1 / Math.Tan(fov / 2);

                ScreenData PointOnScreen = new ScreenData();

                PointOnScreen.X_2dWorld = (Dx-Ex)*(Ez/Dz);
                PointOnScreen.Y_2dWorld = (Dy-Ey)*(Ez/Dz);

                PointOnScreen.color = point.color;

                return PointOnScreen;
            }

            public static List Projections(iCamera C, List points)
            {
                List re = new List();
                System.Threading.Tasks.Parallel.ForEach(points, (i) => {re.Add(Projection(C,i)); });
                return re;
            }

            public static Bitmap DrawPoint(iCamera C, iPoint3D point, Bitmap map,Pen pen)
            {
                ScreenData d = Projection(C, point);
                int BitX = 300 + (int)(100 * (d.X_2dWorld));
                int BitY = 300 + (int)(100 * (d.Y_2dWorld));

                using (var graphics = Graphics.FromImage(map))
                {
                    if(BitX>0 && BitY>0)
                    graphics.DrawRectangle(pen, BitX, BitY, 1, 1);
                }

                return map;
            }

            public static Bitmap DrawPoints(List Data)
            {
                Bitmap re = new Bitmap(600, 600);
       
                    foreach (ScreenData x in Data)
                    {
                        if (x != null)
                        {
                            int BitX = 300 + (int)(100 * (x.X_2dWorld));
                            int BitY = 300 + (int)(100 * (x.Y_2dWorld));

                            if (0 < BitX && 600 > BitX && 0 < BitY && 600 > BitY)
                                re.SetPixel(BitX, BitY, x.color);
                        }
  

                    }
                
                return re;
            }



            public static Bitmap DrawLine(ScreenData point1, ScreenData point2, Pen pen, Bitmap Map)
            {


                int x1;
                int y1;
                int x2 = 0;
                int y2 = 0;

                x2 = 300 + (int)(100 * (point2.X_2dWorld));
                y2 = 300 + (int)(100 * (point2.Y_2dWorld));
                x1 = 300 + (int)(100 * (point1.X_2dWorld));
                y1 = 300 + (int)(100 * (point1.Y_2dWorld));


                using (var graphics = Graphics.FromImage(Map))
                {
                    graphics.DrawLine(pen, x1, y1, x2, y2);
                }
                return Map;
            } 


            public static Bitmap DrawLine(List Data)
            {
                Pen blackPen = new Pen(Color.Black, 1);
                Bitmap re = new Bitmap(600, 600);
                int x1;
                int y1;
                int x2=0;
                int y2=0;
                for(int i=0;i x1 && 0 < y1 && 600 > y1 && 0 < x2 && 600 > x2 && 0 < y2 && 600 > y2)
                    {
                        using (var graphics = Graphics.FromImage(re))
                        {
                            graphics.DrawLine(blackPen, x1, y1, x2, y2);
                        }
                    }
                }
                return re;
            }
        }

你可能感兴趣的:(C# 3D到2D投射)