// улучшенный первый алгоритм
        public static void line3(int x0, int y0, int x1, int y1, cls2D_Picture image, clsRGB color)
        {
            bool steep = false;

            if (Math.Abs(x0 - x1) < Math.Abs(y0 - y1))
            {
                swap(ref x0, ref y0);
                swap(ref x1, ref y1);
                steep = true;
            }
            if (x0 > x1)
            {
                swap(ref x0, ref x1);
                swap(ref y0, ref y1);
            }
            for (int x = x0; x <= x1; x++)
            {
                float t = (x - x0) / (float)(x1 - x0);
                int   y = Convert.ToInt32(y0 * (1 - t) + y1 * t);
                if (steep)
                {
                    image.setPixel(y, x, color);
                }
                else
                {
                    image.setPixel(x, y, color);
                }
            }
        }
Beispiel #2
0
        // создание треугольниов с помощью z буфера
        public static void createTriangleWithZBuffer(clsPolygon polygon, cls2D_Picture image, clsRGB colour, clsZbuffer zBuffer)
        {
            minimalX = functionMinimumX(polygon);
            maximalX = functionMaximumX(polygon, image);
            minimalY = functionMinimumY(polygon);
            maximalY = functionMaximumY(polygon, image);
            clsBarycentricCoordinates barycentricPoint = new clsBarycentricCoordinates(polygon);

            for (int x = minimalX; x < maximalX; x++)
            {
                for (int y = minimalY; y < maximalY; y++)
                {
                    barycentricPoint.Calculating_lambda_coefficients(new cls3D_Point(x, y));
                    if (barycentricPoint.Lambda0 >= 0 && barycentricPoint.Lambda1 >= 0 && barycentricPoint.Lambda2 >= 0)
                    {
                        double z = barycentricPoint.Lambda0 * polygon[0].Z + barycentricPoint.Lambda1 * polygon[1].Z + barycentricPoint.Lambda2 * polygon[2].Z;
                        if (z < zBuffer.getZBuffer(x, y))
                        {
                            zBuffer.setZBuffer(x, y, z);
                            image.setPixel(x, y, colour);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public static void createTriangleWithZBufferForTransformationWithModifiedLight(clsPolygonModified polygon, clsNormsPolygon normsPolygon, cls2D_Picture image, clsZbuffer zBuffer, int i)
        {
            minimalX = funcMinimumX(polygon);
            maximalX = funcMaximumX(polygon, image);
            minimalY = funcMinimumY(polygon);
            maximalY = funcMaximumY(polygon, image);
            clsBarycentricCoordinates barycentricPoint = new clsBarycentricCoordinates(polygon);
            double l0 = clsVectorsOperations.scalarMulty(normsPolygon[0]) / (clsVectorsOperations.lengthNorm(normsPolygon[0]) * clsVectorsOperations.lengthVectorLight());
            double l1 = clsVectorsOperations.scalarMulty(normsPolygon[1]) / (clsVectorsOperations.lengthNorm(normsPolygon[1]) * clsVectorsOperations.lengthVectorLight());
            double l2 = clsVectorsOperations.scalarMulty(normsPolygon[2]) / (clsVectorsOperations.lengthNorm(normsPolygon[2]) * clsVectorsOperations.lengthVectorLight());

            for (int x = minimalX; x <= maximalX; x++)
            {
                for (int y = minimalY; y <= maximalY; y++)
                {
                    barycentricPoint.calc_lambda_for_Transformation(x, y);
                    if (barycentricPoint.lambda0 > 0 && barycentricPoint.lambda1 > 0 && barycentricPoint.lambda2 > 0)
                    {
                        double z = barycentricPoint.lambda0 * polygon[0].originalZ + barycentricPoint.lambda1 * polygon[1].originalZ + barycentricPoint.lambda2 * polygon[2].originalZ;
                        if (z < zBuffer.getZBuffer(x, y))
                        {
                            int brightness = (int)Math.Abs((255 * (barycentricPoint.lambda0 * l0 + barycentricPoint.lambda1 * l1 + barycentricPoint.lambda2 * l2)));
                            image.setPixel(x, y, new clsRGB(brightness, 0, 0));
                            zBuffer.setZBuffer(x, y, z);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public static void createTriangleWithZBufferForTransformation(clsPolygonModified polygon, cls2D_Picture image, clsZbuffer zBuffer, int i)
        {
            minimalX = funcMinimumX(polygon);
            maximalX = funcMaximumX(polygon, image);
            minimalY = funcMinimumY(polygon);
            maximalY = funcMaximumY(polygon, image);
            clsBarycentricCoordinates barycentricPoint = new clsBarycentricCoordinates(polygon);

            for (int x = minimalX; x <= maximalX; x++)
            {
                for (int y = minimalY; y <= maximalY; y++)
                {
                    barycentricPoint.calc_lambda_for_Transformation(x, y);
                    if (barycentricPoint.lambda0 > 0 && barycentricPoint.lambda1 > 0 && barycentricPoint.lambda2 > 0)
                    {
                        double z = barycentricPoint.lambda0 * polygon[0].originalZ + barycentricPoint.lambda1 * polygon[1].originalZ + barycentricPoint.lambda2 * polygon[2].originalZ;
                        if (z < zBuffer.getZBuffer(x, y))
                        {
                            zBuffer.setZBuffer(x, y, z);
                            image.setPixel(x, y, new clsRGB((int)Math.Abs(clsVectorsOperations.getCosNormal(clsLoadData.polygonsForTransformation[i]) * 255), 0, 0));
                        }
                    }
                }
            }
        }
 // модифицированный вариант
 public static void line2(int x0, int y0, int x1, int y1, cls2D_Picture image, clsRGB color)
 {
     for (int x = x0; x <= x1; x++)
     {
         float t = (x - x0) / (float)(x1 - x0);
         int   y = Convert.ToInt32(y0 * (1 - t) + y1 * t);
         image.setPixel(x, y, color);
     }
 }
 // простейший алгоритм отрисовки линии
 public static void line1(int x0, int y0, int x1, int y1, cls2D_Picture image, clsRGB color)
 {
     for (float t = 0.0F; t < 1.0F; t += 0.01F)
     {
         int x = Convert.ToInt32(x0 * (1 - t) + x1 * t);
         int y = Convert.ToInt32(y0 * (1 - t) + y1 * t);
         image.setPixel(x, y, color);
     }
 }
Beispiel #7
0
        public static cls2D_Picture drawTopsFromObjectFile(List <cls3D_Point> points)
        {
            cls2D_Picture pointsImage = new cls2D_Picture(1800, 1500);                  // создаем фон изображения

            for (int i = 0; i < points.Count - 1; i++)
            {
                cls3D_Point temp = points[i];
                pointsImage.setPixel(temp.X, temp.Y, new clsRGB(255, 0, 0));     // рисуем ранее записанные точки
            }
            return(pointsImage);
        }
        // алгоритм Брезенхема
        public static void line4(int x0, int y0, int x1, int y1, cls2D_Picture image, clsRGB color)
        {
            bool steep = false;

            if (Math.Abs(x0 - x1) < Math.Abs(y0 - y1))
            {
                swap(ref x0, ref y0);
                swap(ref x1, ref y1);
                steep = true;
            }
            if (x0 > x1)
            {
                swap(ref x0, ref x1);
                swap(ref y0, ref y1);
            }
            int   dx     = x1 - x0;
            int   dy     = y1 - y0;
            float derror = Math.Abs(dy / (float)dx);
            float error  = 0;
            int   y      = y0;

            for (int x = x0; x <= x1; x++)
            {
                if (steep)
                {
                    image.setPixel(y, x, color);
                }
                else
                {
                    image.setPixel(x, y, color);
                }
                error += derror;
                if (error > 0.5)
                {
                    y     += (y1 > y0 ? 1 : -1);
                    error -= 1.0F;
                }
            }
        }
Beispiel #9
0
        // отрисовка красного квадрата
        public static cls2D_Picture createRedImage()
        {
            cls2D_Picture redImage = new cls2D_Picture(widht, height);

            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    redImage.setPixel(i, j, new clsRGB(255, 0, 0));
                }
            }
            return(redImage);
        }
Beispiel #10
0
        // создание треугольников
        public static void createTriangle(clsPolygon polygon, cls2D_Picture image, clsRGB colour)
        {
            minimalX = functionMinimumX(polygon);
            maximalX = functionMaximumX(polygon, image);
            minimalY = functionMinimumY(polygon);
            maximalY = functionMaximumY(polygon, image);
            clsBarycentricCoordinates barycentricPoint = new clsBarycentricCoordinates(polygon);

            for (int x = minimalX; x < maximalX; x++)
            {
                for (int y = minimalY; y < maximalY; y++)
                {
                    barycentricPoint.Calculating_lambda_coefficients(new cls3D_Point(x, y));
                    if (barycentricPoint.Lambda0 >= 0 && barycentricPoint.Lambda1 >= 0 && barycentricPoint.Lambda2 >= 0)
                    {
                        image.setPixel(x, y, colour);
                    }
                }
            }
        }
Beispiel #11
0
        // отрисовка градиентного квадрата
        public static cls2D_Picture createGradImage()
        {
            cls2D_Picture gradImage = new cls2D_Picture(widht, height);                 // задаем размерность картинки

            int[,] grad = new int[widht, height];
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    grad[i, j] = (i + j) % 256;                                 // задаем массив цветовых пикселей
                }
            }
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    gradImage.setPixel(i, j, new clsRGB(grad[i, j], grad[i, j], grad[i, j]));     // отрисовка полученного массива
                }
            }
            return(gradImage);
        }
Beispiel #12
0
        // отрисовка белого квадрата
        public static cls2D_Picture createWhiteImage()
        {
            cls2D_Picture whiteImage = new cls2D_Picture(widht, height);        // задаем размерность картинки

            int[,] white = new int[widht, height];
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    white[i, j] = 255;                                              // залдаем массив белых пискелей
                }
            }
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    whiteImage.setPixel(i, j, new clsRGB(white[i, j], white[i, j], white[i, j]));        // рисуем массив
                }
            }
            return(whiteImage);                      // возвращаем полученное изображдение
        }
Beispiel #13
0
        // отрисовка черного квадрата
        public static cls2D_Picture createBlackImage()
        {
            cls2D_Picture blackImage = new cls2D_Picture(widht, height);            // задаем размерность картинки

            int[,] black = new int[widht, height];
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    black[i, j] = 0;                                                        // задаем массив черных пикселей
                }
            }
            for (int i = 0; i < widht; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    blackImage.setPixel(i, j, new clsRGB(black[i, j], black[i, j], black[i, j]));        // рисуем массив
                }
            }
            return(blackImage);  // возврат изображения
        }