Sunday, 21 April 2013

Bezier Curve in C++ Graphics


Bezier Curve

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

#define address "d:\\tc\\bgi"

struct Point{
    double x,y;
}c[]={{100,100},{105,140},{140,105},{150,115}};
int n=3;

void draw_Bezier(struct Point[]);
Point getPU(double);
double com(int,int);
double b(int,int,double);
void main()
{
    int sel=0;
    int gd=DETECT,gm,x,y;
    initgraph(&gd,&gm,address);
ABOVE:
    cleardevice();
    draw_Bezier(c);
    char ch=getch();
    if(ch==27) return;
    sel=ch-'0';
    if(sel<0||sel>3) sel=3;
    c[sel].x=c[sel].x+1,c[sel].y=c[sel].y+1;
    goto ABOVE;
}

void draw_Bezier(struct Point c[]){
    Point Pu,oPu;
    oPu.x=c[0].x,oPu.y=c[0].y;
    setcolor(WHITE);
    for(double u=0.1;u<=1.0;u+=0.1)
    {
           Pu=getPU(u);
           line(oPu.x,oPu.y,Pu.x,Pu.y);
           oPu=Pu;
    }
    setcolor(RED);
    for(int i=0;i<=n;i++)
    {
        circle(c[i].x,c[i].y,2);
        if(i>0) line(c[i-1].x,c[i-1].y,c[i].x,c[i].y);
    }

}
Point getPU(double u){
    Point p={0,0};
    for(int i=0;i<=n;i++)
    {
        p.x=p.x+c[i].x*b(n,i,u);
        p.y=p.y+c[i].y*b(n,i,u);
    }
    return p;
}

double b(int n,int k,double u)
{
       return com(n,k)*pow(u,k)*pow(1-u,n-k);
}
double com(int n,int k)
{
    double result=1;
    for(int i=n;i>n-k;i--)
    {
        result=(result*i)/(n-i+1);
    }
    return result;
}

Monday, 1 April 2013

TANK GAME IN C/C++

click here to download code...

My Car in C/C++

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

void car(int,int);
void main()
{
    int gd=DETECT,gm;
    int x=220,y=350,bbb=0,j;
    char c;
    initgraph(&gd,&gm,"d:\\tc\\bgi");
    car(x,y);
ca:
    c=getch();
    if(c=='q'||c=='Q')
    {
        closegraph();
        exit(0);
    }
    else if(c==72)
    {
        bbb++;
        car(x,y);
        goto ca;
    }
    else if(c==75&&x>=10)
    {
        bbb++;
        x-=15;
        car(x,y);
        goto ca;
    }
    else if(c==77&&x<=600)
    {
        bbb++;
        x+=15;
        car(x,y);
        goto ca;
    }
    c=getch();
    if(c=='q'||c=='Q')
    { closegraph();
    exit(0);   }
    else if(c==72)
    {
        bbb++;
        car(x,y);
        goto ca;
    }
    else if(c==75)
    {
        bbb++;
        x-=15;
        car(x,y);
        goto ca;
    }
    else if(c==77)
    {
        bbb++;
        x+=15;
        car(x,y);
        goto ca;
    }
    else
        goto ca;
}

void car(int x,int y)
{
    int i,m=4,t=2;
    cleardevice();
    setlinestyle(1,2,1);
    setcolor(m);
    for(i=0;i<10;i++)
        line(x-i/2,y+i,x+80+i/2,y+i);
    line(x+85,y+10,x+90,y+25);
    line(x-5,y+10,x-10,y+25);
    line(x-10,y+25,x-18,y+40);
    line(x+90,y+25,x+98,y+40);
    line(x,y+12,x+80,y+12);
    line(x-4,y+22,x+84,y+22);
    line(x,y+12,x-4,y+22);
    line(x+80,y+12,x+84,y+22);
    for(i=0;i<6;i++)
    {
        line(x-10-i,y+8,x-10+i,y+12+i/3);
        line(x+90+i,y+8,x+90-i,y+12+i/3);
    }
    for(i=0;i<15;i++)
        line(x-18+i/2,y+40-i,x+98-i/2,y+40-i);
    line(x-18,y+45,x+98,y+45);
    line(x-18,y+40,x-18,y+55);
    line(x+98,y+40,x+98,y+55);
    for(i=0;i<117;i++)
        line(x-18+i,y+45,x-18+i,y+55);
    setlinestyle(1,4,3);
    for(i=3;i<8;i++)
    {
        setcolor(i);
        line(x-28+i,y+25+i,x+106-i,y+25+i);
        line(x-15+i/3,y+29+i,x-5,y+29+i);
        line(x+81,y+29+i,x+91-i/3,y+29+i);
    }
    setlinestyle(1,1,1);
    setcolor(t);
    line(x-12,y+55,x-12,y+58);
    line(x,y+55,x,y+58);
    line(x-10,y+59,x-2,y+59);
    line(x-10,y+57,x-2,y+57);
    line(x+91,y+55,x+91,y+58);
    line(x+78,y+55,x+78,y+58);
    line(x+80,y+57,x+89,y+57);
    line(x+80,y+59,x+89,y+59);
    setcolor(m);
    line(x-18,y+55,x+98,y+55);
    setlinestyle(1,2,3);
    for(i=0;i<5;i++)
    {
        setcolor(4);
        line(x-15+i,y+48,x-10+i,y+52);
        line(x+85+i,y+48,x+90+i,y+52);
        setcolor(15);
        line(x+10,y+48+i,x+65,y+48+i);
    }
}

Sunday, 31 March 2013

Tuesday, 26 March 2013

Define the term line clipping. The parametric equation of line PQ may be defined as P + t (Q-P) where 0 ≤ t ≤ 1. Explain this equation with the help of an example. Derive the expression for t with respect to ith edge and PQ (line to be clipped) in the context of Cyber Beck line clipping algorithm. How will you determine whether a point is PE or PL in Cyrus Beck line clipping algorithm.

Define the term line clipping. The parametric equation of line PQ may be defined as P + t (Q-P) where 0 ≤ t ≤ 1. Explain this equation with the help of an example. Derive the expression for t with respect to ith  edge and PQ (line to be clipped) in the context of Cyber Beck line clipping algorithm. How will you determine whether a point is PE  or PL in Cyrus Beck line clipping algorithm.




Wednesday, 20 March 2013

MDB (Message Driven Bean) for news agency

Write a MDB (Message Driven Bean) for news agency that has to
capture the data from various news sources. The newly written MDB
should accept the XML format of the news. The XML data needs to be
parsed and stored in the database. The news format is as follows:
 
<news_id> </ news_id>
<sources> </source>
<date> </date>
<type_of_news> </type_of_news>
<priority> <priority>
<news_content> </news_content>


import java.util.Date;
import java.util.DateFormat;
import java.util.SimpleDateFormat;
import javax.jms.*;
import org.w3c.dom.Document;
import java.sql.*;

private class News{
    private int id;
    public int priority;
    public date;
    public String sources="",type_of_news,news_content;   
    public News(int id){
        this.id=id;
    }
    public int getID(){
        return id;
    }
}


Public class NewsMDB implements MessageDrivenBean,MessageListener {

    private NewsLocal news;
    private Document dom;

    public void setMessageDrivenContext(MessageDrivenContext mdc) {
        try {
            InitialContext ictx = new InitialContext();
        } catch ( Exception ex ) {
            throw new EJBException("Unable to get News Agency bean", ex);
        }
    }
    public void ejbCreate() { }
    public void ejbRemove() { }
    public void onMessage(Message msg) {
        XMLMessage xml = (XMLMessage)msg;
        try {
            dom=xml.getDocument();
            saveInDB(parseDocument());
        } catch ( Exception ex ) {
            throw new EJBException(ex);
        }
    }
    private void saveInDB(News ns){
        try{
            Class.forName("");//Class required for DataBase
            DriverManager.getConnection("","",""); //Database file path & Userid/password
            PreparedStatement st;
            st=con.createPreparedStatement("Insert into news values(?,?,?,?,?,?)");
            st.setInt(1,ns.getID());
            st.setString(2,ns.sources);
            st.setDate(3,ns.date);
            st.setString(4,ns.type_of_news);
            st.setInt(5,ns.priority);
            st.setString(6,ns.news_content);
            st.execute();
            st.close();
            st=null;
            con.close();
            con=null;
        }catch(SQLException se){
            throw new EJBException("Unable to Save News Agency bean", ex);
        }
    }
    private News parseDocument(){
        Element docEle;
        News news;
        //get the root element
        docEle = dom.getDocumentElement();
        //get a nodelist of elements
        int id=getIntValue(docEle,"news_id");
        news=new News(id);
        news.sources=getIntValue(docEle,"sources");
        news.date=getDateValue(docEle,"date");
        news.type_of_news=getStringValue(docEle,"type_of_news");
        news.priority=getIntValue(docEle,"priority");
        news.news_content=getStringValue(docEle,"news_content");
        return news;
    }
   
    private String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if(nl != null && nl.getLength() > 0) {
            Element el = (Element)nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }
        return textVal;
    }
    private int getIntValue(Element ele, String tagName) {       
        return Integer.parseInt(getTextValue(ele,tagName));
    }
    private Date getDateValue(Element ele, String tagName) {
        String startDateString = getTextValue(ele,tagName);
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        Date newDateString;
        try {
                startDate = df.parse(startDateString);
                newDateString = df.format(startDate);
            } catch (Exception e) {
            throw new EJBException(e);
           }
        return newDateString;
    }
}