?? basic_graph.cpp
字號(hào):
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Basic_graph.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString filename;
long width , height; // BMP圖尺寸
int X0=100, Y0=100;
int Ymax=450, Xmax=600;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Left=0;
Top=0;
Form1->Width=1000;
Form1->Height=700;
Label2->Caption = "";
Label4->Caption = "";
Label6->Caption = "";
Image1->Picture->LoadFromFile("windclr.bmp");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Label2->Caption = "";
Label4->Caption = "";
Label6->Caption = "";
Image1->Picture->LoadFromFile("windclr.bmp");
}
//---------------------------------------------------------------------------
//中點(diǎn)法畫直線
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Canvas->FillRect(ClientRect);
int x0,y0,x1,y1;
x0=StrToInt(Edit1->Text);
y0=StrToInt(Edit2->Text);
x1=StrToInt(Edit3->Text);
y1=StrToInt(Edit4->Text); //輸入端點(diǎn)坐標(biāo)值
int dx,dy,incre,incrne;
int d,x,y;
float k,t;
if(x0==x1) //豎直的直線
{ if(y0>y1)
{ t=y0; y0=y1; y1=t; }
for(t=y0;t<y1;t++)
Image1->Canvas->Pixels[x0][450-t]=clWhite;
}
else //畫斜率大于零的直線
{ if(k>=0)
{ if(x0>x1)
{ t=x0; x0=x1; x1=t;
t=y0; y0=y1; y1=t;
}
dx=x1-x0;
dy=y1-y0;
k=(dy*1.0)/(dx*1.0);
if(k>1)
{ t=x0; x0=y0; y0=t;
t=x1; x1=y1; y1=t;
}
dx=x1-x0;
dy=y1-y0;
d=dy*2-dx;
incre=dy*2;
incrne=(dy-dx)*2;
x=x0;
y=y0;
if(k>1)
Image1->Canvas->Pixels[y][450-x]=clWhite;
else
Image1->Canvas->Pixels[x][450-y]=clWhite;
while(x<x1)
{
if(d<=0)
{
d+=incre;
x++;
}
else
{
d+=incrne;
x++;
y++;
}
if(k>1)
Image1->Canvas->Pixels[y][450-x]=clWhite;
else
Image1->Canvas->Pixels[x][450-y]=clWhite;
}
}
else //畫斜率小于零的直線
{
if(x0>x1)
{ t=x0; x0=x1; x1=t;
t=y0; y0=y1; y1=t;
}
dx=x1-x0;
dy=y1-y0;
k=(dy*1.0)/(dx*1.0);
if(k<-1) //斜率小于-1的直線
{ t=y0; y0=x0; x0=t;
t=y1; y1=x1; x1=t;
}
t=y1; y1=y0; y0=t;
dx=x1-x0;
dy=y1-y0;
d=dy*2-dx;
incre=dy*2;
incrne=(dy-dx)*2;
x=x0;
y=y0;
if(k<-1)
Image1->Canvas->Pixels[y][450-(y0+y1-x)]=clWhite;
else
Image1->Canvas->Pixels[x][450-(y0+y1-y)]=clWhite;
while(x<x1)
{
if(d<=0)
{
d+=incre;
x++;
}
else
{
d+=incrne;
x++;
y++;
}
if(k<-1)
Image1->Canvas->Pixels[y][450-(y0+y1-x)]=clWhite;
else
Image1->Canvas->Pixels[x][450-(y0+y1-y)]=clWhite;
}
}
}
}
//---------------------------------------------------------------------------
//中點(diǎn)法畫圓
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int xc,yc,x,y,d,deltaE,deltaSE,radius;
xc=StrToFloat(Edit5->Text);
yc=StrToFloat(Edit6->Text);
radius=StrToFloat(Edit7->Text); //輸入數(shù)值
x=0;
y=radius;
Image1->Canvas->Pen->Color=clRed;
Image1->Canvas->MoveTo(xc-y-10,Ymax-yc);
Image1->Canvas->LineTo(xc+y+10,Ymax-yc);
Image1->Canvas->MoveTo(xc,Ymax-(yc-y-10));
Image1->Canvas->LineTo(xc,Ymax-(yc+y+10)); //畫出圓的中心線
d=1-radius;
deltaE=3 ;
deltaSE=5-radius*2;
Image1->Canvas->Pixels[x+xc][450-(y+yc)]=clWhite; //1/8圓弧的第一個(gè)點(diǎn)
Image1->Canvas->Pixels[x+xc][450-(y+yc)]=clWhite;
Image1->Canvas->Pixels[y+xc][450-(x+yc)]=clWhite;
Image1->Canvas->Pixels[y-+xc][450-(x+yc)]=clWhite;
Image1->Canvas->MoveTo(x,y);
//利用中點(diǎn)法畫出1/8圓弧
while (y>x)
{
if (d<0)
{
d+=deltaE;
deltaE+=2;
deltaSE+=2;
x++;
}
else
{
d+=deltaSE;
deltaE+=2;
deltaSE+=4;
x++;
y--;
}
Image1->Canvas->Pixels[x+xc] [450-(y+yc)]=clWhite;
Image1->Canvas->Pixels[-x+xc][450-(y+yc)]=clWhite;
Image1->Canvas->Pixels[x+xc] [450-(-y+yc)]=clWhite;
Image1->Canvas->Pixels[-x+xc][450-(-y+yc)]=clWhite;
Image1->Canvas->Pixels[y+xc] [450-(x+yc)]=clWhite;
Image1->Canvas->Pixels[-y+xc][450-(x+yc)]=clWhite;
Image1->Canvas->Pixels[y+xc] [450-(-x+yc)]=clWhite;
Image1->Canvas->Pixels[-y+xc][450-(-x+yc)]=clWhite; //將1/8圓弧對(duì)稱為整圓
}
}
//---------------------------------------------------------------------------
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -