#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
int a,z,i,j;
cout << "Enter the number of equations \n";
cin >> a;
float ar [a][a+1];
cout << "Entering the elements of the array \n";
for (i=0;i<a;i++)
{
for (j=0; j<(a); j++)
{
if (i==j)
{
ar[i][j] = 2;
}
else if(i!=j)
{
ar[i][j] = -1;
}
}
}
for (i=0;i<a;i++)
{
ar[i][a] = -97;
}
cout << " \n\n GAUSS JORDAN ELIMINATION \n";
float temp,max;
int k,l;
for (i=0;i <a; i++)
{
max = ar[i][i];
k=i;
for (j=i;j <a; j++)
{
if (abs(ar[j][i]) > abs(max))
{
max = ar[j][i];
k=j;
}
}
for (l=0;l<a+1;l++)
{
temp = ar[i][l];
ar[i][l] = ar[k][l];
ar[k][l] = temp;
}
int x,y;
float z, rat;
z = ar[i][i];
if (z != 0)
{
for (x=i+1; x <a; x++)
{
rat = (ar[x][i]) / (ar[i][i]);
for (y=0;y<a+1;y++)
{
ar[x][y] = ar[x][y] - (rat*ar[i][y]);
}
}
}
}
int ab;
float x[a];
for (ab=(a-1);ab>-1;ab--)
{
x[ab]=ar[ab][a];
for (i=ab+1;i<a;i++)
{
x[ab]=x[ab]-(ar[ab][i]*x[i]);
}
x[ab] = (x[ab]/ar[ab][ab]);
}
for (i=0;i<a;i++)
{
cout << "x["<<i<<"] \t= " << x[i] <<endl;
}
return 0;
}
Monday, August 24, 2015
Program for Gauss-Jordan Elimination without frills
python code to change extension of all files in a folder
import os
for root,dirs,files in os.walk('./'):
for file in files:
if file.endswith('.oldext'):
F=open(os.path.join(root,file),'r')
W=open('./foldername/'+file.replace('.oldext','.newext'),'w')
for line in F.readlines():
W.write(line)
W.close()
F.close()
print 'All Extensions changed Successfully'
----------------------------------------------------------
change the file extensions as your need
replace oldext with the extension your files are currently having
replace newext with the new extension you want to have for all of ur files
save the above code in a file with .py extension
create a folder named foldername in same directory where the python file is saved
keep all the files whose extension you want to change in the same folder as code file
Sunday, May 4, 2014
Implementation of topological sorting in c++
topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge u->v from vertex u to vertex v, u comes before v in the ordering.
The canonical application of topological sorting is in scheduling a sequence of jobs or tasks based on their dependencies;
#include<iostream.h>
int n,adj[100][100];
int front = -1,rear = -1,queue[100];
void main()
{
int i,j = 0,k;
int topsort[100],indeg[100];
create_graph();
cout<<"The adjacency matrix is:"<<"/n";
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;i<=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
cout<<"Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
cout<<topsort[i];
cout<<"\n";
}
create_graph()
{
int i,max_edges,origin,destin;
cout<<"\n Enter number of vertices:";
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
cout<<"\n Enter edge "<<i<<" (00 to quit):";
cin>>origin>>destin;
if((origin == 0) && (destin == 0))
{
cout<<"Invalid edge!!\n";
i–-;
}
else
adj[origin][destin] = 1;
}return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
cout<<"Queue Underflow”;
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
returnin_deg;
}
The canonical application of topological sorting is in scheduling a sequence of jobs or tasks based on their dependencies;
#include<iostream.h>
int n,adj[100][100];
int front = -1,rear = -1,queue[100];
void main()
{
int i,j = 0,k;
int topsort[100],indeg[100];
create_graph();
cout<<"The adjacency matrix is:"<<"/n";
display();
for(i=1;i<+n;i++)
{
indeg[i]=indegree(i);
if(indeg[i]==0)
insert_queue(i);
}
while(front<=rear)
{
k=delete_queue();
topsort[j++]=k;
for(i=1;i<=n;i++)
{
if(adj[k][i]==1)
{
adj[k][i]=0;
indeg[i]=indeg[i]-1;
if(indeg[i]==0)
insert_queue(i);
}
}
}
cout<<"Nodes after topological sorting are:\n");
for(i=0;i<=n;i++)
cout<<topsort[i];
cout<<"\n";
}
create_graph()
{
int i,max_edges,origin,destin;
cout<<"\n Enter number of vertices:";
scamf("%d",&n);
max_edges = n * (n - 1);
for(i = 1;i <= max_edges;i++)
{
cout<<"\n Enter edge "<<i<<" (00 to quit):";
cin>>origin>>destin;
if((origin == 0) && (destin == 0))
{
cout<<"Invalid edge!!\n";
i–-;
}
else
adj[origin][destin] = 1;
}return;
}
display()
{
int i,j;
for(i = 0;i <= n;i++)
{
for(j = 1;jrear)
{
cout<<"Queue Underflow”;
return;
}
else
{
del_item = queue[front];
front = front + 1;
return del_item;
}
}
int indegree(int node)
{
int i,in_deg = 0;
for(i = 1;i <= n;i++)
if(adj[i][node] == 1)
in_deg++;
returnin_deg;
}
Labels:
algorithm,
c++,
Data Structures
Liang - Barsky Plygon clipping Algorithm
algorithm for Clipping Polygon Segments
INPUT : HSA=<h1,h2,...hn> (Half-segment Array)
w = Rectangle
OUTPUT: cHSA = clipped half-segments and the half-segments
resulting from the evaluation of turning points
cHSA = Ø;
turningPointSets = Ø;
FOR i=1 TO n DO
IF (hi has left dominating point) THEN
IF (SutherlandCohenLineClipping(hi, w, clippedhs, intersection-point,
isIntersectionPoint)) THEN
IF (isIntersectionPoint) THEN
EvaluateTurningPoint(w, intersectionPoint, turningPointSets, hi);
ELSE
cHSA.Add(clippedhs);
algorithm to EvaluateTurningPoint
INPUT : w = a rectangle described by the coordinates (xmin, ymin) and (xmax, ymax)
p = Point
turningPointSets = for each window egde there is a set recording the
turning point of the edge
h = halfsegment that the point p belongs to
OUTPUT: If the point is evaluated as a turning point it is added to the Turning
Point Set of the edge
tp = p;
IF (p.x = w.xmin) THEN //left edge
IF (h.insideAbove) THEN
tp.direction = UP;
ELSE
Tp. direction = DOWN;
END-IF;
turningPointSets[LEFT].add(tp);
ELSE //right edge
IF (h.insideAbove) THEN
tp. direction = UP;
ELSE
tp. direction = DOWN;
END-IF;
turningPointSets[RIGHT].add(tp);
END-IF;
IF (p.y = w.ymin) THEN //bottom edge
IF (h.leftPoint > w.ymin) THEN
tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
ELSE
tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
END-IF;
turningPointSets[BOTTOM].add(tp);
ELSE
IF (p.y = w.ymax) THEN //top edge
IF (h.leftPoint > w.ymin) THEN
tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
ELSE
tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
END-IF;
turningPointSets[BOTTOM].add(tp);
END-IF;
END-IF;
algorithm to Get Direction
INPUT: tp = turning point
p = point of the same half segment that the turning point tp belongs
and is above tp
(x, y) = the left coordinate of the vertex of the window edge
insideAbove = insideAbove flag’s value
IF (insideAbove) THEN
IF (tp.x > p.x) THEN
return RIGHT;
ELSE
return LEFT;
END-IF;
ELSE
IF (tp.x > p.x) THEN
return LEFT;
ELSE
return RIGHT;
END-IF;
algorithm to Create NewSegments
INPUT: edge = indicates which edge is been handled(LEFT, RIGHT, TOP or
BOTTOM)
bPoint,ePoint = end points of the edge
turningPointSet = a set of the turning points of the edge
cHSA = set of half segments in which the new half segments will be added
OUTPUT: cHSA with the new half segments
IF edge == TOP or edge == LEFT THEN
InsideAbove = false;
ELSE /*RIGHT or BOTTOM edges*/
InsideAbove = true;
END-IF;
begin = 0;
end = turningPointSet.size();
tp = turningPointSet[begin];
IF (tp.Direction== LEFT or tp.Direction == DOWN) and not tp.Rejected THEN
cHSA.addHalfSegments(tp,bPoint,InsideAbove);
DiscardTurningPoints(turningPointSet, tp, ASCENDIN_GORDER, begin);
END-IF;
tp = turningPointSet[end];
IF (tp.Direction== RIGHT or tp.Direction == UP) and not tp.Rejected
and there is no rejected turning point equals to tp THEN
cHSA.addHalfSegments(tp,ePoint,InsideAbove);
DiscardTurningPoints(turningPointSet, DESCENDING_ORDER, end);
END-IF;
WHILE (begin<end) DO
tp1 = GetNotRejectedTurningPoint(turningPointSet, ASCENDING_ORDER, begin);
IF tp1 == NULL THEN
return;
END-IF;
tp2 = GetNotRejectedTurningPoint(turningPointSet, DESCENDING_ORDER, end);
IF tp2 == NULL THEN
return;
END-IF;
cHSA.addHalfSegments(tp1,tp2,InsideAbove);
END-WHILE;
INPUT : HSA=<h1,h2,...hn> (Half-segment Array)
w = Rectangle
OUTPUT: cHSA = clipped half-segments and the half-segments
resulting from the evaluation of turning points
cHSA = Ø;
turningPointSets = Ø;
FOR i=1 TO n DO
IF (hi has left dominating point) THEN
IF (SutherlandCohenLineClipping(hi, w, clippedhs, intersection-point,
isIntersectionPoint)) THEN
IF (isIntersectionPoint) THEN
EvaluateTurningPoint(w, intersectionPoint, turningPointSets, hi);
ELSE
cHSA.Add(clippedhs);
algorithm to EvaluateTurningPoint
INPUT : w = a rectangle described by the coordinates (xmin, ymin) and (xmax, ymax)
p = Point
turningPointSets = for each window egde there is a set recording the
turning point of the edge
h = halfsegment that the point p belongs to
OUTPUT: If the point is evaluated as a turning point it is added to the Turning
Point Set of the edge
tp = p;
IF (p.x = w.xmin) THEN //left edge
IF (h.insideAbove) THEN
tp.direction = UP;
ELSE
Tp. direction = DOWN;
END-IF;
turningPointSets[LEFT].add(tp);
ELSE //right edge
IF (h.insideAbove) THEN
tp. direction = UP;
ELSE
tp. direction = DOWN;
END-IF;
turningPointSets[RIGHT].add(tp);
END-IF;
IF (p.y = w.ymin) THEN //bottom edge
IF (h.leftPoint > w.ymin) THEN
tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
ELSE
tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
END-IF;
turningPointSets[BOTTOM].add(tp);
ELSE
IF (p.y = w.ymax) THEN //top edge
IF (h.leftPoint > w.ymin) THEN
tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
ELSE
tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
END-IF;
turningPointSets[BOTTOM].add(tp);
END-IF;
END-IF;
algorithm to Get Direction
INPUT: tp = turning point
p = point of the same half segment that the turning point tp belongs
and is above tp
(x, y) = the left coordinate of the vertex of the window edge
insideAbove = insideAbove flag’s value
IF (insideAbove) THEN
IF (tp.x > p.x) THEN
return RIGHT;
ELSE
return LEFT;
END-IF;
ELSE
IF (tp.x > p.x) THEN
return LEFT;
ELSE
return RIGHT;
END-IF;
algorithm to Create NewSegments
INPUT: edge = indicates which edge is been handled(LEFT, RIGHT, TOP or
BOTTOM)
bPoint,ePoint = end points of the edge
turningPointSet = a set of the turning points of the edge
cHSA = set of half segments in which the new half segments will be added
OUTPUT: cHSA with the new half segments
IF edge == TOP or edge == LEFT THEN
InsideAbove = false;
ELSE /*RIGHT or BOTTOM edges*/
InsideAbove = true;
END-IF;
begin = 0;
end = turningPointSet.size();
tp = turningPointSet[begin];
IF (tp.Direction== LEFT or tp.Direction == DOWN) and not tp.Rejected THEN
cHSA.addHalfSegments(tp,bPoint,InsideAbove);
DiscardTurningPoints(turningPointSet, tp, ASCENDIN_GORDER, begin);
END-IF;
tp = turningPointSet[end];
IF (tp.Direction== RIGHT or tp.Direction == UP) and not tp.Rejected
and there is no rejected turning point equals to tp THEN
cHSA.addHalfSegments(tp,ePoint,InsideAbove);
DiscardTurningPoints(turningPointSet, DESCENDING_ORDER, end);
END-IF;
WHILE (begin<end) DO
tp1 = GetNotRejectedTurningPoint(turningPointSet, ASCENDING_ORDER, begin);
IF tp1 == NULL THEN
return;
END-IF;
tp2 = GetNotRejectedTurningPoint(turningPointSet, DESCENDING_ORDER, end);
IF tp2 == NULL THEN
return;
END-IF;
cHSA.addHalfSegments(tp1,tp2,InsideAbove);
END-WHILE;
algorithm for Polygon Reconstruction
INPUT: HSA=<h1,h2,...hn> (Halfsegment Array)
OUTPUT: HSA = each halfsegment has the face, cycle, and edge numbers set
VARIABLES: face = array that stores in position i the last cycle number of the
face i
hsSet = array that stores in the position i if the half segment hi
had already the face number, the cycle number and the edge
number set. This array is initialized with values false.
IF HSA is not sorted in halfsegment order THEN
Sort HSA in halfsegment order;
END-IF;
IF the halfsegments of HSA do not have the partner number set THEN
Set partner number of the halfsegments of HSA;
END-IF;
face[0] = 0; /*0 is assigned to the first cycle of the first face */
lastFaceNumber = 0;
isFirstHS = true;
FOR i=1 TO n DO
IF hi has left dominating point and not hsSet[i] THEN
IF isFirstHS THEN
isFirstHS = false;
hi.faceNumber = 0;
hi.cycleNumber = 0;
ELSE
existingFaceNumber = GetFaceNumber(HSA, hi, hsSet, i);
IF existingFaceNumber is equal to -1 THEN
lastFaceNumber++;
hi.faceNumber = lastFaceNumber;
hi.cycleNumber = 0;
/*to store the first cycle number of the face lastFace*/
face[faceNumber-1]=0;
ELSE
hi.faceNumber = existingFaceNumber;
face[faceNumber]++;
hi.cycleNumber = face[faceNumber];
END-IF;
END-IF;
hi.edgeNumber = 0;
ComputeCycle(HSA, hi, hsSet);
END-IF;
END-FOR;
window clipping in
Signature: (line x rect) -> line, (region x rect) --> region
Syntax: windowclippingin( _, _ )
Meaning: computes the part of the object that is inside the window.
Example: query Flaechen feed extend[InWindow: windowclippingin(
.geoData, bbox(thecenter))] project[InWindow]
filter[not(isempty(.InWindow))] consume
window clipping out
Signature: (line x rect) -> line, (region x rect) --> region
Syntax: windowclippingout( _, _ )
Meaning: computes the part of the object that is outside the window.
Example: query windowclippingout(trajectory(train7), bbox(thecenter))
Labels:
algorithm,
computer graphics
Sunday, April 6, 2014
Wednesday, April 2, 2014
Errors in programs of computer graphics related to GRAPHICS.H
Some of the Errors Startup and New Programmers face while writing Graphics programs in C or C++ :
- Messages like " Cannot open include file < graphics.h > " .
- Linker errors relating to < graphics.h > .
- Messages like : Graphics not initialized .
- Copy the file [ egavga.bgi ] from the folder BGI to BIN folder
- GoTo OPTIONS > LINKER > LIBRARIES and mark the Graphics option
- Use int gdriver , gmode = DETECT; initgraph( &gdriver, &gmode, "" ) ; before writing any graphics code ;
Labels:
c,
c++,
computer graphics,
Troubleshoot
Subscribe to:
Posts (Atom)