Skip to main content

Posts

Showing posts from August, 2012

C program to implement bresenham's line drawing algorithm

#include<graphics.h> #include<conio.h> #include<math.h> void bress(float x1,float y1,float x2,float y2)  { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if(dx>dy) { p=2*dy-dx; if(x1<x2) { x=x1;y=y1;end=x2; if(y1<y2)inc=1; if(y1>y2)inc=-1; } else { x=x2;y=y2;end=x1; if(y2<y1)inc=1; if(y2>y1)inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else { y=y+inc;p=p+2*(dy-dx); } x++; } } else { p=2*dx-dy; if(y1<y2) { y=y1;x=x1;end=y2; if(x1<x2)inc=1; if(x1>x2)inc=-1; } else { y=y2;x=x2;end=y1; if(x2<x1)inc=1; if(x2>x1)inc=-1; } while(y<=end) { putpixel(x,y,15); if(p<0)p=p+2*dx; else { x=x+inc;p=p+2*(dx-dy); } y++; if(current==0&&c==10) { current=1;c=-1; } if(current==1&&c==6) { current=0;c=-1; } c++; } }  } void main(...

C program to implement DDA algorithm

#include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #define round(a) ((int) (a+0.5)) void lineDDA(int xa,int ya,int xb,int yb); int main(void) {    /* request auto detection */    int gdriver = DETECT, gmode, errorcode;    int xa,ya,xb,yb;    /* initialize graphics and local variables */    initgraph(&gdriver, &gmode, "..\\bgi");    lineDDA(0,0,640,480);    lineDDA(640,0,0,480);    lineDDA(320,0,320,480);    lineDDA(0,240,640,240);    getch();    closegraph();   } void lineDDA(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,steps,k; float xinc,yinc,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float) steps; yinc=dy/(float) steps; putpixel(round(x),round(y),5); for(k=0;k<steps;k++) {       x+=xinc; y+=yinc; putpixel(round(x),round(y),2); ...

C++ program to show simple animation in Visual Studio 2008

#include "graphics.h" int main( ) {     initwindow(1000, 700, "Animation");   for(int i=0;i<47;i++) //for +x-axis { line(500,0,500,1000); line(0,350,1000,350); circle(530+(i*10),320,30); delay(100); clearviewport(); } for(int i=0;i<47;i++) //for -x-axis { line(500,0,500,1000); line(0,350,1000,350); circle(0+(i*10),320,30); delay(100); clearviewport(); } for(int i=0;i<32;i++) //for +y-axis { line(500,0,500,1000); line(0,350,1000,350); circle(470,320-(i*10),30); delay(100); clearviewport(); } for(int i=0;i<32;i++) //for -y-axis { line(500,0,500,1000); line(0,350,1000,350); circle(470,700-(i*10),30); delay(100); clearviewport(); } while(!kbhit()) { outtextxy(500,350,"Press any key to exit..."); delay(100); }     return 0; }

Java program to calculate BMI (Body Mass Index)

import java.util.Scanner; public class BMI_Calc {         public static void main(String args[])         {                 double wt, ht,bmi;                 Scanner sc=new Scanner(System.in);                 System.out.print("Enter your wieght in pounds: ");                 wt=sc.nextDouble();                 System.out.print("\nEnter your hieght in inches: ");                 ht=sc.nextDouble();                 wt=wt*0.45359237;                 ht=ht*0.0254;                 bmi=wt/(ht*ht);                 System.out.print("\n\nBMI =...

Java program to display formatted output

Suppose the tuition for a university is $5,000 this year and increases 4.5% every year. Write a program that displays the tuition for the next four years. Display two digits after the decimal point. public class Q1 {         public static void main(String args[])         {                 double fee=5000;                 for(int i=0;i<4;i++)                 {                         fee=(fee*0.045) + fee;                         System.out.format("Increased fee for %d year = $%6.2f%n",(i+1),fee);                 }         } }

Java program To read a string and change it to alphabetical order. eg STRING=GINRST

import java.io.*; class charsort { public static void main(String args[])throws IOException    { String s1;       char temp;           DataInputStream in=new DataInputStream(System.in);       System.out.print("\n\n Enter string:  ");       s1=in.readLine();       char z[]=new char[s1.length()];       z[0]+=s1.charAt(0);       for(int i=1;i<s1.length();i++)       { z[i]+=s1.charAt(i);                    for(int j=0;j<=i;j++)          { for(int k=j+1;k<=i;k++)            { if(z[j]>z[k])               { temp=z[k];                  z[k]=z[j];                  z[j]=temp;     ...

Java program to find perfect integers

import java.util.Scanner; public class PerfectIntegers {         public static void main(String args[])         {                 int num,sum=0,count=2,p_count=0;                 Scanner sc=new Scanner(System.in);                 System.out.println("First four Perfect Integers are:");                 while(true)                 {                               for(int i=1;i<=count/2;i++)                         {                                 if(count%i==0)                  ...