Thursday, March 29, 2012

OpenGl house3D C# tao framework, 3DMax project

Hi there. Today I'll show you how to write something like a game with C#, OpenGL, Tao framework and  3D Max. It looks like that:




So first off all we must come up with structure of our program. it will consist of the following classes: House, camera, controller, exterior.

Than we need to draw the house, I did this job in 3DMax and it was really easy. You can find many different lessons or documentation, on how to do this in the internet, or even by some books but I prefer the first :)

You can DOWNLOAD it from HERE. And here is the project code:


The MainForm:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ShadowEngine;
using ShadowEngine.OpenGL;
using Tao.OpenGl;
using ShadowEngine.Sound;

namespace Casa
{
    public partial class MainForm : Form
    {
        //handle the viewport
        uint hdc;
        controller controller = new controller();
        int moving;
        static Vector2 formPos;
        bool lines;
        bool pressedH;

        public static Vector2 FormPos
        {
            get { return MainForm.formPos; }
            set { MainForm.formPos = value; }
        }

        public MainForm()
        {
            InitializeComponent();
            //identifier of where I will draw
            hdc = (uint)pnlViewPort.Handle;
            //making the mistake that happened
            string error = "";
            //Command initialization of the viewport
            OpenGLControl.OpenGLInit(ref hdc, pnlViewPort.Width, pnlViewPort.Height, ref error);

            if (error != "")
            {
                MessageBox.Show(error);
            }

            // start position of the camera angle so as defined in perspective, etc etc
            controller.Camera.InitCamera();
            //Enables the lights
            Lighting.SetupLighting();
            ContentManager.SetTextureList("textures\\"); //specify the location of the textures
            ContentManager.LoadTextures(); //the charge
            ContentManager.SetModelList("models\\"); // the specific location of the office
            ContentManager.LoadModels(); //the charge
            Camera.CenterMouse();
            controller.creatingObjects();
        }

        private void tmrPaint_Tick(object sender, EventArgs e)
        {
            // opengl clean paint
            Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
            //draws the entire scene
            controller.Camera.Update(moving);
            controller.DrawScene();
            //change the buffer
            Winapi.SwapBuffers(hdc);
            //finish paint
            Gl.glFlush();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            formPos = new Vector2(this.Left, this.Top); // first coordinates of camera
        }
     
        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {


            if (!pressedH)
            {
                panel1.Visible = true;
                panel2.Visible = true;
                panel3.Visible = true;
                panel4.Visible = true;
                pressedH = true;
            }
            else
            {
                panel1.Visible = false;
                panel2.Visible = false;
                panel3.Visible = false;
                panel4.Visible = false;
                pressedH = false;
            }
         
            if (e.KeyCode == Keys.Escape)
            {
                Close();
            }
            if (e.KeyCode == Keys.W)
            {
                moving = 1;
            }

            if (e.KeyCode == Keys.L)
            {
                if (lines)
                {
                    Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL);
                    lines = false;
                }
                else
                {
                    Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE);
                    lines = true;
                }
            }
        }

        private void pnlViewPort_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                moving = 1;
            }
            if (e.Button == MouseButtons.Right)
            {
                moving = -1;
            }
        }

        private void pnlViewPort_MouseUp(object sender, MouseEventArgs e)
        {
            moving = 0;
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            moving = 0;
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void pnlViewPort_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}






The class House:


using System;
using System.Collections.Generic;
using System.Text;
using ShadowEngine;
using ShadowEngine.ContentLoading;
using Tao.OpenGl;

namespace Casa
{
    public class House
    {
        ModelContainer m;
        Mesh aspa;
        float bladeAngle;

     

        public void Create()
        {        
            m = ContentManager.GetModelByName("house3.3DS");      
            m.CreateDisplayList();
            aspa = m.GetMeshWithName("ventilado0");
            aspa.CalcCenterPoint(); // calculating the midpoint of the object
            m.RemoveMeshByName("ventilado0"); //remove the mesh        
        }

        public void CreateCollisions()
        {
            Collision.AddCollisionSegment(new Point3D(-24.4f, -14.1f, 0), new Point3D(18.9f, -14.1f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(-24.4f, -14.1f, 0), new Point3D(-24.4f, 13.2f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(-20.2f, -0.1f, 0), new Point3D(-4.8f, -0.1f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(-0.5f, 0.7f, 0), new Point3D(-0.5f, -8.7f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(19.4f, 14.4f, 0), new Point3D(-24.4f, 14.4f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(19.4f, 14.4f, 0), new Point3D(19.4f, -14.4f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(-17.5f, 0.5f, 0), new Point3D(-17.5f, 11, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(13.4f, -0.15f, 0), new Point3D(18.74f, -0.15f, 0), 0.5f);
            Collision.AddCollisionSegment(new Point3D(-0.43f, -9.1f, 0), new Point3D(12.4f, -9.1f, 0), 0.5f);
            //Collision.GhostMode = true;
        }

        public void Draw()
        {
            Gl.glPushMatrix();
            Gl.glTranslatef(0, 2.3f, 0);
            Gl.glScalef(0.1f, 0.1f, 0.1f);
            m.DrawWithTextures();

            #region Ventilator (rotation, drawing...)
            Gl.glPushMatrix();
            bladeAngle += 25;
            if (bladeAngle > 3600)
            {
                bladeAngle = 0;
            }
                     
            //rotaition of ventilator
            Gl.glTranslatef(aspa.CenterPoint.X, aspa.CenterPoint.Y, aspa.CenterPoint.Z);
            Gl.glRotatef(-bladeAngle, 0, 0, 1);
            Gl.glTranslatef(-aspa.CenterPoint.X, -aspa.CenterPoint.Y, -aspa.CenterPoint.Z);
         
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName(aspa.Name + ".jpg"));  
            aspa.Draw();
            Gl.glPopMatrix();
         
            #endregion

            Gl.glPopMatrix();
        }
    }
}


Class Camera:

using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using System.Drawing;
using ShadowEngine; 

namespace Casa
{
    public class Camera
    {
        #region Camera constants
        const double div1 = Math.PI / 180;
        const double div2 = 180 / Math.PI;
        #endregion 
        
        #region Private atributes

        static float eyex, eyey, eyez;
        static float centerx, centery, centerz;
        static float forwardSpeed = 0.3f;
        static float yaw, pitch;
        static float rotationSpeed = 1/5f;
        static double i, j, k;

        #endregion

        public static float Pitch
        {
            get { return Camera.pitch; }
            set { Camera.pitch = value; }
        }

        public static float Yaw
        {
            get { return Camera.yaw; }
            set { Camera.yaw = value; }
        }

        public void InitCamera() //initilizing pos of x,y,z
        {
            eyex = -17.1f;
            eyey = 7.3f;
            eyez = -9.4f;
            centerx = -3;
            centery = 2;
            centerz = -2; 
            Look();
        }

        public void Look()
        {
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Glu.gluLookAt(eyex, eyey, eyez, centerx, centery, centerz, 0, 1, 0);
        }

        static public float AngleToRad(double pAngle)
        {
            return (float)(pAngle * div1);
        }

        static public float RadToAngle(double pAngle)
        {
            return (float)(pAngle * div2);
        }

        public void UpdateDirVector()
        {
            k = Math.Cos(AngleToRad((double)yaw));
            i = -Math.Sin(AngleToRad((double)yaw));
            j = Math.Sin(AngleToRad((double)pitch));     
            
            
            centerz = eyez - (float)k;
            centerx = eyex - (float)i;
            centery = eyey - (float)j;
        }

        public static void CenterMouse()
        {
            Winapi.SetCursorPos(MainForm.FormPos.X + 512, MainForm.FormPos.Y + 384);   
        }

        public void Update(int pressedButton)
        {
            #region Target chamber

                Pointer position = new Pointer();
                Winapi.GetCursorPos(ref position);   

                int difX = MainForm.FormPos.X + 512 - position.x;
                int difY = MainForm.FormPos.Y + 384 - position.y;

                if (position.y < MainForm.FormPos.Y + 384)
                {
                    pitch -= rotationSpeed * difY;
                }
                else
                    if (position.y > MainForm.FormPos.Y + 384)
                    {
                        pitch += rotationSpeed * -difY;
                    }
                if (position.x < MainForm.FormPos.X + 512)
                {
                    yaw += rotationSpeed * -difX;
                }
                else
                    if (position.x > MainForm.FormPos.X + 512)
                    {
                        yaw -= rotationSpeed * difX;
                    }
                UpdateDirVector();
                CenterMouse();


                if (pressedButton == 1) // pressed the left button of mouse
                {
                    if (!Collision.CheckCollision(new Point3D(eyex - (float)i * forwardSpeed, eyez - (float)k * forwardSpeed, 0)))
                    {
                        eyex -= (float)i * forwardSpeed;
                        eyez -= (float)k * forwardSpeed;
                    }   
                }
                if (pressedButton == -1) // pressed the left button of mouse
                {
                    if (!Collision.CheckCollision(new Point3D(eyex + (float)i * forwardSpeed, eyez + (float)k * forwardSpeed, 0)))
                    {
                        eyex += (float)i * forwardSpeed;
                        eyez += (float)k * forwardSpeed;
                    }
                }

            #endregion

            Look();  
        }
    }
}


Class Controller:



using System;
using System.Collections.Generic;
using System.Text;
using ShadowEngine;


namespace Casa
{
    public class controller
    {
        Camera camera = new Camera();
        House house = new House();
        Exterior sky = new Exterior();

        public void creatingObjects()
        {
            house.Create();
            house.CreateCollisions(); // making walls
            Sprite.Create();  // draw text on screen (coordinates)
        }

        public Camera Camera
        {
            get { return camera; }
        }

        public void DrawScene()
        {
            house.Draw(); // drawing the house
            sky.Draw();  // outside the house
            DebugMode.WriteCamaraPos(200, 200);
            Collision.DrawColissions();
        }
    }
}


Class Exterior:

using System;
using System.Collections.Generic;
using System.Text;
using ShadowEngine;
using Tao.OpenGl;

namespace Casa
{
    class Exterior
    {
        public void Draw()
        {
            // size height and length
            int width = 240;
            int height = 200;
            int length = 240;

            // begins at these coordinates
            int x = 10;
            int y = -3;
            int z = 7;

            //encentrar the square
            x = x - width / 2;
            y = y - height / 2;
            z = z - length / 2;

            Gl.glEnable(Gl.GL_TEXTURE_2D);
            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("back.jpg"));

            //begins to draw squares
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(-1, 1, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3d(x + width, y, z);
            Gl.glNormal3d(-1, -1, 1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3d(x + width, y + height, z);
            Gl.glNormal3d(1, -1, 1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3d(x, y + height, z);
            Gl.glNormal3d(1, 1, 1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x, y, z);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("front.jpg"));
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(1, 1, -1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3d(x, y, z + length);
            Gl.glNormal3d(1, -1, -1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3d(x, y + height, z + length);
            Gl.glNormal3d(-1, -1, -1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3d(x + width, y + height, z + length);
            Gl.glNormal3d(-1, 1, -1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x + width, y, z + length);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("top.jpg"));
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(-1, -1, 1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3d(x + width, y + height, z);
            Gl.glNormal3d(-1, -1, -1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3d(x + width, y + height, z + length);
            Gl.glNormal3d(1, -1, -1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3d(x, y + height, z + length);
            Gl.glNormal3d(1, -1, 1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x, y + height, z);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("left.jpg"));
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(1, -1, 1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3d(x, y + height, z);
            Gl.glNormal3d(1, -1, -1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3d(x, y + height, z + length);
            Gl.glNormal3d(1, 1, -1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3d(x, y, z + length);
            Gl.glNormal3d(1, 1, 1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x, y, z);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("right.jpg"));
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(-1, 1, 1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x + width, y, z);
            Gl.glNormal3d(-1, 1, -1);
            Gl.glTexCoord2f(1.0f, 0.0f); Gl.glVertex3d(x + width, y, z + length);
            Gl.glNormal3d(-1, -1, -1);
            Gl.glTexCoord2f(1.0f, 1.0f); Gl.glVertex3d(x + width, y + height, z + length);
            Gl.glNormal3d(-1, -1, 1);
            Gl.glTexCoord2f(0.0f, 1.0f); Gl.glVertex3d(x + width, y + height, z);
            Gl.glEnd();

            Gl.glBindTexture(Gl.GL_TEXTURE_2D, ContentManager.GetTextureByName("GRASS2.JPG"));
            Gl.glBegin(Gl.GL_QUADS);
            Gl.glNormal3d(1, 1, 1);
            Gl.glTexCoord2f(8.0f, 0.0f); Gl.glVertex3d(x, 1, z);
            Gl.glNormal3d(1, 1, -1);
            Gl.glTexCoord2f(8.0f, 8.0f); Gl.glVertex3d(x, 1, z + length);
            Gl.glNormal3d(-1, 1, -1);
            Gl.glTexCoord2f(0.0f, 8.0f); Gl.glVertex3d(x + width, 1, z + length);
            Gl.glNormal3d(-1, 1, 1);
            Gl.glTexCoord2f(0.0f, 0.0f); Gl.glVertex3d(x + width, 1, z);
            Gl.glEnd();
        }
    }
}

Class winApi



using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Casa
{
    public struct Pointer
    {
        public int x;
        public int y;
    }

    public static class Winapi
    {
        [DllImport("GDI32.dll")]
        public static extern void SwapBuffers(uint hdc);

        [DllImport("user32.dll")]
        public static extern void SetCursorPos(int x, int y);

        [DllImport("user32.dll")]
        public static extern void GetCursorPos(ref Pointer point);
    }
}




Friday, March 9, 2012

Sort words by alphabet С++

Hi, this time I'll show you how to  sort words by alphabet on С++.
It's rather easy. 

So here is the code:


#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector> 
#include <algorithm> 


using namespace std; 


string initial = "qwerty asdfgh zxcvbn";
vector<string> s; 


vector<string> split(const string &s, const char *by = " "
{
   vector<string> res;
   int i, j;
   int n = s.size();
   for (i = 0; i <= n; i = j + 1)
    {
      for (j = i; j < n && strchr(by, s[j]) == NULL;) j++;
      res.push_back(s.substr(i, j-i));   
    }


     return res;
}


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Initial string: " << initial << endl;


    s = split(initial);
    sort(s.begin(),s.end());
    initial = "";


    for (int i=0; i<s.size(); i++)
    {
         initial += s[i]+" ";
    }


     cout << "Final string: " << initial << endl;
   return 0;


 }

Program on C++ that trasform int to string, string to int, char to int

Hi, today it's gonna be another C++ program that finds all numbers between 1000 and 10 000 (it's four - digitnumbers and this interval can be changed).
Those numbers looks like abcd where a,b,c,d - all different  and ab - cd = a+ b+c+d .

So to write it I choosed the way to change the numbers in strings. So now it really easy to check if conditions are met.

To transform int to string on C++ I used itoa function and to transform string to int on C++ I used atoi function.

To transform char to int on C++ I used code like this:
char s;
cin>>s;
int i = s - '0';
 So here is the code:


#include "stdafx.h" 
#include <iostream> 
#include <string
#include <stdio.h> 
#include <stdlib.h> 


using namespace std; 
// itoa : int to string; 
// atoi : string to int; 


int _tmain(int argc, _TCHAR* argv[]) 

  string abs;   
  string cds;     
  int sum = 0;
  int n; 


  for(int n = 1000; n < 10000; n++)
  {
     char buffer[5];
     string str = itoa(n, buffer, 10); 
     int k = 1;


     for(int i = 1; i< 4; i++) 
      if (str[i-1] != str[i]) 
        k++;   
    if (k == 4)   
    { 
      abs = str[0]; 
      abs += str[1];
      cds = str[2]; 
      cds += str[3]; 


       sum += (str[0]-'0') + (str[1]-'0') + (str[2]-'0') + (str[3]-'0'); 


      int iab = atoi(abs.c_str());
      int icd = atoi(cds.c_str()); 
      if (iab - icd == sum)  
       cout<<n<<endl; 


      sum = 0;
     };


   } 


   return 0;


 } 

Saturday, March 3, 2012

Jacobi method pascal / delphi

In numerical linear algebra, the Jacobi method is an algorithm for determining the solutions of a system of linear equations with largest absolute values in each row and column dominated by the diagonal element. Each diagonal element is solved for, and an approximate value plugged in. The process is then iterated until it converges. This algorithm is a stripped-down version of the Jacobi transformation method of matrix diagonalization. The method is named after German mathematician Carl Gustav Jakob Jacobi.





Given a square system of n linear equations:
A\mathbf x = \mathbf b
where:
A=\begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\a_{n1} & a_{n2} & \cdots & a_{nn} \end{bmatrix}, \qquad  \mathbf{x} = \begin{bmatrix} x_{1} \\ x_2 \\ \vdots \\ x_n \end{bmatrix} , \qquad  \mathbf{b} = \begin{bmatrix} b_{1} \\ b_2 \\ \vdots \\ b_n \end{bmatrix}.
Then A can be decomposed into a diagonal component D, and the remainder R:
A=D+R \qquad \text{where} \qquad D = \begin{bmatrix} a_{11} & 0 & \cdots & 0 \\ 0 & a_{22} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\0 & 0 & \cdots & a_{nn} \end{bmatrix}, \qquad R = \begin{bmatrix} 0 & a_{12} & \cdots & a_{1n} \\ a_{21} & 0 & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \cdots & 0 \end{bmatrix}
,\qquad \mathbf{x}^{(k+1)} = D^{-1} (\mathbf{b} - R \mathbf{x}^{(k)}).
The element-based formula is thus:
 x^{(k+1)}_i  = \frac{1}{a_{ii}} \left(b_i -\sum_{j\ne i}a_{ij}x^{(k)}_j\right),\quad i=1,2,\ldots,n.
Note that the computation of xi(k+1) requires each element in x(k) except itself. Unlike the Gauss–Seidel method, we can't overwrite xi(k) with xi(k+1), as that value will be needed by the rest of the computation. The minimum amount of storage is two vectors of size n.

So here is the code:

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;
 const e = 0.000001;
var n,i,j,k,m,i1,j1,l:integer; a:array[1..5,1..5]of extended;
b:array[1..5,1..5]of extended; tk:array[1..5,1..5]of extended;
q,p,d,r,c,s,sign,ab,eb:real; t:array[1..5,1..5]of extended;
cm: array[1..5,1..5]of extended;

begin
writeln('Input n '); // Matrix size
read(n);
writeln('Input matrix A ');

// Matrix input
  for i:=1 to n do
    for j:=1 to n do
      read(a[i,j]);

    ab:=a[1,2];
    eb:=1;

    for i1:=1 to n do
      for j1:=1 to n do
        if i1<>j1 then
        begin
          if abs(a[i1,j1])>ab then
            begin
              i:=i1;
              j:=j1
            end;
        end;

  for i1:=1 to n do
  for j1:=1 to n do
    if i1=j1 then
      begin
        t[i1,i1]:=1;
        tk[i1,i1]:=1;
      end
    else
      t[i1,j1]:=0;

while eb>e do
begin           //---1
    p:=2*a[i,j];
    q:=a[i,i]-a[j,j];
    d:=sqrt(p*p+q*q);
    sign:=p*q;

  if sign<>0 then
  sign:=sign/abs(sign)
    else
    sign:=0;
                  //---2
  if (q<>0)and((abs(p))>=(abs(q))) then
    begin
      r:=abs(q)/(2*d);
      c:=sqrt(0.5+r);
      s:=(sqrt(0.5-r))*sign
    end
  else
  if (q<>0)and ((abs(p))<(abs(q))) then
    begin
      r:=abs(q)/(2*d);
      c:=sqrt(0.5+r);
      s:=(abs(p))*sign/(2*c*d)
    end
  else
  begin
    c:=sqrt(2)/2;
    s:=c
  end;
                  // ----3
  b[i,i]:=c*c*a[i,i]+s*s*a[j,j]+2*c*s*a[i,j];
  b[j,j]:=s*s*a[i,i]+c*c*a[j,j]-2*c*s*a[i,j];
  b[i,j]:=0;    //---4
  b[j,i]:=0;

  //----5
  for m:=1 to n do
    if (m<>i)and(m<>j) then
    begin
      b[i,m]:=c*a[m,i]+s*a[m,j];
      b[m,i]:=b[i,m];
      b[j,m]:=-s*a[m,i]+c*a[m,j];
      b[m,j]:=b[j,m];
    end;
  //--6
  for m:=1 to n do
  for l:=1 to n do
    if (m<>i)and(m<>j)and(l<>i)and(l<>j) then
    begin
      b[m,l]:=a[m,l];
      b[l,m]:=b[m,l]
    end;
   //---matrix T
  for i1:=1 to n do
  for j1:=1 to n do
    if i1=j1 then
    begin
      tk[i1,i1]:=1;
      cm[i1,i1]:=0;
    end
    else
  begin
    cm[i1,j1]:=0;
    tk[i1,j1]:=0
  end;

tk[i,i]:=c;
tk[j,j]:=c;
tk[j,i]:=-s;
tk[i,j]:=s;

for i1:=1 to n do
  for j1:=1 to n do
  for k:=1 to n do
    cm[i1,j1]:=cm[i1,j1]+tk[i1,k]*t[k,j1];

for i1:=1 to n do
  for j1:=1 to n do
    begin
      t[i1,j1]:=cm[i1,j1];
    end ;

    eb:=b[1,2];

for i1:=1 to n do
  for j1:=1 to n do
  if i1<>j1 then
    begin
      if abs(b[i1,j1])>eb then
      begin
        eb:=abs(b[i1,j1]);
        i:=i1;
        j:=j1;
      end;
    end;

  eb:=abs(eb);

    for i1:=1 to n do
      for j1:=1 to n do
      a[i1,j1]:=b[i1,j1];

end;
    
writeln('Vlasni Paru');
for i1:=1 to n do
 begin
    for j1:=1 to n do
    write(b[i1,j1]:4:4,' ');
    writeln;
  end;


writeln;
writeln('Vectors');
  for i1:=1 to n do
  begin
    for j1:=1 to n do
    writeln('X',j1,'=', t[i1,j1]:2:4);
    writeln('----------');
  end;
    readln;
    readln;

end.

Thursday, March 1, 2012

Program on c++ that returns number in reverse.

Hi, this time i will give you a program on c++ that returns number in reverse.
For example if we have 12345 it will return 54321
So here is the code I wrote:


#include "stdafx.h"
#include <iostream>

using namespace std;

unsigned Revert (unsigned number)
{
unsigned result=0,i;

while (number> 0)
{
i = number%10;
result = result * 10 + i;
number = number / 10;
}

return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
unsigned numb;
cout<<"Input number\n";
cin>> numb;

cout<<Revert(numb)<<endl;

return 0;
}