home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsd / dithering / CDBox_HSVt (.txt) < prev    next >
RISC OS BBC BASIC V Source  |  1994-02-08  |  7KB  |  290 lines

  1.  >Segment3
  2.  : Cy Booker
  3.  : 86 Church View
  4.  : Main Road
  5.  : Crockenhill
  6.  : Swanley
  7.  : Kent
  8.  : BR8 8 JW
  9.      Test algorithm for drawing an hsv cone dynamically
  10.  Instead of drawing curves, we draw quadrangles
  11.  We pre-calculate all trig multiplications
  12.  The circle is split into NT% = 8*2^N% angles [N% = 0, 1, 2, 3]
  13.  The outer radius is R [= Radius * Value]
  14.  Each annulus is DR% wide
  15.  There are NA% annuli = floor(R / DR%)
  16.  Note tried just storing points at corners of each quadrangle
  17.  but this results in too much screen overwriting, so now going
  18.  to store for each quadrangle the four points, suitably grown
  19.  in one half pixel
  20.  No, this leaves some gaps, so trying to align to 2 OS unit "pixel" grid
  21.  No, if align all up to 2 pixel grid, at least leaves no gaps
  22.      Note that for radius R, the max distance between straight line and
  23.      actual radius occurs when N% = 0, and get d = r*(1-COS(PI/8))
  24.      which for r= 128 => d= 9.74
  25.  The above means when try blanking out need to add 10 OS units to radius
  26.  to ensure blank all
  27.   This produces a nice demo of the finnished program
  28.      The lhs is the value bar, click with mouse to set value
  29. $+ " {"+ 
  30. )+ "}"
  31. "7Value = -1                      :
  32.  0 <= Value < 1.0
  33. #BRadius = 127.5                  :
  34.  Radius of cone, in OS units
  35. $AN% = 0                          :
  36.  measure of accuracy [0, 3]
  37. %4HalfPixel% = 1                  :
  38.  min(x,y half)
  39. &$EigthHue%   = (360 * &10000) / 8
  40. RadiusFudge = 10
  41. SegmentPartsPerFrame% = 13
  42.  X%(8-1, 32-1, 3)
  43.  Y%(8-1, 32-1, 3)
  44.  Hue%(8-1)
  45.  Saturation%(32-1)
  46.  Drawn%(32*8*8 - 1)
  47. /5DisplayedRadius = -1            :
  48.  sentinel value
  49.  "Wimp_SetColour", 1
  50.  512, 512
  51.  -512, -512, 40, 1024
  52. _Change_Value(1.00)
  53.  x%, y%, b%
  54. 9+   
  55. _Change_Value((y%+512)/1024)
  56. _Update_HSV
  57. _Update_HSV
  58.  n%, i%, j%
  59.  NA% = 0 
  60.  NToDraw% = 0 
  61.  N% = 3 
  62.     N% += 1
  63. _Calc_HSV_Constants
  64. F   n% = SegmentPartsPerFrame%
  65.  n% > NToDraw% 
  66.  n% = NToDraw%
  67.  n%=0 
  68.  1, "ASSERT"
  69.  i%= 1 
  70. JC    j%= 
  71. (NT%*NA%) - 2                :
  72.  now in range [-1, n-1)
  73. K        
  74.       j% += 1
  75. M'    
  76.  j%=(NT%*NA%-1) 
  77.  Drawn%(j%)
  78.  Drawn%(j%) 
  79. O$      j%=0:
  80.  Drawn%(j%):j%+=1:
  81. P'      
  82.  j%>=NT%*NA% 
  83.  1, "ASSERT"
  84. Q        
  85. R.    
  86. _Draw_SegmentPart(j% 
  87.  NT%, j% 
  88.  NT%)
  89.     Drawn%(j%) = 
  90.   NToDraw% -= n%
  91. W    :    
  92. _Change_Value(v)
  93.  v%, c%, x0, x1, x2, x3, y0, y1, y2, y3
  94.  Value=v 
  95.   Value = v
  96.  "Wimp_SetColour", 12
  97.   v% = Value * 1024
  98.  -512, -512, 40, v%
  99.  "Wimp_SetColour", 11
  100.  -512, -512+v%, 40, 1024
  101.   R = Radius * Value
  102. bG  N% = 0                                        :
  103.  start off coarse
  104. _Calc_HSV_Constants
  105.  DisplayedRadius > R 
  106. e(    
  107.  now must immediately blank out
  108.  "Wimp_SetColour", 1
  109.  NA% >= 1 
  110. h(      x0 = R                : y0 = 0
  111. i(      x1 = DisplayedRadius  : y1 = 0
  112.       
  113.  t%= 1 
  114.         t = 2*
  115. *(t%/15)
  116.         cos = 
  117.         sin = 
  118. n1        x2 = R*cos               : y2 = R*sin
  119. o?        x3 = DisplayedRadius*cos : y3 = DisplayedRadius*sin
  120.         
  121.  x0, y0
  122.         
  123.  x1, y1
  124.         
  125.  &55, x2, y2
  126.         
  127.  x3, y3
  128.         
  129.  &55, x1, y1
  130.         x0 = x2 : y0 = y2
  131.         x1 = x3 : y1 = y3
  132.       
  133. x        
  134. y%      
  135.  0, 0, DisplayedRadius
  136. z        
  137. |7  :
  138.  draw grey center of cone (greyness is "value")
  139.   c% = (Value*255) + 0.5
  140.  "ColourTrans_SetGCOL", (c% * &010101) << 8,,,&100,0
  141.  0, 0, R - DR%*NA%
  142. '  DisplayedRadius = R + RadiusFudge
  143. _Calc_HSV_Constants
  144. A  OT% = 1 << N%                   :
  145.  #divisions in one octant
  146. C  NT% = 8 * OT%                   :
  147.  #divisions in whole circle
  148. :  DR% = 32 
  149.  OT%                :
  150.  size of each annuli
  151. :  NA% = 
  152. floor(R, DR%)           :
  153.  #annuli (complete)
  154.  NA% >= 1 
  155. _Calc_Octant
  156. _Calc_Octant
  157.  a%, t%, a, cos, r, sin, t
  158.  a%= 0 
  159.  NA%-1
  160. ,    :
  161.  get radius of middle of annuli a%
  162. (    r = (R - (a% * DR%)) - (DR% / 2)
  163. 2    Saturation%(a%) = ((r / R) * &10000) + 0.5
  164.  t%= 0 
  165.  OT%-1
  166. 5    :
  167.  angle in middle of segment in range (0, 1)
  168.     t = (t% + 0.5) / OT%
  169. 1    Hue%(t%) = (360 * &10000 * (t / 8)) + 0.5
  170. %  Value% = (Value * &10000) + 0.5
  171.   delta = 
  172. (HalfPixel% / R)
  173.  t%= 0 
  174.  OT%-1
  175.     :
  176.  angles {0, .., PI/4}
  177. !    tmin = (
  178. /4) * (t% / OT%)
  179. #    tmax = tmin + ((
  180. /4) / OT%)
  181.     cosmin = 
  182. (tmin)
  183.     sinmin = 
  184. (tmin)
  185.     cosmax = 
  186. (tmax)
  187.     sinmax = 
  188. (tmax)
  189.  a%= 0 
  190.  NA%-1
  191.       rmax = R - (a% * DR%)
  192.       rmin = rmax - DR%
  193. /      X%(t%, a%, 0) = 
  194. roundup(rmin*cosmin)
  195. /      Y%(t%, a%, 0) = 
  196. roundup(rmin*sinmin)
  197. /      X%(t%, a%, 1) = 
  198. roundup(rmax*cosmin)
  199. /      Y%(t%, a%, 1) = 
  200. roundup(rmax*sinmin)
  201. /      X%(t%, a%, 2) = 
  202. roundup(rmax*cosmax)
  203. /      Y%(t%, a%, 2) = 
  204. roundup(rmax*sinmax)
  205. /      X%(t%, a%, 3) = 
  206. roundup(rmin*cosmax)
  207. /      Y%(t%, a%, 3) = 
  208. roundup(rmin*sinmax)
  209.   Drawn%() = 
  210.   NToDraw% = NT% * NA%
  211.  [-1 .. +1) --> 0
  212. round(x)
  213. ((x / 2) + 0.5) * 2
  214. roundup(x)
  215. = 2 * 
  216. ceiling(x, 2)
  217. rounddown(x)
  218. = 2 * 
  219. floor(x, 2)
  220.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  221.  integer round x/y towards +infinity
  222. ceiling(x, y)
  223. x <> 
  224.     x = x / y
  225.     x = 
  226.  x <> 
  227.     x = 1 + 
  228.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  229.  integer round x/y towards -infinity
  230. floor(x, y)
  231. x <> 
  232.     x = -
  233.     x = x / y
  234. _Draw_SegmentPart(a%, t%)
  235.  x0%,x1%,x2%,x3%, y0%,y1%,y2%,y3%, hue%, saturation%
  236. _Draw_SegmentPart_Point(x0%,y0%, a%, t%, 0)
  237. _Draw_SegmentPart_Point(x1%,y1%, a%, t%, 1)
  238. _Draw_SegmentPart_Point(x2%,y2%, a%, t%, 2)
  239. _Draw_SegmentPart_Point(x3%,y3%, a%, t%, 3)
  240. 6  hue% = Hue%(t% 
  241.  OT%) + ((t% 
  242.  OT%) * EigthHue%)
  243.  hue% >= 360*&10000 
  244.  1, "ASSERT"
  245.  hue% <= 0 
  246.  1, "ASSERT"
  247. #  saturation% = Saturation%(a%)
  248.  "ColourTrans_ConvertHSVToRGB", hue%, saturation%, Value% 
  249.  red%, green%, blue%
  250.   red%   = 
  251. rgb(red%) << 8
  252. !  green% = 
  253. rgb(green%) << 16
  254.    blue%  = 
  255. rgb(blue%) << 24
  256.  "ColourTrans_SetGCOL", red% 
  257.  green% 
  258.  blue%,,, &100, 0
  259.  x0%, y0%
  260.  x1%, y1%
  261.  &55, x2%, y2%
  262.  x3%, y3%
  263.  &55, x0%, y0%
  264.    MOVE x0%, y0%
  265.    DRAW x1%, y1%
  266.    DRAW x2%, y2%
  267.    DRAW x3%, y3%
  268.    DRAW x0%, y0%
  269. rgb(A%)
  270. &  A% = ((A% * 255) / &10000) + 0.5
  271.  A% < 0 
  272.  1 , "negative"
  273.  A% > 255 
  274.  1, "too big"
  275. _Draw_SegmentPart_Point(
  276.  x%, 
  277.  y%, a%, t%, i%)
  278.   x% = X%(t% 
  279.  OT%, a%, i%)
  280.   y% = Y%(t% 
  281.  OT%, a%, i%)
  282.   t% = t% 
  283.  t% > 7 
  284.  1, "bad t%"
  285.   t% = t% 
  286.  (t% >> 1)
  287.  x%, y%
  288.  x% = -x%
  289.  y% = -y%
  290.