0% found this document useful (0 votes)
12 views4 pages

Triangle Transformations in C Graphics

The document contains C code that demonstrates basic graphics operations including drawing, translating, scaling, and rotating triangles using the graphics.h library. It includes three main functions: one for translating a triangle, one for scaling it, and another for rotating it in both clockwise and anticlockwise directions. Each operation is visually represented on the screen with different colors for the original and transformed triangles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

Triangle Transformations in C Graphics

The document contains C code that demonstrates basic graphics operations including drawing, translating, scaling, and rotating triangles using the graphics.h library. It includes three main functions: one for translating a triangle, one for scaling it, and another for rotating it in both clockwise and anticlockwise directions. Each operation is visually represented on the screen with different colors for the original and transformed triangles.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

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

void drawTri(int x1,int y1,int x2,int y2,int x3,int y3)


{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
int main()
{
int x1,y1,x2,y2,x3,y3;
float tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

// ORIGINAL CO-ORDINATES
x1=100; y1=100;
x2=150; y2=50;
x3=200; y3=100;
// DRAW ORIGINAL TRIANGLE
setcolor(WHITE);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(80,40,"Original triangle");

// Translating FACTORS
tx=4;
ty=5;

// Translation
x1=x1+tx;
y1=y1+ty;
x2=x2+tx;
y2=y2+ty;
x3=x3+tx;
y3=y3+ty;

// Triangle after translation


setcolor(YELLOW);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(180,160,"Translated triangle");

getch();
closegraph();
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

void drawTri(int x1,int y1,int x2,int


y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
int main()
{
int x1,y1,x2,y2,x3,y3;
float sx,sy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

// ORIGINAL CO-ORDINATES
x1=100; y1=100;
x2=150; y2=50;
x3=200; y3=100;
// DRAW ORIGINAL TRIANGLE
setcolor(WHITE);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(80,40,"Original triangle");

// SCALING FACTORS
sx=1.5;
sy=1.5;

// Scaling
x1=x1*sx;
y1=y1*sy;
x2=x2*sx;
y2=y2*sy;
x3=x3*sx;
y3=y3*sy;

// Triangle after scaling


setcolor(YELLOW);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(180,160,"Scaled triangle");

getch();
closegraph();
return 0;
}
#include<stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>

void drawTri(int x1,int y1,int x2,int y2,int x3,int y3)


{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}

void rotateTri(int *x1,int *y1,int *x2,int *y2,int *x3,int *y3,float angle)


{
float radian=angle*(3.14/180);
int temp_x1 = *x1;
int temp_y1 = *y1;
*x1= temp_x1*cos(radian)-temp_y1*sin(radian);
*y1= temp_x1*sin(radian)+temp_y1*cos(radian);
int temp_x2 = *x2;
int temp_y2 = *y2;
*x2= temp_x2*cos(radian)-temp_y2*sin(radian);
*y2= temp_x2*sin(radian)+temp_y2*cos(radian);
int temp_x3 = *x3;
int temp_y3 = *y3;
*x3= temp_x3*cos(radian)-temp_y3*sin(radian);
*y3= temp_x3*sin(radian)+temp_y3*cos(radian);
}

int main()
{
int x1,y1,x2,y2,x3,y3;
float angle;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

// ORIGINAL CO-ORDINATES
x1=300; y1=200;
x2=350; y2=150;
x3=400; y3=200;

// DRAW ORIGINAL TRIANGLE


setcolor(WHITE);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(180,160,"Original triangle");

// Angle for clockwise rotation


angle=45;

// Triangle after clockwise rotation


setcolor(YELLOW);
rotateTri(&x1,&y1,&x2,&y2,&x3,&y3,angle);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(70,320,"Cloclwise rotation");

// Angle for clockwise rotation


angle=-60;

// Triangle after anticlockwise rotation


setcolor(RED);
rotateTri(&x1,&y1,&x2,&y2,&x3,&y3,angle);
drawTri(x1,y1,x2,y2,x3,y3);
outtextxy(130,80,"Anticloclwise rotation");

getch();
closegraph();
return 0;
}

You might also like