#include
#pragma hdrstop
#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define M 200
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
PaintBox1->Width=401;
PaintBox1->Height=401;
PaintBox1->Refresh();
PaintBox1->Canvas->Pen->Width=1; //定义画笔
//画坐标系
PaintBox1->Canvas->MoveTo(0,200);
PaintBox1->Canvas->LineTo(400,200);
PaintBox1->Canvas->MoveTo(200,0);
PaintBox1->Canvas->LineTo(200,400);
for(int i=50;i<400;i+=50) //循环完成坐标标注
{
PaintBox1->Canvas->MoveTo(i,197);
PaintBox1->Canvas->LineTo(i,200);
PaintBox1->Canvas->MoveTo(200,i);
PaintBox1->Canvas->LineTo(203,i);
}
//画X坐标箭头
PaintBox1->Canvas->MoveTo(395,197);
PaintBox1->Canvas->LineTo(400,200);
PaintBox1->Canvas->MoveTo(395,203);
PaintBox1->Canvas->LineTo(400,200);
//画Y坐标箭头
PaintBox1->Canvas->MoveTo(197,5);
PaintBox1->Canvas->LineTo(200,0);
PaintBox1->Canvas->MoveTo(203,5);
PaintBox1->Canvas->LineTo(200,0);
bool logical=true;
int i;
double y1_change1,y1_change2,y2_change1,y2_change2; //这几个值保存经过比例转换的纵坐标值
double max,multiple;
double k1,b1,k2,b2; //k,b为两直线的斜率和截距
double x,y; //x,y为交点坐标
double t;
k1=StrToFloat(Edit1->Text);
b1=StrToFloat(Edit2->Text);
k2=StrToFloat(Edit3->Text);
b2=StrToFloat(Edit4->Text);
if(k1==k2)
{
logical=false;
Label18->Left=528;
Label18->Top=264;
Label18->Caption="两直线平行,无交点";
}
else
{
x=(b2-b1)/(k1-k2);
y=k1*x+b1;
}
if(logical)
t=fabs(fabs(x)>fabs(y)?x:y); //求出交点较大值的绝对值,以便合理确定单位长度表示的大小
else
t=fabs(fabs(b1)>fabs(b2)?b1:b2); //当两直线平行时求他们在Y轴上的截距,以便合理确定单位长度表示的大小
//下面的if-else语句用于判断两条直线的交点在什么范围以便合理地确定单位长度所表示的大小
if(t>=1)
{
for(i=1;;i++)
if(t>=i&&t<=3*i)
{
max=4*i; //这个max就是坐标值的上线
break;
}
}
else
{
for(i=2;;i*=2)
if(t>1/(double)i&&t<=3/(double)i)
{
max=4/(double)i;
break;
}
}
//下面的Label1~Label17都是对x,y轴上进行标注用的,通过上面的max可以算出来单位长度标注为多少合适
Label1->Caption=-3*max/4; Label8->Caption=3*max/4;
Label2->Caption=-2*max/4; Label9->Caption=2*max/4;
Label3->Caption=-max/4; Label10->Caption=max/4;
Label5->Caption=max/4; Label11->Caption=-max/4;
Label6->Caption=2*max/4; Label12->Caption=-2*max/4;
Label7->Caption=3*max/4; Label13->Caption=-3*max/4;
Label4->Caption=0;
multiple=200/max; //进行比例换算
y1_change1=k1*200+b1*multiple;
y1_change2=k1*(-200)+b1*multiple;
y2_change1=k2*200+b2*multiple;
y2_change2=k2*(-200)+b2*multiple;
PaintBox1->Canvas->MoveTo(200+M,M-y1_change1);
PaintBox1->Canvas->LineTo(-200+M,M-y1_change2);
PaintBox1->Canvas->MoveTo(200+M,M-y2_change1);
PaintBox1->Canvas->LineTo(-200+M,M-y2_change2);
if(logical) //如果有交点就把交点显示出来
{
Label18->Left=PaintBox1->Left+M+x*multiple;
Label18->Top=PaintBox1->Top+M-y*multiple;
Label18->Caption="("+FloatToStr(x)+","+FloatToStr(y)+")";
}
}
--------------------next---------------------
阅读(1230) | 评论(0) | 转发(0) |