C # Implementation of 3D effect complete instance _c# tutorial

Source: Internet
Author: User
Tags scalar

This article illustrates how C # achieves the 3D effect. Share to everyone for your reference, specific as follows:

A new type of file

private static double[] Addvector (double[] A, double[] b) {return new double[] {a[0] + b[0], a[1] + b[1], a[2] + b[
2]}; private static double[] scalarproduct (double[] vector, double scalar) {return new double[] {vector[0] * scalar, ve
CTOR[1] * scalar, vector[2] * scalar};  } private static Double Dotproduct (double[] A, double[] b) {return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; Static double norm (double[] vector) {return math.sqrt (dotproduct (vector, vector));} private static double[] Normali Ze (double[] vector) {return scalarproduct (vector, 1.0/norm (vector));} private static double[] Crossproduct (double[ ] A, double[] b {return new double[] {(a[1] * b[2]-a[2] * b[1]), (a[2) * B[0]-a[
0] * b[2]), (a[0] * b[1]-a[1] * b[0])};
          private static double[] vectorproductindexed (double[] v, double[] m, int i) {return new double[] { V[i + 0] * M[0] + v[i + 1] * M[4] + V[i + 2] * M[8] + v[i + 3] * m[12], v[i + 0] * M[1] + v[i + 1] * M[5] + v[i + 2] * M[9] + v[i + 3] * m[13),  V[i + 0] * M[2] + v[i + 1] * M[6] + v[i + 2] * m[10]+ V[i + 3] * m[14], v[i + 0] * M[3] + v[i + 1] * M[7]
+ V[i + 2] * m[11]+ V[i + 3] * m[15]}; private static double[] Vectorproduct (double[] v, double[] m) {return vectorproductindexed (V, m, 0);} private Stat
    IC double[] matrixproduct (double[] A, double[] b) {double[] O1 = Vectorproductindexed (A, b, 0);
    double[] O2 = Vectorproductindexed (A, B, 4);
    double[] O3 = Vectorproductindexed (A, B, 8);
    double[] O4 = Vectorproductindexed (A, B, 12);  return new double[] {o1[0], o1[1], o1[2], o1[3], o2[0], o2[1], o2[2], o2[3], o3[0],
O3[1], o3[2], o3[3], o4[0], o4[1], o4[2], o4[3]}; private static double[] Cameratransform (double[] C, double[] A) {double[] W = normalize (Addvector (C, Scalarproduct (
    A,-1)); Double[] y = new double[] {0, 1, 0};
    double[] U = Normalize (Crossproduct (y, W));
    double[] v = crossproduct (w, u);
    double[] t = scalarproduct (C,-1); 
          return new double[] {u[0], v[0], w[0], 0, u[1], v[1], w[1], 0, u[2], v[2], w[2], 0,
Dotproduct (U, t), dotproduct (V, t), Dotproduct (W, t), 1};
    private static double[] Viewingtransform (double FoV, double n, double f) {FoV *= (math.pi/180);
    Double cot = 1.0/math.tan (FOV/2);
return new double[] {cot, 0, 0, 0, 0, cot, 0, 0, 0, 0, (f + N)/(F-n),-1, 0, 0, 2 * f * N/(F-n), 0};
    public static Image Generate (string captchatext) {int fontsize = 24;
    Font font = new Font ("Arial", fontsize);
    SizeF SizeF; using (Graphics g = graphics.fromimage (new Bitmap (1, 1)) {SizeF = g.measurestring (captchatext, font, 0, String
    Format.genericdefault);
    int image2d_x = (int) sizef.width;
int image2d_y = (int) (FontSize * 1.3);    Bitmap image2d = new Bitmap (image2d_x, image2d_y);
    Color black = Color.Black;
    Color white = Color.White;
      using (Graphics g = graphics.fromimage (image2d)) {g.clear (black);
    g.DrawString (Captchatext, Font, brushes.white, 0, 0);
    } Random rnd = new Random (); double[] T = Cameratransform (new double[] {rnd. Next ( -90, -200), Rnd.
    Next (+)}, new double[] {0, 0, 0});
    t = matrixproduct (T, Viewingtransform (60, 300, 3000));
    double[][] coord = new double[image2d_x * image2d_y][];
    int count = 0; for (int y = 0; y < image2d_y; y + 2) {for (int x = 0; x < image2d_x + +) {int XC = x-
        IMAGE2D_X/2;
        int ZC = Y-IMAGE2D_Y/2; Double YC =-(double) (image2d. GetPixel (x, y).
        ToArgb () & 0xff)/256 * 4;
        double[] xyz = new double[] {XC, YC, ZC, 1};
        XYZ = Vectorproduct (xyz, T);
        Coord[count] = xyz;
      count++;
    an int image3d_x = 256; Int image3d_y = image3d_x * 9/16;
    Bitmap Image3d = new Bitmap (image3d_x, image3d_y);
    Color fgcolor = Color.White;
    Color bgcolor = Color.Black;
      using (Graphics g = graphics.fromimage (Image3d)) {g.clear (bgcolor);
      Count = 0;
      Double scale = 1.75-(double) image2d_x/400;  for (int y = 0; y < image2d_y; y + 2) {for (int x = 0; x < image2d_x + +) {if (x
            > 0) {double x0 = coord[count-1][0] * scale + IMAGE3D_X/2;
            Double y0 = coord[count-1][1] * scale + IMAGE3D_Y/2;
            Double x1 = coord[count][0] * scale + IMAGE3D_X/2;
            Double y1 = coord[count][1] * scale + IMAGE3D_Y/2;
          G.drawline (New Pen (Fgcolor), (float) x0, (float) y0, (float) x1, (float) y1);
        } count++;
}} return Image3d;

 }

Note Referencing namespaces:

Using System.Drawing;

Second, the page calls

Response.ContentType = "Image/pjpeg";
Captcha.generate ("I am 3D content"). Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);

Read more about C # Interested readers can view the site topics: "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Data structure and algorithm tutorial", "C # object-oriented Program design Introductory Course" and "C # programming Thread Usage Skill Summary"

I hope this article will help you with C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.