Skip to main content

2D Transformations

Include:


stdio.h
math.h
conio.h
graphics.h


float input[20][3];
float resM[20][3];
float scaleM[3][3];
float R[3][3],T[3][3];
int edges;

// Plotting Center Of Screen
void plot(float mat[20][3])
{
int i;
setcolor(10);
line(0,240,639,240);
line(320,0,320,479);
outtextxy(295,243,"0,0");

setcolor(WHITE);
for(i=0;i line(320+mat[i][0],240-mat[i][1],320+mat[i+1][0],240-mat[i+1][1]);
line(320+mat[0][0],240-mat[0][1],320+mat[i][0],240-mat[i][1]);
}
// Accepting Co-ordinates for Polygon
void accept()
{
int i,j;
printf("\n Enter no of edges in fig:");
scanf("%d",&edges);
printf("\n Enter co-ordinates of matrix:");

for(i=0;i {
for(j=0;j<2;j++)
{
printf("\n A[%d][%d]:",i,j);
scanf("%f",&input[i][j]);
}
}
for(i=0;i input[i][2]=1;

plot(input);
getch();
}

void mult(float a[20][3],float b[3][3],float c[20][3])
{
int i;
for(i=0;i {
c[i][0]=(a[i][0]*b[0][0])+(a[i][1]*b[1][0])+(a[i][2]*b[2][0]);
c[i][1]=(a[i][0]*b[0][1])+(a[i][1]*b[1][1])+(a[i][2]*b[2][1]);
c[i][2]=(a[i][0]*b[0][2])+(a[i][1]*b[1][2])+(a[i][2]*b[2][2]);
}
}
// Function for Scaling Operation
void scale()
{
int i;
float sx,sy;
printf("\n Enter ScaleX: ");
scanf("%f",&sx);
printf("\n Enter ScaleY:");
scanf("%f",&sy);

scaleM[0][1]=scaleM[1][0]=0;
scaleM[0][2]=scaleM[2][0]=scaleM[2][1]=0;
scaleM[2][2]=1;

scaleM[0][0]=sx;
scaleM[1][1]=sy;
plot(input);
mult(input,scaleM,resM);
plot(resM);
getch();
}


// Function For Translation Operation
void translate()
{
int tx,ty,i;
printf("\n Enter Tx and Ty: ");
scanf("%d%d",&tx,&ty);
T[0][0]=T[1][1]=T[2][2]=1;
T[0][1]=T[0][2]=T[1][0]=T[1][2]=0;
T[2][0]=tx;
T[2][1]=ty;

plot(input);
mult(input,T,resM);
plot(resM);
getch();
}


// Function For Rotatation Operation
void rotate()
{
float R[3][3],a,b,thita;
int thita1,xm,ym,clock=1;
xm=input[0][0];
ym=input[0][1];

printf("\n Enter Rotation Angle : ");
scanf("%d",&thita1);
printf("\n Clockwise or Anticlockwise(0 or 1) : ");
scanf("%d",&clock);

thita=((3.14*thita1)/180);

if(clock==1)
{
a=-xm*cos(thita)+ym*sin(thita)+xm;
b=-xm*sin(thita)-ym*cos(thita)+ym;
R[0][0]=R[1][1]=cos(thita);
R[0][1]=sin(thita);
R[1][0]=-sin(thita);
R[2][0]=a;
R[2][1]=b;
R[0][2]=R[1][2]=0;
R[2][2]=1;
}
else
{
a=-xm*cos(thita)-ym*sin(thita)+xm;
b=xm*sin(thita)-ym*cos(thita)+ym;
R[0][0]=R[1][1]=cos(thita);
R[0][1]=-sin(thita);
R[1][0]=sin(thita);
R[2][0]=a;
R[2][1]=b;
R[0][2]=R[1][2]=0;
R[2][2]=1;

}
mult(input,R,resM);
plot(resM);
getch();
}


// Main Function
void main()
{
int gd=DETECT,gm,ch;
initgraph(&gd,&gm,"BGI");
cleardevice();
while(1)
{
cleardevice();
printf("\n 1.Accept\n 2.Scaling\n 3.Translate\n 4.Rotation\n 5.Exit");
printf("\n\n Enter Your Choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:
accept();
break;
case 2:
cleardevice();
scale();
break;
case 3:
cleardevice();
translate();
break;

case 4:
cleardevice();
rotate();
break;

case 5:exit(1);
}
}
}

Comments

Popular posts from this blog

4. Lex and Yacc Program to detect errors in a 'C' Language Program

Lex and Yacc Program to detect errors in a 'C' Language Program   Lex Code : %{ #include"y.tab.h" #include<stdio.h> int LineNo = 1 ; %} identifier [ a - zA - Z ][ _a - zA - Z0 - 9 ]* number [ 0 - 9 ]+|([ 0 - 9 ]*\.[ 0 - 9 ]+) %% main \(\) return MAIN ; if return IF ; else return ELSE ; while return WHILE ; int | char | flaot return TYPE ; { identifier } return VAR ; { number } return NUM ; \> | \< | \<= | \>= | == return RELOP ; [\ t ] ; [\ n ] LineNo ++; . return yytext [ 0 ]; %% Yacc Code : %{ #include<string.h> #include<stdio.h> extern int LineNo ; int errno = 0 ; %} % token NUM VAR RELOP % token MAIN IF ELSE WHILE TYPE % left '-' '+' % left '*' '/' %% PROGRAM : MAIN BLOCK ; BLOCK : '{' CODE '}' ; CODE : BLOCK | STATEMENT CODE | STATEMENT ; STATEMENT : DECST ';' | DECST { printf ( "\nLine number %d...

Selenium + Python + UnexpectedAlertPresentException: Dealing with annoying alerts

Handling  UnexpectedAlertPresentException   Alerts who hates them? I Do!  Who doesn't hate an annoying alert causing your tests / scraping job to fail? I must say they are pretty much on point on the Unexpected part!  Fortunately, there are easy ways to mitigate the issue. 1. Disable alerts completely: driver . execute_script( 'window.alert = function(){};' ); execute this script just before where you anticipate the alert and you're golden. 2. You want to see the alert text but not disturb the execution flow. driver . execute_script( 'window.alert = console.info;' ); Now the alerts have been redirected to the console and you don't have to worry about them. (Unless you have to - then you'd have to monitor the console) 3. You know exactly when it comes and want to accept the alert and move on. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 from selenium import webdriver from selenium.webdriver.s...

2. Lex program that detects statement type i.e. Simple or Compound

Lex program that detects statement type i.e. Simple or Compound Note: Only AND | OR | BUT conjunctions are supported. Program: % option noyywrap %{ char test = 's' ; %} %% ( "" [ aA ][ nN ][ dD ] "" )|( "" [ oO ][ rR ] "" )|( "" [ bB ][ uU ][ tT ] "" ) { test = 'c' ;} . {;} \ n return 0 ; %% main () { yylex (); if ( test == 's' ) printf ( "\n Its a simple sentence" ); else if ( test == 'c' ) printf ( "\n This is compound sentence" ); }