Click here to Skip to main content
15,885,998 members
Articles / Multimedia / GDI+

C# Application to Create and Recognize Mouse Gestures (.NET)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (39 votes)
17 Mar 2008CPOL5 min read 221.4K   8.1K   144  
This program can create and recognize mouse gestures.
using System;
using System.Drawing;
using System.Collections;
using System.Drawing.Drawing2D;

namespace MouseGesture
{
	#region GestureRegularization abstract class
	//The class contains useful functions to manipolate points
	public abstract class GestureRegularization
	{
		#region ReduceGesturePoints static function
		//Remove points from an array points
		public static int ReduceGesturePoints(ArrayList Points)
		{
			//Compute poitns to remove
			int ExcessivePoints=Points.Count-MainBoard.GestureMaximumPoints;

			for(int i=0; i<ExcessivePoints; i++)
			{
				//Compute min segment in arraylist, returning the index of its
				//first point
				int index=GestureRegularization.GetFirstIndexMinSegment(Points);
				//Add mid point of min segment
				GestureRegularization.AddMidPoint(Points,index);
				//Remove points which identified min segment
				GestureRegularization.RemovePoint(Points,index+2);
				GestureRegularization.RemovePoint(Points,index);
			}

			//Return numbers of points removed
			return ExcessivePoints;
		}
		#endregion

		#region ExpandGesturePoints static function
		//Add points to points arraylist
		public static int ExpandGesturePoints(ArrayList Points)
		{
			//Compute missing points
			int GapPoints=MainBoard.GestureMaximumPoints-Points.Count;

			for(int i=0; i<GapPoints; i++)
			{
				//Compute max segment, returning index of its first point
				int index=GestureRegularization.GetFirstIndexMaxSegment(Points);
				//Add max segment mid poitn to arraylist
				GestureRegularization.AddMidPoint(Points,index);
			}

			//Returns added points number
			return GapPoints;
		}
		#endregion

		#region GetFirstIndexMinSegment static function
		//Get min segment among points
		public static int GetFirstIndexMinSegment(ArrayList Points)
		{
			double[] Lenghts=new double[Points.Count-1];
			//Get segments lengths
			Lenghts=GestureRegularization.GetSegmentsLenghts(Points);
			
			//Get min segment, storing index
			int ret=0;
			for(int i=1; i<Lenghts.Length; i++)
			{
				if(Lenghts[ret]>Lenghts[i])
					ret=i;
			}

			//Returns min segment first point
			return ret;
		}
		#endregion

		#region GetFirstIndexMaxSegment static function
		//Get max segment among points
		public static int GetFirstIndexMaxSegment(ArrayList Points)
		{
			double[] Lenghts=new double[Points.Count-1];
			//Get segments lengths
			Lenghts=GestureRegularization.GetSegmentsLenghts(Points);
			
			//Get max segment, storing index
			int ret=0;
			for(int i=1; i<Lenghts.Length; i++)
			{
				if(Lenghts[ret]<Lenghts[i])
					ret=i;
			}

			//Returns max segment first point
			return ret;
		}
		#endregion

		#region RemovePoint static function
		//Remove point from points arraylist
		public static void RemovePoint(ArrayList Points,int Index)
		{
			//It would be a good idea to check Points length... anyway, since
			//in this program gesture re composed by more than 3 points, everything is fine...
			//There could be a better way :P
			if(Index==0) //If index is start, then removes next one
				Points.RemoveAt(1);
			else if(Index==Points.Count-1)	//If index is the last, remove the earlier
				Points.RemoveAt(Points.Count-2);
			else
				Points.RemoveAt(Index);
		}
		#endregion
		
		#region AddMidPoint static function
		//Adds mid point between to points
		public static void AddMidPoint(ArrayList Points,int Index)
		{
			//First mid point can't be the last, so there is no problem using Index+1
			//Get mid point between Index point and Index+1 point
			PointF MidPoint=GestureRegularization.GetMidPoint(((PointF)Points[Index]),((PointF)Points[Index+1]));
			//Insert new point
			Points.Insert(Index+1,MidPoint);
		}
		#endregion

		#region GetSegmentsLenghts static function
		//Get lengths of every segment
		public static double[] GetSegmentsLenghts(ArrayList Points)
		{
			double[] vect=new double[Points.Count-1];
			//For each couple, compute segment length
			for(int i=0; i<Points.Count-1; i++)
				vect[i]=GestureRegularization.GetSegmentLength(((PointF)Points[i]),((PointF)Points[i+1]));

			return vect;
		}
		#endregion

		#region GetSegmentLength static function
		//Get Segment length between 2 points
		public static double GetSegmentLength(PointF pointA, PointF pointB)
		{
			//length=sqrt((x0-x1)^2+(y0-y1)^2)
			double DX2=(pointA.X-pointB.X)*(pointA.X-pointB.X);
			double DY2=(pointA.Y-pointB.Y)*(pointA.Y-pointB.Y);

			return Math.Sqrt(DX2+DY2);		
		}
		#endregion

		#region GetMidPoint static function
		//Get mid point between 2 ponts
		public static PointF GetMidPoint(PointF pointA, PointF pointB)
		{
			float x=((pointA.X+pointB.X)/2);

			float y=((pointA.Y+pointB.Y)/2);

			return new PointF(x,y);
		}
		#endregion

		#region GetMaxDeltaX static function
		//This function gets max delta-x among all points
		public static double GetMaxDeltaX(ArrayList Points)
		{
			double ret=0;
			for(int i=0; i<Points.Count-1; i++)
			{
				//Get x excursion between 2 consecutive points
				double Length=((PointF)Points[i]).X-((PointF)Points[i+1]).X;
				//Gets length positive value
				Length=(Length>0)?(Length):(-Length);
				//Check is this length is best than last one
				if(Length>ret)
					ret=Length;
			}
			//Return max delta x
			return ret;
		}
		#endregion

		#region GetMaxDeltaY static function
		//This function gets max delta-y among all points
		public static double GetMaxDeltaY(ArrayList Points)
		{
			double ret=0;
			for(int i=0; i<Points.Count-1; i++)
			{
				//Get y excursion between 2 consecutive points
				double Length=((PointF)Points[i]).Y-((PointF)Points[i+1]).Y;
				//Gets length positive value
				Length=(Length>0)?(Length):(-Length);
				//Check is this length is best than last one
				if(Length>ret)
					ret=Length;
			}
			//Return max delta y
			return ret;
		}
		#endregion
	}
	#endregion
}

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) Apex s.r.l.
Italy Italy
I got my Computer Science (Engineering) Master's Degree at the Siena University (Italy), but I'm from Rieti (a small town next to Rome).
My hobbies are RPG, MMORGP, programming and 3D graphics.
At the moment I'm employed at Apex s.r.l. (Modena, Italy) as a senior software developer, working for a WPF/WCF project in Rome.

Comments and Discussions