Dark Bit Factory & Gravity

ARCHIVE => Archive => Cobra => Topic started by: GrahamK on January 11, 2007

Title: Fun with superellipsoids
Post by: GrahamK on January 11, 2007
Here is a quicky demo for you, generally plots out a super-ellipsoid.. Don't know what one of those is ? well its a maths function which can be used to generate a range of shapes, from stars to spheres, and cubes with rounded corners, cylinders etc all from one funky function.
Plotted them out using voxel like points for effect...

You will need to download the 'experimental' patch to get it to work as there are a couple of new commands in there (quad specifically).


Code: [Select]
program
  uses pure2d,keyset         
               
 
procedure EvalSuperEllipse(t1,t2,p1,p2:real,byref pX,pY,pZ:real)
var
      tmp:real
      ct1,ct2,st1,st2:real
begin
        ct1 := cos(t1*(180.0/Pi));
        ct2 := cos(t2*(180.0/Pi));
        st1 := sin(t1*(180.0/Pi));
        st2 := sin(t2*(180.0/Pi));

        tmp := (abs(ct1)^p1)*SGN(ct1)
        px := tmp  * (abs(ct2)^p2) * SGN(ct2)
        py :=  (abs(st1)^p1) *SGN(st1)
        pz := tmp  * (abs(st2)^p2)* SGN(st2)
end
 
procedure Voxel(x,y,s,col:integer)
begin
    if s < 3 then s = 3
    Quad(x,y-s, x+s,y-s/2, x,y, x-s, y-s/2,col,true)
    Quad(x-s,y-s/2, x,y, x,y+s,x-s,y+s/2,ToRGBA(0.75*Red(col),0.75*Green(col),0.75*Blue(col)),true)
    Quad(x,y,x+s,y-s/2,x+s,y+s/2,x,y+s,ToRGBA(0.5*Red(col),0.5*Green(col),0.5*Blue(col)),true)
end

 
procedure Plot3d(x,y,z:real)
var
    px,py:real;
    zc:real
    col:integer;
begin
    px = ((x*256)/z)+400
    py = ((y*256)/z)+300
    zc = z-minz
    zc = zc * (200.0/(maxz-minz))
   
    // calculation for effect, as no depth checkin means stuff gets overdrawn
    col = ToRGBA(255-zc,zc,zc)
    //Rect(px-1,py-1,zc/10,zc/10,col,true)
    voxel(px,py,8,col)
end

procedure calc3d(x,y,z,ax,ay:real)
var
    xr,yr,zr:real
begin
    zr=(z*Cos(ax))-(y*Sin(ax))
    yr=(y*Cos(ax))+(z*Sin(ax))
    xr=(x*Cos(ay))-(zr*Sin(ay))
    zr=(zr*Cos(ay))-(x*Sin(ay))
    zr=zr+2000
               
    plot3d(xr,yr,zr)           
    maxz=Max(maxz,zr)
    minz=Min(minz,zr)
end

var
    i,j : real
    ax,ay:real
    d1:real=0.001
    d2:real=0.003         
   
    maxz:real=-99999
    minz:real=99999
    scale:real=1000     
   
    power1,power2:real=1
    n:real=32

    twopi,pid2,delta :real
    theta1,theta2,theta3:real
    px,py,pz:real   
begin
  openscreen         
               
   twopi = pi*2.0
   pid2  = pi/2.0
   
   
   d1=0.001
   d2=0.003
   
 
 
  while not keydown(vk_escape)
    cls                     
        ax = ax + 0.3
        if ax > 360 then ax=ax-360
        ay = ay + 0.7             
        if ay>360 then ay=ay-360
   
        power1 = power1 + d1
        if (power1>3) or (power1<0) then
            d1=-d1         
            power1 = power1 + d1
        endif
           

        power2 = power2 + d2
        if (power2>3) or (power2<0) then
            d2=-d2         
            power2 = power2 + d2
        endif
       
     delta = 0.01 * TWOPI / n
     for j =0 to n /2
     
      theta1 := j * TWOPI / n - PID2
      theta2 := (j + 1) * TWOPI / n - PID2

      for i = 0 to n
         if (i = 0) or (i = n) then
            theta3 = 0
         else
            theta3 = i * TWOPI / n
         endif

        EvalSuperEllipse(theta2,theta3,power1,power2,px,py,pz);
        calc3d(px*scale,py*scale,pz*scale,ax,ay)
      next
    next
             
   
//    Text(0,0,'Power1:'+power1+' power2:'+power2)
    flip
  wend
 
  closescreen
end

the power values determine the shape, uncomment the text line if you want to see the values.
Hope you enjoy it.
Title: Re: Fun with superellipsoids
Post by: mike_g on January 11, 2007
When I compile I get an error message: Undefined symbol (Quad) on line 24
Title: Re: Fun with superellipsoids
Post by: Clyde on January 11, 2007
You need to install the "Eperimental Patch" dude at Squeaky Designs. I did the same thing, as I didnt read the topic properlly.

And Cheers Graham for posting a real neat and cool snippet, nice one :)
Title: Re: Fun with superellipsoids
Post by: mike_g on January 11, 2007
Cheers, I'll do that then.

Edit: >_<
Title: Re: Fun with superellipsoids
Post by: GrahamK on January 11, 2007
Yeah, I've got to do something about that patch thing...
Once I do the full release I'll be removing the patches etc, and just having the full download.

 :||
Title: Re: Fun with superellipsoids
Post by: Shockwave on January 11, 2007
These things take a little time.. At least you have something out there that works and is stable.
Title: Re: Fun with superellipsoids
Post by: GrahamK on January 11, 2007
Fingers crossed!!!  :updance: