0% found this document useful (0 votes)
12 views

Computer Graphics Ellipse Lab - 3 Assignment

This document contains the source code for a C program that draws ellipses using different algorithms. It prompts the user to select an algorithm (1-4) and enter values for the ellipse's center point and axes. Based on the selection, it will use either a direct, polar, midpoint, or invalid algorithm to draw the ellipse in blue, red, white or no color. The program initializes graphics mode, draws the ellipse based on the algorithm, and closes graphics mode when finished.
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 views

Computer Graphics Ellipse Lab - 3 Assignment

This document contains the source code for a C program that draws ellipses using different algorithms. It prompts the user to select an algorithm (1-4) and enter values for the ellipse's center point and axes. Based on the selection, it will use either a direct, polar, midpoint, or invalid algorithm to draw the ellipse in blue, red, white or no color. The program initializes graphics mode, draws the ellipse based on the algorithm, and closes graphics mode when finished.
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
You are on page 1/ 5

Computer graphics Ellipse lab -3 assignment

Name:Abdela Aman
RollNo:-16071059

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

#include <math.h>

int main()

//request auto detection

int gdriver = DETECT, gmode, errorcode;

float x,y,dtheta,theta,p;

int choice;

// initialize graphics mode

initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

// read result of initialization

errorcode = graphresult();

if (errorcode != grOk) // an error occurred

printf("Graphics error: %s\n", grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

printf("Enter your algorithm to draw the circle(1-4):");

scanf("%d",&choice);

float h,k,a,b;

printf("+++++++enter the following info for choice ++++++++\n");

1|Page
Computer graphics Ellipse lab -3 assignment

printf("enter the center of h\n");

scanf("%f",&h);

printf("enter the center of k\n");

scanf("%f",&k);

printf("enter the major axis a\n");

scanf("%f",&a);

printf("enter the minor axis b\n");

scanf("%f",&b);

// draw a line

switch(choice)

case 1:

printf("The algrorithm is direct algorithm");

x=0;

y=0;

while(b*b*x<=a*a*y)

x=x+1;

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

y=rand(y);

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

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

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

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

while(y>=0)

y=y-1;

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

2|Page
Computer graphics Ellipse lab -3 assignment

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

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

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

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

//put_ellipse_pixel(h,k,x,y,RED);

break;

case 2:

printf("The algrorithm is polar algorithm");

if(a>b)

dtheta=1/a;

else

dtheta=1/b;

theta=0;

while(theta!=1.57)

x=a*cos(theta);

y=b*sin(theta);

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;

break;

case 3:

printf("The algrorithm is midpoint algorithm");

x=0; y=b;

p=(b*b)-(a*a*b)+((a*a)/4);

3|Page
Computer graphics Ellipse lab -3 assignment

while((2*x*b*b)<(2*y*a*a))

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

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

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

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

if(p<0)

x=x+1;

p=p+(2*b*b*x)+(3*b*b);

else

p=p+(2*b*b*x+b*b)-(2*a*a*y)+(3*b*b+2*a*a);

y=y-1;

p=b*b*((x+0.5)*(x*0.5))+a*a*((y-1)*(y-1))-a*a*b*b;

while(y>=0)

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

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

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

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

if(p<0)

y=y-1;

p=p-(2*a*a*y)+(3*a*a);

4|Page
Computer graphics Ellipse lab -3 assignment

else

y=y-1;

x=x+1;

p=p+(2*b*b*x)-(2*a*a*y)+(3*a*a)+2*b*b;

break;

default:

printf("invlaid input\n");

getch();

closegraph();

return 0;

5|Page

You might also like