范例-控制台-控制台画圆升级版:字符绘图类

class fig2d{
    
ctor(){
        
var points={{x=0;y=0}} //点数组
        var psn=1 //点的个数
        var tmppoints={}
    }
    xscale=1;
    yscale=1;
    add=
function(x,y){
        points[psn+1]={x=x;y=y}
        psn++
    }
    
//绘图函数
    //如果没给参数,图形就直接绘制到屏幕,否则输出到指定文件中
    plot=function(...){
        
var arg={...}
        output=..io.stdout
        
if #arg>0{
            output=..io.open(arg[1],
"a")
            
if output==null
                output=..io.stdout
        }
        
for i=1;psn
            tmppoints[i]={x=..math.round(
this.xscale*points[i].x);
                y=..math.round(
this.yscale*points[i].y)}
        ..table.sort(tmppoints,
this.comProc)
        pos=2
        
if tmppoints[pos].x==0&&tmppoints[pos].y==0
            output.write(
'*')
        
while pos<=psn{
            
var prex=tmppoints[pos-1].x
            
var prey=tmppoints[pos-1].y
            
var curx=tmppoints[pos].x
            
var cury=tmppoints[pos].y
            
if prex==curx
                
for i=prey+1;cury-1
                        output.write(
' ')
            
else{
                
for i=prex+1;curx
                        output.write(
'\n')
                
for i=0;cury-1
                        output.write(
' ')
            }
            
if prex!=curx || prey!=cury
                output.write(
'*')
            pos++
        }
        output.write(
'\n')
    }
    
//清空先前数据,重新加入新数据
    clf=function(){
        points={{x=0;y=0}}
        psn=1
    }
    comProc=
function(e){
        
if owner.x<e.x
            
return true
        
if owner.x>e.x
            
return false
        
if owner.y<e.y
            
return true
        
return false
    }
}
io.open()
//创建二维绘图对象
f=fig2d()
//绘制圆形,椭圆
r=7
cx=8
cy=8
for theta=0;2*math.pi;math.pi/12
    f.add(cx+r*math.cos(theta),cy+r*math.sin(theta))
f.plot()
f.yscale=2
f.plot()
f.yscale=4
f.plot()
f.xscale=5
f.yscale=11
f.plot()
execute("pause")
f.plot(
"large.txt"// 图形太大,存到文本文件中
//在先前图形的基础上再加上条正弦线
execute("cls")
for theta=0;8*math.pi;math.pi/12
    f.add(cx+4*math.sin(theta/2),theta)
f.xscale=1
f.yscale=2
f.plot()
execute("pause")
io.close()

你可能感兴趣的:(function,图形,output,plot)