Code: Select all
import "ecere"
class Ellipse : Surface
{
private:
Array<Point> arrayPoint;
public:
Point Origin; //原点
int Ra, Rb; // 椭圆弧的长轴和短轴
int nStep; // 四分之一椭圆弧要用多少段直线描述
property Point Origin
{
get {return Origin;}
set {Origin = value;}
}
property int Ra
{
get {return Ra;}
set {Ra = value;}
}
property int Rb
{
get {return Rb;}
set {Rb = value;}
}
property int nStep
{
set {nStep = value;}
}
Ellipse()
{
nStep = 8;
arrayPoint =Array<Point>{};
};
void setStep( int Step )
{
nStep = Step;
};
// 返回圆周上的点
void returnRound()
{
int i;
//int m = nStep*4;
double n = 90.0/nStep/180.0*Pi;
Array<Point> m_point{};
for( i=0; i<=nStep; ++i )
{
Point point{};
point.x = (int)(Ra*sin( i*n ));
point.y = (int)(Rb*cos( i*n ));
m_point.Add(point);
}
// 将[0,90]椭圆弧上的点扩充成整椭圆上的点,同时将圆心放到指定位置
// 已知第一象限的点(x,y),用对称的方法可以生成另外三个象限的点。生成方法是:
// 第二象限(x,y)-->(x,-y)
// 第三象限(x,y)-->(-x,-y)
// 第四象限(x,y)-->(-x,y)
// 第一象限
for( i=0; i<=nStep; ++i )
{
Point point{};
point.x = m_point[i].x + Origin.x;
point.y = m_point[i].y + Origin.y;
arrayPoint.Add(point);
}
// 第二象限
for( i=nStep; i>=0; --i )
{
Point point{};
point.x = m_point[i].x + Origin.x;
point.y = -m_point[i].y + Origin.y;
arrayPoint.Add(point);
}
// 第三象限
for( i=0; i<=nStep; ++i )
{
Point point{};
point.x = -m_point[i].x + Origin.x;
point.y = -m_point[i].y + Origin.y;
arrayPoint.Add(point);
}
// 第四象限
for( i=nStep; i>=0; --i )
{
Point point{};
point.x = -m_point[i].x + Origin.x;
point.y = m_point[i].y + Origin.y;
arrayPoint.Add(point);
}
}
void draw(Surface surface)
{
Iterator<Point> i { arrayPoint };
Point n0;
// 返回圆周上的点
returnRound();
//画点
i.Next();
n0 = i.data;
do
{
Point n1 = i.data;
surface.DrawLine(n0.x,n0.y,n1.x,n1.y);
n0 = n1;
} while(i.Next());
};
~Ellipse()
{
};
}
class Form1 : Window
{
text = "Form1";
background = activeBorder;
borderStyle = sizable;
hasMaximize = true;
hasMinimize = true;
hasClose = true;
size = { 576, 432 };
Ellipse ellipse{};
void OnRedraw(Surface surface)
{
surface.SetForeground(blue);
ellipse.Origin = {200,200};
ellipse.Ra = 80;
ellipse.Rb = 50;
//ellipse.setStep(100);
ellipse.draw(surface);
}
}
Form1 form1 {};