#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
#include <dos.h>
#define PI 3.14159
void OpenG(void)
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
}
void CloseG(void)
{
closegraph();
}
void ellip_angle(int xc,int yc, int rx,int ry,int color)
{
float px,py;
int delta=10;
float rad,i;
for(i=0;i<=90;i=i+delta){
rad = (PI*i)/180;
px = (rx * cos(rad));
py = (ry * sin(rad));
putpixel (xc + px, yc + py,color);
putpixel (xc - px, yc + py,color);
putpixel (xc + px, yc - py,color);
putpixel (xc - px, yc - py,color);
delay(200);
}
}
void ellip2point(int xc,int yc, float rx,float ry)
{
int i,delta = 10;
float rad,start_rad,start_deg=0;
float x1,x2,y1,y2,y;
start_rad = (start_deg*PI)/180;
x1 = rx*cos(start_rad);
y1 = ry*sin(start_rad);
rad = (delta*PI)/180;
for(i=0;i<360/delta/4;i++){
x2 = (x1*cos(rad)) + ((rx/ry)*y1*sin(rad));
// printf("x=%f ",x2);
y2 = (y1*cos(rad)) - ((ry/rx)*x1*sin(rad));
// printf(" y=%f\n",y2);
line(xc+x1,yc+y1,xc+x2,yc+y2);
line(xc-x1,yc+y1,xc-x2,yc+y2);
line(xc+x1,yc-y1,xc+x2,yc-y2);
line(xc-x1,yc-y1,xc-x2,yc-y2);
x1 = x2;
y1 = y2;
delay(200);
}
}
void main(void)
{
OpenG();
ellip_angle(300,240, 200,150,YELLOW);
ellip2point(300,240, 200,150);
getch();
CloseG();
}

͸ԺÒÂâ»Ãá¡ÃÁ

â»Ãá¡ÃÁ    ÁÕÊͧ Function ÊͧẺ ¤×Í  ellip_angle(int xc,int yc, int rx,int ry,int color)
xc,yc à»ç¹¨Ø´ center ¢Í§Ç§ÃÕ
 rx,ry à»ç¹ÃÑÈÁÕ·Ò§´éҹ᡹ x áÅÐ y
color ÊÕ

ÍÒÈÑÂËÅÑ¡ÒâͧÊÁ¡ÒÃǧÃÕ
x = xc + rx cosq
y = yc + ry sinq

ellip2point(int xc,int yc, float rx,float ry) xc,yc à»ç¹¨Ø´ center ¢Í§Ç§ÃÕ
rx,ry à»ç¹ÃÑÈÁÕ·Ò§´éҹ᡹ x áÅÐ y
ÍÒÈÑÂÊÁ¡ÒäÇÒÁÊÑÁ¾Ñ¹¸ìÃÐËÇèÒ§¨Ø´ x , y áµèÅШشº¹Ç§ÃÕ
x2 = x1 cos D q + (rx/ry) y1 sinD q
y2 = y1 cos D q + (ry/rx) x1 sinD q