« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

graph library

// graph library
//   A library to implement graphic on JavaScript by Giovanni Nicco
//   Commands have  Java syntax
//
//
//                                
//   drawArc(x,y,l,h,alpha0,alpha1): draw an arc of oval from alpha0 to alpha1 (radians)
//   drawImage(x,y,fileName) : draws an image (fileName) in x,y 
//   drawOval(x,y,width,heigth): draw an oval (or circle) (use last setColor color)
//   drawLine(x0,y0,x1,y1): draws a line from x0,y0 to x1,y1
//   drawRect(x0,y0,width,heigth): draws a rectangle
//   drawString(s,x,y): draws a strings s in x,y
//   fillArc(x,y,l,h,alpha0,alpha1): draw a filled arc of oval from alpha0 to alpha1 (radians)
//   fillRect(x0,y0,width,heigth,color): draw a filled rectangle (need color)
//   fillOval(x,y,width,heigth): draw a filled oval (or circle) (use last setColor color)
//   setColor(c); sets the color of next figures to be c (unsetted color is black)
//   setPoint(x,y); draw a point in x,y (black or of the color setted by setColor()
var red='red';
var green='green';
var blue='blue';

function drawArc(x,y,l,h,alpha0,alpha1){
                rx=l/2;ry=h/2;
                xc=x+rx;yc=y+ry
                da=Math.PI/360
                for(var alpha=alpha0;alpha<=alpha1;alpha+=da){
                        drawLine(xc+rx*Math.cos(alpha),yc-ry*Math.sin(alpha),xc+rx*Math.cos(alpha+da),yc-ry*Math.sin(alpha+da));
                }
}

function drawImage(x,y,i) {
    document.writeln(''+x+'px; top:'+y+'px">'+i+'">');
}

function drawLine(x0,y0,x1,y1){
        dx=x1-x0;dy=y1-y0;
        if(y1>=y0){versoy=1}else{versoy=-1}
        if(x1>=x0){versox=1}else{versox=-1}
        npx=Math.abs(dx);npy=Math.abs(dy);
        var i=0;
        if( dx*dx >= dy*dy){ // tg <= 45�
                deY=dy/npx;
                with(document){
                        while(i++<=npx){
                                write('' +x0+ '; top:' +y0+ '">.');
                                x0+=versox;y0+=deY
                        }
                }
        }
        else{ // tg > 45�
                deX=dx/npy;
                with(document){
                        while(i++<=npy){
                                write('' +x0+ '; top:' +y0+ '">.');
                                y0+=versoy;x0+=deX;
                        }
                }
         }
}

function drawOval(x,y,l,h){
        rx=l/2;ry=h/2;
        xc=x+rx;yc=y+ry
        da=Math.PI/360
        for(var alpha=0;alpha<=6.29;alpha+=da){
                drawLine(xc+rx*Math.cos(alpha),yc- ry*Math.sin(alpha),xc+rx*Math.cos(alpha+da),yc- ry*Math.sin(alpha+da));
        }
}


function drawRect(x0,y0,l,h){
        x0=Math.floor(x0);y0=Math.floor(y0);
        l=Math.ceil(l);h=Math.ceil(h);
        drawLine(x0,y0,x0+l,y0);
        drawLine(x0,y0,x0,y0+h);
        drawLine(x0+l,y0+h,x0+l,y0);
        drawLine(x0+l,y0+h,x0,y0+h);
}

function drawString(s,x,y){
        document.write('' +x+ 'px; top:' +y+ 'px">'+s+'');
}


function fillArc(x,y,l,h,alpha0,alpha1){
        rx=l/2;ry=h/2;
        xc=x+rx;yc=y+ry
        da=Math.PI/180
        for(var alpha=alpha0;alpha<=alpha1;alpha+=da){
                drawLine(xc,yc,xc+rx*Math.cos(alpha+da),yc-ry*Math.sin(alpha+da));
        }
}


function fillOval(x,y,l,h){
        var rx=l/2; var ry=h/2;
        var xc=x+rx;var yc=y+ry
        for(var xx=x+l;xx>=-x;xx--){
                alpha=Math.acos((xx-x)/l)
                drawLine(xc+rx*Math.cos(alpha),yc-ry*Math.sin(alpha),xc+rx*Math.cos(-alpha),yc-ry*Math.sin(-alpha));
        }
}

function fillRect(x,y,w,h,color){
        document.write(     '
'+ y+'; left:'+ x+'; width:'+w+ '; height:'+h+'; background-color: '+ color+' " >
' ) } function setColor(c){ document.write(''+c+'>'); } function setPoint(x,y){ document.write(' ' +x+ 'px; top:' +y+ 'px">. ' ); }
« Newer Snippets
Older Snippets »
1 total  XML / RSS feed