daniel__lee
高階會員
![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif) ![](./myimg/board/mystar.gif)
![](images/icon_photo_none.gif) 發表:18 回覆:124 積分:113 註冊:2002-11-10
發送簡訊給我
|
這是我以前參考計算機圖學做的 (完整版) 附執行檔 //---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
#include <math.h>
#define PI 3.141593
#define D 1
#define VD 300
float Theta=0,Phi=0,SN1,SN2,CN1,CN2,
X,Y,Z,XE,YE,ZE,XS,YS,ZS,phi,theta;
int mouse_x,mouse_y;
bool flag=0; void DefineViewingParameters();
void Define3DFunction();
void ViewingCoordinates();
void ScreenCoordinates();
void Plot(); TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender)
{
Timer1->Enabled=false;
Timer1->Interval=200;
Form1->Caption="ELLIPSOD + ROTATE BY DANIEL LEE";
Form1->Canvas->FillRect(Rect(0,0,ClientWidth,ClientHeight));
Form1->Canvas->TextOutA(5,5,"用滑鼠選擇繪圖位置");
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
Form1->Canvas->TextOutA(5,5,"用滑鼠選擇繪圖位置 X="+IntToStr(X)+" Y="+IntToStr(Y));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
AnsiString InputString;
mouse_x=X;
mouse_y=Y;
Form1->Canvas->FillRect(Rect(0,0,ClientWidth,ClientHeight));
Form1->Canvas->TextOutA(5,5,"用滑鼠選擇繪圖位置 X="+IntToStr(X)+" Y="+IntToStr(Y));
if(Timer1->Enabled==false)
{
theta=0;
phi=0;
}
Timer1->Enabled^=1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Phi=phi; Theta=theta;
DefineViewingParameters();
Form1->Canvas->FillRect(Rect(0,0,ClientWidth,ClientHeight));
Form1->Canvas->TextOutA(5,5,"用滑鼠選擇繪圖位置 X="+IntToStr(mouse_x)+" Y="+IntToStr(mouse_y));
Define3DFunction();
phi+=5;
if(phi>180) phi=0;
}
//---------------------------------------------------------------------------
void DefineViewingParameters()
{
Theta=Theta*PI/180;
Phi=Phi*PI/180;
SN1=sin(Theta);SN2=sin(Phi);CN1=cos(Theta);CN2=cos(Phi);
}
//---------------------------------------------------------------------------
void Define3DFunction()
{
float theta2,thetat,phi2,phit,r =0.5;
for(phi2=0;phi2<=180;phi2+=10)
{
flag=0; phit = (float) phi2 *PI /180;
for(theta2=0;theta2<=360; theta2+=2)
{
thetat = (float) theta2 * PI/180;
X = r * sin(phit)*cos(thetat);
Y = r * sin(phit)*sin(thetat);
Z = r * cos(phit);
ViewingCoordinates();
ScreenCoordinates();
Plot();
}
}
for(theta2=0;theta2<=360;theta2+=10)
{
flag=0; thetat = (float) theta2 *PI /180;
for(phi2=0;phi2<=180; phi2+=2)
{
phit = (float) phi2 * PI/180;
X = r * sin(phit)*cos(thetat);
Y = r * sin(phit)*sin(thetat);
Z = r * cos(phit);
ViewingCoordinates();
ScreenCoordinates();
Plot();
}
}
}
//---------------------------------------------------------------------------
void ViewingCoordinates()
{
XE=-X * SN1 + Y * CN1;
YE=-X*CN1*CN2-Y*SN1*CN2+Z*SN2;
ZE=-X*SN2*CN1-Y*SN2*SN1-Z*CN2+D;
}
//---------------------------------------------------------------------------
void ScreenCoordinates()
{
XS=(VD)*XE/ZE;
YS=(VD)*YE/ZE;
XS=XS+mouse_x;
YS=-YS+mouse_y;
}
//---------------------------------------------------------------------------
void Plot()
{
if((XS < 0)||(XS >Form1->Width)||(YS<0)||(YS>Form1->Height))
{
flag=0;
return;
}
if(flag==0)
{
Form1->Canvas->MoveTo(XS,YS);
Form1->Canvas->Pixels[XS][YS]=clRed;
flag=1;
Form1->Canvas->Pen->Color=clBlue;
return;
}
Form1->Canvas->LineTo(XS,YS);
}
//------------------------------------------------------------------------END ~ 勿在浮沙上面築高塔 ~
------ ~ 勿在浮沙上面築高塔 ~
|