public static void line3(int x0, int y0, int x1, int y1, Image2D image, ColorRGB 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); } } }
public static void rawTriangleWithZBuffer(Polygon polygon, Image2D image2D, ColorRGB colorRgb, ZBuffer zBuffer) { int xMin = Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X) < 0 ? 0 : Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X); int xMax = Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X) > image2D.Width ? image2D.Width : Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X); int yMin = Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y) < 0 ? 0 : Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y); int yMax = Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y) > image2D.Height ? image2D.Height : Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y); BarycentricPoint barycentricPoint = new BarycentricPoint(polygon); for (int x = xMin; x < xMax; x++) { for (int y = yMin; y < yMax; y++) { barycentricPoint.calculateLambds(new Point3D(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); image2D.setPixel(x, y, colorRgb); } } } } }
private void button2_Click(object sender, EventArgs e) { int height = Convert.ToInt32(textBox1.Text); int widht = Convert.ToInt32(textBox2.Text); Image2D whiteImage = new Image2D(widht, height); int[,] white = new int[widht, 255]; 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 ColorRGB(white[i, j], white[i, j], white[i, j])); } } Bitmap resultBlackImage = ImageProcessor.Image2DtoBitmap(whiteImage); pictureBox1.Image = resultBlackImage; }
public static void line2(int x0, int y0, int x1, int y1, Image2D image, ColorRGB 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, Image2D image, ColorRGB 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); } }
public static void line4(int x0, int y0, int x1, int y1, Image2D image, ColorRGB 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; } } }
private void rawPoints(object sender, EventArgs e) { Image2D pointsImage = new Image2D(1000, 1000); foreach (Point3D temp in points) { pointsImage.setPixel(temp.X, temp.Y, new ColorRGB(255, 0, 0)); } Bitmap image = ImageProcessor.Image2DtoBitmap(pointsImage); pictureBox1.Image = image; }
private void button3_Click(object sender, EventArgs e) { int height = Convert.ToInt32(textBox1.Text); int widht = Convert.ToInt32(textBox2.Text); Image2D redImage = new Image2D(widht, height); for (int i = 0; i < widht; i++) { for (int j = 0; j < height; j++) { redImage.setPixel(i, j, new ColorRGB(255, 0, 0)); } } Bitmap resultBlackImage = ImageProcessor.Image2DtoBitmap(redImage); pictureBox1.Image = resultBlackImage; }
public static void rawTriangleWithGuruAndZBuffer(Polygon polygon, Image2D image2D, ZBuffer zBuffer) { int xMin = Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X) < 0 ? 0 : Math.Min(Math.Min(polygon[0].X, polygon[1].X), polygon[2].X); int xMax = Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X) > image2D.Width ? image2D.Width : Math.Max(Math.Max(polygon[0].X, polygon[1].X), polygon[2].X); int yMin = Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y) < 0 ? 0 : Math.Min(Math.Min(polygon[0].Y, polygon[1].Y), polygon[2].Y); int yMax = Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y) > image2D.Height ? image2D.Height : Math.Max(Math.Max(polygon[0].Y, polygon[1].Y), polygon[2].Y); BarycentricPoint barycentricPoint = new BarycentricPoint(polygon); double l0 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[0]); double l1 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[1]); double l2 = MatrixUtil.cosDirectionEarthNormal(polygon.Norms[2]); for (int x = xMin; x < xMax; x++) { for (int y = yMin; y < yMax; y++) { barycentricPoint.calculateLambds(new Point3D(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)) { int brightness = (int)Math.Abs((255 * (barycentricPoint.Lambda0 * l0 + barycentricPoint.Lambda1 * l1 + barycentricPoint.Lambda2 * l2))); zBuffer.setZBuffer(x, y, z); image2D.setPixel(x, y, new ColorRGB(brightness, brightness, brightness)); } } } } }