?? 111.txt
字號:
#include <graphics.h> /*圖形系統(tǒng)頭文件*/
#include <conio.h>
#include <dos.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define PI 3.1415926
int A=50;
float w=0.2;
static double yy=0.0,xx=0.0;
char ch;
void sphere(float R,int alfa,int beta)
{ /*R為球體半徑,alfa、beta分別為半徑與經(jīng)緯線的夾角*/
float x[4],y[4],z[4]; /*定義旋轉(zhuǎn)變換前點坐標數(shù)組*/
float x1[4],z1[4]; /*定義旋轉(zhuǎn)變換后點坐標數(shù)組*/
int i,j,k;
float sx[4],sy[4];
int shfill[10]; /*定義存放了5個頂點坐標序列的數(shù)組*/
double yn;
double a1,a2,b1,b2,c,d;
c=alfa*PI/180.0; /*每次旋轉(zhuǎn)的角度*/
d=beta*PI/180.0;
cleardevice();
for(j=0;j<180;j=j+20)
{
a1=j*PI/180.0;
a2=(j+20)*PI/180.0;
for(i=0;i<360;i=i+20)
{
b1=i*PI/180;
b2=(i+20)*PI/180;
x[0]=R*sin(a1)*cos(b1); /*求出圖形旋轉(zhuǎn)前點的坐標*/
y[0]=R*sin(a1)*sin(b1);
z[0]=R*cos(a1);
x[1]=R*sin(a2)*cos(b1);
y[1]=R*sin(a2)*sin(b1);
z[1]=R*cos(a2);
x[2]=R*sin(a2)*cos(b2);
y[2]=R*sin(a2)*sin(b2);
z[2]=R*cos(a2);
x[3]=R*sin(a1)*cos(b2);
y[3]=R*sin(a1)*sin(b2);
z[3]=R*cos(a1);
for(k=0;k<4;k++) /*求出圖形旋轉(zhuǎn)后點的坐標*/
{
x1[k]=x[k]*cos(c)-y[k]*sin(c);
z1[k]=-x[k]*sin(c)*sin(d)-y[k]*cos(c)*sin(d)+z[k]*cos(d);
sx[k]=300-x1[k]; /*將三維坐標轉(zhuǎn)化為屏幕坐標*/
sy[k]=250-z1[k];
}
yn=-(x1[2]-x1[0])*(z1[3]-z1[1])+(x1[3]-x1[1])*(z1[2]-z1[0]);
if(yn>=0.0) /*對可見部分進行畫線,實現(xiàn)消隱*/
{
moveto(sx[0],sy[0]);
lineto(sx[1],sy[1]);
lineto(sx[2],sy[2]);
lineto(sx[3],sy[3]);
lineto(sx[0],sy[0]);
shfill[0]=(int)sx[0],shfill[1]=(int)sy[0];
shfill[2]=(int)sx[1],shfill[3]=(int)sy[1];
shfill[4]=(int)sx[2],shfill[5]=(int)sy[2];
shfill[6]=(int)sx[3],shfill[7]=(int)sy[3];
shfill[8]=(int)sx[0],shfill[9]=(int)sy[0];
if(i==20) /*對一段經(jīng)曲面進行填充為綠色*/
{ setfillstyle(1,1); }
else
setfillstyle(1,15);
fillpoly(5,shfill); /*用當(dāng)前顏色填充多邊形*/
}
}
}
}
void main()
{
int gdrive=DETECT,gmode,k,t,i=0; /*自動探測圖形顯示器適配器種類*/
int size;
initgraph(&gdrive,&gmode,""); /*初始化圖形系統(tǒng)*/
setcolor(RED); /*設(shè)置當(dāng)前畫筆顏色為紅色*/
for(k=1;k<=360;k=k+1)
{
sphere(40,k,k); /*調(diào)用sphere函數(shù)*/
delay(10000);
}
getch();
closegraph(); /*關(guān)閉圖形系統(tǒng)*/
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -