Click here to Skip to main content
15,891,253 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.9K   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 class Shape3d
    {
       protected Point3d[] pts = new Point3d[8];
        public Point3d[] Point3dArray
        {
            get { return pts; }
        }

        protected Point3d center = new Point3d(0, 0, 0);
        public Point3d Center
        {
            set
            {
                double dx = value.X - center.X;
                double dy = value.Y - center.Y;
                double dz = value.Z - center.Z;
                Point3d.Offset(pts, dx, dy, dz);
                center = value;
            }
            get { return center; }
        }

        protected Color lineColor = Color.Black;
        public Color LineColor
        {
            set { lineColor = value; }
            get { return lineColor; }
        }

        public void RotateAt(Point3d pt, Quaternion q)
        {
            // transform origin to pt
            Point3d[] copy = Point3d.Copy(pts);
            Point3d.Offset(copy,  - pt.X,  - pt.Y,  - pt.Z);

            // rotate
            q.Rotate(copy);
            q.Rotate(center);

            // transform to original origin
            Point3d.Offset(copy, pt.X, pt.Y, pt.Z);
            pts = copy;
        }
        
        public virtual void Draw(Graphics g,Camera cam)
        {
        }
    }
}

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