Friday, January 31, 2020

Scilab Control statements and Loops

Scilab Control statements and Loops


Control statements from scilab decides which statements can be executed based on condition provided. Loops from scilab decides how many times statements can be executed.There are many control statements and loops are available in scilab for ex. if…else, if…elseif…elseif…else,for , while, select etc.

1) If …..else statement: 



in if…else statement logical condition like less than, greater than, equal to etc  is given in if statement as well as else part of it. The condition in if part is true then statements written in if part will be executed otherwise statements from else part will be executed.

Syntax for if…else: 

               if condition
                               //statement
                         else
                              //statement
                         end
Program:

x=input ("Enter age");
if (x>18)
                             disp("You are allowed ");
else
                             disp("You are not allowed")
end
Output:


2)  If…elseif…..elseif…else statement:


If we have multiple conditions to check then we can use if…elseif…elseif…..else scilab statement.

Syntax for if…elseif...elseif..else: 

                 if condition
                                 //statement
                         elseif condition
                                //statement
                         elseif condition
                                   // statement
                         else
                                   // statement
                         end

Program:
     a=55
     if(a>70)
           disp("Distinction")
    elseif(a<70 & a>60)
          disp("first class")
    elseif(a>=40 & a<60)
          disp("second class")
    else
         disp("failed")
    end

Output:

    


3) Select case statement:


If you want to choose one case out of many then select case statement can be used. It is similar like lookup table.

Syntax for select:

     select variable,
      case value 1 then statement 1;
      case value 2 then statement 2;
      case value 3 then statement 3;
      …….
      case value n then statement n;
      end

Program:

       x=input("enter day from week");
select x
case 1 then disp("Sunday");
case 2 then disp("Monday");
case 3 then disp("Tuesday");
case 4 then disp("Wednesday");
case 5 then disp("Thursday");
case 6 then disp("Friday");
case 7 then disp("Saturday");
end
Output:




4)  For loop: 


for loop is used to execute statements written in the body of loop recursively till meeting final condition of loop. In for loop initial value, final value and increment needs to be specified. If increment value is not specified, then 1 will be taken as default value. You can use if…else, select , while inside for loop.

Syntax for for loop:

for variable name=initial value: increment: final value
                        // statements
end


Program:

for i=0:10
                         if(i<5)
                         disp("less than five");
                        else
                         disp("greater than five")
                        end
end
Output:



5) While loop:


While loop will execute till condition becomes true. Once condition is true loop will be terminated.

Syntax for while:

                                   While (condition)
                                           //statements
                                   end
Program: 

c=input("enter number");
while(c<10)
          disp(c);
          c=c+1;
 end

Output:


Bresenham’s line drawing algorithm in C-Language

Bresenham’s line drawing algorithm in C-Language:


For Slope<1


Bresenhams line drawing algorithm removes disadvantages of DDA algorithm. this algorithm uses integer calculation instead of floating-point calculation to reduce complexity and time consumption. In bresenhams algorithm pixel which is nearer to line will be displayed as a part of line based on distance from center of pixel and actual line.






From above diagram, equation of line at point (y, xa+1) is y=m.(xa+1)+c. we will calculate distance D1 and distance D2 to check which pixel is nearer to line .

D1=y-ya
D1= m.(xa+1)+c-ya                                                   -------------------------------------------------(1)
D2=ya+1 -y
D2= ya+1 - m.(xa+1)+c                             -------------------------------------------------(2)

We will decide nearer pixel based on D1-D2 and multiplied by dx for integer calculation as a decision parameter. 

Pa= dx*(D1-D2)
Pa= 2.dy.xa  -2.dx. y+b  where b=2.dy+dx.(2c-1) , dx=x2-x1 and dy=y2-y2..……(3)

At next step decision parameter will be

Pa+1 = 2.dy.xa+1  -2.dx. ya+1 +b                                               --------------------------(4)

We will find relation between current decision parameter and next decision parameter by subtracting   Pfrom  Pa+1.

Pa+1- P= (2.dy.xa+1  -2.dx. ya+1 +b) – (2.dy.xa  -2.dx. y+b)

Pa+1 = Pa+ 2.dy -2.dx(ya+1 - ya)                                            -----------------------------(5)

Initial decision parameter calculated at (x0,y0) is
P0=2.dy-dx.

Steps:

1.Accept line endpoints A(x1,y1) and B(x2,y2) as input.
2.Calculate dx=x2-x1,dy=y2-y1,2dx,2dy
3.Calculate initial decision parameter p0=2dy-dx
4.If pk<0: 
                   x1=x1+1,
                   y1=y1 and
                   pk+1=pk+2dx
  else:
                   x1=x1+1,
                   y1=y1+1 and
                   pk+1=pk+2dx-2dy
5 . Repeat step 4 dx times

6. Plot all pixels and Stop 


Program:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>

void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,dx,dy,p,i;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter x1");
scanf("%d",&x1);
printf("Enter y1");
scanf("%d",&y1);
printf("Enter x2");
scanf("%d",&x2);
printf("Enter y2");
scanf("%d",&y2);

dx=(x2-x1);
dy=(y2-y1);
p=2*dy-dx;
putpixel(x1,y1,3);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x1=x1+1;
y1=y1;
p=p+2*dy;
}
else
{
x1=x1+1;
y1=y1+1;
p=p+2*dy-2*dx;
}
putpixel(x1,y1,3);
}
getch();
}

Output:


Tuesday, January 28, 2020

Midpoint circle drawing algorithm in C-Language

Midpoint circle drawing algorithm in C-Language:


Circle: it is locus of points equidistant  from  center . It means all points on circle are of distance r  from center point (xc,yc) in all sides.


Equation of circle with center (xc,yc) and radius r
    (x-xc)2+(y-yc)2=r2
 Center is at (0,0)
    x2+y2=r2     Where x and y is any point on circle

Symmetry of Circle:


Steps:

Step 1: Accept Center point (xc,yc)  of Circle and radius r of Circle.

Step 2: calculate initial decision parameter p=1-r

step 3:initiate x1=0 and y1=r;

Step 4: if p<0:

                                     x1=x1+1, y1=y1, P=P+2*x1+1

        else:
            x1=x1+1,y1=y1-1, p=p+2*x1+1-2*y1
Step 5: repeat step 3 until x1>=y1
Step 6: plot all points with respect to center point in all octants.
        putpixel(x1+xc,y1+yc,color);  
        putpixel(-x1+xc,y1+yc,color);
        putpixel(-x1+xc,-y1+yc,color);
        putpixel(x1+xc,-y1+yc,color);
        putpixel(y1+xc,x1+yc,color);
        putpixel(-y1+xc,x1+yc,color);
        putpixel(y1+xc,-x1+yc,color);
        putpixel(-y1+xc,-x1+yc,color);
Step 7: stop.

Program:


#include<stdio.h>
#include<graphics.h>

#include<conio.h>


void main()

{

int xc,yc,r,p,x1,y1;

int gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("Enter center coorinates of Circle");

scanf("%d%d",&xc,&yc);

printf("Enter radius of circle");

scanf("%d",&r);

x1=0,y1=r;   //initialize x1 and y1
p=1-r;
do{
if(p<0)
{
 x1=x1+1;
 y1=y1;
 p=p+2*x1+1;
}
else
{
 x1=x1+1;
 y1=y1-1;
 p=p+2*x1+1-2*y1;
}
putpixel(x1+xc,y1+yc,4);  //putpixel for eight octants of circle
putpixel(-x1+xc,y1+yc,4);
putpixel(-x1+xc,-y1+yc,4);
putpixel(x1+xc,-y1+yc,4);
putpixel(y1+xc,x1+yc,4);
putpixel(-y1+xc,x1+yc,4);
putpixel(y1+xc,-x1+yc,4);
putpixel(-y1+xc,-x1+yc,4);

}while(x1<=y1);       // stop condition when x>=y
getch();


}

Output:



Monday, January 27, 2020

How to draw concentric circles in C-Language?

Circle: it is locus of points equidistant  from  center . It means all points on circle are of distance r  from center point (xc,yc) in all sides.

Concentric circles: 

circles with same center point (xc,yc) and different radius are called concentric circles.

using direct circle() function from graphics library and for loop  we can draw concentric circles.delay() function from dos.h can be used for seeing circle plotting one circle at a time.

Program to draw concentric circles:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
 int gd=DETECT,gm,i,xc,yc,r;
 initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 printf("Enter center points");
 scanf("%d %d",&xc,&yc);
 printf("Enter radius of outer circle");
 scanf("%d",&r);
 circle(xc,yc,r);
 for(i=1;i<=r;i++)
 {
  delay(100);
  setcolor(i);
  circle(xc,yc,i);
 }
getch();

}

Output:


Thursday, January 23, 2020

DDA line drawing algorithm Implementation in C-Language

Computer graphics programming can be written in computer languages like C, C++, java and many more.

DDA line drawing algorithm:


DDA ( Digital Differential Analyzer ) Algorithm is scan conversion of line. It accepts end points of line A(x1,y1) and B(x2,y2) , finds intermediate pixel location and display on display device.
we know line equation y=mx+c where m is slope and c is y-intercept as constant.
slope m=(y2-y1)/(x2-x1)
              = dy/dx      where  dx=(x2-x1)  and dy=(y2-y1)   

we can calculate dy if we know dx as dy=m.dx and  we can calculate dx if we know dy as dx=dy/m.
from above equation we can say if dx=1 then dy=m and if dy=1 then dx=1/m.

DDA algorithm increments by unit value in one coordinate and second coordinate can be calculated. Unit increment is based on slope. if abs(slope) <1 then increment by one in x coordinate and y coordinate increment  can be calculated by yinc=m.
xinc=1,yinc=m
if abs(slope)>1 then increment by one in y coordinate and calculate x coordinate increment as xinc=1/m.
yinc=1,xinc=1/m
using increment factor all intermediate values can be calculated as
xnext=xcurrent+xinc
ynext=ycurrent+yinc
this prcess is recurssive till we reach  last coordinate of line (x2,y2).


Algorithm Steps:

1.Accept input (x1,y1) and (x2,y2) as end points of Line
2. Calculate dx=(x2-x1)  and dy=(y2-y1).
3. If abs(dx)>=abs(dy)
               length=dx
       else
           length=dy
4. Calculate xinc=dx/length and yinc=dy/length.
5. Plot first pixel (x1,y1) .
6. Calculate xnext=xcurrent+xinc  and ynext=ycurrent+yinc and plot(xnext, ynext).
7. Repeat step 6 ,length times.   
8. Stop  

DDA line drawing algorithm Implementation in C-Language:


Program:

#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>

void main()
{
int gd=DETECT,gm;
int i,len;
float x1,y1,x2,y2,dx,dy,xinc,yinc;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter x1");
scanf("%f",&x1);
printf("Enter y1");
scanf("%f",&y1);
printf("Enter x2");
scanf("%f",&x2);
printf("Enter y2");
scanf("%f",&y2);
dx=(x2-x1);
dy=(y2-y1);
if(abs(dx)>=abs(dy))
{
len=dx;
}
else
{
len=dy;
}
xinc=dx/len;
yinc=dy/len;
putpixel(x1,y1,4);
for(i=1;i<=len;i++)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,4);
}
getch();
}

Output: