## Saturday, March 31, 2012

### 3D Rotation Algorithm about arbitrary axis with C/C++ code

When an object is to be rotated about an axis that is not parallel to one of the coordinate axes, we need to perform some additional transformations. In this case, we also need rotations to align the axis with a selected coordinate axis and to bring the axis back to its original orientation. Given the specifications for the rotation axis and the rotation angle, we can accomplish the required rotation in five steps.
1. Translate the object so that the rotation axis passes through the coordinate origin.
2. Rotate the object so that the axis of rotation coincides with one of the coordinate axes.
3. Perform the specified rotation about that coordinate axis.
4. Apply inverse rotations to bring the rotation axis back to its original orientation.
5. Apply the inverse translation to bring the rotation axis back to its original position.

### Cohen-Sutherland Line Clipping Algorithm with C/C++

Theory
In this method, every line endpoint is assigned a four digit binary code(region code) that identifies the location of the point relative to the boundary.
b1 : left
b2 : right
b3 : below
b4 : above
The value 1 indicates its relative position. If a point is within clipping rectangle then region code is 0000. So ,
If x – xwmin < 0 , b1 = 1
If xwmax – x < 0 , b2 = 1
If y – ywmin < 0 , b3 = 1
If ywmin – y < 0 , b4 = 1
If the region codes of both end points are 0000 then we accept
the line.  Any  line that have one in the same bit position is rejected  i.e  If RA AND RB ≠ 0 ,line is completely outside .
The lines which con not be identified as completely inside or outside a window by these tests are checked for intersection with the window boundary. Such lines may or may not cross into the window interior.

### Liang-Barsky Line Clipping Algorithm with C/C++

Theory
In Liang-Barsky algorithm, we first write the point-clipping conditions in parametric form as

Each of these for inequalities can be expressed as , k = 1, 2, 3, …..
where parameter p and q are defined as

Any line that is parallel to one of the clipping boundaries has pk = 0 for the value of k corresponding to that boundary. If, for that value of k, we also find qk < 0, then the line is completely outside the boundary and can be eliminated from further consideration. If qk >=0, the line is inside the parallel clipping boundary.
When pk <0, the infinite extension of the line proceeds from the outside to the inside of the inside of the infinite extension of this particular boundary. If pk > 0, the line proceeds from the insides to the outside. For a nonzero value of pk, we can calculate the value of u that corresponds to the point where the infinitely extended line intersects the extension of boundary k as u = qk / pk.

### How to make Vertex Table, Edge Table and Surface Table to store geometric information of an object??

Listing the geometric data in three tables provides a convenient reference to the individual components of each object. Also, the object can be displayed efficiently be using data from the edge table to draw the component lines. Here I discuss about how to create those tables with the help of C++ code.
Vertex table consists of the array of vertices with the coordinate value of each vertex. In C++, a vertex table can be created by creating a class that has . For example
class vertex
{
private:
Point3D *points;
public:
vertex()
{
points = new Point3D();
}
};

## Wednesday, March 28, 2012

### Numerical Methods: Multiplication of two matrices using two dimensional array in C

Let we have two 2 x 2 matrices A and B
$A = \begin{bmatrix} a_{00} & a_{01}\\ a_{10} & a_{11} \end{bmatrix} \text{ and } B = \begin{bmatrix} b_{00} & b_{01} \\ b_{10} & b_{11} \end{bmatrix}$
The multiplication of $A$ and $B$ works as follows

1. Multiply $a_{00}$ with $b_{00}$ and $a_{01}$ with $b_{10}$ and sum them together. So the first element $r_{00}$ becomes  $a_{00}$ . $b_{00}$ $a_{01}$ . $b_{10}$
2. Multiply $a_{00}$ with $b_{01}$ and $a_{01}$ with $b_{11}$ and sum them together. So the first element $r_{01}$ becomes  $a_{00}$ . $b_{01}$ $a_{01}$ . $b_{11}$
3. Multiply $a_{10}$ with $b_{00}$ and $a_{11}$ with $b_{10}$ and sum them together. So the first element $r_{10}$ becomes  $a_{10}$ . $b_{00}$ $a_{11}$ . $b_{10}$
4. Multiply $a_{10}$ with $b_{01}$ and $a_{11}$ with $b_{11}$ and sum them together. So the first element $r_{11}$ becomes  $a_{10}$ . $b_{01}$ $a_{11}$ . $b_{11}$
So the resulting matrix $R$ becomes,
$R = \begin{bmatrix} a_{00}.b_{00}+a_{01}.b_{10} & a_{00}.b_{01} + a_{01}.b_{11} \\ a_{10}.b_{00} + a_{11}.b_{10} & a_{10}.b_{01} + a_{11}.b_{11}\end{bmatrix}$
Note: In order to multiply two matrices, $A$ and $B$, the number of columns in $A$ must equal the number of rows in $B$. Thus, if $A$ is an $m * n$ matrix and $B$ is an $r * s$ matrix, $n = r$.
Source Code
#include<stdio.h>
int main()
{
int r1, c1, r2, c2, matrix1[10][10], matrix2[10][10], result[10][10];
int i, j, k;
printf("Enter the row and column of the first matrix: ");
scanf("%d%d",&r1,&c1);
printf("Enter the row and column of the second matrix: ");
scanf("%d%d",&r2,&c2);
if(c1 != r2){
printf("Matrix multiplication impossible");
}
printf("Enter the first matrix: \n");
for(i = 0; i <r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &matrix1[i][j]);
printf("Enter the second matrix: \n");
for(i = 0; i <r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &matrix2[i][j]);
for(i = 0; i < r1; i++ ){
for(j = 0; j < c2; j++){
result[i][j] = 0;
for(k = 0; k < c1; k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
printf("The multiplication of the matrix is: \n");
for(i = 0; i < r1; i++){
for(j = 0; j < c2; j++){
printf("%d", result[i][j]);
printf(" ");
}
printf("\n");
}
return 0;
}


## Friday, March 23, 2012

### 3D Transformation [Translation, Rotation and Scaling] in C/C++

Translation
In a three-dimensional homogeneous coordinates representation, a point is translated from position P = (x, y, z) to position P’ = (x’, y’, z’) with the following equations.
 x’ = x + tx y’ = y + ty z’ = z + tz
To generate a rotation transformation for an object, we must designate an axis of rotation (about which the the object is to be rotated) and the amount of angular rotation. Unlike 2D applications, where all transformations are carried out in the xy plane, a three-dimensional rotation can be specified around any line in space. I will cover rotation about arbitrary axis in another post, here the discussion is restricted to rotation about coordinate axes.

## Sunday, March 11, 2012

### Computer Graphics: Fractals generation using Mandelbrot Set with C/C++ in Code::Blocks

"Clouds are not spheres, mountains are not cones, coastlines are not circles, and bark is not smooth, nor does lightning travel in a straight line." – Benoit Mandelbrot
The Mandelbrot set is created by a general technique where a function of the form
zn+1 = f(zn)
is used to create a series of a complex variable. In the case of the Mandelbrot, the function is
f(zn) = zn2 + zo
This series is generated for every initial point zo on some partition of the complex plane separating the complex plane into two categories:
1. Points inside the Mandelbrot set
2. Points outside the Mandelbrot set

## Friday, March 9, 2012

### Bezier Curves and Bezier Surfaces generation with C/C++ in Code::Blocks

Brief Theory of Bezier Curve
In order to draw curvy surface we implement Bezier curve algorithm. In drawing Bezier we first define n+1 control point pk = (xk, yk, zk) with k varying from 0 to n. And the coordinate points are blended to produce following position vectors that define the path of an approximation Bezier polynomial function between p0 and pn.

The Bezier blending functions BEZk,n(u) are the Bernstein polynomials:  BEZk,n(u)=C(n,k)uk(1-u)n-k
Where the C(n, k) are binomial coefficients.
Equivalently, we can define Bezier blending functions with the recursion calculation.
BEZk,n (u) = (1-u) BEZk,n-1(u)+u BEZk-1,n-1(u), n>k≥1
With BEZk,k = uk , and BEZ0,k = (1-u)k.

## Sunday, March 4, 2012

### Controlling Camera and Line of Sight in OpenGL

Camera controlling is essential in 3D viewing. OpenGL provides a function gluLookAt() for this purpose. The syntax of the function gluLookAt() is
void gluLookAt ( GLdouble eyex,
GLdouble eyey,
GLdouble eyez,
GLdouble centerx,
GLdouble centery,
GLdouble centerz,
GLdouble upx,
GLdouble upy,
GLdouble upz );

It takes three sets of arguments, which specify the location of the viewpoint, define a reference point toward which the camera is aimed, and indicate which direction is up. The first three numbers represent where the camera is positioned.

## Friday, March 2, 2012

### String To Integer and Integer to String Conversion in C

In modern languages like Java, C# etc., there is a provision of function to handle String to Integer and Integer to String conversion. But in case of C, you should explicitly convert.
String to Integer Conversion
String is a array of characters. So changing each character successively into integer  changes whole string into integer. To change a character into an integer, here is a simple idea. The idea is, subtract the zero character from the character you want to change. For example to change  character 1 into integer 1 subtract character 0 from character one i.e. ‘1’ - ‘0’ results integer 1. A portion of a C code is given below: