Click here to Skip to main content
15,886,798 members
Articles / Mobile Apps

A Raytracer for the Compact Framework

Rate me:
Please Sign up or sign in to vote.
4.62/5 (38 votes)
22 Jun 2004CPOL9 min read 101.2K   541   46  
An implementation of a simple raytracer for the CF as an intro to graphics theory.
using System;

namespace raytracer
{
	/// <summary>
	/// vector class.
	/// </summary>
	public class vector
	{
		public vector()
		{
			// clear vector
			m_x=m_y=m_z=0.0f;
		}

		public vector(float x,float y,float z)
		{
			// set members
			m_x=x;
			m_y=y;
			m_z=z;
		}

		public vector(vector vec)
		{
			// copy members
			m_x=vec.m_x;
			m_y=vec.m_y;
			m_z=vec.m_z;
		}

		/// Arithmetic
		
		static public vector operator + (vector v1,vector v2)
		{
			vector result=new vector();

			// perform arithmetic
			result.m_x=v1.m_x+v2.m_x;
			result.m_y=v1.m_y+v2.m_y;
			result.m_z=v1.m_z+v2.m_z;

			return result;
		}

		static public vector operator - (vector v1,vector v2)
		{
			vector result=new vector();

			// perform arithmetic
			result.m_x=v1.m_x-v2.m_x;
			result.m_y=v1.m_y-v2.m_y;
			result.m_z=v1.m_z-v2.m_z;

			return result;
		}

		static public vector operator * (vector v1,vector v2)
		{
			vector result=new vector();

			// perform arithmetic
			result.m_x=v1.m_x*v2.m_x;
			result.m_y=v1.m_y*v2.m_y;
			result.m_z=v1.m_z*v2.m_z;

			return result;
		}

		static public vector operator * (vector vec,float f)
		{
			vector result=new vector();

			// perform arithmetic
			result.m_x=vec.m_x*f;
			result.m_y=vec.m_y*f;
			result.m_z=vec.m_z*f;

			return result;
		}

		static public vector operator / (vector v1,vector v2)
		{
			vector result=new vector();

			// perform arithmetic
			if(v2.m_x!=0.0f)
				result.m_x=v1.m_x/v2.m_x;
			else
				result.m_x=0.0f;

			if(v2.m_y!=0.0f)
				result.m_y=v1.m_y/v2.m_y;
			else
				result.m_y=0.0f;

			if(v2.m_z!=0.0f)
				result.m_z=v1.m_z/v2.m_z;
			else
				result.m_z=0.0f;

			return result;
		}

		/// Return magnitude of vector
		public float magnitude()
		{
			return (float)Math.Sqrt( (double) ( (m_x*m_x) + (m_y*m_y) + (m_z*m_z) ) );
		}

		/// Normalize vector
		public vector normalize()
		{
			vector result=new vector();

			// get magnitude
			float mag=this.magnitude();

			// prevent divide by zero
			if(mag==0.0f)
				return result;

			// divide vector by magnitude
			result.m_x=m_x/mag;
			result.m_y=m_y/mag;
			result.m_z=m_z/mag;

			return result;
		}

		/// Get vector's dot product
		public float dotproduct(vector vec)
		{
			float mag=this.magnitude()*vec.magnitude();

			if(mag!=0.0f)
				return ( (m_x*vec.m_x) + (m_y*vec.m_y) + (m_z*vec.m_z) )/mag;
			else
				return 0.0f;
		}

		// get vector's cross product
		public vector crossproduct(vector vec)
		{
			vector result=new vector();

			result.m_x=(m_y*vec.m_z) - (m_z*vec.m_x);
			result.m_y=(m_z*vec.m_x) - (m_x*vec.m_z);
			result.m_z=(m_x*vec.m_y) - (m_y*vec.m_x);

			return result;
		}

		/// Vector information
		public float m_x,m_y,m_z;
	}
}

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
Software Developer (Senior)
United Kingdom United Kingdom
I was a Reseach Scientist working on pain relief strategies for intractable pain.
When not doing that I am busy trying to finish my PhD in Mathematical Physiology.
When not doing that I write computer gamesSmile | :)

Comments and Discussions