Welcome to DrChip

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Welcome to DrChip

Post by Henko »

Code: Select all

option base 1 ! option angle degrees
graphics ! graphics clear ! refresh off ! draw color 0,0,0
cx=screen_width()/2 ! cy=screen_height()/2
np=14 ! nz=10 ! nl=20 ! scale=100
dim point(np,3),pt(np,3),pix(np,2),line(nl,2),centre(3)
for i=1 to np ! for j=1 to 3
  read point(i,j) ! if i<=nz then centre(j)+=point(i,j)
  next j ! next i
for j=1 to 3 ! centre(j)/=nz ! next j
for i=1 to nl ! for j=1 to 2 ! read line(i,j) ! next j ! next i
for i=1 to np ! for j=1 to 3
  point(i,j)=scale*(point(i,j)-centre(j))
  next j ! next i
data 3,2,1, 6,2,1, 6,0,1, 3,0,1, 3,2,6
data 6,2,6, 6,0,6, 3,0,6, 4.5,3,1, 4.5,3,6
data 6,0,2, 6,0,3, 6,1.5,2, 6,1.5,3
data 1,2, 2,3, 3,4, 4,1, 5,6, 6,7, 7,8
data 8,5, 1,5, 2,6, 3,7, 4,8, 1,9, 2,9, 5,10, 6,10, 9,10
data 11,13, 13,14, 14,12

loop: a+=3
  graphics clear
  rotate(point,pt,10*cos(5*a),a,10*sin(3*a))
  trans(pt,pt,0,0,400*(1+sin(4*a)))
  project(pt,pix)
  disp(pix,line)
goto loop
end

def rotate(pin(,),pout(,),ax,ay,az)
sax=sin(ax) ! cax=cos(ax)
say=sin(ay) ! cay=cos(ay)
saz=sin(az) ! caz=cos(az)
for i=1 to .np ! for j=1 to 3 ! pout(i,j)=pin(i,j) ! next j ! next i
if ax then 
  for i=1 to .np
    temp=pout(i,2) ! pout(i,2)=cax*temp-sax*pout(i,3)
    pout(i,3)=sax*temp+cax*pout(i,3)
    next i
  end if
if ay then 
  for i=1 to .np
    temp=pout(i,1) ! pout(i,1)=cay*temp-say*pout(i,3)
    pout(i,3)=say*temp+cay*pout(i,3)
    next i
  end if
if az then 
  for i=1 to .np
    temp=pout(i,1) ! pout(i,1)=caz*temp-saz*pout(i,2)
    pout(i,2)=saz*temp+caz*pout(i,2)
    next i
  end if
end def

def trans(pin(,),pout(,),dx,dy,dz)
for i=1 to .np
  pout(i,1)=pin(i,1)+dx
  pout(i,2)=pin(i,2)+dy
  pout(i,3)=pin(i,3)+dz
  next i
end def

def project(pin(,),pout(,))
for i=1 to .np
  fac=900/(900+pin(i,3))
  pout(i,1)=fac*pin(i,1) ! pout(i,2)=fac*pin(i,2)
  next i
end def

def disp (pp(,),ll(,))
for i=1 to .nl
  p1=ll(i,1) ! p2=ll(i,2)
  draw line pp(p1,1)+.cx,.cy-pp(p1,2) to pp(p2,1)+.cx,.cy-pp(p2,2)
  next i
refresh
end def

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Welcome to DrChip

Post by Henko »

For iPhone in stead of iPad : put "scale=50" in stead of 100 in the 4th line of the code
For fast 5S or 6 : change the loop variable a+=3 into a+=.5 for example

DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Re: Welcome to DrChip

Post by DrChip »

Very cool!

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Welcome to DrChip

Post by Henko »

The work of DrChip inspires me to meddle a little with 3d graphics.
I added comments to my code snippet, in order to be able to understand my own program after some time. Also polygons are added with selectable number of sides, color, and transparency.
I'll also have a look ad hidden line techniques (still have to program a 3d scenery in my flightsim).
@ DrChip: sorting is extremely fast in sB, look at the sorting commands in the "misc" section of the doc.

Code: Select all

option base 1 ! option angle degrees
graphics ! refresh off
fill color .5,.5,0 ! fill alpha .5 ! draw color 0,0,0
cx=screen_width()/2 ! cy=screen_height()/2

np=20     ' number of points
nz=10     ' first nz points are used to calculate the centre of the body
nl=20     ' number of lines
nf=5      ' number of polygons
if device_type$="iPhone" then scale=50 else scale=100   ' size factor
a=45      ' starting value for the rotation loop
da=1      ' increment value for the loop variable


dim point(np,3),pt(np,3),pix(np,2),line(nl,2),poly(nf,11),centre(3)

' read coordinates and sum up for calculation of the centre
for i=1 to np ! for j=1 to 3
  read point(i,j) ! if i<=nz then centre(j)+=point(i,j)
  next j ! next i

for j=1 to 3 ! centre(j)/=nz ! next j ' coordinates of the centre

' read the point numbers of the lines (start and end point)
for i=1 to nl ! for j=1 to 2 ! read line(i,j) ! next j ! next i

' read the data of the polygons
' (-,1) : the number of sides for this polygon (2<nn<7)
' (-,2),(-,3),(-,4) : the fill RGB value for this polygon
' (-,5) : the fill alpha value for this polygon
' (-,6) through (-,nn+5) : the nn vertex numbers of the polygon
'
for i=1 to nf ! read poly(i,1)
  for j=2 to poly(i,1)+5 ! read poly(i,j)
    next j
  next i

' translate the body to the centre of the 3d space
for i=1 to np ! for j=1 to 3
  point(i,j)=scale*(point(i,j)-centre(j))
  next j ! next i

' coordinate data
data 3,2,1, 6,2,1, 6,0,1, 3,0,1, 3,2,6
data 6,2,6, 6,0,6, 3,0,6, 4.5,3,1, 4.5,3,6
data 6,0,2, 6,0,3, 6,1.5,2, 6,1.5,3
data 3,1.5,5, 3,1.25,5.5, 3,.75,5.5, 3,.5,5, 3,.75,4.5, 3,1.25,4.5
' line data
data 1,2, 2,3, 3,4, 4,1, 5,6, 6,7, 7,8, 8,5, 1,5, 2,6, 3,7
data 4,8, 1,9, 2,9, 5,10, 6,10, 9,10, 11,13, 13,14, 14,12
' polygon data
data 4, 0,.5,.5, .2, 3,4,8,7
data 4, .8,.2,0, .4, 1,5,10,9
data 3, .5,0,.5, .6, 5,6,10
data 4, .6,.6,.6, .3, 11,12,14,13
data 6, 0,.8,.8, .5, 15,16,17,18,19,20

loop: a+=da ' the loop variable, the amount of degrees per rotation
  graphics clear
  rotate(point,pt,10*cos(5*a),a,10*sin(3*a)) ' rotate about the centre
  trans(pt,pt,0,0,400*(1+sin(4*a))) ' translate in the z-direction
  project(pt,pix)   ' projection from 3d into 2d, adding perspective
  disp(pix,line,poly) ' tranform to screen coordinates and display it
goto loop
end

def rotate(pin(,),pout(,),ax,ay,az)
sax=sin(ax) ! cax=cos(ax)
say=sin(ay) ! cay=cos(ay)
saz=sin(az) ! caz=cos(az)
for i=1 to .np ! for j=1 to 3 ! pout(i,j)=pin(i,j) ! next j ! next i
if ax then 
  for i=1 to .np
    temp=pout(i,2) ! pout(i,2)=cax*temp-sax*pout(i,3)
    pout(i,3)=sax*temp+cax*pout(i,3)
    next i
  end if
if ay then 
  for i=1 to .np
    temp=pout(i,1) ! pout(i,1)=cay*temp-say*pout(i,3)
    pout(i,3)=say*temp+cay*pout(i,3)
    next i
  end if
if az then 
  for i=1 to .np
    temp=pout(i,1) ! pout(i,1)=caz*temp-saz*pout(i,2)
    pout(i,2)=saz*temp+caz*pout(i,2)
    next i
  end if
end def

def trans(pin(,),pout(,),dx,dy,dz)
for i=1 to .np
  pout(i,1)=pin(i,1)+dx
  pout(i,2)=pin(i,2)+dy
  pout(i,3)=pin(i,3)+dz
  next i
end def

def project(pin(,),pout(,))
for i=1 to .np
  fac=900/(900+pin(i,3))
  pout(i,1)=fac*pin(i,1) ! pout(i,2)=fac*pin(i,2)
  next i
end def

def disp (pp(,),ll(,),ff(,))
dim fx(6),fy(6)
for i=1 to .nl
  p1=ll(i,1) ! p2=ll(i,2)
  draw line pp(p1,1)+.cx,.cy-pp(p1,2) to pp(p2,1)+.cx,.cy-pp(p2,2)
  next i
if .nf=0 then ready
for i=1 to .nf
  npoly=ff(i,1) ! fill color ff(i,2),ff(i,3),ff(i,4) ! fill alpha ff(i,5)
  for j=1 to npoly
    px=ff(i,j+5)
    fx(j)=pp(px,1)+.cx ! fy(j)=.cy-pp(px,2)
    next j
  fill poly fx,fy count npoly
  next i
ready: refresh
end def

Post Reply