The Programming Project: Linux
Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, May 5, 2014

open GL Programming in LINUX


To compile a program (using open GL graphics library) in Linux, go to the current directory where the program is residing say test.c. type the following command gcc -o xyz test.c -lglut -lGLU -lGL -lGLU make sure you have the open GL library installed! After the execution of the command a output file with name xyz will be created in the current directory. To run the output file type in the following command ./xyz

To sum up consider a program vinod.c in the folder Graphics, to execute it perform the following steps.
administrator@ubuntu:~/Graphics$ gcc -o vin vinod.c -lglut -lGLU -lGL -lGLU
administrator@ubuntu:~/Graphics$ ./vin


Here is an example of a simple program which will just create a window with a text

#include <GL/glut.h>
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("MY FIRST GRAPHICS PROGRAM");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Saturday, September 14, 2013

Calculating Factorial in Python

A simple program in Python to calculate the factorial of an natural number:

#File: fact.py
#A simple program
def main():
    print "Factorial Function"
    x=input("Enter a natural number:")
    l=1
    for i in range(x):
        l=l*x
        x=x-1
    print l
main()

Run the python interpreter from the folder where the file (fact.py) is residing and import it as shown below:


administrator@ubuntu:~/python$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import fact
Factorial Function
Enter a natural number:350
123587405826548875014395199766546457224532073946919515879429330230093035357491314216934583295011178445941552109432761532449767761892237043444942213964090091669490545661255111334533069825455607852789836451585122902099649977304226794874840601811017764137584868137504975397325925882541777117706619490238363409254589994079334626893194608016888986949684994333459029365214555784862353939102567266745712846824819004146064184543888123533464975621179287075018586481357643313075153359002713294611632614208134036650116689052585573350955360246170451786972351365370405722036294385680478287278827977511411909071460914807681131728232182991517416470483157998067487290163200000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>>


Tuesday, September 10, 2013

Applet

This Java code simply display a "Quote" on the Applet. Here is the snapshot.
 

import java.awt.*;
import java.applet.*;
/*
<applet code="AppletQuote" width=900 height=80>
</applet>
*/
public class AppletQuote extends Applet
    {
    public void init()
        {
        setBackground(Color.cyan);
        }
    public void paint(Graphics g)
        {
        Quotes qts = new Quotes();
            int n = (int)(Math.random()*10);
              g.drawString(qts.getQuote(n),30,30);   
            }
        }
class Quotes
    {
    Quotes()
        {
        }
    public String getQuote(int n)
        {
        return (quotes[n]);
        }
    private static String quotes[]={"I like mathematics because it is not human and has nothing particular to do with this planet or with the whole accidental universe                         - because like Spinoza's God, it won't love us in return.  ~Bertrand Russell",
                    "If there is a God, he's a great mathematician.  ~Paul Dirac",
                    "Do not worry about your problems with mathematics, I assure you mine are far greater. ~Albert Einstein",
                    "There are things which seem incredible to most men who have not studied mathematics. ~ Aristotle",
                    "Mathematics is a game played according to certain simple rules with meaningless marks on paper. ~ David Hilbert",
                    "Go down deep enough into anything and you will find mathematics~ Dean Schlicter",
                    " Life is good for only two things, discovering mathematics and teaching mathematics. ~ Simeon Poisson",
                    "In mathematics you don't understand things. You just get used to them. ~ Johann von Neumann",
                    " Medicine makes people ill, mathematics make them sad and theology makes them sinful. ~ Martin Luther",
                    "A man whose mind has gone astray should study mathematics. ~ Francis Bacon"};
    }