Click here to Skip to main content
15,884,062 members
Articles / Multimedia / GDI+

Quaternion Mathematics and 3D Library with C# and GDI+

Rate me:
Please Sign up or sign in to vote.
4.91/5 (42 votes)
1 Jun 2009CPOL3 min read 117.6K   7.3K   96  
Rotating 3D objects using Quaternion and Drawing 3D objects with GDI+ Graphics
using System;
using System.Collections.Generic;
using System.Drawing;

namespace YLScsDrawing.Drawing3d
{
    public struct Point3d
    {
        public double X, Y, Z; // coordinate system follows right-hand rule

        public Point3d(double x, double y, double z)
        {
            X = x; Y = y; Z = z;
        }

        public Point3d(Vector3d v)
        {
            X = v.X; Y = v.Y; Z = v.Z;
        }

        public Point3d Copy()
        {
            return new Point3d(this.X, this.Y, this.Z);
        }

        public Vector3d ToVector3d()
        {
            return new Vector3d(X, Y, Z);
        }

        public void Offset(double x, double y, double z)
        {
            this.X += x;
            this.Y += y;
            this.Z += z;
        }

        public static Point3d[] Copy(Point3d[] pts)
        {
            Point3d[] copy = new Point3d[pts.Length];
            for (int i = 0; i < pts.Length; i++)
            {
                copy[i] = pts[i].Copy();
            }
            return copy;
        }

        public static void Offset(Point3d[] pts, double offsetX, double offsetY, double offsetZ)
        {
            for (int i = 0; i < pts.Length; i++)
            {
                pts[i].Offset(offsetX, offsetY, offsetZ);
            }
        }

        public PointF GetProjectedPoint(double d /* project distance: from eye to screen*/)
        {
            return new PointF((float)(this.X * d / (d + this.Z)), (float)(this.Y * d / (d + this.Z)));
        }

        public static PointF[] Project(Point3d[] pts, double d /* project distance: from eye to screen*/)
        {
            PointF[] pt2ds = new PointF[pts.Length];
            for (int i = 0; i < pts.Length; i++)
            {
                pt2ds[i] = pts[i].GetProjectedPoint(d);
            }
            return pt2ds;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions