+ All Categories

MCSL054

Date post: 06-Apr-2018
Category:
Upload: niftyviews
View: 217 times
Download: 0 times
Share this document with a friend
25
Part - I Ans 1 import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ShoppingCartViewerCookie extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); // Get the current session ID by searching the received cookies. String sessionid = null; Cookie[] cookies = req.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("sessionid")) { sessionid = cookies[i].getValue(); break; } } } // If the session ID wasn't sent, generate one. // Then be sure to send it to the client with the response. if (sessionid == null) { sessionid = generateSessionId(); Cookie c = new Cookie("sessionid", sessionid); res.addCookie(c); } out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>"); out.println("<BODY>"); // Cart items are associated with the session ID String[] items = getItemsFromCart(sessionid); // Print the current cart items. out.println("You currently have the following items in your cart:<BR>"); if (items == null) { out.println("<B>None</B>"); } else { out.println("<UL>"); for (int i = 0; i < items.length; i++) { out.println("<LI>" + items[i]); } out.println("</UL>"); } // Ask if they want to add more items or check out. out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>");
Transcript

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 1/25

Part - I

Ans 1

import java.io.*;import javax.servlet.*;

import javax.servlet.http.*;

public class ShoppingCartViewerCookie extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {

res.setContentType("text/html");PrintWriter out = res.getWriter();

// Get the current session ID by searching the received cookies.String sessionid = null;Cookie[] cookies = req.getCookies();if (cookies != null) {

for (int i = 0; i < cookies.length; i++) {if (cookies[i].getName().equals("sessionid")) {sessionid = cookies[i].getValue();break;

}}

}

// If the session ID wasn't sent, generate one.// Then be sure to send it to the client with the response.if (sessionid == null) {sessionid = generateSessionId();Cookie c = new Cookie("sessionid", sessionid);res.addCookie(c);

}

out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>");out.println("<BODY>");

// Cart items are associated with the session IDString[] items = getItemsFromCart(sessionid);

// Print the current cart items.out.println("You currently have the following items in your cart:<BR>");if (items == null) {out.println("<B>None</B>");

}

else {out.println("<UL>");for (int i = 0; i < items.length; i++) {out.println("<LI>" + items[i]);

}out.println("</UL>");

}

// Ask if they want to add more items or check out.out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>");

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 2/25

out.println("Would you like to<BR>");out.println("<INPUT TYPE=submit VALUE=\" Add More Items \">");out.println("<INPUT TYPE=submit VALUE=\" Check Out \">");out.println("</FORM>");

// Offer a help page.out.println("For help, click <A HREF=\"/servlet/Help" +

"?topic=ShoppingCartViewerCookie\">here</A>");

out.println("</BODY></HTML>");}

private static String generateSessionId() {String uid = new java.rmi.server.UID().toString(); // guaranteed uniquereturn java.net.URLEncoder.encode(uid); // encode any special chars

}

private static String[] getItemsFromCart(String sessionid) {// Not implemented

}

}

Ans 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="faqonpracticls.jsp"><h2>View Faq on MCAPractils</h2></a></center>

</body>

</html>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 3/25

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 

</td>

</tr>

<tr>

<td>Phone Number :</td>

<td> 022-25699002 </td>

</tr>

<tr>

<td>Email :</td>

<td> [email protected] </td>

</tr>

<tr>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 4/25

<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>

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 studentget any help/consideration?

<br><br>

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

</p>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 5/25

<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>

Ans 3

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><%@page import="java.sql.*,database.DBConnection"%>

<html>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 6/25

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

 </head><body>

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

<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()) {

%><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>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 7/25

<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()) {

%><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>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 8/25

<%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.cname from student,courses where student.cid=courses.cid andstudent.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><th>Age</th><th>Address</th><th>Course</th><th>Semester</th>

</tr><%

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 9/25

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 andstudent.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");

}catch(ClassNotFoundException e){

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

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

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 10/25

}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;

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'),

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 11/25

(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`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

LOCK TABLES `studycenter` WRITE;INSERT INTO `ignou`.`studycenter` VALUES ('Jaipur',1,'IIIM',1),('Mumbai',2,'ABCD',2);UNLOCK TABLES;

Ans 4

<?xml version="1.0"?>

<catalog>

<book id="bk101">

<author>Gambardella, Matthew</author>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 12/25

<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>

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 13/25

</book>

</catalog>

Part – 2

Qustion 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++)

{

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 14/25

buildActiveList (scan, active, edges);

if (active->next)

{

fillScan (scan, active);

updateActiveList (scan, active);

resortActiveList (active);}

}

/* 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)

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 15/25

{

/* 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;

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);

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 16/25

}

}

}

/* 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);

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;

}

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 17/25

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 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);

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 18/25

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++)

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)

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 19/25

{

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)

{

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 ... */

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 20/25

}

void resortActiveList (Edge * active)

{

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

active->next = NULL;

while (p){

q = p->next;

insertEdge (active, p);

p = q;

}

}

Ans 4

#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;

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 21/25

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

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

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();

}

Ans 5

#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 swappingvoid nkjSwap(int * , int *);

void nkjInit()

{

glClearColor(1.0,1.0,1.0,0.0);

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

glPointSize(4);

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 22/25

glMatrixMode(GL_PROJECTION);

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)

{

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 23/25

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

return;

}

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)

{

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 24/25

//this implies line is totaly invisible

return -1;

}

else

{

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

8/3/2019 MCSL054

http://slidepdf.com/reader/full/mcsl054 25/25

}

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

{

int code=0;

//code sequence UDLRif(x>xMax)

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();

}