CG Exp 9,10,11

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

EXP : 6 Write a program for line draw in gas Raster Graphics Display

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

main()

int gd=DETECT, gm;

//init graphics

initgraph(&gd, &gm, "C:/TURBOC3/BGI");

/*

if you are using turboc2 use below line to init graphics:


initgraph(&gd, &gm, "C:/TC/BGI");

*/
//draw a line

lineto(100,200); //will draw line to point(100,200)


getch();

closegraph();

return 0;

}
Exp : 7 Write a program for circle drawing in Raster Graphics Display

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

#include<graphics.h>

void circlepoints(int,int);

void main()

int x,y,p,r;

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
clrscr();

printf("Enter the radius for the required circle: ");


scanf("%d",&r);

x=0; y=r; p=1-r;


while(x<y)

x++;

if(p>0)

{
p=p+2*(x-y)+1;

y--;

else

p=p+2*x+1;

circlepoints(x,y);

getch();

closegraph();
}
void circlepoints(int x,int y)

{
putpixel(x+300, y+300,8);

putpixel(x+300,-y+300,8);

putpixel(-x+300, y+300,8);

putpixel(-x+300,-y+300,8);

putpixel(y+300, x+300,8);

putpixel(y+300,-x+300,8);

putpixel(-y+300, x+300,8);

putpixel(-y+300,-x+300,8);
}
Exp : 8 Write a program to draw a line using Bresenham's line drawing algorithm.

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

void drawline(int x0, int y0, int x1, int y1)

int dx, dy, p, x, y;

dx=x1-x0;

dy=y1-y0;

x=x0;

y=y0;
p=2*dy-dx;

while(x<x1)
{

if(p>=0)
{

putpixel(x,y,7);

y=y+1;

p=p+2*dy-2*dx;

}
else

putpixel(x,y,7);

p=p+2*dy;

x=x+1;

}
int main()
{

int gd=DETECT, gm, error, x0, y0, x1, y1;


initgraph(&gd, &gm, "c:\\turboc3\\bgi");

printf("Enter x0 co-ordinates of first point: ");

scanf("%d", &x0);

printf("Enter y0 co-ordinates of second point: ");

scanf("%d", &y0);

printf("Enter x1 co-ordinates of first point: ");

scanf("%d", &x1);

printf("Enter y1 co-ordinates of second point: ");


scanf("%d",&y1);

drawline(x0, y0, x1, y1);


getch();

return 0;
}
Exp : 9 Write a program to draw a circle using Midpoint circle drawing algorithm. Modify the same for
arc and sector

#include <graphics.h>

#include <conio.h>

#include <math.h>

void drawCircle(int xc, int yc, int r) {

int x = 0, y = r;

int d = 1 - r;

while (x < y) {

putpixel(xc + x, yc + y, WHITE);

putpixel(xc - x, yc + y, WHITE);

putpixel(xc + x, yc - y, WHITE);

putpixel(xc - x, yc - y, WHITE);

putpixel(xc + y, yc + x, WHITE);

putpixel(xc - y, yc + x, WHITE);

putpixel(xc + y, yc - x, WHITE);

putpixel(xc - y, yc - x, WHITE);

if (d < 0) {

d += 2 * x + 3;

} else {

d += 2 * (x - y) + 5;

y--;

}
x++;

void drawArc(int xc, int yc, int r, float startAngle, float endAngle) {

float angle;

for (angle = startAngle; angle <= endAngle; angle += 0.1) {

int x = xc + r * cos(angle * M_PI / 180);

int y = yc + r * sin(angle * M_PI / 180);

putpixel(x, y, WHITE);

void drawSector(int xc, int yc, int r, float startAngle, float endAngle) {

int x1 = xc + r * cos(startAngle * M_PI / 180);

int y1 = yc + r * sin(startAngle * M_PI / 180);

int x2 = xc + r * cos(endAngle * M_PI / 180);

int y2 = yc + r * sin(endAngle * M_PI / 180);

drawArc(xc, yc, r, startAngle, endAngle);

line(xc, yc, x1, y1);

line(xc, yc, x2, y2);

int main() {

int xc = 300;

int yc = 300;
int r = 100;

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\Turboc3\\bgi");

drawCircle(xc, yc, r);

drawArc(xc, yc, r, 30, 150);

drawSector(xc, yc, r, 150, 330);

getch();

closegraph();

return 0;

Output:
Exp : 10 Write a Program to rotate a point about origin.
#include <stdio.h>

#include <math.h>
void rotatePoint(double x, double y, double theta, double *newX, double *newY) {

double radians = theta * (M_PI / 180.0);

*newX = x * cos(radians) - y * sin(radians);

*newY = x * sin(radians) + y * cos(radians);

int main() {

double x, y, theta;

double newX, newY;


printf("Enter the coordinates of the point (x y): ");

scanf("%lf %lf", &x, &y);


printf("Enter the angle of rotation (in degrees): ");

scanf("%lf", &theta);
rotatePoint(x, y, theta, &newX, &newY);

printf("New coordinates after rotation: (%.2f, %.2f)\n", newX, newY);

return 0;

getch();

Output:

Enter the coordinates of the point (x y): 3 4

Enter the angle of rotation (in degrees): 90

New coordinates after rotation: (-4.00, 3.00)


Exp : 11 Write a program to rotate a triangle about origin.

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<process.h>

#include<math.h>

void triangle(int x1,int y1,int x2,int y2,int x3,int y3);

void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);


void main()

int gd=DETECT,gm;

int x1,y1,x2,y2,x3,y3;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");

printf("Enter the 1st point for the triangle:");

scanf("%d%d",&x1,&y1);

printf("Enter the 2nd point for the triangle:");

scanf("%d%d",&x2,&y2);

printf("Enter the 3rd point for the triangle:");

scanf("%d%d",&x3,&y3);

triangle(x1,y1,x2,y2,x3,y3);
getch();

cleardevice();

Rotate(x1,y1,x2,y2,x3,y3);

setcolor(1);

triangle(x1,y1,x2,y2,x3,y3);

getch();
}
void triangle(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 Rotate(int x1,int y1,int x2,int y2,int x3,int y3)

int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;

float Angle;

printf("Enter the angle for rotation:");


scanf("%f",&Angle);

cleardevice();
Angle=(Angle*3.14)/180;

a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);

a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);

b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);

a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);

b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");

triangle(a1,b1,a2,b2,a3,b3);

}
Output :
Enter the 1st point for the triangle: 100 100

Enter the 2nd point for the triangle: 150 50


Enter the 3rd point for the triangle: 200 100

Enter the angle for rotation: 30

You might also like