Click here to Skip to main content
15,896,268 members
Articles / Programming Languages / C#

Fuzzy Logic Dot Net Sample

Rate me:
Please Sign up or sign in to vote.
4.58/5 (28 votes)
5 Dec 200513 min read 157.9K   6.4K   82  
A Fuzzy Logic Library in C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;


namespace FuzzyGraphic
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class FuzzyGraphic : System.Windows.Forms.UserControl
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private Color backGroundColor;
		Bitmap bitmap;
		int nBitmapXPos = 0;
		int nBitmapYPos = 0;


		public Color BackGroundColor
		{
			get
			{
				return backGroundColor;
			}
			set
			{
				backGroundColor = value;
			}
		}

		public int BitmapXPos
		{
			get
			{
				return nBitmapXPos;
			}
			set
			{
				nBitmapXPos = value;
			}
		}

		public int BitmapYPos
		{
			get
			{
				return nBitmapYPos;
			}
			set
			{
				nBitmapYPos = value;
			}
		}

		public FuzzyGraphic()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			backGroundColor = Color.Blue;
			bitmap = new Bitmap( "..\\..\\..\\pointer.bmp" );
			bitmap.MakeTransparent( Color.Black ); 			

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// FuzzyGraphic
			// 
			this.Name = "FuzzyGraphic";
			this.Size = new System.Drawing.Size(152, 150);
			this.SizeChanged += new System.EventHandler(this.OnSizeChanged);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);

		}
		#endregion

		private void OnSizeChanged(object sender, System.EventArgs e)
		{
			this.Invalidate();
		}

		private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			Graphics grfx = e.Graphics;
			Pen pen = new Pen( Color.Black );
			SolidBrush brush = new SolidBrush( Color.Black );
			Font font = new Font( this.Font, this.Font.Style );
			int nSize = this.Width / 10;
			int nFontHeight = ( int )this.Font.GetHeight();
		
			/// set the position
			float fUnit = this.Width / 100;
			float fTenth = this.Width / 10;
			float fIncrement = 0;

			if( BitmapXPos > 0 )
			{
				if( BitmapXPos % 10 != 0 )
				{
					fIncrement = ( BitmapXPos / 10 ) * fTenth +  ( ( BitmapXPos % 10 ) * fUnit );
				}
				if( BitmapXPos % 10 == 0 )
				{
					fIncrement = ( BitmapXPos / 10 ) * fTenth;
				}
			}
			else
				fIncrement = 0;




			/// set the background color
			/// Test Code for the background Color
			Color testColor = BackGroundColor;

			int nBlue = testColor.B;
			int nRed = testColor.R;
			int nGreen = testColor.G;

			if( BitmapXPos > 0 && BitmapXPos <= 10 )
			{
				nBlue = 255;
				nRed = 0;
			}
			else if( BitmapXPos > 11 && BitmapXPos <= 20 )
			{
				nBlue = 200;
				nRed = 50;
			}
			else if( BitmapXPos > 21 && BitmapXPos <= 30 )
			{
				nBlue = 175;
				nRed = 100;
			}
			else if( BitmapXPos > 31 && BitmapXPos <= 40 )
			{
				nBlue = 150;
				nRed = 125;

			}
			else if( BitmapXPos > 41 && BitmapXPos <= 50 )
			{
				nBlue = 125;
				nRed = 150;
			}
			else if( BitmapXPos > 51 && BitmapXPos <= 60 )
			{
				nBlue = 100;
				nRed = 175;
			}
			else if( BitmapXPos > 61 && BitmapXPos < 70 )
			{
				nBlue = 75;
				nRed = 200;
			}
			else if( BitmapXPos > 71 && BitmapXPos < 80 )
			{
				nBlue = 50;
				nRed = 225;
			}
			else if( BitmapXPos > 81 && BitmapXPos < 90 )
			{
				nBlue = 25;
				nRed = 255;
			}
			else if( BitmapXPos > 91 )
			{
				nBlue = 0;
				nRed = 255;
			}

			testColor = Color.FromArgb( testColor.A, Color.FromArgb( nRed, testColor.G, nBlue ) ); 

			BackGroundColor = testColor;

			this.BackColor = BackGroundColor;


			/// display the bitmap ( the minused number is to correct for the size of the bitmap )
			grfx.DrawImage( bitmap, fIncrement - 15, BitmapYPos );


			/// draw the grid
			grfx.DrawString( "Temperature Gradient", font, brush, this.Left, this.Top );
			grfx.DrawString( "0", font, brush, this.Left - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left, this.Bottom - 20 - nFontHeight, this.Left, this.Top + nFontHeight + 3 ); 
			grfx.DrawString( "10", font, brush, this.Left + nSize - 5, this.Bottom - 15 - nFontHeight ); 
			grfx.DrawLine( pen, this.Left + nSize, this.Bottom - 20 - nFontHeight, this.Left + nSize, this.Top + nFontHeight + 3 );
			grfx.DrawString( "20", font, brush, this.Left + ( nSize * 2 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 2 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 2 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "30", font, brush, this.Left + ( nSize * 3 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 3 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 3 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "40", font, brush, this.Left + ( nSize * 4 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 4 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 4 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "50", font, brush, this.Left + ( nSize * 5 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 5 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 5 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "60", font, brush, this.Left + ( nSize * 6 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 6 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 6 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "70", font, brush, this.Left + ( nSize * 7 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 7 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 7 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "80", font, brush, this.Left + ( nSize * 8 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 8 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 8 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "90", font, brush, this.Left + ( nSize * 9 ) - 5, this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Left + ( nSize * 9 ), this.Bottom - 20 - nFontHeight, this.Left + ( nSize * 9 ), this.Top + nFontHeight + 3 );
			grfx.DrawString( "100", font, brush, this.Right - ( font.Size * 4 ), this.Bottom - 15 - nFontHeight );
			grfx.DrawLine( pen, this.Right - ( font.Size * 2 ), this.Bottom - 20 - nFontHeight, this.Right - ( font.Size * 2 ), this.Top + nFontHeight + 3 );

		}

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions