CG Exp 9,10,11
CG Exp 9,10,11
CG Exp 9,10,11
#include<graphics.h>
#include<conio.h>
main()
//init graphics
/*
*/
//draw a line
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();
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>
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()
{
scanf("%d", &x0);
scanf("%d", &y0);
scanf("%d", &x1);
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>
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;
putpixel(x, y, WHITE);
void drawSector(int xc, int yc, int r, float startAngle, float endAngle) {
int main() {
int xc = 300;
int yc = 300;
int r = 100;
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) {
int main() {
double x, y, theta;
scanf("%lf", &theta);
rotatePoint(x, y, theta, &newX, &newY);
return 0;
getch();
Output:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
scanf("%d%d",&x1,&y1);
scanf("%d%d",&x2,&y2);
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);
int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float 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