You are on page 1of 28

MCSL-054 Solved assignment 2013-14

By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk



PART-I: Lab for MCS-051 (Advanced Internet Technologies)

Question 1: Write a program using Servlet/JSP that accepts the account number of a
bank customer as input. The program should print the details of the account. Make
necessary assumptions whereever required.

Answer.1
<%@ page language="java" import="java.sql.*;"%>
<%
int offset=0;
int ofset = 0;
if(request.getParameter("offset")!=null)
{
offset= Integer.parseInt(request.getParameter("offset").toString());
}

int total_count = 0;
int total_page = 0;
int per_page =5;
String name="";
if(offset>1){
ofset = offset*per_page-per_page;
}

if((request.getParameter("account_no")!=null) &&
request.getParameter("account_no")!="")
{

String user = session.getAttribute("userid").toString();
String account_no = request.getParameter("account_no");
String sd[] = request.getParameter("sdate").toString().split("-");
String ed[] = request.getParameter("edate").toString().split("-");
String sdate = sd[2]+"-"+sd[0]+"-"+sd[1] ;
String edate = ed[2]+"-"+ed[0]+"-"+ed[1];
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "userdetails";
String driver = "com.mysql.jdbc.Driver";
String username = "root";
String userPassword = "root";
String color = "#F9EBB3";

try {
Class.forName(driver).newInstance();
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


conn = DriverManager.getConnection(url+dbName,username,userPassword);

Statement stcount = conn.createStatement();
String strQuerycount = "select count(*),account_no from transaction where
username='"+user+"' and account_no='"+account_no+"' and cdate>='"+sdate+"' and
cdate<='"+edate+"' group by account_no";
ResultSet rscount = stcount.executeQuery(strQuerycount);
if(rscount.next())
{
total_count=rscount.getInt(1);
}

if(total_count%per_page==0)
{
total_page = total_count/per_page;
}
else
{
total_page = total_count/per_page+1;
}

Statement st1 = conn.createStatement();
String strQuery1 = "select * from user where username='"+user+"'";
ResultSet rs1 = st1.executeQuery(strQuery1);

if(rs1.next())
{
name =rs1.getString(1) + " "+ rs1.getString(2);
}

Statement st = conn.createStatement();
String strQuery = "select * from transaction where username='"+user+"' and
account_no='"+account_no+"' and cdate>='"+sdate+"' and cdate<='"+edate+"' limit
"+ofset+","+per_page;

ResultSet rs = st.executeQuery(strQuery);
int count=0;
if(offset>1)
count=offset*per_page-per_page;

%>
<table style="padding-left:150px;">
<tr style="background-color:efefef;">
<td><b>Account No</b></td><td>:</td><td><%=account_no%></td>
</tr>
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


<tr style="background-color:efefef;">
<td><b>Username</b></td><td>:</td><td><%=user%></td>
</tr>
<tr style="background-color:efefef;">
<td><b>Name</b></td><td>:</td><td><%=name%></td>
</tr>

</table>
<br><br><br>
<table width="700px" align="center" style="border:1px solid #000000;" >
<tr>
<td colspan=8 align="center" style="background-color:ffeeff"><b>Transaction
Report</b></td>
</tr>
<tr style="background-color:efefef;">
<td><b>SNo</b></td>
<td><b>Amount</b></td>
<td><b>Net Amount</b></td>
<td><b>Cash Type</b></td>
<td><b>Date</b></td>
</tr>
<tr style="background-color:<%=color%>;">
<td><%=count%></td>
<td><%=rs.getInt(4)%></td>
<td><%=rs.getInt(7)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(5)%></td>
</tr>
</table>
conn.close();
}














MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk






Question 2: Write a JSP Program, which displays a web page containing two web links one
for showing schedule of counselling sessions at a Study Centre and other for FAQs on
Programme on offer by the University. When one click on link Study Centre it goes to a
page which show the counselling schedule for MCA students. Clicking on link for FAQs
on Programme on Offer, another JSP page open, which consists of some FAQ related to
different programme offered in the University.

Answer.2

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="faqonprograms.jsp"><h2>View Faq on Programs on
offer</h2></a></center>
</body>
</html>


detailofstudycenter.jsp
<html>
<head>
<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> Karrox IGNOU Study Centre </td>
</tr>
<tr>
<td>Address :</td>
<td> karrox technologies Ltd, Opp Shreyas Cinema, Ghatkopar(w),Mumbai-
86
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


</td>
</tr>
<tr>
<td>Phone Number :</td>
<td> 022-25699002 </td>
</tr>
<tr>
<td>Email :</td>
<td> ignou@karrox.com </td>
</tr>
<tr>
<td>Program Coordinator :</td>
<td> Mudadi Sanyasi J</td>
</tr>
<tr>
<td> counselling schedule for MCA :</td>
<td><a href =schedule.doc> click here</a></td>
</tr>


</table>
</body>
</html>


faqonprograms.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.
</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.
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


</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
students at a IGNOU study centres. Also the program should enable any student to change
his/her address. This program should provide the practical attendance detail of students in
different semesters of MCA/BCA/CIT.

Answer.3

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");
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


while(res.next()) {
%>
<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();
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


ResultSet res = stmt.executeQuery("select sid,sname,address from
studycenter where rid="+rid+"");
while(res.next()) {
%>
<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>
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


<th>Gender</th>
<th>Age</th>
<th>Address</th>
<th>Course</th>
<th>Semester</th>
</tr>
<%
int rid = Integer.parseInt(request.getParameter("rid"));
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.c
name 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>
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


</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 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.c
name 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


MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


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");
}
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();
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


}
}


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;


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,
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


`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`)
) 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 students record in a department of a
University.
Answer.4

<?xml version=1.0 ?>
<student Records>
<student Record>
<Name> Ajay Mishra</Name>
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


<PROGRAMME> MCA </PROGRAMME>
<E-No> 063501077</E-No>
<Address> H-45 Indira Nagar</Address>
<ph-no> 9444444685 </Ph-no>
<Student Record>
<Name> Shweta Sankhla </Name>
<PROGRAMME> MCA </PROGRAMME>
<E-NO> 063502656 </E-NO>
<ADDRE SS> A-17 Sangam Nagar Indore </ADDRESS>
<ph.no> 9958888889 </ph.no>
</student record>
</student records>

Issued.jsp

<% @ language=java import=java.co.x %>
<html.
<body>
<% string b1 = response.getParameter(a);
Class.for Name(Com.mysql.jdbc.Driver);
Connection con = drivermanager.getconnetion(jdbc:mysql://localhostinstitute, root,
1234>
Statement st= con.createstatement();
Result rs= st.execute update (update library set status=issued whereBook_title=
%>

Search.jsp

<html>
<body>
<form action=searchavailable method=post>
Enter Book name<input type=text name=a>
Search For availability
<input type=submit valuesearch for availability>
</form>
</body>
</html>



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

Question 1: Write a program in C/C++ using OpenGL to draw a rectangle of red colour
inside of a circle of blue colour on a background of orange colour.
Answer.
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk




#include <windows.h>
#include <gl/glut.h>
#include <math.h>
const float PI=3.14;

void drawCircle(){
glBegin(GL_LINE_LOOP);
glColor3f(1.0,0.0,0.0);
for(int i =0; i <= 300; i++){
double angle = 2 * PI * i / 300;
double x = 5*cos(angle);
double y = 5*sin(angle);
glVertex2d(x,y);
}
glEnd();
}
void drawRect(){
glColor3f(0.0,0.0,1.0);
glRectf(-5.0,5.0,5.0,-5.0);
}
void init(void){
glClearColor(0.0,1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10.0,-10.0,10.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawRect();
drawCircle();
glutSwapBuffers();
}

int main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(320,320);
glutInitWindowPosition(50,50);
glutCreateWindow("2D Shapes");
init();
glutDisplayFunc(display);
glutMainLoop();
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


return 0;
}






Question 2: 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);
}
}
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


/* Free edge records that have been malloced ... */
}
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 malloced ... */
}
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);
}
yPrev = v1.y;
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


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);
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


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);
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


}
}
/* Free edge records that have been malloced ... */
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 malloced ... */
}
void fillScan (int scan, Edge * active)
{
Edge * p1, * p2;
int i;
p1 = active->next;
while (p1)
{
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


p2 = p1->next;
for (i=p1->xIntersect; i<p2->xIntersect; i++)
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 malloced ... */
}
/* 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;
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


}
}
void deleteAfter (Edge * q)
{
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 malloced ... */
}
void resortActiveList (Edge * active)
{
Edge * q, * p = active->next;
active->next = NULL;
while (p)
{
q = p->next;
insertEdge (active, p);
p = q;
}
}


MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk



Question 3: Write a program in C or C++ to implement Cyrus Beck line clipping algorithm.
Answer.
// Cyrus-Beck Line Clipping algorithm
struct Point2D
{ float x,y;
}
const int MAXP = 100;
struct Polygon
{ int nPoints;
Point2D v[MAXP];
}
const int MAXN = 100;
typedef Point2D Normal[MAXN];

void CalcNormals (Polygon p, Normal & n)
{ int i,j,k;
point2D v;
for (i = 0; i < p.nPoints; i++)
{ j = (i+1)%p.nPoints;
k = (i+2)%p.nPoints;
// make vector be -1/mI + 1J
n[i].x = -(p.v[j].y - p.v[i].y)/(p.v[j].x - p.v[i].x);
n[i].y = 1.0;
v.x = p.v[k].x - p.v[i].x;
v.y = p.v[k].y - p.v[i].y;
if (DotProduct (n[i],v) > 0) // inner normal
{ n[i].x *= -1;
n[i].y = -1;
}
}
}

float DotProduct (Point2D v1, Point2D v2)
{
return v1.x*v2.x + v1.y*v2*y;
}

void CBClip (Point2D p1, Point2D p2, Normal n, Polygon p, Boolean &
visible,
Point2D & rp, Point2D & q)
{ float t1,t2,t,num,den;
Point2D dirV,F; // vectors
int I;

t1 = 0.0;
t2 = 1.0;
// compute the direction vector
dirV.x = p2.x - p1.x;
dirV.y = p2.y - p1.y;

visible = TRUE;
i = 0;
while ( (i < p.nPoints) && visible)
{ F.x = p1.x - p.v[i].x;
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


F.y = p1.y - p.v[i].y;

num = DotProduct (n[i],F);
den = DotProduct (n[i],dirV);

if (den == 0.0) // Parallel or Point
{ if (num > 0.0)
visible = FALSE; // Parallel and outside or point (p1 ==
p2) and outside
}
else
{ t = -(num/den);
if (den < 0.0) // entering
{ if (t <= 1.0)
if (t > t1)
t1 = t;
}
else if ( t >= 0.0) //exiting
if (t < t2)
t2 = t;
}
i++;
}
if ( t1 <= t2)
{ rp.x = p1.x + t1*dirV.x;
rp.y = p1.y + t1*dirV.y;
q.x = p1.x + t2.dirV.x
q.y = p1.y + t2*dirV.y
}
else
visible = FALSE;
}




Question 4: Write a program in C/C++ using OpenGL to draw a hard wire diagram as shown
in figure given below.
Answer.

Int main (int arg c , char ** arg v ) {
glvtInit ( & arg c, arg v);
glvInit Window Size ( 320, 320 );
glvInit Window Position ( 60, 60 );
glvInit Create Window ( 2 D Shapes);
glvInit Display Func ( display);
init GL();
glvertex 3f (-1. 0 f, - 0.5 f, - 4.0f);
glvertex 3f (1. 0 f, - 0.5 f, - 4.0f);
glvertex 3f (0. 0 f, - 0.5 f, - 4.0f);
glvertex 3f (0. 0 f, - 0.9 f, - 4.0f);
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


glvertex 3f (1. 0 f, - 0.0f, 0.0f);
glvertex 2f (-1. 8 f, - 0.1f );
glvertex 2f (-1. 2 f, - 0.1f );
glvertex 2f (-1. 2 f, - 0.7f );
glvertex 2f (-1. 8 f, - 0.7f );
glvertex 2f (-3.0 f, s.of, 2.9f );
glEnd ( );
}



Question 5: Write a program in C/C++ using OpenGL to implement Sutherland Hodgman
polygon clipping algorithm to clip the following polygon against the rectangular window as
given below. Make suitable assumptions.

Answer.

#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){
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


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;
}
MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk


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;


MCSL-054 Solved assignment 2013-14
By Sheel:- roshan_gupta1@rediffmail.com
Write at your own risk



For More Ignou Solved Assignments Please Visit - www.ignousolvedassignments.com

Connect on Facebook :
http://www.facebook.com/pages/IgnouSolvedAssignmentscom/346544145433550

Subscribe and Get Solved Assignments Direct to your Inbox :
http://feedburner.google.com/fb/a/mailverify?uri=ignousolvedassignments_com