Top Banner
Course Code : MCSL-054 Course Title : Laboratory Course Assignment Number : MCA(5)/L054/Assign/2011 Question 1: Write a Servlet program for session tracking using cookies. Hint : package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.net.*; import java.util.*; public class ShowSession extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Searching the Web"; String heading; Integer accessCount = new Integer(0);; if (session.isNew()) { heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; Integer oldAccessCount = (Integer)session.getValue("accessCount"); if (oldAccessCount != null) { accessCount = new Integer(oldAccessCount.intValue() + 1); } } // Use putAttribute in version 2.2 of servlet API. session.putValue("accessCount", accessCount); out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" + "<H2>Information on Your Session:</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n" + "<TR>\n" + " <TD>ID\n" + " <TD>" + session.getId() + "\n" + "<TR>\n" + " <TD>Creation Time\n" +
24

MCS-054-2011

Feb 21, 2015

Download

Documents

Faraz Ahmad
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MCS-054-2011

Course Code : MCSL-054

Course Title : Laboratory Course

Assignment Number : MCA(5)/L054/Assign/2011

Question 1: Write a Servlet program for session tracking using cookies.

Hint :

package hall;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.net.*;

import java.util.*;

public class ShowSession extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

HttpSession session = request.getSession(true);

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String title = "Searching the Web";

String heading;

Integer accessCount = new Integer(0);;

if (session.isNew()) {

heading = "Welcome, Newcomer";

} else {

heading = "Welcome Back";

Integer oldAccessCount = (Integer)session.getValue("accessCount");

if (oldAccessCount != null) {

accessCount = new Integer(oldAccessCount.intValue() + 1);

}

}

// Use putAttribute in version 2.2 of servlet API.

session.putValue("accessCount", accessCount);

out.println(ServletUtilities.headWithTitle(title) +

"<BODY BGCOLOR=\"#FDF5E6\">\n" +

"<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" +

"<H2>Information on Your Session:</H2>\n" +

"<TABLE BORDER=1 ALIGN=CENTER>\n" +

"<TR BGCOLOR=\"#FFAD00\">\n" +

" <TH>Info Type<TH>Value\n" +

"<TR>\n" +

" <TD>ID\n" +

" <TD>" + session.getId() + "\n" +

"<TR>\n" +

" <TD>Creation Time\n" +

Page 2: MCS-054-2011

" <TD>" + new Date(session.getCreationTime()) + "\n" +

"<TR>\n" +

" <TD>Time of Last Access\n" +

" <TD>" + new Date(session.getLastAccessedTime()) + "\n" +

"<TR>\n" +

" <TD>Number of Previous Accesses\n" +

" <TD>" + accessCount + "\n" +

"</TABLE>\n" +

"</BODY></HTML>");

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

Question 2: Write a JSP Program, which displays a web page containing two web links one

for showing details of your Study Centre and other for FAQs on Practicls in MCA

Programme . When one click on link Study Centre it goes to a page which shows all the

details of study centre and counselling schedule for MCA students .Clicking on link for

FAQs on Practicls in MCA ,another JSP page consisting of some FAQ related to different

practical courses in MCA, will open.

Hint :

Main.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Main</title>

</head>

<body>

<br><br><br>

<center> <a href="detailofstudycenter.jsp"><h2>View Details of study

center</h2></a></center> <br>

<center> <a href="faqonpracticls.jsp"><h2>View Faq on MCA Practils</h2></a></center>

</body>

</html>

detailofstudycenter.jsp

<html>

<head>

Page 3: MCS-054-2011

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Student Centre Detail Page:</title>

</head>

<body>

<table align="center" border="1">

<tr>

<td>Study Centre Name :</td>

<td> Solutioncab Technologies </td>

</tr>

<tr>

<td>Address :</td>

<td> Solutioncab technologies, jaipur Rajasthan </td>

</tr>

<tr>

<td>Phone Number :</td>

<td> 0141-25699002 </td>

</tr>

<tr>

<td>Email :</td>

<td> [email protected] </td>

</tr>

<tr>

<td>Program Coordinator :</td>

<td> Praveen kumar</td>

</tr>

<tr>

<td> counselling schedule for MCA :</td>

<td><a href =”schedule.doc”> click here</a></td>

</tr>

</table>

</body>

</html>

faqonpracticls.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>FAQs</title>

</head>

<body><br><br><br>

<center><h2><u> FAQs on Practicls </u></h2></center><br>

<p>Q1. If attendance is less than 75% in practical session of a course due to some

reasons can student get any help/consideration?

<br><br>

Ans : The student will NOT be allowed to appear in Term-End Practical Exam.

Page 4: MCS-054-2011

</p>

<p>Q2. If Attendance is less than 75% then how can a student give Term-End Practical

Exam?

<br><br>

Ans : The student will NOT be allowed to appear in Term-End Practical Exam.

</p>

<p>Q3. Can the MCA students appear in the Term-End Practical Exam without filling the Term-

End Exam Form ?

<br><br>

Ans : No.

</p>

<p>Q4. What should we do to give my Backlog Term End Practical Exam ?

<br><br>

Ans : Collect attendance proof from the study Centre & then submit to IGNOU Regional Centre

before your Term End Examination (Theory).

</p>

</body>

</html>

Question 3:Write a program using JSP and JDBC to provide details of MCA/BCA/CIT

study centres in your regional centre of IGNOU. This program should provide the list of

students in different semesters of MCA/BCA/CIT.

Hint:

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*,database.DBConnection"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<div align="center" style="margin-top: 30px;">Select your regional center

<select name="scenter">

<option value="-1">Please select one</option>

<%

Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet res = stmt.executeQuery("select rid,rname from regionalcenter");

while(res.next()) {

%>

Page 5: MCS-054-2011

<option value="<%= res.getInt("rid") %>"

onclick="javascript:window.location='page2.jsp?rid=<%= res.getInt("rid") %>'"><%=

res.getString("rname") %></option>

<%

}

%>

</select>

</div>

</body>

</html>

page2.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*,database.DBConnection"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<%

int rid = Integer.parseInt(request.getParameter("rid"));

%>

<div align="center" style="margin-top: 30px;">

<a href="index.jsp">Back</a><br>

<a href="page3.jsp?rid=<%= rid %>">Student details for different semester(MCA,BCA

and CIT) regional center wise</a>

<table style="margin-top: 30px;" align="center" width="100%" border="1">

<tr>

<th colspan="3" align="center">Study center details</th>

</tr>

<tr>

<th>Id</th>

<th>Name</th>

<th>Address</th>

</tr>

<%

Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet res = stmt.executeQuery("select sid,sname,address from studycenter

where rid="+rid+"");

while(res.next()) {

Page 6: MCS-054-2011

%>

<tr>

<td><%= res.getInt("sid") %></td>

<td><a href="page4.jsp?sid=<%= res.getInt("sid") %>"><%=

res.getString("sname") %></a></td>

<td><%= res.getString("address") %></td>

</tr>

<%

}

%>

</table>

</div>

</body>

</html>

page3.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*,database.DBConnection"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<div align="center" style="margin-top: 20px;">

<a href="index.jsp">Back</a>

</div>

<table align="center" style="margin-top: 30px;" align="center" width="100%" border="1">

<tr>

<th colspan="7" align="center">Student details</th>

</tr>

<tr>

<th>Id</th>

<th>Name</th>

<th>Gender</th>

<th>Age</th>

<th>Address</th>

<th>Course</th>

<th>Semester</th>

</tr>

<%

int rid = Integer.parseInt(request.getParameter("rid"));

Page 7: MCS-054-2011

Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet res = stmt.executeQuery("select

student.id,student.name,student.gender,student.age,student.address,student.sem,courses.cname

from student,courses where student.cid=courses.cid and student.rid="+rid+"");

while(res.next()) {

%>

<tr>

<td><%= res.getInt("id") %></td>

<td><%= res.getString("name") %></td>

<td><%= res.getString("gender") %></td>

<td><%= res.getString("age") %></td>

<td><%= res.getString("address") %></td>

<td><%= res.getString("cname") %></td>

<td><%= res.getString("sem") %></td>

</tr>

<%

}

%>

</table>

</body>

</html>

page4.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="java.sql.*,database.DBConnection"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<div align="center" style="margin-top: 20px;">

<a href="index.jsp">Back</a>

</div>

<table align="center" style="margin-top: 30px;" align="center" width="100%" border="1">

<tr>

<th colspan="7" align="center">Student details</th>

</tr>

<tr>

<th>Id</th>

<th>Name</th>

<th>Gender</th>

Page 8: MCS-054-2011

<th>Age</th>

<th>Address</th>

<th>Course</th>

<th>Semester</th>

</tr>

<%

int sid = Integer.parseInt(request.getParameter("sid"));

Connection con = DBConnection.getDBConnection();

Statement stmt = con.createStatement();

ResultSet res = stmt.executeQuery("select

student.id,student.name,student.gender,student.age,student.address,student.sem,courses.cname

from student,courses where student.cid=courses.cid and student.sid="+sid+"");

while(res.next()) {

%>

<tr>

<td><%= res.getInt("id") %></td>

<td><%= res.getString("name") %></td>

<td><%= res.getString("gender") %></td>

<td><%= res.getString("age") %></td>

<td><%= res.getString("address") %></td>

<td><%= res.getString("cname") %></td>

<td><%= res.getString("sem") %></td>

</tr>

<%

}

%>

</table>

</body>

</html>

DBConnection.java

package database;

import java.sql.*;

public class DBConnection {

/** Creates a new instance of DBConnection */

public DBConnection() { }

public static Connection getDBConnection()

{

Connection con = null;

try

{

Class.forName("com.mysql.jdbc.Driver");

}

Page 9: MCS-054-2011

catch(ClassNotFoundException e)

{

System.out.println("Could not load driver class "+e);

}

try

{

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ignou","root","root");

}

catch (SQLException e) {System.out.println("Could not get connection "+e);}

return con;

}

public static void closeDBConnection(Connection con)

{

try

{

if(con!=null) con.close();

}

catch (SQLException e) {

System.out.println("Could not close connection "+e);

}

}

public static void main(String[] args) {

getDBConnection();

}

}

ignou.sql

CREATE DATABASE IF NOT EXISTS ignou;

USE ignou;

DROP TABLE IF EXISTS `ignou`.`courses`;

CREATE TABLE `ignou`.`courses` (

`cid` int(11) NOT NULL AUTO_INCREMENT,

`cname` varchar(100) DEFAULT NULL,

PRIMARY KEY (`cid`)

) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

LOCK TABLES `courses` WRITE;

INSERT INTO `ignou`.`courses` VALUES (2,'BCA'),

(3,'CIT'),

(1,'MCA');

UNLOCK TABLES;

Page 10: MCS-054-2011

DROP TABLE IF EXISTS `ignou`.`regionalcenter`;

CREATE TABLE `ignou`.`regionalcenter` (

`rid` int(11) NOT NULL AUTO_INCREMENT,

`rname` varchar(100) DEFAULT NULL,

`address` text,

PRIMARY KEY (`rid`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

LOCK TABLES `regionalcenter` WRITE;

INSERT INTO `ignou`.`regionalcenter` VALUES (1,'Jaipur','Mansarovar Jaipur'),

(2,'Mumbai','Mulund Mumbai');

UNLOCK TABLES;

DROP TABLE IF EXISTS `ignou`.`student`;

CREATE TABLE `ignou`.`student` (

`name` varchar(100) DEFAULT NULL,

`gender` varchar(6) DEFAULT NULL,

`age` int(11) DEFAULT NULL,

`address` text,

`cid` int(11) DEFAULT NULL,

`sid` int(11) DEFAULT NULL,

`id` int(11) NOT NULL AUTO_INCREMENT,

`sem` int(11) DEFAULT NULL,

`rid` int(11) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

LOCK TABLES `student` WRITE;

INSERT INTO `ignou`.`student` VALUES ('Meena','Female',25,'Mumbai (MH)',2,2,2,4,2),

('Deepak','Male',25,'Alsisar, Jhunjhunu (Rajasthan)',1,1,1,5,1);

UNLOCK TABLES;

DROP TABLE IF EXISTS `ignou`.`studycenter`;

CREATE TABLE `ignou`.`studycenter` (

`address` text,

`sid` int(11) NOT NULL,

`sname` varchar(100) DEFAULT NULL,

`rid` int(11) DEFAULT NULL,

PRIMARY KEY (`sid`)

Page 11: MCS-054-2011

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

LOCK TABLES `studycenter` WRITE;

INSERT INTO `ignou`.`studycenter` VALUES ('Jaipur',1,'IIIM',1),

('Mumbai',2,'ABCD',2);

UNLOCK TABLES;

Question 4: Create an XML document for keeping books records in a Library.

Hint:

<?xml version="1.0"?>

<catalog>

<book id="bk101">

<author>Gambardella, Matthew</author>

<title>XML Developer's Guide</title>

<genre>Computer</genre>

<price>44.95</price>

<publish_date>2000-10-01</publish_date>

<description>An in-depth look at creating applications

with XML.</description>

</book>

<book id="bk102">

<author>Ralls, Kim</author>

<title>Midnight Rain</title>

<genre>Fantasy</genre>

<price>5.95</price>

<publish_date>2000-12-16</publish_date>

<description>A former architect battles corporate zombies,

an evil sorceress, and her own childhood to become queen

of the world.</description>

</book>

<book id="bk103">

<author>Corets, Eva</author>

<title>Maeve Ascendant</title>

<genre>Fantasy</genre>

<price>5.95</price>

<publish_date>2000-11-17</publish_date>

<description>After the collapse of a nanotechnology

society in England, the young survivors lay the

foundation for a new society.</description>

</book>

</catalog>

Page 12: MCS-054-2011

PART-II: Lab for MCS-053 (Computer Graphics and Multimedia)

Question 1:Write a program in C/C++ using OpenGL to draw a circle of red colour inside

of a rectangle of blue colour on a background of green colour.

Hint:

Question 2:Write a program in C or C++ to implement Scan-Line Polygon Filling

Algorithm.

Hint:

Write a program in C or C++ to implement Scan-Line Polygon Filling Algorithm

Answer

typedef struct tEdge

{

int yUpper;

float xIntersect, dxPerScan;

struct tEdge * next;

} Edge;

typedef struct tdcPt

{

int x;

int y;

} dcPt;

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

Page 13: MCS-054-2011

}

}

/* Free edge records that have been malloc’ed ... */

}

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

}

}

/* Free edge records that have been malloc’ed ... */

}

void buildEdgeList (int cnt, dcPt * pts, Edge * edges[])

{

Edge * edge;

dcPt v1, v2;

int i, yPrev = pts[cnt - 2].y;

v1.x = pts[cnt-1].x; v1.y = pts[cnt-1].y;

for (i=0; i<cnt; i++)

{

v2 = pts[i];

if (v1.y != v2.y)

{

/* nonhorizontal line */

edge = (Edge *) malloc (sizeof (Edge));

if (v1.y < v2.y) /* up-going edge */

makeEdgeRec (v1, v2, yNext (i, cnt, pts), edge, edges);

else /* down-going edge */

makeEdgeRec (v2, v1, yPrev, edge, edges);

}

Page 14: MCS-054-2011

yPrev = v1.y;

v1 = v2;

}

}

/* For an index, return y-coordinate of next nonhorizontal line */

int yNext (int k, int cnt, dcPt * pts)

{

int j;

if ((k+1) > (cnt-1))

j = 0;

else

j = k + 1;

while (pts[k].y == pts[j].y)

if ((j+1) > (cnt-1))

j = 0;

else

j++;

return (pts[j].y);

}

void buildEdgeList (int cnt, dcPt * pts, Edge * edges[])

{

Edge * edge;

dcPt v1, v2;

int i, yPrev = pts[cnt - 2].y;

v1.x = pts[cnt-1].x; v1.y = pts[cnt-1].y;

for (i=0; i<cnt; i++)

{

v2 = pts[i];

if (v1.y != v2.y)

{

/* nonhorizontal line */

edge = (Edge *) malloc (sizeof (Edge));

if (v1.y < v2.y) /* up-going edge */

makeEdgeRec (v1, v2, yNext (i, cnt, pts), edge, edges);

else /* down-going edge */

makeEdgeRec (v2, v1, yPrev, edge, edges);

}

}

}

/* Store lower-y coordinate and inverse slope for each edge. Adjust

and store upper-y coordinate for edges that are the lower member

of a monotically increasing or decreasing pair of edges */

void makeEdgeRec

(dcPt lower, dcPt upper, int yComp, Edge * edge, Edge * edges[])

{

edge->dxPerScan =(float) (upper.x - lower.x) / (upper.y - lower.y);

Page 15: MCS-054-2011

edge->xIntersect = lower.x;

if (upper.y < yComp)

edge->yUpper = upper.y - 1;

else

edge->yUpper = upper.y;

insertEdge (edges[lower.y], edge);

}

/* Inserts edge into list in order of increasing xIntersect field. */

void insertEdge (Edge * list, Edge * edge)

{

Edge * p, * q = list;

p = q->next;

while (p != NULL)

{

if (edge->xIntersect < p->xIntersect)

p = NULL;

else

{

q = p;

p = p->next;

}

}

edge->next = q->next;

q->next = edge;

}

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

}

Page 16: MCS-054-2011

}

/* Free edge records that have been malloc’ed ... */

void buildActiveList (int scan, Edge * active, Edge * edges[])

{

Edge * p, * q;

p = edges[scan]->next;

while (p)

{

q = p->next;

insertEdge (active, p);

p = q;

}

}

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

}

}

/* Free edge records that have been malloc’ed ... */

}

void fillScan (int scan, Edge * active)

{

Edge * p1, * p2;

int i;

p1 = active->next;

while (p1)

{

p2 = p1->next;

for (i=p1->xIntersect; i<p2->xIntersect; i++)

Page 17: MCS-054-2011

setPixel ((int) i, scan);

p1 = p2->next;

}

}

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

}

}

/* Free edge records that have been malloc’ed ... */

}

/* Delete completed edges. Update ’xIntersect’ field for others */

void updateActiveList (int scan, Edge * active)

{

Edge * q = active, * p = active->next;

while (p)

if (scan >= p->yUpper)

{

p = p->next;

deleteAfter (q);

}

else

{

p->xIntersect = p->xIntersect + p->dxPerScan;

q = p;

p = p->next;

}

}

void deleteAfter (Edge * q)

Page 18: MCS-054-2011

{

Edge * p = q->next;

q->next = p->next;

free (p);

}

void scanFill (int cnt, dcPt * pts)

{

Edge * edges[WINDOW_HEIGHT], * active;

int i, scan;

for (i=0; i<WINDOW_HEIGHT; i++)

{

edges[i] = (Edge *) malloc (sizeof (Edge));

edges[i]->next = NULL;

}

buildEdgeList (cnt, pts, edges);

active = (Edge *) malloc (sizeof (Edge));

active->next = NULL;

for (scan=0; scan<WINDOW_HEIGHT; scan++)

{

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);

}

}

/* Free edge records that have been malloc’ed ... */

}

void resortActiveList (Edge * active)

{

Edge * q, * p = active->next;

active->next = NULL;

while (p)

{

q = p->next;

insertEdge (active, p);

p = q;

}

}

Question 3:

Write a program in C/C++ using OpenGL to draw a hard wire diagram as shown in

figure given below. Use basic primitives of openGL.

Page 19: MCS-054-2011

120

40

40 120

Question 4:Write a program in C/C++ using OpenGL to perform a 3-Dimensional

transformation, such as translation ,rotation and reflection, on a given triangle.

Hint:

#include <stdio.h>

#include <stdlib.h>

#include<graphics.h>

#include<conio.h>

void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d);

void draw3d(int fs,int x[20],int y[20],int tx,int ty,int d)

{

int i,j,k=0;

for(j=0;j<2;j++)

{

for(i=0;i<fs;i++)

{

if(i!=fs-1)

line(x[i]+tx+k,y[i]+ty-k,x[i+1]+tx+k,y[i+1]+ty-k);

else

line(x[i]+tx+k,y[i]+ty-k,x[0]+tx+k,y[0]+ty-k);

}

k=d;

}

for(i=0;i<fs;i++)

{

line(x[i]+tx,y[i]+ty,x[i]+tx+d,y[i]+ty-d);

}

}

void main()

{

int gd=DETECT,gm;

int x[20],y[20],tx=0,ty=0,i,fs,d;

initgraph(&gd,&gm,"");

Page 20: MCS-054-2011

printf("no of sides (front view only) : ");

scanf("%d",&fs);

printf("co-ordinates : ");

for(i=0;i<fs;i++)

{

printf("(x%d,y%d)",i,i);

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

}

printf("Depth :");

scanf("%d",&d);

draw3d(fs,x,y,tx,ty,d);

printf("translation (x,y)");

scanf("%d%d",&tx,&ty);

draw3d(fs,x,y,tx,ty,d);

getch();

}

Question 5:Write a program in C/C++ to implement Cohen-Sutherland line clipping

algorithm. In this implementation consider two cases of a line: totally visible, totally

invisible, against the rectangular clipping window.

Hint:

#include<windows.h>

#include<gl/gl.h>

#include<gl/glu.h>

#include<gl/glut.h>

#include<stdio.h>

#include<math.h>

//function that implements Sutherand-Cohen algorithm

void nkjImpementsSutherlandCohen(int [], int , ... );

//function to deside visibiity of any line

int nkjDecideVisibility(int [],int *,int *,int *,int *);

//function to generate bit code of points

int nkjGenerateCode(int,int, int, int, int ,int);

//to perform swapping

void nkjSwap(int * , int *);

void nkjInit()

{

glClearColor(1.0,1.0,1.0,0.0);

glColor3f(0.0f,0.0f,0.0f);

glPointSize(4);

glMatrixMode(GL_PROJECTION);

Page 21: MCS-054-2011

glLoadIdentity();

gluOrtho2D(0.0,200.0,0.0,200.0);

}

void nkjDisplayLines()

{

int points[]={60,40,20,20};// points for window position xMax, yMax,

// xMin, yMin

int xMax,yMax,xMin,yMin;

xMax=60;

yMax=40;

xMin=yMin=20;

glClear(GL_COLOR_BUFFER_BIT);

//Drawing Window

glBegin(GL_LINES);

glVertex2i(xMin,yMin);

glVertex2i(xMin,yMax);

glVertex2i(xMin,yMax);

glVertex2i(xMax,yMax);

glVertex2i(xMax,yMax);

glVertex2i(xMax,yMin);

glVertex2i(xMax,yMin);

glVertex2i(xMin,yMin);

//Total 4 points two for p and two for

q nkjImpementsSutherlandCohen(points,4,40,80,120,30);

glEnd();

glFlush();

}

void nkjImpementsSutherlandCohen(int polygonPoints[], int vertexPoints, ... )

{

int x1, y1, x2,y2;

int ind, total, decision;

va_list ptr;

va_start(ptr, vertexPoints);

if(vertexPoints%4!=0)

{

printf("nkjError Message! Wrong number of arguments given......\n");

return;

}

Page 22: MCS-054-2011

total=vertexPoints/4;

glClear(GL_COLOR_BUFFER_BIT);

for(ind=0;ind<total;ind++)

{

x1=va_arg(ptr,int);

y1=va_arg(ptr,int);

x2=va_arg(ptr,int);

y2=va_arg(ptr,int);

decision= nkjDecideVisibility(polygonPoints,&x1,&y1,&x2,&y2);

if(decision!=-1)

{

//this implies ine must be drawn and points are stored

//in the corresponding variables

glVertex2i(x1,y1);

glVertex2i(x2,y2);

}

}

}

int nkjDecideVisibility(int points[], int *x1,int *y1, int *x2, int *y2)

{

int xMax,yMax,xMin,yMin;

int code1,code2;

xMax=points[0];

yMax=points[1];

xMin=points[2];

yMin=points[3];

for(;;)

{

code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);

code2=nkjGenerateCode(xMax,yMax,xMin,yMin,*x2,*y2);

if(code1==0 && code2==0)

{

//this indicates line is totaly visible

return 1;

}

else if((code1 & code2)!=0)

{

//this implies line is totaly invisible

return -1;

}

else

{

Page 23: MCS-054-2011

if(*x1>xMax)

{

//finding intersection of line[(x1,y1),(x2,y2)] and xMax

*y1=(((*y2-*y1)/(*x2-*x1))*(xMax-*x1)) + *y1;

*x1=xMax;

}

else if(*x1<xMin)

{

//finding intersection of line[(x1,y1),(x2,y2)] and xMin

*y1=(((*y2-*y1)/(*x2-*x1))*(xMin-*x1)) + *y1;

*x1=xMin;

}

if(*y1>yMax)

{

//finding intersection of line[(x1,y1),(x2,y2)] and yMax

*x1=((yMax-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;

*y1=yMax;

}

else if(*y1<yMin)

{

//finding intersection of line[(x1,y1),(x2,y2)] and yMin

*x1=((yMin-*y1)*((*x2-*x1)/(*y2-*y1))) + *x1;

*y1=yMin;

}

}

//generating new code for the clipped points

code1=nkjGenerateCode(xMax,yMax,xMin,yMin,*x1,*y1);

if(code1==0)

{

//interchange two points and respective flags

nkjSwap(x1,x2);

nkjSwap(y1,y2);

nkjSwap(&code1,&code2);

}

}

return -1; //this will never execute, just to satisfy compiler

}

int nkjGenerateCode(int xMax, int yMax, int xMin, int yMin, int x, int y)

{

int code=0;

//code sequence UDLR

if(x>xMax)

Page 24: MCS-054-2011

code|=1;//0001 Right bit

else if(x<xMin)

code|=2;//0010 Left bit

if(y>yMax)

code|=8;//1000 Up/Top bit

else if(y<yMin)

code|=4;//0100 Down/Bottom nit

return code;

}

void nkjSwap(int *x, int *y)

{

*x=*x^*y;

*y=*x^*y;

*x=*x^*y;

}

void main(int argc, char **argv)

{

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

glutInitWindowSize(400,400);

glutInitWindowPosition(10,10);

glutCreateWindow("Sutherland-Cohen by Abhi");

glutDisplayFunc(nkjDisplayLines);

nkjInit();

glutMainLoop();

}