Вы находитесь на странице: 1из 9

Punjabi university ,computer science department

ASSIEMENT-3

NAME:-Abdela Aman

ROLLNO:-16071059

//PRORAM THAT IMPLEMENT ELLIPSE USING ELLIPSE ALGORTHIM

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

#include<iostream>

#include<math.h>

using namespace std;

void direct(int,int,int,int);

void polar_domain(int,int,int,int);

void polar_incremental_domain(int,int,int,int);

void Mid_point(int,int,int,int);

void drawEllipse(int,int,int,int);

int main()

int gdriver = DETECT,gmode;

int h,k,a,b;

char c;

int ch;

initgraph(&gdriver,&gmode,"C:\Users\pc\Desktop\nmok\";

do

cout<<"*********** MENU **********\t"<<endl;

1
Punjabi university ,computer science department

cout<<"press 1. for Direct Ellipse Algorithm\n";

cout<<"press 2. for Polar Domain Ellipse Algorithm\n";

cout<<"press 3. for Polar Incremental Domain Ellipse Algorithm\n";

cout<<"press 4. for Mid Point Ellipse Algorithm\n";

cout<<"please enter your choice \t";

cin>>ch;

switch(ch)

case 1:

cout<<"Enter the coordinateof the ellipse please :"<<endl;

cin>>h>>k;

cout<<"\nEnter the major and minor axis please:\t";

cin>>a>>b;

direct(h,k,a,b);

break;

case 2:

cout<<"Enter the coordinateof the ellipse please :"<<endl;

cin>>h>>k;

cout<<"\nEnter the major and minor axis please:\t";

cin>>a>>b;

polar_domain(h,k,a,b);

break;

case 3:

cout<<"Enter the coordinate of the ellipse please :"<<endl;

cin>>h>>k;

cout<<"\nEnter the major and minor axis please:\t";

2
Punjabi university ,computer science department

cin>>a>>b;

polar_incremental_domain(h,k,a,b);

break;

case 4:

cout<<"Enter the coordinateof the ellipse please :"<<endl;

cin>>h>>k;

cout<<"\nEnter the major and minor axis please:\t";

cin>>a>>b;

Mid_point(h,k,a,b);

break;

default:

cout<<"wrong choice"<<endl;

break;

cout<<"\n\n Do you want to continue?(press Y or y)"<<endl;

cin>>c;

}while(c=='Y'|| c=='y');

getch();

closegraph();

void direct(int h,int k,int a,int b)

double x,y,xend;

x=0;

3
Punjabi university ,computer science department

xend=a;

while(x<=xend)

y=b*sqrt(1-(x*x)/(a*a));

//putpixel(x,y,RED);

putpixel(x+h,y+k,RED);

putpixel(h-x,y+k,RED);

putpixel(h-x,k-y,RED);

putpixel(h+x,k-y,RED);

x=x+1;

ellipse(h,k,0,360,a,b);

getch();

closegraph();

void polar_domain(int h,int k,int a,int b)

double dtheta,theta,x,y;

if(a>b)

dtheta=(1.0/(double)a);

else

dtheta=(1.0/(double)b);

4
Punjabi university ,computer science department

theta=0;

while(theta<=3.14)

x=(int)(a*cos(theta)+0.5);

y=(int)(b*sin(theta)+0.5);

putpixel(h+x,k+y,RED);

putpixel(h+x,k-y,RED);

putpixel(h-x,k-y,RED);

putpixel(h-x,k+y,RED);

theta=theta+dtheta;

//ellipse( xc, yc,0,360,a,b);

/* clean up */

getch();

closegraph();

void polar_incremental_domain(int h,int k,int a,int b)

double dtheta,x,y,c,s,Tab,Tba,xtemp;

if(a>b)

dtheta=1.0/(double)a;

else

dtheta=1.0/(double)b;

5
Punjabi university ,computer science department

x=a;

y=0;

c=cos(dtheta);

s=sin(dtheta);

//theta=0;

Tab=(double)a/(double)b;

Tba=(double)b/(double)a;

while (y<=b)

xtemp=x;

x=(xtemp*c)-(Tab*y*s);

y=(y*c)+(Tba*xtemp*s);

putpixel(h+x,k+y,RED);

putpixel(h+k,k-y,RED);

putpixel(h-x,k-y,RED);

putpixel(h-x,k+y,RED);

ellipse(h,k,0,360,a,b);

getch();

closegraph();

////////////////////////////////////////////////////////////

void Mid_point(int h,int k,int a,int b)

int midx, midy, xradius, yradius;

6
Punjabi university ,computer science department

int xrad2, yrad2, twoxrad2, twoyrad2;

int x, y, dp, dpx, dpy;

xradius = a, yradius = b;

/* finding the center postion to draw ellipse */

midx = h;

midy = k;

xrad2 = xradius * xradius;

yrad2 = yradius * yradius;

twoxrad2 = 2 * xrad2;

twoyrad2 = 2 * yrad2;

x = dpx = 0;

y = yradius;

dpy = twoxrad2 * y;

putpixel(midx + x, midy + y, WHITE);

putpixel(midx - x, midy + y, WHITE);

putpixel(midx + x, midy - y, WHITE);

putpixel(midx - x, midy - y, WHITE);

dp = (int) (0.5 + yrad2 - (xrad2 * yradius) + (0.25 * xrad2));

while (dpx < dpy) {

x = x + 1;

dpx = dpx + twoyrad2;

if (dp < 0) {

dp = dp + yrad2 + dpx;

} else {

y = y - 1;

7
Punjabi university ,computer science department

dpy = dpy - twoxrad2;

dp = dp + yrad2 + dpx - dpy;

/* plotting points in y-axis(top/bottom) */

putpixel(midx + x, midy + y, WHITE);

putpixel(midx - x, midy + y, WHITE);

putpixel(midx + x, midy - y, WHITE);

putpixel(midx - x, midy - y, WHITE);

delay(100);

delay(500);

dp = (int)(0.5 + yrad2 * (x + 0.5) * (x + 0.5) +

xrad2 * (y - 1) * (y - 1) - xrad2 * yrad2);

while (y > 0) {

y = y - 1;

dpy = dpy - twoxrad2;

if (dp > 0) {

dp = dp + xrad2 - dpy;

} else {

x = x + 1;

dpx = dpx + twoyrad2;

dp = dp + xrad2 - dpy + dpx;

8
Punjabi university ,computer science department

/* plotting points at x-axis(left/right) */

putpixel(midx + x, midy + y, WHITE);

putpixel(midx - x, midy + y, WHITE);

putpixel(midx + x, midy - y, WHITE);

putpixel(midx - x, midy - y, WHITE);

delay(100);

ellipse(midx,midy,0,360,xradius,yradius);

getch();

/* deallocate memory allocated for graphic screen */

closegraph();

//return 0;

Вам также может понравиться