Wednesday, June 18, 2014

Php mysql connection class with oop


<?php
class dbConnect
{
var $host;
var $username;
var $password;
var $database;

function __construct($host,$username,$password,$database)
{
$this->host=$host;
$this->username=$username;
$this->password=$password;
$this->database=$database;
$this->connection();
}
function connection()
{
$conn=mysqli_connect($this->host,$this->username,$this->password,$this->database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
echo "connected";
mysqli_close($conn);
}

}
$log=new dbConnect("localhost","root","","login");

?>

Thursday, June 12, 2014

Java-Loops


  • Loops are very useful mechanism to execute a code several times in java programming.
  • Java has 3 looping mechanisms. They are
    • For loop
    • While loop
    • do....while loop
                          For Loop
      For loop is very good choice if you know how many times a task wants to execute.
      Basic syntax:
                 for(initialization;expression;update)
                 {
                      //statements
                  }
      Example:
                   public class ForLoop
                  {
                  public static void main(String args [])
                 {
                       for(int x=1;x<10;x++)
                       {
                         System.out.println(x);
                       }
                  }
                   }

       You can get the answer like this....


                                 While loop
   Basic syntax: 
              while(expression)
              {
                  //statements
               }
    Example:
              public class WhileLoop
              {
                   public static void main(String args [])
                  {
                         int x=1;
 while(x<10)
 {
                        System.out.println(x);
 x++;
 }
                   }
                }

       You can get the answer like this..        



                    Do....While  loop
       Do while loop is similar to the while loop. The different thing is in while loop before                    executing the code check  the condition. But in do while loop after executing the code                check  the condition.

       Basic Syntax:
        do
         {
               //statements
          }
         while(expression);

         Example:
          
                public class DoWhileLoop
                {
                   public static void main(String args [])
                  {
                     int x=1;
                 do
                 {
                    System.out.println(x);
                    x++;
                  }
while(x<10);
                   }
                   }

             You can get the answer like this..   
         

Sunday, June 8, 2014

Rotate a square with respect to the origin



#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>

int screenheight = 600;
int screenwidth = 800;
int flag = 0;

double angle = 30;            

typedef struct{
    float x;
    float y;
}Point2D;

Point2D p1,p2,p3,p4;

void DrawSquare(Point2D pt1, Point2D pt2,Point2D pt3, Point2D pt4){
       glPointSize(2.0);
       glBegin(GL_LINE_LOOP);
       glVertex2i(pt1.x, pt1.y);
   glVertex2i(pt3.x, pt3.y);
  glVertex2i(pt2.x, pt2.y);

  glVertex2i(pt4.x, pt4.y);
       glEnd();

       glPointSize(5.0);
       glBegin(GL_POINTS);
       glVertex2i(pt2.x, pt2.y);
       glEnd();
       glFlush();
}

Point2D translate(Point2D p, float tx, float ty){
   p.x =p.x;
   p.y = p.y;
   return p;

}
Point2D  rotate(Point2D p, float ang){

    ang = ang * 3.14 / 180.0;                            
    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(int button, int state, int x, int y) {
glPointSize(5.0);
if(state == GLUT_DOWN)
{
if(button == GLUT_LEFT_BUTTON)
{
  if (flag==0){
p1.x = x;
p1.y = screenheight - y;
p3.y=screenheight - y;
p4.x=x;
flag = 1;
} else if(flag==1){
p2.x = x;
p2.y = screenheight - y;
p3.x=x;
p4.y=screenheight - y;
flag = 0;
DrawSquare(p1,p2,p3,p4);


}

}
else if (button == GLUT_RIGHT_BUTTON)
{
Point2D Rotated_p2 = rotate(p2,angle);
Point2D Rotated_p1 = rotate(p1,angle);
Point2D Rotated_p3 = rotate(p3,angle);
Point2D Rotated_p4 = rotate(p4,angle);
                   DrawSquare(Rotated_p1,Rotated_p2,Rotated_p3,Rotated_p4);
}
}
}

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( "Draw Dots" );



       glMatrixMode( GL_PROJECTION );

       glLoadIdentity();

       gluOrtho2D( 0, 800, 0, 600 );

       glViewport(0, 0, 800, 600);



       glutMouseFunc( myMouse );

       glutDisplayFunc( myDisplay );

       glutMainLoop();



       return( 0 );

}

Saturday, June 7, 2014

Rotate a square 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 X1,Y1,X2,Y2,click;
float len;
float m;
double angle = 30;                //The angle for the rotation (in degrees)



typedef struct{

    float x;

    float y;

}Point2D;



Point2D p1,p2,p3,p4;



void DrawLineSegment(Point2D pt1, Point2D pt2){

       glPointSize(1.0);

       glBegin(GL_LINES);

       glVertex2i(pt1.x, pt1.y);

       glVertex2i(pt2.x, pt2.y);

       glEnd();



       glPointSize(6.0);

       glBegin(GL_POINTS);

  glVertex2i(pt1.x, pt1.y);
       glVertex2i(pt2.x, pt2.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,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(int button, int state, int x, int y) {

       if(state == GLUT_DOWN)
       {

              if(button == GLUT_LEFT_BUTTON)
              {
if(click == 0)
{
X1=x;
Y1=screenheight - y;
p1.x = X1;
                p1.y = Y1;
click++;

}
else if(click ==1)
{
click++;
glClear(GL_COLOR_BUFFER_BIT);
            Point2D p1;
            p1.x = X1;
            p1.y = Y1;
            p2.x = x;
            p2.y = screenheight - y;
            p3.x = x;
            p3.y = Y1;
            p4.x = X1;
            p4.y = screenheight - y;
DrawLineSegment(p1, p3);
            DrawLineSegment(p3, p2);
DrawLineSegment(p2, p4);
DrawLineSegment(p1, p4);
}                
             }

              else if (button == GLUT_RIGHT_BUTTON)
              {
    m=(p2.y-p1.y)/(p2.x-p1.x);
len=sqrt((p2.y-p2.x)*(p2.y-p2.x)+(p1.y-p1.x)*(p1.y-p1.x));
Point2D Translated_p1 = translate(p1,-(p1.x+p2.x)/2,-(p1.y+p2.y)/2);
Point2D Translated_p2 = translate(p2,-(p1.x+p2.x)/2,-(p1.y+p2.y)/2);
Point2D Translated_p3 = translate(p3,-(p1.x+p2.x)/2,-(p1.y+p2.y)/2);
Point2D Translated_p4 = translate(p4,-(p1.x+p2.x)/2,-(p1.y+p2.y)/2);

                     Point2D Rotated_p1 = rotate(Translated_p1,angle);
Point2D Rotated_p2 = rotate(Translated_p2,angle);
Point2D Rotated_p3 = rotate(Translated_p3,angle);
Point2D Rotated_p4 = rotate(Translated_p4,angle);

Point2D Translated_p12 = translate(Rotated_p1,(p1.x+p2.x)/2,(p1.y+p2.y)/2);
Point2D Translated_p22 = translate(Rotated_p2,(p1.x+p2.x)/2,(p1.y+p2.y)/2);
Point2D Translated_p32 = translate(Rotated_p3,(p1.x+p2.x)/2,(p1.y+p2.y)/2);
Point2D Translated_p42 = translate(Rotated_p4,(p1.x+p2.x)/2,(p1.y+p2.y)/2);

                     DrawLineSegment(Translated_p12,Translated_p32);
DrawLineSegment(Translated_p32,Translated_p22);
DrawLineSegment(Translated_p22,Translated_p42);
DrawLineSegment(Translated_p12,Translated_p42);
              }

       }

}



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( "Draw Dots" );



       glMatrixMode( GL_PROJECTION );

       glLoadIdentity();

       gluOrtho2D( 0, 800, 0, 600 );

       glViewport(0, 0, 800, 600);



       glutMouseFunc( myMouse );

       glutDisplayFunc( myDisplay );

       glutMainLoop();



       return( 0 );

}

Rotate a line



#include <windows.h>
#include <gl/Gl.h>
#include <gl/glut.h>
#include <cmath>
 
int screenheight = 600;
int screenwidth = 800;
bool flag = true;

double angle = 30;                //The angle for the rotation (in degrees)

typedef struct{
    float x;
    float y;
}Point2D;

Point2D p1,p2;

void DrawLineSegment(Point2D pt1, Point2D pt2){
       glPointSize(1.0);
       glBegin(GL_LINES);
       glVertex2i(pt1.x, pt1.y);
       glVertex2i(pt2.x, pt2.y);
       glEnd();

       glPointSize(6.0);
       glBegin(GL_POINTS);
       glVertex2i(pt2.x, pt2.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, 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(int button, int state, int x, int y) {
       if(state == GLUT_DOWN) {
              if(button == GLUT_LEFT_BUTTON) {
                     glClear(GL_COLOR_BUFFER_BIT);
                     Point2D p1;
                     p1.x = 0;
                     p1.y = 0;

                     p2.x = x;
                     p2.y = screenheight - y;

                     DrawLineSegment(p1, p2);
              }
              else if (button == GLUT_RIGHT_BUTTON) {
                     Point2D Rotated_p2 = rotate(p2,angle);
                     DrawLineSegment(p1,Rotated_p2);
              }
       }
}

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);

       glutMouseFunc( myMouse );
       glutDisplayFunc( myDisplay );

       glutMainLoop();
       return( 0 );
}