Archive

Posts Tagged ‘Cpp’

C++ Graphics – 08 – ROTATION of LINE, TRIANGLE and RECTANGLE

December 8, 2009 Leave a comment

ROTATION of LINE, TRIANGLE and RECTANGLE.

#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>

const int MAX = 5;
const double PI = 3.14;

void GetCords(float mat1[][MAX], float mat2[][MAX], int n){
	int i, j;
	for(i=0; i<n; i++){
		printf("\n Enter Coordinate no %d:- \n", i+1);
		for(j=0; j<2; j++){
			scanf("%f", &mat1[j][i]);
			}
		}

	for(i=0; i<n; i++){
		mat2[0][i] = mat1[0][i];
		mat1[0][i] = 320 + mat1[0][i];
		mat2[1][i] = mat1[1][i];
		mat1[1][i] = 240 - mat1[1][i];
		}

	setcolor(GREEN);
	line(0, 240, 640, 240);
	outtextxy(630, 245, "x");
	line(320, 0, 320, 480);
	outtextxy(310, 0, "y");
	outtextxy(280, 245, "(0,0)");

	setcolor(YELLOW);
	moveto(mat1[0][0], mat1[1][0]);
	for(i=0; i<n; i++){
			lineto(mat1[0][i], mat1[1][i]);
			moveto(mat1[0][i], mat1[1][i]);
			}
	lineto(mat1[0][0], mat1[1][0]);

	}

void Rotate(float mat2[][MAX], int n){
	int ang, i, j, k;
	float tmat[MAX][MAX], tot[MAX][MAX];
	float rad;
	printf("\n Enter the Angle (in Degrees):- \n");
	scanf("%d", &ang);

	rad = (ang * PI) / 180;

	tmat[0][0] = cos(rad);
	tmat[0][1] = -sin(rad);
	tmat[1][0] = sin(rad);
	tmat[1][1] = cos(rad);

	for(i=0; i<2; i++){
		for(j=0; j<n; j++){
			tot[i][j] = 0;
			for(k=0; k<2; k++){
				tot[i][j] = tot[i][j] + (tmat[i][k] * mat2[k][j]);
				}
			}
		}

	for(i=0; i<n; i++){
		tot[0][i] = 320 + tot[0][i];
		tot[1][i] = 240 - tot[1][i];
		}

	setcolor(LIGHTRED);
	moveto(tot[0][0], tot[1][0]);
	for(i=0; i<n; i++){
			lineto(tot[0][i], tot[1][i]);
			moveto(tot[0][i], tot[1][i]);
			}
	lineto(tot[0][0], tot[1][0]);
	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm,"\\tc");
	float mat1[MAX][MAX], mat2[MAX][MAX];
	int ch, n;
	while(1){

		clrscr();
		cleardevice();
		printf("\n ROTATION ");
		printf("\n ~~~~~~~~\n");
		printf("\n 1 -> LINE.");
		printf("\n 2 -> TRIANGLE.");
		printf("\n 3 -> RECTANGLE");
		printf("\n 4 -> Exit.");
		printf("\n Enter your choice: ");
		scanf("%d", &ch);
		clrscr();
		cleardevice();
		switch(ch){
			case 1:		// LINE.
				n=2;
				break;
			case 2:		// TRIANGLE.
				n=3;
				break;
			case 3:		// RECTANGLE.
				n=4;
				break;
			default:
				gotoxy(10, 10);
				printf(" Dev. By: Manoj Pandey.");
				gotoxy(15, 15);
				printf("MCA 3d Sem.");
				getch();
				closegraph();
				exit(1);
			} // end of switch.
		GetCords(mat1, mat2, n);	// Input of Coordinates.
		Rotate(mat2, n);	// Rotating and Displaying.
		getch();
		} // end of while.
	} // end of main.

C++ Graphics – 07 – INVERSE TRANSLATION of LINE, TRIANGLE and RECTANGLE

December 7, 2009 Leave a comment

INVERSE TRANSLATION of LINE, TRIANGLE and RECTANGLE.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>

const int MAX = 5;

void GetCords(int mat[][MAX], int n){
	int i, j;
	for(i=0; i<n; i++){
		printf("\n Enter Coordinate no %d:- \n", i+1);
		for(j=0; j<2; j++){
			scanf("%d", &mat[j][i]);
			}
		}

	for(i=0; i<n; i++){
		mat[0][i] = 320 + mat[0][i];
		mat[1][i] = 240 - mat[1][i];
		}

	setcolor(WHITE);
	line(0, 240, 640, 240);
	outtextxy(630, 245, "x");
	line(320, 0, 320, 480);
	outtextxy(310, 0, "y");
	outtextxy(280, 245, "(0,0)");

	setcolor(YELLOW);
	moveto(mat[0][0], mat[1][0]);
	for(i=0; i<n; i++){
			lineto(mat[0][i], mat[1][i]);
			moveto(mat[0][i], mat[1][i]);
			}
	lineto(mat[0][0], mat[1][0]);

	}

void invTransform(int mat[][MAX], int n){
	int dx, dy, i;
	printf("\n Enter Cordinates (dx, dy):- \n");
	scanf("%d %d", &dx, &dy);

	for(i=0; i<n; i++){
		mat[0][i] -= dx;
		mat[1][i] += dy;
		}

	setcolor(LIGHTRED);
	moveto(mat[0][0], mat[1][0]);
	for(i=0; i<n; i++){
			lineto(mat[0][i], mat[1][i]);
			moveto(mat[0][i], mat[1][i]);
			}
	lineto(mat[0][0], mat[1][0]);

	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm,"\\tc");
	int mat[MAX][MAX], n;
	int ch;
	while(1){
		clrscr();
		cleardevice();
		printf("\n INVERSE TRANSLATION ");
		printf("\n ~~~~~~~~~~~~~~\n");
		printf("\n 1 -> LINE.");
		printf("\n 2 -> TRIANGLE.");
		printf("\n 3 -> RECTANGLE");
		printf("\n 4 -> Exit.");
		printf("\n Enter your choice: ");
		scanf("%d", &ch);
		clrscr();
		cleardevice();
		switch(ch){
			case 1:		// LINE.
				n=2;
				break;
			case 2:		// TRIANGLE.
				n=3;
				break;
			case 3:		// RECTANGLE.
				n=4;
				break;
			default:
				closegraph();
				exit(1);
			} // end of switch.
		GetCords(mat, n);	// Input Cordinates
		invTransform(mat, n);	// Inverse Translating Figures.
		getch();
		} // end of while.
	} // end of main.

C++ Graphics – 06 – Translation of LINE, TRIANGLE and RECTANGLE

December 6, 2009 Leave a comment

Translation of LINE, TRIANGLE and RECTANGLE.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>

const int MAX = 5;

void GetCords(int mat[][MAX], int n){
	int i, j;
	for(i=0; i<n; i++){
		printf("\n Enter Coordinate no %d:- \n", i+1);
		for(j=0; j<2; j++){
			scanf("%d", &mat[j][i]);
			}
		}

	for(i=0; i<n; i++){
		mat[0][i] = 320 + mat[0][i];
		mat[1][i] = 240 - mat[1][i];
		}

	setcolor(GREEN);
	line(0, 240, 640, 240);
	outtextxy(630, 245, "x");
	line(320, 0, 320, 480);
	outtextxy(310, 0, "y");
	outtextxy(280, 245, "(0,0)");

	setcolor(YELLOW);
	moveto(mat[0][0], mat[1][0]);
	for(i=0; i<n; i++){
			lineto(mat[0][i], mat[1][i]);
			moveto(mat[0][i], mat[1][i]);
			}
	lineto(mat[0][0], mat[1][0]);

	}

void Transform(int mat[][MAX], int n){
	int dx, dy, i;
	printf("\n Enter Cordinates (dx, dy):- \n");
	scanf("%d %d", &dx, &dy);
	for(i=0; i<n; i++){
		mat[0][i] += dx;
		mat[1][i] -= dy;
		}

	setcolor(LIGHTRED);
	moveto(mat[0][0], mat[1][0]);
	for(i=0; i<n; i++){
			lineto(mat[0][i], mat[1][i]);
			moveto(mat[0][i], mat[1][i]);
			}
	lineto(mat[0][0], mat[1][0]);

	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm,"\\tc");
	int mat[MAX][MAX], n;
	int ch;
	while(1){
		clrscr();
		cleardevice();
		printf("\n TRANSFORAMTION ");
		printf("\n ~~~~~~~~~~~~~~\n");
		printf("\n 1 -> LINE.");
		printf("\n 2 -> TRIANGLE.");
		printf("\n 3 -> RECTANGLE");
		printf("\n 4 -> Exit.");
		printf("\n Enter your choice: ");
		scanf("%d", &ch);
		clrscr();
		cleardevice();
		switch(ch){
			case 1:		// LINE.
				n=2;
				break;
			case 2:		// TRIANGLE.
				n=3;
				break;
			case 3:		// RECTANGLE.
				n=4;
				break;
			default:
				gotoxy(10, 10);
				printf(" Dev. By: Manoj Pandey.");
				gotoxy(15, 15);
				printf("MCA 3d Sem.");
				getch();
				closegraph();
				exit(1);
			} // end of switch.
		GetCords(mat, n);	// Input of Coordinates.
		Transform(mat, n);	// Translating and Displaying.
		getch();
		} // end of while.
	} // end of main.

C++ Graphics – 05 – Ellipse program by Midpoint Algorithm

December 5, 2009 Leave a comment

Ellipse Program by Midpoint Algorithm.

Similarly to the Circle Midpoint algorithm this algorithm also draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°).

#include <graphics.h>
#include <conio.h>
#include <dos.h>

void ellipsePoint(int x, int y){
	putpixel(x+320, y+240, WHITE);
	putpixel(y+320, x+240, WHITE);
	putpixel(y+320, -x+240, WHITE);
	putpixel(x+320, -y+240, WHITE);
	putpixel(-x+320, -y+240, WHITE);
	putpixel(-y+320, -x+240, WHITE);
	putpixel(-y+320, x+240, WHITE);
	putpixel(-x+320, y+240, WHITE);
	}

void ellipseMidPoint(int a, int b){
	double d2;
	int x=0;
	int y=b;
	double d1 = b*b-(a*a*b)+(0.25*a*a);
	ellipsePoint(x, y);
	while( (a*a*(y-0.5)) > (b*b*(x+1)) ){
		if(d1 < 0)
			d1 += b*b*(2*x+3);
		else{
			d1 += b*b*(2*x+3)+a*a*(-2*y+2);
			y--;
			}
		x++;
		ellipsePoint(x, y);
		}
	d2 = b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
	while(y > 0){
		if(d2 < 0){
			d2 += b*b*(2*x+2)+a*a*(-2*y+3);
			x++;
			}
		else
			d2 += a*a*(-2*y+3);
		y--;
		ellipsePoint(x, y);
		}
	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm, "e:\\tc\\");
	ellipseMidPoint(100, 200);
	getch();
	closegraph();
	}

C++ Graphics – 04 – Circle program by Midpoint Algorithm

December 4, 2009 Leave a comment

Circle program by Midpoint Algorithm.

This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°). You can determine where to stop because when y = x, you have reached 45°.

#include <graphics.h>
#include <conio.h>
#include <dos.h>

void CirclePoints(int x, int y){
	putpixel(x+320, y+240, WHITE);
	putpixel(y+320, x+240, WHITE);
	putpixel(y+320, -x+240, WHITE);
	putpixel(x+320, -y+240, WHITE);
	putpixel(-x+320, -y+240, WHITE);
	putpixel(-y+320, -x+240, WHITE);
	putpixel(-y+320, x+240, WHITE);
	putpixel(-x+320, y+240, WHITE);
	}

void MidPointCircle(int r){
	int x = 0;
	int y = r;
	double d = 5.0/4.0 - r;
	CirclePoints(x, y);
	while(y > x){
		if(d < 0)
			d += 2.0 * x + 3.0;
		else{
			d += 2.0 * (x - y) + 5.0;
			y--;
			}
		x++;
		CirclePoints(x, y);
		delay(50);
		}
	}

void main(){
	int gd=DETECT, gm;
	initgraph(&gd, &gm, "\\tc");
	MidPointCircle(200);
	getch();
	closegraph();
	}

Bresenham’s Circle algorithm is derived from the Midpoint Circle algorithm.