Sunday, 31 March 2013
Thursday, 28 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;
}
}
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;
}
}
Tuesday, 19 March 2013
Dependency relations
Determine the dependency relations among the following instructions:
I1: a=b+c;
I2: b=a+d;
I3: e=a/f;
Then, Read Sets & Write Sets of given Instruction
For I1, R1=
{b, c} W1= {a}
For I2, R2=
{a, d} W2= {b}
For I3, R3=
{a, f} W3= {e}
For Instructions I1 & I2
R1 ^ W2 = {b} ;
Anti Dependence
R2 ^ W1 = {a} ;
Flow Dependence
W1 ^ W2 = {}
Therefore I1 & I2 are both Flow Dependent and Anti
Dependent.
For Instruction I2 & I3
R2 ^ W3 = {}
R3 ^ W2 = {}
W2 ^ W3 = {}
Therefore instructions I2 & I3 are independent of each
other, and can be run in parallel.
For Instruction I1 & I3
R1 ^ W3 = {}
R3 ^ W1 = {a} ;
Flow Dependence
W1 ^ W3 = {}
Therefore instructions I1 & I3 are Flow Dependent.
Sunday, 17 March 2013
Water Jug Problem
A
Water Jug Problem: You are given two Jugs, a 4-gallon one and a
3-gallon one. Neither have any measuring markers on it. There is a
pump that can be used to fill the jugs with water. How can you get
exactly 2 gallons of water into the 4-gallon jug?
The state space for this problem can be described as the set of ordered pairs of integers (x, y), such that x=0, 1,2,3, or 4 and y = 0,1,2, 0r 3; x represents the number of gallons of water in the 4-gallon jug, and y represents the quality of water in the 3-gallon jug. The Start State is (0,0). The goal state is (2,n) for any value of n (since the problem does not specify how many gallons need to be in the 3-gallon jug).
1 (x, y) If x < 4 -> (4,y) Fill the 4-gallon jug.
2. (x, y) If y < 3 -> (x,3) Fill the 3-gallon jug.
3 (x, y) If x >0 -> (x – d, y) Pour some water out of the 4-gallon jug
4 (x, y) If y > 0 -> (x, y - d) pour some water out of the 3-Gallon jug
5 (x, y) If x > 0 -> (0, y) Empty the 4-gallon jug on the ground
6 (x, y) If y > 0 -> (x, 0) Empty the 3-gallon jug on the ground
7 (x, y) If x + y> 4 and y > 0 -> (4,y – (4 -x)) ;pour some water from the 3-Gallon jug in to the
4 - gallon jug until the 4 -gallon jug is full.
8 (x, y) If x + y> 3 and x > 0 -> (x-(3-y),3) ;pour water from the 4 -Gallon jug in to the
3 -gallon jug until the 3 -gallon jug is full.
9. (x, y) If x + y <4 and y > 0 -> (x+y,0) ;pour all the water from the 3-Gallon jug in to the
4- gallon jug
10 (x, y) If x + y < 3 and x > 0 -> (x+y,0) ;pour all the water from the 4-Gallon jug in to the
3- gallon jug
11 (0,2) -> (2,0) ;pour all 2 gallons from the 3-Gallon jug in to the
4-Gallon jug.
12. (2,y) -> (0,y) ;Empty the 2 gallons in the 4.gallons in the 4-gallon jug on the
Ground.
Production rules for the water jug problem.
Gallons of water in the 4-gallon jug. Gallons of water in the 3-gallon jug Rule Applied
0 0 2
0 3 9
3 0 2
3 3
4 2 7
0 2 5 0r 12
2 0 9 or 11
One solution for the water jug problem.
0 0 2
4 0 1
1 3 8
1 0 6
0 1 10
4 1 1
2 3 8
2 0 6
Second solution for the water jug problem
The state space for this problem can be described as the set of ordered pairs of integers (x, y), such that x=0, 1,2,3, or 4 and y = 0,1,2, 0r 3; x represents the number of gallons of water in the 4-gallon jug, and y represents the quality of water in the 3-gallon jug. The Start State is (0,0). The goal state is (2,n) for any value of n (since the problem does not specify how many gallons need to be in the 3-gallon jug).
1 (x, y) If x < 4 -> (4,y) Fill the 4-gallon jug.
2. (x, y) If y < 3 -> (x,3) Fill the 3-gallon jug.
3 (x, y) If x >0 -> (x – d, y) Pour some water out of the 4-gallon jug
4 (x, y) If y > 0 -> (x, y - d) pour some water out of the 3-Gallon jug
5 (x, y) If x > 0 -> (0, y) Empty the 4-gallon jug on the ground
6 (x, y) If y > 0 -> (x, 0) Empty the 3-gallon jug on the ground
7 (x, y) If x + y> 4 and y > 0 -> (4,y – (4 -x)) ;pour some water from the 3-Gallon jug in to the
4 - gallon jug until the 4 -gallon jug is full.
8 (x, y) If x + y> 3 and x > 0 -> (x-(3-y),3) ;pour water from the 4 -Gallon jug in to the
3 -gallon jug until the 3 -gallon jug is full.
9. (x, y) If x + y <4 and y > 0 -> (x+y,0) ;pour all the water from the 3-Gallon jug in to the
4- gallon jug
10 (x, y) If x + y < 3 and x > 0 -> (x+y,0) ;pour all the water from the 4-Gallon jug in to the
3- gallon jug
11 (0,2) -> (2,0) ;pour all 2 gallons from the 3-Gallon jug in to the
4-Gallon jug.
12. (2,y) -> (0,y) ;Empty the 2 gallons in the 4.gallons in the 4-gallon jug on the
Ground.
Production rules for the water jug problem.
Gallons of water in the 4-gallon jug. Gallons of water in the 3-gallon jug Rule Applied
0 0 2
0 3 9
3 0 2
3 3
4 2 7
0 2 5 0r 12
2 0 9 or 11
One solution for the water jug problem.
0 0 2
4 0 1
1 3 8
1 0 6
0 1 10
4 1 1
2 3 8
2 0 6
Second solution for the water jug problem
Saturday, 16 March 2013
Sutherland Hodgman Polygon Clipping
Write C/C++ program to implement the Sutherland Hodgman polygon clipping algorithm. Using this program clip the polygon against the rectangular window. Make suitable assumptions.
#include <windows.h>
#include <gl/glut.h>
struct Point{
float x,y;
} w[4],oVer[4];
int Nout;
void drawPoly(Point p[],int n){
glBegin(GL_POLYGON);
for(int i=0;i<n;i++)
glVertex2f(p[i].x,p[i].y);
glEnd();
}
bool insideVer(Point p){
if((p.x>=w[0].x)&&(p.x<=w[2].x))
if((p.y>=w[0].y)&&(p.y<=w[2].y))
return true;
return false;
}
void addVer(Point p){
oVer[Nout]=p;
Nout=Nout+1;
}
Point getInterSect(Point s,Point p,int edge){
Point in;
float m;
if(w[edge].x==w[(edge+1)%4].x){ //Vertical Line
m=(p.y-s.y)/(p.x-s.x);
in.x=w[edge].x;
in.y=in.x*m+s.y;
}
else{//Horizontal Line
m=(p.y-s.y)/(p.x-s.x);
in.y=w[edge].y;
in.x=(in.y-s.y)/m;
}
return in;
}
void clipAndDraw(Point inVer[],int Nin){
Point s,p,interSec;
for(int i=0;i<4;i++)
{
Nout=0;
s=inVer[Nin-1];
for(int j=0;j<Nin;j++)
{
p=inVer[j];
if(insideVer(p)==true){
if(insideVer(s)==true){
addVer(p);
}
else{
interSec=getInterSect(s,p,i);
addVer(interSec);
addVer(p);
}
}
else{
if(insideVer(s)==true){
interSec=getInterSect(s,p,i);
addVer(interSec);
}
}
s=p;
}
inVer=oVer;
Nin=Nout;
}
drawPoly(oVer,4);
}
void init(){
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
glClear(GL_COLOR_BUFFER_BIT);
w[0].x =20,w[0].y=10;
w[1].x =20,w[1].y=80;
w[2].x =80,w[2].y=80;
w[3].x =80,w[3].y=10;
}
void display(void){
Point inVer[4];
init();
// As Window for Clipping
glColor3f(1.0f,0.0f,0.0f);
drawPoly(w,4);
// As Rect
glColor3f(0.0f,1.0f,0.0f);
inVer[0].x =10,inVer[0].y=40;
inVer[1].x =10,inVer[1].y=60;
inVer[2].x =60,inVer[2].y=60;
inVer[3].x =60,inVer[3].y=40;
drawPoly(inVer,4);
// As Rect
glColor3f(0.0f,0.0f,1.0f);
clipAndDraw(inVer,4);
// Print
glFlush();
}
int main(int argc,char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("Polygon Clipping!");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
#include <windows.h>
#include <gl/glut.h>
struct Point{
float x,y;
} w[4],oVer[4];
int Nout;
void drawPoly(Point p[],int n){
glBegin(GL_POLYGON);
for(int i=0;i<n;i++)
glVertex2f(p[i].x,p[i].y);
glEnd();
}
bool insideVer(Point p){
if((p.x>=w[0].x)&&(p.x<=w[2].x))
if((p.y>=w[0].y)&&(p.y<=w[2].y))
return true;
return false;
}
void addVer(Point p){
oVer[Nout]=p;
Nout=Nout+1;
}
Point getInterSect(Point s,Point p,int edge){
Point in;
float m;
if(w[edge].x==w[(edge+1)%4].x){ //Vertical Line
m=(p.y-s.y)/(p.x-s.x);
in.x=w[edge].x;
in.y=in.x*m+s.y;
}
else{//Horizontal Line
m=(p.y-s.y)/(p.x-s.x);
in.y=w[edge].y;
in.x=(in.y-s.y)/m;
}
return in;
}
void clipAndDraw(Point inVer[],int Nin){
Point s,p,interSec;
for(int i=0;i<4;i++)
{
Nout=0;
s=inVer[Nin-1];
for(int j=0;j<Nin;j++)
{
p=inVer[j];
if(insideVer(p)==true){
if(insideVer(s)==true){
addVer(p);
}
else{
interSec=getInterSect(s,p,i);
addVer(interSec);
addVer(p);
}
}
else{
if(insideVer(s)==true){
interSec=getInterSect(s,p,i);
addVer(interSec);
}
}
s=p;
}
inVer=oVer;
Nin=Nout;
}
drawPoly(oVer,4);
}
void init(){
glClearColor(0.0f,0.0f,0.0f,0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,100.0,0.0,100.0,0.0,100.0);
glClear(GL_COLOR_BUFFER_BIT);
w[0].x =20,w[0].y=10;
w[1].x =20,w[1].y=80;
w[2].x =80,w[2].y=80;
w[3].x =80,w[3].y=10;
}
void display(void){
Point inVer[4];
init();
// As Window for Clipping
glColor3f(1.0f,0.0f,0.0f);
drawPoly(w,4);
// As Rect
glColor3f(0.0f,1.0f,0.0f);
inVer[0].x =10,inVer[0].y=40;
inVer[1].x =10,inVer[1].y=60;
inVer[2].x =60,inVer[2].y=60;
inVer[3].x =60,inVer[3].y=40;
drawPoly(inVer,4);
// As Rect
glColor3f(0.0f,0.0f,1.0f);
clipAndDraw(inVer,4);
glFlush();
}
int main(int argc,char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(400,400);
glutInitWindowPosition(100,100);
glutCreateWindow("Polygon Clipping!");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Subscribe to:
Posts (Atom)


