Page 1 of 1

Box to display functions

Posted: Sun Nov 30, 2014 11:42 am
by Henko
this is a little program that i use for plotting math functions. Explanation via comments in the code.
The function is specified using normal coordinates. The function draw_it translates to pixel coordinates and does clippling if required.Clipping may be set on or off. Move the box with the left upper pixel coordinates: all content will move with it.
The x-y origin is set using relative position within the box (i.e. values between 0 and 1).

Code: Select all

graphics ! graphics clear .8,.8,.8    ' test program
fill color .8,.8,.8 ! draw color 0,0,0
box(10,40,300,300,.5,.5,30)  ' define the graphics box
draw color 0,0,0 ! draw size 1
for a=0 to 51 step .1                ' draw a spiral
  r=a/10 ! xn=r*cos(a) ! yn=r*sin(a)
  draw_it(xo,yo,xn,yn,0) ! xo=xn ! yo=yn
  next a
draw_it(-4,6,4,-7,0)          ' draw an un-clipped line
draw_it(3,6,-2,-10,1)         ' draw a clipped line
refresh
end

def box(xlu,ylu,width,height,xm,ym,scale)
' xlu and ylu = left upper corner in pixel coordinates
' width and height = size of graphics box in pixels
' xm and ym = relative position of graph origin
' scale = number of pixels per graph unit
'
org_x=xlu+xm*width ! org_y=ylu+(1-ym)*height
xrl=xlu+width ! yrl=ylu+height
refresh off
draw size 4
draw rect xlu-4,ylu-4 to xrl+4,yrl+4
draw size 1 ! draw alpha .2
for x=org_x to xrl step scale ! draw line x,ylu to x,yrl ! next x
for x=org_x to xlu step -scale ! draw line x,ylu to x,yrl ! next x
for y=org_y to yrl step scale ! draw line xlu,y to xrl,y ! next y
for y=org_y to ylu step -scale ! draw line xlu,y to xrl,y ! next y
draw color 0,0,1 ! draw size 2 ! draw alpha 1
draw line xlu,org_y to xrl,org_y
draw line org_x,ylu to org_x,yrl
refresh
end def

def draw_it(xs,ys,xe,ye,clip)
' all 4 coordinates in graph units
'
x1=box.org_x+box.scale*xs ! y1=box.org_y-box.scale*ys
x2=box.org_x+box.scale*xe ! y2=box.org_y-box.scale*ye
if not clip then draw
dx=x2-x1 ! dy=y2-y1
if dx<>0 then p=dy/dx else p=sign(dy)*1000
q=y1-x1*p
if x1<box.xlu then            ' clip left side of the box
  if x2<box.xlu then return
  x1=box.xlu ! y1=p*x1+q
  end if
if x2>box.xrl then             ' clip right side
  if x1>box.xrl then return
  x2=box.xrl ! y2=p*x2+q
  end if
if y1<box.ylu then             ' clip upper side
  if y2<box.ylu then return
  y1=box.ylu ! x1=(y1-q)/p 
  end if
if y2>box.yrl then             ' clip lower side
  if y1>box.yrl then return
  y2=box.yrl ! x2=(y2-q)/p
  end if
draw:
draw line x1,y1 to x2,y2
end def

Re: Box to display functions

Posted: Sun Nov 30, 2014 12:22 pm
by Mr. Kibernetik

Code: Select all

graphics ! graphics clear .8,.8,.8    ' test program
fill color .8,.8,.8 ! draw color 0,0,0
box(10,40,300,300,.5,.5,30)  ' define the graphics box
shadow on
draw color 0,0,0 ! draw size 1
for a=0 to 51 step .1                ' draw a spiral
  r=a/10 ! xn=r*cos(a) ! yn=r*sin(a)
  draw_it(xo,yo,xn,yn,0) ! xo=xn ! yo=yn
  next a
draw_it(-4,6,4,-7,0)          ' draw an un-clipped line
draw_it(3,6,-2,-10,1)         ' draw a clipped line
refresh
end

def box(xlu,ylu,width,height,xm,ym,scale)
' xlu and ylu = left upper corner in pixel coordinates
' width and height = size of graphics box in pixels
' xm and ym = relative position of graph origin
' scale = number of pixels per graph unit
'
org_x=xlu+xm*width ! org_y=ylu+(1-ym)*height
xrl=xlu+width ! yrl=ylu+height
refresh off
draw size 4
fill color 1,1,1
fill rect xlu-4,ylu-4 to xrl+4,yrl+4
draw rect xlu-4,ylu-4 to xrl+4,yrl+4
draw size 1 ! draw alpha .2
for x=org_x to xrl step scale ! draw line x,ylu to x,yrl ! next x
for x=org_x to xlu step -scale ! draw line x,ylu to x,yrl ! next x
for y=org_y to yrl step scale ! draw line xlu,y to xrl,y ! next y
for y=org_y to ylu step -scale ! draw line xlu,y to xrl,y ! next y
draw color 0,0,1 ! draw size 2 ! draw alpha 1
draw line xlu,org_y to xrl,org_y
draw line org_x,ylu to org_x,yrl
refresh
end def

def draw_it(xs,ys,xe,ye,clip)
' all 4 coordinates in graph units
'
x1=box.org_x+box.scale*xs ! y1=box.org_y-box.scale*ys
x2=box.org_x+box.scale*xe ! y2=box.org_y-box.scale*ye
if not clip then draw
dx=x2-x1 ! dy=y2-y1
if dx<>0 then p=dy/dx else p=sign(dy)*1000
q=y1-x1*p
if x1<box.xlu then            ' clip left side of the box
  if x2<box.xlu then return
  x1=box.xlu ! y1=p*x1+q
  end if
if x2>box.xrl then             ' clip right side
  if x1>box.xrl then return
  x2=box.xrl ! y2=p*x2+q
  end if
if y1<box.ylu then             ' clip upper side
  if y2<box.ylu then return
  y1=box.ylu ! x1=(y1-q)/p 
  end if
if y2>box.yrl then             ' clip lower side
  if y1>box.yrl then return
  y2=box.yrl ! x2=(y2-q)/p
  end if
draw:
draw line x1,y1 to x2,y2
end def