January 2019

C Program to find the points using DDA algorithm

Algorithm:-


Suppose at step i, the pixels is (xi,yi)
The line of equation for step i
              yi=mxi+b......................equation 1
Next value will be
              yi+1=mxi+1+b.................equation 2
              m =DDA Algorithm
             
              yi+1-yi=∆y.......................equation 3
              yi+1-xi=∆x......................equation 4
              yi+1=yi+∆y
              ∆y=m∆x
              yi+1=yi+m∆x
              ∆x=∆y/m
              xi+1=xi+∆x
              xi+1=xi+∆y/m
Case1: When |M|<1 assume="" sub="" that="" then="" x="">1
<1 assume="" sub="" that="" then="" x="">
              x= x1,y=y1 set ∆x=1
              yi+1=y1+m,     x=x+1
              Until x = x2

Case2: When |M|<1 assume="" sub="" that="" then="" y="">1
<1 assume="" sub="" that="" then="" y="">
              x= x1,y=y1 set ∆y=1
              xi+1=DDA Algorithm,     y=y+1
              

              Until y → y2



#include<stdio.h>
#include<math.h>
int main()
{
int x1,x2,y1,y2;
float x,y;
int i=1,roundx,roundy,step;
float dx,dy,xinc,yinc,xnew,ynew;
printf("Enter the end points first\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end points second\n");
scanf("%d%d",&x2,&y2);
printf("step\txold\tyold\txnew\tynew\tround(x),round(y)");
dx=(x2-x1);
dy=y2-y1;
if(abs(dx)>abs(dy))
{
step=abs(dx);
}
else
{
step=abs(dy);
}
x=x1;
y=y1;
xinc=dx/step;
yinc=dy/step;

xnew=(x+xinc);
ynew=(y+yinc);
roundx=roundl(xnew);
roundy=roundl(ynew);
printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t(%d,%d)",i,x,y,xnew,ynew,roundx,roundy);
do
{
i++;
x=xnew;
y=ynew;
xnew=(x+xinc);
ynew=(y+yinc);
roundx=roundf(xnew);
roundy=roundf(ynew);
printf("\n%d\t%.2f\t%.2f\t%.2f\t%.2f\t(%d,%d)",i,x,y,xnew,ynew,roundx,roundy);
}
while(i<step);
}



C Programming Languages | C Program to draw lines using DDA algorithm

#include<stdio.h>
#include<math.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,x2,y1,y2;
float x,y;
int i=1,roundx,roundy,step;
float dx,dy,xinc,yinc,xnew,ynew;
printf("Enter the end points first\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end points second\n");
scanf("%d%d",&x2,&y2);
dx=(x2-x1);
dy=y2-y1;
if(abs(dx)>abs(dy))
{
step=abs(dx);
}
else
{
step=abs(dy);
}
x=x1;
y=y1;
xinc=dx/step;
yinc=dy/step;
xnew=(x+xinc);
ynew=(y+yinc);
roundx=roundl(xnew);
roundy=roundl(ynew); 
putpixel(roundx,roundy,RED); 
do
{
i++;
x=xnew;
y=ynew;
xnew=(x+xinc);
ynew=(y+yinc);
roundx=roundf(xnew);
roundy=roundf(ynew);
putpixel(roundx,roundy,RED);
}
while(i<step);
getch();
closegraph;
}

C Program To Calculate Values Using Fixed Point Iteration Method

Algorithm

  1. Start
  2. Read values of x0 and e.
    *Here x0 is the initial approximation
    e is the absolute error or the desired degree of accuracy, also the stopping criteria*
  3. Calculate x1 = g(x0)
  4. If [x1 – x0] <= e, goto step 6.
    *Here [ ] refers to the modulus sign*
  5. Else, assign x0 = x1 and goto step 3.
  6. Display x1 as the root.
  7. Stop
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define EST 0.05
#define F(x) exp(-x)-x
#define G(x) exp(-x)
int main()
{
int i=1;
float error,x0,x1,temp;
printf("Enter the initial guess x0:\n");
scanf("%f",&x0);
printf("Iteration x0\t\t x1\t Error\n");
x1=G(x0);
error=fabs((x1-x0)/x1);
printf("%d\t %.4f\t%.4f\t %.4f\n",i,x0,x1,error);
do
{
i++;
temp=x1;
x0=temp;
x1=G(x0);
error=fabs((x1-x0)/x1);
printf("%d\t %.4f\t%.4f\t %.4f\n",i,x0,x1,error);
}while (error>EST);
}

C Program To Calculate Values Using Honors Method 

Pseudo Code

coefficients := [-19, 7, -4, 6] # list coefficients of all x^0..x^n in order
x := 3
accumulator := 0
for i in length(coefficients) down to 1 do
    # Assumes 1-based indexing for arrays
    accumulator := ( accumulator * x ) + coefficients[i]
done
# accumulator now has the answer


#include<stdio.h>


#include<conio.h>


#include<math.h>


//Given Polynomial = F(x) (4*x*x*x*x)+(5*x*x*x)+(6*x*x)+(7*x)+8


int main()


{


int i,x,n;


printf("Enter the degree of the polynomial:\n");


scanf("%d",&n);


int a[n+1],b[n+1];


printf("Enter the cofficient of polynomial starting from the higher degree:\n");


for(i=n;i>=0;i--)


{


scanf("%d",&a[i]);


}


printf("Enter the value at which polynomial has to be evaluated:\n");


scanf("%d",&x);


b[n]=a[n];


for(i=n-1;i>=0;i--)


{


b[i]=a[i]+(b[i+1]*x);


}


printf("The value of given polynomial at X=5 is %d.",b[0]);


}



no image

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x0,x1,y0,y1,dx,dy,i=0;
float slope,Po,x2,y2,P2,temp,x,y;
printf("Enter the two ends points:\n");
scanf("%d%d",&x0,&y0);
printf("Enter the next two end points:\n");
scanf("%d%d",&x1,&y1);
dx=x1-x0;
dy=y1-y0;
slope=dy/dx;
if (slope<1)//Satisfying formula for the slope.
{
Po=2*(fabs(dy))-(fabs(dx));
}
else
{
Po=2*(fabs(dx))-(fabs(dy));
}
if(Po<0)
{
x2=x0+1;
y2=y0;
P2=Po+(2*(abs(dy)));
}
else
{
x2=x0+1;
y2=y0+1;
P2=Po+2*dy-2*dx;
}
printf("k\tPk\tXk+1,Yk+1\tPk+1");
printf("\n%d\t%.2f\t%.2f,%.2f\t%.2f",i,Po,x2,y2,P2);
do
{
i++;
temp=P2;
Po=temp;//assigining new value to Po
x=x2;
x0=x;//assigning new value to x0
y=y2;
y0=y;//assigning new value to y0
if(Po<0)
{
x2=x0+1;
y2=y0;
P2=Po+(2*(abs(dy)));
}
else
{
x2=x0+1;
y2=y0+1;
P2=Po+2*dy-2*dx;
}
printf("\n%d\t%.2f\t%.2f,%.2f\t%.2f",i,Po,x2,y2,P2);
}while(i<dx);//using do-while loop for finding value up to dx times.

}