Circle, line, Polygon - C++ program
============================
Circle
=========
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xcent=100,ycent=100,radius=100;
void plotpixel(int,int);
void main()
{
int gd=0,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int x=0,y=radius,p=0;
plotpixel(x,y);
p=1-radius;
while(x<y)
{
if(p<0)
{
x = x+1;
p = (p+2) * (x+1);
}
else
{
x = x+1;
y=y-1;
p= (p+2) * (x-y) +1;
}
plotpixel(x, y);
}
getch();
}
void plotpixel(int x, int y)
{
putpixel(xcent+x, ycent+y, 15);
putpixel(xcent-x, ycent+y, 15);
putpixel(xcent+x, ycent-y, 15);
putpixel(xcent-x, ycent-y, 15);
putpixel(xcent+y, ycent+x, 15);
putpixel(xcent-y, ycent+x, 15);
putpixel(xcent+y, ycent-x, 15);
putpixel(xcent-y, ycent-y, 15);
}
Line
======
#include <graphics.h>
//#include <stdlib.h>
//#include <stdio.h>
#include <conio.h>
void line1(int,int,int,int);
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(getmaxcolor());
line1(5, 20, 145, 20);
getch();
closegraph();
return 0;
}
void line1(int x1,int y1,int x2,int y2)
{
int dx=x2-x1;
if(dx<0)
dx=-dx;
int dy=y2-y1;
if(dy<0)
dy=-dy;
int i,j;
for( i=0, j=0; i<=dx j<=dy; i++,j++)
{
putpixel(x1+i,y1+j,15);
}
}
Polygon
========
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void main()
{
int arr[10][2],n;
printf("Enter the number of edges : ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the X and Y position for %d edge : ",i+1);
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawpolygon(n,arr,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
No comments:
Post a Comment