Saturday, June 21, 2014

Opengl,C++ : Draw pattern with Geometric transformation

First specify the starting points using left mouse click.Then press "k" to draw a given pattern.

 #include <windows.h>  
 #include<gl/GL.h>  
 #include<GL/glut.h>  
 #include<cmath>  
   
 double angle=120;
   
 typedef struct {  
      float x;  
      float y;  
 }Point2D;  
   
 Point2D p1,p2,p3;  
   
 void drawLineSegment(Point2D p1,Point2D p2,Point2D p3)  
 {  
      glPointSize(1.0);  
      glBegin(GL_LINES);  
           glVertex2i(p1.x,p1.y);  
           glVertex2i(p2.x,p2.y);  
           glVertex2i(p2.x,p2.y);  
           glVertex2i(p3.x,p3.y);  
      glEnd();  
      glFlush();  
 }  
   
 void drawPolygonSegment(Point2D p1,Point2D p2,Point2D p3)  
 {  
      glPointSize(1.0);  
      glBegin(GL_LINES);  
           glVertex2i(p1.x,p1.y);  
           glVertex2i(p2.x,p2.y);  
           glVertex2i(p2.x,p2.y);  
           glVertex2i(p3.x,p3.y);  
           glVertex2i(p3.x,p3.y);  
           glVertex2i(p1.x,p1.y);  
      glEnd();  
      glFlush();  
 }  
 Point2D translate(Point2D p,float tx,float ty)  
 {  
      p.x=p.x+tx;  
      p.y=p.y+ty;  
      return p;  
 }  
   
 Point2D  rotate(Point2D p, float ang){
    ang = ang * 3.14 / 180.0;                                 //angle in radians
    Point2D ptemp;

    ptemp.x = p.x * cos(ang) - p.y * sin(ang);
    ptemp.y = p.x * sin(ang) + p.y * cos(ang);

	return ptemp;
}
   
 Point2D ref_4(Point2D p)  
 {  
      p.x=-p.x;  
      p.y=p.y;  
   
      return p;  
 }  
   
 Point2D Scale(Point2D p,float sx,float sy)  
 {  
      p.x=p.x*sx;  
      p.y=p.y*sy;  
   
      return p;  
 }  
   
 void mouse(int button,int state,int x,int y){   
    if(state == GLUT_DOWN) {   
     if(button == GLUT_LEFT_BUTTON) {   
                
                    p1.x=0;   
                    p1.y=0;   
                    p2.x=75;   
                    p2.y=200;   
                    p3.x=0;   
                    p3.y=200;   
                       
       }   
    }   
  }   
     
 void trans()
{
	drawLineSegment(translate(p1,500,500),translate(p2,500,500),translate(p3,500,500));

	Point2D scale1_p1=translate(Scale(p1,0.75,0.75),0,200);
	Point2D scale1_p2=translate(Scale(p2,0.75,0.75),0,200);
	Point2D scale1_p3=translate(Scale(p3,0.75,0.75),0,200);
	drawLineSegment(translate(scale1_p1,500,500),translate(scale1_p2,500,500),translate(scale1_p3,500,500));

	Point2D scale2_p1=translate(Scale(p1,0.5,0.5),0,350);
	Point2D scale2_p2=translate(Scale(p2,0.5,0.5),0,350);
	Point2D scale2_p3=translate(Scale(p3,0.5,0.5),0,350);
	drawLineSegment(translate(scale2_p1,500,500),translate(scale2_p2,500,500),translate(scale2_p3,500,500));

	Point2D ref_p1=ref_4(p1);
	Point2D ref_p2=ref_4(p2);
	Point2D ref_p3=ref_4(p3);
	drawLineSegment(translate(ref_p1,500,500),translate(ref_p2,500,500),translate(ref_p3,500,500));

	Point2D refscale1_p1=ref_4(scale1_p1);
	Point2D refscale1_p2=ref_4(scale1_p2);
	Point2D refscale1_p3=ref_4(scale1_p3);
	drawLineSegment(translate(refscale1_p1,500,500),translate(refscale1_p2,500,500),translate(refscale1_p3,500,500));

	Point2D refscale2_p1=ref_4(scale2_p1);
	Point2D refscale2_p2=ref_4(scale2_p2);
	Point2D refscale2_p3=ref_4(scale2_p3);
	drawLineSegment(translate(refscale2_p1,500,500),translate(refscale2_p2,500,500),translate(refscale2_p3,500,500));

	int i=1;
	while(i<=2){

		Point2D rotate_p1=rotate(p1,angle*i);
		Point2D rotate_p2=rotate(p2,angle*i);
		Point2D rotate_p3=rotate(ref_p2,angle*i);
		drawPolygonSegment(translate(rotate_p1,500,500),translate(rotate_p2,500,500),translate(rotate_p3,500,500));

		Point2D rotateScale1_p1=rotate(scale1_p1,angle*i);
		Point2D rotateScale1_p2=rotate(scale1_p2,angle*i);
		Point2D rotateScale1_p3=rotate(refscale1_p2,angle*i);
		drawPolygonSegment(translate(rotateScale1_p1,500,500),translate(rotateScale1_p2,500,500),translate(rotateScale1_p3,500,500));

		Point2D rotateScale2_p1=rotate(scale2_p1,angle*i);
		Point2D rotateScale2_p2=rotate(scale2_p2,angle*i);
		Point2D rotateScale2_p3=rotate(refscale2_p2,angle*i);
		drawPolygonSegment(translate(rotateScale2_p1,500,500),translate(rotateScale2_p2,500,500),translate(rotateScale2_p3,500,500));
		i++;
	}
}
 void key(unsigned char key,int x,int y)  
 {  
      if(key=='k')glutIdleFunc(trans);  
 }  
  void myDisplay(){   
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);   
    glClear(GL_COLOR_BUFFER_BIT);   
    glColor3f(0.0f, 0.0f, 0.0f);   
  }   
     
  int main( int argc, char ** argv ) {   
    glutInit( &argc, argv );   
    glutInitWindowPosition( 0, 0 );   
    glutInitWindowSize( 1000, 1000 );   
    glutCreateWindow( "My Drawing Screen" );   
     glMatrixMode( GL_PROJECTION );   
    glLoadIdentity();   
    gluOrtho2D( 0, 1000, 0, 1000 );   
    glViewport(0, 0, 1000, 1000);   
    glutDisplayFunc( myDisplay );   
     glutMouseFunc( mouse );   
     glutKeyboardFunc(key);   
    glutMainLoop();   
    return( 0 );   
  }   

Friday, June 20, 2014

Opengl,C++ : Window-To_Viewport Mapping

To get the display

 #include <GL/glut.h>  
 const int screenWidth = 640;  
 const int screenHeight = 480;      
 // defining the Window  
 void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)  
 {  
      glMatrixMode(GL_PROJECTION);  
      glLoadIdentity();  
      gluOrtho2D(left, right, bottom, top);  
 }  
 // setting the Viewport  
 void setViewport(int left, int right, int bottom, int top)  
 {  
      glViewport(left, bottom, right - left, top - bottom);  
 }  
 void myInit(void)   
 {  
      glClearColor (1.0, 1.0, 1.0, 0.0);  // set black background color  
      glColor3f (1.0f, 0.0f, 0.0f);    // set the drawing color  
 }  
 void drawShape()  
 {       
      glBegin(GL_LINE_LOOP);  
           glVertex2i(150,100);  
           glVertex2i(350, 100);  
           glVertex2i(400, 300);  
           glVertex2i(500, 300);  
           glVertex2i(250,450);  
           glVertex2i(00, 300);  
           glVertex2i(100, 300);  
      glEnd();  
      glFlush();  
 }  
 void myDisplay(void)  
 {  
      glClear(GL_COLOR_BUFFER_BIT);          // clear the screen  
       // set the window  
      int bottomX = 0;  
      int bottomY = 0;  
      int width = 64;  
      int height = 48;  
      int i=0;  
      int j=0;  
      //setViewport( left, right, bottom, top)  
      for(int bottomX=0;bottomX<640;bottomX+=width)  
      {  
           for(int bottomY=0;bottomY<480;bottomY+=height)  
           {  
                setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport  
                if((j%2==0 || i%2==1) && (j%2==1 || i%2==0)){  
                setWindow(0.0, 640.0, 0.0, 480.0);   
                drawShape();  
                }  
                else{  
                     setWindow(0.0,640.0, 480.0, 0.0);   
                     drawShape();  
                }  
                     j++;  
           }  
           i++;  
      }  
 }  
 int main(int argc, char** argv)  
 {  
      glutInit(&argc,argv);  
      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
      glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size  
      glutCreateWindow("Window-To-Viewport Mapping");  
      glutDisplayFunc(myDisplay);  
      myInit();  
      glutMainLoop();  
      return 0;  
 }  

Opengl,C++ : Window-To_Viewport Mapping

To get the display

 #include <GL/glut.h>  
 const int screenWidth = 640;  
 const int screenHeight = 480;      
 // defining the Window  
 void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)  
 {  
      glMatrixMode(GL_PROJECTION);  
      glLoadIdentity();  
      gluOrtho2D(left, right, bottom, top);  
 }  
 // setting the Viewport  
 void setViewport(int left, int right, int bottom, int top)  
 {  
      glViewport(left, bottom, right - left, top - bottom);  
 }  
 void myInit(void)   
 {  
      glClearColor (1.0, 1.0, 1.0, 0.0);  // set black background color  
      glColor3f (1.0f, 0.0f, 0.0f);    // set the drawing color  
 }  
 void drawShape()  
 {       
      glBegin(GL_LINE_LOOP);  
           glVertex2i(150,100);  
           glVertex2i(350, 100);  
           glVertex2i(400, 300);  
           glVertex2i(500, 300);  
           glVertex2i(250,450);  
           glVertex2i(00, 300);  
           glVertex2i(100, 300);  
      glEnd();  
      glFlush();  
 }  
 void myDisplay(void)  
 {  
      glClear(GL_COLOR_BUFFER_BIT);          // clear the screen  
      setWindow(0.0, 640.0, 0.0, 480.0); // set the window  
      int bottomX = 0;  
      int bottomY = 0;  
      int width = 64;  
      int height = 48;  
      int i=0;  
      int j=0;  
      //setViewport( left, right, bottom, top)  
      for(int bottomX=0;bottomX<640;bottomX+=width)  
      {  
           for(int bottomY=0;bottomY<480;bottomY+=height)  
           {  
                setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport                 
                drawShape();  
           }  
      }  
 }  
 int main(int argc, char** argv)  
 {  
      glutInit(&argc,argv);  
      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
      glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size  
      glutCreateWindow("Window-To-Viewport Mapping");  
      glutDisplayFunc(myDisplay);  
      myInit();  
      glutMainLoop();  
      return 0;  
 }  

Opengl,C++ : Window-To_Viewport Mapping

 #include <GL/glut.h>  
 const int screenWidth = 640;  
 const int screenHeight = 480;      
 // defining the Window  
 void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)  
 {  
      glMatrixMode(GL_PROJECTION);  
      glLoadIdentity();  
      gluOrtho2D(left, right, bottom, top);  
 }  
 // setting the Viewport  
 void setViewport(int left, int right, int bottom, int top)  
 {  
      glViewport(left, bottom, right - left, top - bottom);  
 }  
 void myInit(void)   
 {  
      glClearColor (1.0, 1.0, 1.0, 0.0);  // set black background color  
      glColor3f (1.0f, 0.0f, 0.0f);    // set the drawing color  
 }  
 void drawShape()  
 {       
      glBegin(GL_LINE_LOOP);  
           glVertex2i(150,100);  
           glVertex2i(350, 100);  
           glVertex2i(400, 300);  
           glVertex2i(500, 300);  
           glVertex2i(250,450);  
           glVertex2i(00, 300);  
           glVertex2i(100, 300);  
      glEnd();  
      glFlush();  
 }  
 void myDisplay(void)  
 {  
      glClear(GL_COLOR_BUFFER_BIT);          // clear the screen  
      setWindow(0.0, 640.0, 0.0, 480.0); // set the window  
      int bottomX = 0;  
      int bottomY = 0;  
      int width = 64;  
      int height = 48;  
      //setViewport( left, right, bottom, top)  
      setViewport(bottomX, bottomX + width, bottomY, bottomY + height); // set the viewport  
      drawShape();  
 }  
 int main(int argc, char** argv)  
 {  
      glutInit(&argc,argv);  
      glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
      glutInitWindowSize(screenWidth,screenHeight); //setting the SCREEN WINDOW size  
      glutCreateWindow("Window-To-Viewport Mapping");  
      glutDisplayFunc(myDisplay);  
      myInit();  
      glutMainLoop();  
      return 0;  
 }  

Saturday, June 14, 2014

BulletedList Control

Properties
1. BulletStyle
2. FirstBulletNumber
3. DisplayMode-Text, HyperLink or LinkButton.
4. Traget- used when DisplayMode is HyperLink.

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_28_BulletedList.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:BulletedList ID="BulletedList1" runat="server" DisplayMode="LinkButton"   
       onclick="BulletedList1_Click">  
       <asp:ListItem Value="1">UK</asp:ListItem>  
       <asp:ListItem Value="2">US</asp:ListItem>  
       <asp:ListItem Value="3">India</asp:ListItem>  
       <asp:ListItem Value="4">France</asp:ListItem>  
     </asp:BulletedList>  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _28_BulletedList  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void BulletedList1_Click(object sender, BulletedListEventArgs e)  
     {  
       ListItem li=BulletedList1.Items[e.Index];  
       Response.Write("Text = " + li.Text + ", ");  
       Response.Write("Value = " + li.Value + ", ");  
       Response.Write("Index = " + e.Index.ToString());  
       Response.Write("<br/>");  
     }  
   }  
 }  

RadioButtonList

Usefull Properties
1. RepeatDirection
2. RepeatColumns
3. RepeatLayout
4. SelectedItem
5. SelectedIndex
Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_27_RadiobuttonList.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:RadioButtonList ID="RadioButtonList1" runat="server"   
       RepeatDirection="Horizontal">  
       <asp:ListItem Value="1">Red</asp:ListItem>  
       <asp:ListItem Value="2">Green</asp:ListItem>  
       <asp:ListItem Value="3">Blue</asp:ListItem>  
       <asp:ListItem Value="4">Orange</asp:ListItem>  
     </asp:RadioButtonList>  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
 &nbsp;&nbsp;  
     <asp:Button ID="Button2" runat="server" onclick="Button2_Click"   
       Text="Clear Selection" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _27_RadiobuttonList  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
        if (RadioButtonList1.SelectedValue!=null)  
         {  
           Response.Write("Text = " + RadioButtonList1.SelectedItem.Text + ", ");  
           Response.Write("Value = " + RadioButtonList1.SelectedItem.Value + ", ");  
           Response.Write("Index = " + RadioButtonList1.SelectedIndex.ToString());  
           Response.Write("<br/>");  
         }  
     }  
     protected void Button2_Click(object sender, EventArgs e)  
     {  
       RadioButtonList1.SelectedIndex = -1;  
     }  
   }  
 }  

Checkboxlist and Listbox Real Time example

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_26.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:CheckBoxList ID="CheckBoxList1" runat="server"   
       RepeatDirection="Horizontal" AutoPostBack="True"   
       onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">  
       <asp:ListItem Value="1">Diploma</asp:ListItem>  
       <asp:ListItem Value="2">Graduate</asp:ListItem>  
       <asp:ListItem Value="3">Post Graduate</asp:ListItem>  
       <asp:ListItem Value="4">Doctrate</asp:ListItem>  
     </asp:CheckBoxList>  
     <br />  
     <asp:ListBox ID="ListBox1" runat="server" Height="92px"   
       SelectionMode="Multiple" Width="110px"></asp:ListBox>  
     <br />  
     <br />  
     <asp:Label ID="Label1" runat="server"></asp:Label>  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _26  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)  
     {  
       ListBox1.Items.Clear();  
       foreach (ListItem li in CheckBoxList1.Items)  
       {  
         if (li.Selected)  
         {  
           ListBox1.Items.Add(li.Text);  
         }  
       }  
       if (CheckBoxList1.SelectedIndex == -1)  
       {  
         Label1.ForeColor = System.Drawing.Color.Red;  
       }  
       else  
       {  
         Label1.ForeColor = System.Drawing.Color.Black;  
       }  
       Label1.Text = ListBox1.Items.Count.ToString()+" items selected";  
     }  
   }  
 }  

ListBox Control

Properties
1. SelectionMode:-By default selection mode is single. Which means user can select one list item. When try to select other item it clear the selection of the other item.
2. To select multiple items:- properties--> SelectionMode=multiple
Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_25_ListBox.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">  
       <asp:ListItem Value="1">Diploma</asp:ListItem>  
       <asp:ListItem Value="2">Graduate</asp:ListItem>  
       <asp:ListItem Value="3">Post Graduate</asp:ListItem>  
       <asp:ListItem Value="4">Doctrate</asp:ListItem>  
     </asp:ListBox>  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _25_ListBox  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       foreach (ListItem li in ListBox1.Items)  
       {  
         if (li.Selected)  
         {  
           Response.Write("Text = " + li.Text + ", ");  
           Response.Write("Value = " + li.Value + ", ");  
           Response.Write("Index = " + ListBox1.Items.IndexOf(li).ToString());  
           Response.Write("<br/>");  
         }  
       }  
     }  
   }  
 }  

Select or Deselect All list items in CheckBoxlist

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_24_Select_Deselect.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:Button ID="Button2" runat="server" onclick="Button2_Click1"   
       Text="Select All" />  
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
     <asp:Button ID="Button3" runat="server" onclick="Button3_Click"   
       Text="Deselect All" />  
     <br />  
     <asp:CheckBoxList ID="CheckBoxList1" runat="server">  
       <asp:ListItem Value="1">Diploma</asp:ListItem>  
       <asp:ListItem Value="2">Graduate</asp:ListItem>  
       <asp:ListItem Value="3">Post Graduate</asp:ListItem>  
       <asp:ListItem Value="4">Doctrate</asp:ListItem>  
     </asp:CheckBoxList>  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
     <br />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _24_Select_Deselect  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         CheckBoxList1.SelectedValue = "2"; //When the page is loaded Graduate will be selected using list item value  
         //CheckBoxList1.SelectedIndex = 0;  //When the page is loaded Diploma will be selected using list item index  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       foreach (ListItem li in CheckBoxList1.Items)  
       {  
         if (li.Selected)  
         {  
           Response.Write(li.Text + ", ");  
         }  
       }  
     }  
     protected void Button2_Click1(object sender, EventArgs e)  
     {  
       foreach (ListItem li in CheckBoxList1.Items)  
       {  
         li.Selected = true;  
       }  
     }  
     protected void Button3_Click(object sender, EventArgs e)  
     {  
       foreach (ListItem li in CheckBoxList1.Items)  
       {  
         li.Selected = false;  
       }  
     }  
   }  
 }  

CheckBoxlist Control

If we want the user to select more than one option, then a CheckBoxList control can be used.
Usefull properties

1. RepeatDirection
2. RepeatColumns
3. Enabled
4. SelectedIndex :- If we select the multiple item It only returns the selected index of the one list item object (lowest level index).
5. SelectedValue :- If we select the multiple item It only returns the selected value of the one list item object (lowest level index).
6. SelectedItem :- It returns only one item.

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_23_CheckBoxList.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2"   
       RepeatDirection="Horizontal">  
       <asp:ListItem Value="1">Diploma</asp:ListItem>  
       <asp:ListItem Value="2">Graduate</asp:ListItem>  
       <asp:ListItem Value="3">Post Graduate</asp:ListItem>  
       <asp:ListItem Value="4">Doctrate</asp:ListItem>  
     </asp:CheckBoxList>  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
     <br />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _23_CheckBoxList  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       if (CheckBoxList1.SelectedItem != null)  
       {  
          Response.Write(CheckBoxList1.SelectedItem.Text);  
       }  
       /*foreach (ListItem li in CheckBoxList1.Items)  
       {  
         if (li.Selected)  
         {  
           Response.Write("Text = " + li.Text + ", ");  
           Response.Write("Value = " + li.Value + ", ");  
           Response.Write("Index = " + CheckBoxList1.Items.IndexOf(li));  
           Response.Write("<br/>");  
         }  
       }*/  
     }  
   }  
 }  

Retrieving selected Item text, value, and index of an asp.net dropdownlist

DropDownList Selected Item
To retrieve
1. selected item Text :- DropDownList1.SelectedIndex.Text
2. selected item Value :- DropDownList1.SelectedIndex.value or DropDownList1.SelectedValue
3. selected item index :- DropDownList1.SelectedIndex

The SelectedIndex and SelectedValue properties of the Dropdownlist can also be used to have list item selected in the Dropdownlist.

Example:
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_21_dropdownlist.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
       <asp:ListItem Value="-1">Select</asp:ListItem>  
       <asp:ListItem Value="1">Asia</asp:ListItem>  
       <asp:ListItem Value="2">Europe</asp:ListItem>  
       <asp:ListItem Value="3">Africa</asp:ListItem>  
     </asp:DropDownList>  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _21_dropdownlist  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)//Only during be the Initial page load when the page first load  
       {  
         DropDownList1.SelectedIndex = 1; //Asia will be selected by default  
         //DropDownList1.SelectedValue = "1";  
       }  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       if (DropDownList1.SelectedValue == "-1")  
         Response.Write("Plese select the Country");  
       else  
       {  
         Response.Write("Text is "+DropDownList1.SelectedItem.Text+"<br/>");  
         Response.Write("Value is "+DropDownList1.SelectedItem.Value + "<br/>");  
         Response.Write("Index is "+DropDownList1.SelectedIndex + "<br/>");  
       }  
     }  
   }  
 }  

Wednesday, June 11, 2014

Opengl,C++ : Draw pattern with Geometric transformation

First specify the starting points using left mouse click.Then press "k" to draw a given pattern.

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
    
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 double angle = 90;   
 double sangle = 45; //The angle for the rotation (in degrees)  
   
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
   
 Point2D p1,p2,p3,p4,sp1,sp2,sp3;  
 void DrawpolygonSegment(Point2D pt1, Point2D pt2,Point2D pt3,Point2D pt4)  
 {  
        glPointSize(1.0);  
     glBegin(GL_LINES);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
             glVertex2i(pt4.x, pt4.y);  
             glVertex2i(pt1.x, pt1.y);  
     glEnd();  
     glFlush();  
   
 }  
 void DrawLineSegment(Point2D pt1, Point2D pt2,Point2D pt3)  
 {  
      glLineWidth(1.0);  
      glBegin(GL_LINES);  
           glVertex2i(pt1.x, pt1.y);  
           glVertex2i(pt2.x, pt2.y);  
      glBegin(GL_LINES);  
           glVertex2i(pt2.x, pt2.y);  
           glVertex2i(pt3.x, pt3.y);  
      glEnd();  
      glFlush();  
   
 }  
   
   
 Point2D translate(Point2D p, float tx, float ty){  
     p.x =p.x+tx; //.....wite the equations for translation   
     p.y = p.y+ty; //.....wite the equations for translation  
     return p;  
 }  
   
 Point2D rotate(Point2D p,Point2D c, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   
      ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);  
   
      return ptemp;  
 }  
   
 Point2D reflect_4(Point2D p){  
      Point2D ptemp;  
      ptemp.x=p.x;  
      ptemp.y=-p.y;  
      return ptemp;  
 }  
   
 Point2D Scale(Point2D p,double sx,double sy)  
 {  
      Point2D sp;  
      sp.x=sx*(p.x+90);  
      sp.y=sy*(p.y);  
      return sp;  
 }  
   
 void trans()  
 {  
      Point2D Trans_p1 = translate(p1,300,300);  
      Point2D Trans_p2 = translate(p2,300,300);  
   Point2D Trans_p3 = translate(p3,300,300);  
      DrawLineSegment(Trans_p1,Trans_p2,Trans_p3);  
        
      Point2D Scale_p1 = translate(Scale(p1,0.5,0.5),300,300);  
      Point2D Scale_p2 = translate(Scale(p2,0.5,0.5),300,300);  
   Point2D Scale_p3 = translate(Scale(p3,0.5,0.5),300,300);  
      DrawLineSegment(Scale_p1,Scale_p2,Scale_p3);  
   
      Point2D ref4_p1 =translate(reflect_4(p1),300,300);  
      Point2D ref4_p2 = translate(reflect_4(p2),300,300);  
   Point2D ref4_p3 = translate(reflect_4(p3),300,300);  
      DrawLineSegment(ref4_p1,ref4_p2,ref4_p3);  
   
      Point2D refScale4_p1 =translate(reflect_4(Scale(p1,0.5,0.5)),300,300);  
      Point2D refScale4_p2 = translate(reflect_4(Scale(p2,0.5,0.5)),300,300);  
   Point2D refScale4_p3 = translate(reflect_4(Scale(p3,0.5,0.5)),300,300);  
      DrawLineSegment(refScale4_p1,refScale4_p2,refScale4_p3);  
   
      //Point2D rotate_p1 =rotate(Trans_p1,angle);  
      int i=1;  
      while(i<=3){  
           Point2D rotate_p2 =rotate(Trans_p2,Trans_p1,angle*i);  
           Point2D rotate_p3 =rotate(Trans_p3,Trans_p1,angle*i);  
           Point2D rotate_p4 =rotate(ref4_p2,Trans_p1,angle*i);                 
           DrawpolygonSegment(Trans_p1,rotate_p2,rotate_p3,rotate_p4);  
   
           Point2D rotate_Scalep1 =rotate(Scale_p1,Trans_p1,angle*i);  
           Point2D rotate_Scalep2 =rotate(Scale_p2,Trans_p1,angle*i);  
           Point2D rotate_Scalep3 =rotate(Scale_p3,Trans_p1,angle*i);  
           Point2D rotate_Scalep4 =rotate(refScale4_p2,Trans_p1,angle*i);            
           DrawpolygonSegment(rotate_Scalep1,rotate_Scalep2,rotate_Scalep3,rotate_Scalep4);  
   
           i++;  
      }  
   
           sp1.x=0;  
           sp1.y=0;  
           sp2.x=100;  
           sp2.y=16;  
           sp3.x=175;  
           sp3.y=0;  
      i=1;  
   
      while(i<=4){  
                if(i==1)sangle=45;  
                if(i==2)sangle=135;  
                if(i==3)sangle=225;  
                if(i==4)sangle=315;  
                Point2D Trans_sp1 = translate(sp1,300,300);  
                Point2D Trans_sp2 = translate(sp2,300,300);  
                Point2D Trans_sp3 = translate(sp3,300,300);  
                //DrawLineSegment(Trans_sp1,Trans_sp2,Trans_sp3);  
   
                Point2D rotate_sp2 =rotate(Trans_sp2,Trans_sp1,sangle);  
                Point2D rotate_sp3 =rotate(Trans_sp3,Trans_sp1,sangle);  
   
                DrawLineSegment(Trans_p1,rotate_sp2,rotate_sp3);  
   
                Point2D ref4_sp1 =translate(reflect_4(sp1),300,300);  
                Point2D ref4_sp2 =translate( reflect_4(sp2),300,300);  
                Point2D ref4_sp3 =translate( reflect_4(sp3),300,300);  
   
                Point2D rotateref_sp2 =rotate(ref4_sp2,Trans_sp1,sangle);  
                Point2D rotateref_sp3 =rotate(ref4_sp3,Trans_sp1,sangle);  
   
                DrawLineSegment(Trans_p1,rotateref_sp2,rotateref_sp3);  
                     i++;  
      }  
 }  
   
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='k') glutIdleFunc(trans);  
 }  
   
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
                         
                                     p1.x=0;  
                                     p1.y=0;  
                                     p2.x=70;  
                                     p2.y=40;  
                                     p3.x=300;  
                                     p3.y=0;  
                                     //DrawLineSegment(p1,p2,p3);  
           }  
      }  
 }  
   
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
    
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
     glutDisplayFunc( myDisplay );  
        glutMouseFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

Monday, June 2, 2014

Opengl,C++ : Rotate and Translate Squre with respect to origin

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 double angle = 30;   
 //The angle for the rotation (in degrees)  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
 Point2D p1,p2,p3,p4;  
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
      glClear(GL_COLOR_BUFFER_BIT);       
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
        glVertex2i(pt1.x, pt1.y);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
 }  
 Point2D translate(Point2D p, float tx, float ty){  
     p.x =p.x; //.....wite the equations for translation   
     p.y = p.y; //.....wite the equations for translation  
     return p;  
 }  
 Point2D rotate(Point2D p,Point2D c,float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);  
      return ptemp;  
 }  
 void myMouse()  
 {  
      glClear(GL_COLOR_BUFFER_BIT);  
                               p1.x = 200;  
                               p1.y =200;  
                               p2.x = 400;  
                               p2.y = 200;  
                               p3.x = 400;  
                               p3.y =400;  
                               p4.x=200;  
                               p4.y=400;  
                               DrawPolygonSegment(p1,p2,p3,p4);  
 }  
 void translatepolygon_R()  
 {  
      Point2D c;  
      c.x=(p1.x+p2.x)/2;  
      c.y=(p1.y+p4.y)/2;  
      Point2D Rotated_p1 = rotate(p1,c,angle);  
   Point2D Rotated_p2 = rotate(p2,c,angle);  
      Point2D Rotated_p3 = rotate(p3,c,angle);  
      Point2D Rotated_p4 = rotate(p4,c,angle);  
   DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);  
 }  
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='R') glutIdleFunc(translatepolygon_R);  
      angle=angle+4;  
      p1.x=p1.x+4;  
      p2.x=p2.x+4;  
      p3.x=p3.x+4;  
      p4.x=p4.x+4;  
 }  
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
        glutDisplayFunc( myDisplay );  
     glutDisplayFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

Opengl,C++ : Rotate Squre with respect to its centre

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 double angle = 30;   
 //The angle for the rotation (in degrees)  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
 Point2D p1,p2,p3,p4;  
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glClear(GL_COLOR_BUFFER_BIT);
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
        glVertex2i(pt1.x, pt1.y);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
 }  
 Point2D rotate(Point2D p,Point2D c,float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   ptemp.x = p.x * cos(ang) - p.y * sin(ang)+(c.x- cos(ang)*c.x +sin(ang)*c.y);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang)+(c.y- sin(ang)*c.x-cos(ang)*c.y);  
      return ptemp;  
 }  
 void myMouse()  
 {  
      glClear(GL_COLOR_BUFFER_BIT);  
                               p1.x = 200;  
                               p1.y =200;  
                               p2.x = 400;  
                               p2.y = 200;  
                               p3.x = 400;  
                               p3.y =400;  
                               p4.x=200;  
                               p4.y=400;  
                               DrawPolygonSegment(p1,p2,p3,p4);  
 }  
 void translatepolygon_R()  
 {  
      Point2D c;  
      c.x=(p1.x+p2.x)/2;  
      c.y=(p1.y+p4.y)/2;  
      Point2D Rotated_p1 = rotate(p1,c,angle);  
   Point2D Rotated_p2 = rotate(p2,c,angle);  
      Point2D Rotated_p3 = rotate(p3,c,angle);  
      Point2D Rotated_p4 = rotate(p4,c,angle);  
   DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);  
 }  
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='R') glutIdleFunc(translatepolygon_R);  
      angle=angle+4;  
 }  
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
        glutDisplayFunc( myDisplay );  
     glutDisplayFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

Opengl,C++ : Rotate Squre with respect to the origin

 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 double angle = 30;   
 //The angle for the rotation (in degrees)  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
 Point2D p1,p2,p3,p4;  
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
        glVertex2i(pt1.x, pt1.y);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
 }  
 Point2D rotate(Point2D p, float ang){  
   ang = ang * 3.14 / 180.0;                 //angle in radians  
   Point2D ptemp;  
   ptemp.x = p.x * cos(ang) - p.y * sin(ang);  
   ptemp.y = p.x * sin(ang) + p.y * cos(ang);  
      return ptemp;  
 }  
 void myMouse()  
 {  
      glClear(GL_COLOR_BUFFER_BIT);  
                               p1.x = 200;  
                               p1.y =200;  
                               p2.x = 400;  
                               p2.y = 200;  
                               p3.x = 400;  
                               p3.y =400;  
                               p4.x=200;  
                               p4.y=400;  
                               DrawPolygonSegment(p1,p2,p3,p4);  
 }  
 void translatepolygon_R()  
 {  
      Point2D Rotated_p1 = rotate(p1,angle);  
   Point2D Rotated_p2 = rotate(p2,angle);  
      Point2D Rotated_p3 = rotate(p3,angle);  
      Point2D Rotated_p4 = rotate(p4,angle);  
   DrawPolygonSegment(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);  
 }  
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='R') glutIdleFunc(translatepolygon_R);  
 }  
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
        glutDisplayFunc( myDisplay );  
     glutDisplayFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

Sunday, June 1, 2014

Mapping virtual path to physical path using Server.MapPath method

Server.MapPath() returns the physical path for a given virtual path. This method can be used in several different ways, depending on the characters that we passed into this method.
Server.MapPath(“.”) --> Current physical directory of the page that you are running
Server.MapPath(“..”)--> Parent physical directory of the page that you are running
Server.MapPath(“~”) --> The physical path of the root directory of the application.
Example:
PageInElectronicFolder.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageInElectronicFolder.aspx.cs" Inherits="_19_MappingVirtualpath_Physicalpath.Categories.Electronics.PageInElectronicFolder" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
     </asp:DropDownList>  
   </div>  
   </form>  
 </body>  
 </html>  

PageInElectronicFolder.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 namespace _19_MappingVirtualpath_Physicalpath.Categories.Electronics  
 {  
   public partial class PageInElectronicFolder : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       DataSet DS = new DataSet();//To read the xml file  
       string strPhysicalPath = Server.MapPath("~/Data/Countries/Countries.xml");// "~/Data/Countries/Countries.xml" is a virtual path  
                                          // "~" is used to get the root directory of the Application project  
       DS.ReadXml(strPhysicalPath);  
       DropDownList1.DataTextField = "CountryName";  
       DropDownList1.DataValueField = "CountryId";  
       DropDownList1.DataSource = DS;  
       DropDownList1.DataBind();  
       ListItem li = new ListItem("Select", "-1");  
       //DropDownList1.Items.Add(li);  
       DropDownList1.Items.Insert(0, li);  
     }  
   }  
 }  

Countries.xml :-
 <?xml version="1.0" encoding="utf-8" ?>  
 <Countries>  
  <Country>  
   <CountryId>101</CountryId>  
   <CountryName>London</CountryName>  
  </Country>  
  <Country>  
   <CountryId>102</CountryId>  
   <CountryName>Canada</CountryName>  
  </Country>  
  <Country>  
   <CountryId>103</CountryId>  
   <CountryName>Australiya</CountryName>  
  </Country>  
  <Country>  
   <CountryId>104</CountryId>  
   <CountryName>India</CountryName>  
  </Country>  
 </Countries>  

PageOnRootDirectory.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageOnRootDirectory .aspx.cs" Inherits="_19_MappingVirtualpath_Physicalpath.Page" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
     </asp:DropDownList>  
   </div>  
   </form>  
 </body>  
 </html>  

PageOnRootDirectory.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 namespace _19_MappingVirtualpath_Physicalpath  
 {  
   public partial class Page : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       DataSet DS = new DataSet();  
       string strPhysicalPath = Server.MapPath("~/Data/Countries/Countries.xml");  
       DS.ReadXml(strPhysicalPath);  
       DropDownList1.DataTextField = "CountryName";  
       DropDownList1.DataValueField = "CountryId";  
       DropDownList1.DataSource = DS;  
       DropDownList1.DataBind();  
       ListItem li = new ListItem("Select", "-1");  
       //DropDownList1.Items.Add(li);  
       DropDownList1.Items.Insert(0, li);  
     }  
   }  
 }  


If the PageInElectronicFolder.aspx.cs has to find the Countries.xml file It has to navigate the root directory from there go to the Data directory and then to the Countries directory. If we use “../../Data/Countries/Countries.xml” for the virtual path then there is problem…..because we copy the coding from PageInElectronicFolder.aspx.cs to PageOnRootDirectory .aspx.cs then it won’t work for that we use “~”.This symbol resolves to the root application directory, no matter how deep you are in the folder hierarchy. The following code will work from any folder in the application. DS.ReadXml(Server.MapPath("~/Data/Countries/Countries.xml"));

Data binding DropDownList to an XML file

To add the XML file:-
Right click on the project-->Add -->New Item-->Select visual C# tab--> Select XML.
ReadXml() method of the Dataset object can be used to read from the XML file into a DataSet.
Server.MapPath() method return the physical path for a given virtual path.

Example to add list items that are present in XML file.
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_18_Dropdown_with_XML.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
     </asp:DropDownList>  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Data;  
 namespace _18_Dropdown_with_XML  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       DataSet DS = new DataSet();  
       string strPhysicalPath = Server.MapPath("Countries.xml");  
       DS.ReadXml(strPhysicalPath);  
       DropDownList1.DataTextField = "CountryName";  
       DropDownList1.DataValueField = "CountryId";  
       DropDownList1.DataSource = DS;  
       DropDownList1.DataBind();  
       ListItem li = new ListItem("Select","-1");  
       //DropDownList1.Items.Add(li);  
       DropDownList1.Items.Insert(0,li);  
     }  
   }  
 }  

Countries.xml:-
 <?xml version="1.0" encoding="utf-8" ?>   
 <Countries>  
   <Country>  
    <CountryId>101</CountryId>  
    <CountryName>London</CountryName>  
   </Country>  
  <Country>  
   <CountryId>102</CountryId>  
   <CountryName>Canada</CountryName>  
  </Country>  
  <Country>  
   <CountryId>103</CountryId>  
   <CountryName>Australiya</CountryName>  
  </Country>  
  <Country>  
   <CountryId>104</CountryId>  
   <CountryName>India</CountryName>  
  </Country>  
 </Countries>  


To insert a ListItem at a specific location use the Insert() method.Here we have to specify which value you want to show in the dropdown list and which value you want to halt as the value of the list item object.

DropDownList

If you are adding listitem objects to the dropdownlist in the Page_Load event, make sure you do only when the page is loaded for the first time. Otherwise, every time, you post the page back by clicking button the list items will be added again causing duplication.
Example to add list items when the page is loaded
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_16_dropdownlist.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:DropDownList ID="DropDownList1" runat="server">  
       <asp:ListItem Selected="True" Value="1">Male</asp:ListItem>  
       <asp:ListItem Value="2">Female</asp:ListItem>  
     </asp:DropDownList>  
     <br />  
     <br />  
     <asp:DropDownList ID="DropDownList2" runat="server">  
     </asp:DropDownList>  
     <br />  
     <br />  
     <asp:Button ID="Button1" runat="server" Text="Button" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _16_dropdownlist  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
       {  
         ListItem city1 = new ListItem("London", "1");  
         ListItem city2 = new ListItem("Canada", "2");  
         ListItem city3 = new ListItem("Japan", "3");  
         DropDownList2.Items.Add(city1);  
         DropDownList2.Items.Add(city2);  
         DropDownList2.Items.Add(city3);  
       }  
     }  
   }  
 }  

Button, LinkButton, ImageButton

The Button, LinkButton, ImageButton are used to post a page to the server.
   Button:- The Button control is used to display a push button.
   LinkButton:- LinkButton displays the button like a Hyperlink.
   ImageButton:- ImageButton provides the flexibility of associating an image with the button using ImageUrl property.
Properties
    CommandName
    CommandArgument
    CuasesValidation
    ValidationGroup
    PostBackURL

Example :-
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_14_link_imagebutton.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
       onclientclick="alert(&quot;You are about to submit the page&quot;)"   
       Text="Submit" />  
     <br />  
     <br />  
     <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"   
       onclientclick="return confirm(&quot;Are you sure u want to delete&quot;)">Submit</asp:LinkButton>  
     <br />  
     <br />  
     <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Image/1452.jpg"   
       onclick="ImageButton1_Click"   
       onclientclick="alert(&quot;your are about to submit the page&quot;)"   
       Width="43px" />  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _14_link_imagebutton  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       Response.Write("submit button is clicked");  
     }  
     protected void LinkButton1_Click(object sender, EventArgs e)  
     {  
       Response.Write("link button is clicked");  
     }  
     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)  
     {  
       Response.Write("image button is clicked");  
     }  
   }  
 }  

Onclientclick is used to associate some javascript that we want to run in response to the click event on the client.

HyperLink Control

Properties
Text – The link text that will be shown to the user.
Navigate Url – The url of the page to which the user will be sent.
Image Url – The url of the image, that will be displayed for the link. If you specify both the Text and Image Url, the image will be displayed instead of text. If the image is not unavailable, the text will be displayed.

Methods
   Focus() – call this method to set the input focus when the page loads.

Events
   No events specific to HyperLink control.

Opengl,C++ : Shrinked Polygon

The following program Specify a point for the Polygon by the left mouse button and shrinked polygon (say, by half) with respect to the origin, when you press the key "Z".
 #include <windows.h>  
 #include <gl/Gl.h>  
 #include <gl/glut.h>  
 #include <cmath>  
 int screenheight = 600;  
 int screenwidth = 800;  
 bool flag = true;  
 int first = 0;  
 typedef struct{  
   float x;  
   float y;  
 }Point2D;  
 Point2D p1,p2,p3,p4;  
 void DrawPolygonSegment(Point2D pt1, Point2D pt2, Point2D pt3, Point2D pt4){  
     glPointSize(1.0);  
        glBegin(GL_POLYGON);  
             glVertex2i(pt1.x, pt1.y);  
             glVertex2i(pt2.x, pt2.y);  
             glVertex2i(pt3.x, pt3.y);  
             glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glPointSize(6.0);  
     glBegin(GL_POINTS);  
     glVertex2i(pt2.x, pt2.y);  
        glVertex2i(pt3.x, pt3.y);  
        glVertex2i(pt4.x, pt4.y);  
     glEnd();  
     glFlush();  
 }  
 Point2D translate(Point2D p){  
     p.x =p.x/2; //.....wite the equations for translation   
     p.y = p.y/2; //.....wite the equations for translation  
     return p;  
 }  
 void myMouse(int button, int state, int x, int y) {  
     if(state == GLUT_DOWN) {  
        if(button == GLUT_LEFT_BUTTON) {  
            glClear(GL_COLOR_BUFFER_BIT);  
                           p1.x = 0;  
                           p1.y =0;  
                           switch(first)  
                               {  
                               case 0:  
                                    p2.x = x;  
                                    p2.y = screenheight - y;  
                                    first = 1;  
                                    break;       
                               case 1:  
                                    p3.x = x;  
                                    p3.y = screenheight - y;  
                                    first = 2;  
                                    break;  
                               case 2:  
                                    p4.x = x;  
                                    p4.y = screenheight - y;  
                                    DrawPolygonSegment(p1,p2,p3,p4);  
                                    first = 0;  
                                    break;       
                               }  
        }  
     }  
 }  
 void Shrinked_Polygon()  
 {  
      Point2D translate_p1 = translate(p1);  
   Point2D translate_p2 = translate(p2);  
      Point2D translate_p3 = translate(p3);  
      Point2D translate_p4 = translate(p4);  
   DrawPolygonSegment(translate_p1,translate_p2,translate_p3,translate_p4);  
 }  
 void key(unsigned char key, int x, int y)  
 {  
      if(key=='Z') glutIdleFunc(Shrinked_Polygon);  
 }  
 void myDisplay(){  
     glClearColor(1.0f, 1.0f, 1.0f, 0.0f);  
     glClear(GL_COLOR_BUFFER_BIT);  
     glColor3f(0.0f, 0.0f, 0.0f);  
 }  
 int main( int argc, char ** argv ) {  
     glutInit( &argc, argv );  
     glutInitWindowPosition( 0, 0 );  
     glutInitWindowSize( 800, 600 );  
     glutCreateWindow( "My Drawing Screen" );  
        glMatrixMode( GL_PROJECTION );  
     glLoadIdentity();  
     gluOrtho2D( 0, 800, 0, 600 );  
     glViewport(0, 0, 800, 600);  
        glutDisplayFunc( myDisplay );  
     glutMouseFunc( myMouse );  
        glutKeyboardFunc(key);  
     glutMainLoop();  
     return( 0 );  
 }  

CheckBox Control

Properties
    Checked
    Text
    AutoPostBack
Methods
    Focus() –To set the input focus, to a specific checkbox, call this method for that checkbox control.
Events
    CheckedChanged – This is fired when the checked status of the check button control is changed.
Here an example to print text in the textbox
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_12_CheckboxControl.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
 </head>  
 <body>  
   <form id="form1" runat="server">  
   <div>  
     <fieldset style="width:300px">  
       <legend><b>Gender</b></legend>  
       <asp:CheckBox ID="CheckBox1" runat="server" Text="Graduate" />  
       <br />  
       <asp:CheckBox ID="CheckBox2" runat="server" Text="PostGraduate" />  
       <br />  
       <asp:CheckBox ID="CheckBox3" runat="server" Text="Doctarate" />  
       <br />  
       <br />  
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  
     </fieldset>  
   </div>  
   </form>  
 </body>  
 </html>  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 using System.Text;  
 namespace _12_CheckboxControl  
 {  
   public partial class WebForm1 : System.Web.UI.Page  
   {  
     protected void Page_Load(object sender, EventArgs e)  
     {  
       if (!IsPostBack)  
         CheckBox1.Focus();  
     }  
     protected void Button1_Click(object sender, EventArgs e)  
     {  
       StringBuilder sbUserChoise = new StringBuilder();  
       if (CheckBox1.Checked)  
         sbUserChoise.Append(CheckBox1.Text);  
       if (CheckBox2.Checked)  
         sbUserChoise.Append(", "+CheckBox2.Text);  
       if (CheckBox3.Checked)  
         sbUserChoise.Append(", " + CheckBox3.Text);  
       Response.Write("Your Choises " + sbUserChoise.ToString());  
     }  
   }  
 }